main.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. "use strict";
  3. /* jshint esversion:6, strict:global */
  4. //<Polyfills>
  5. if (!document.scrollingElement) document.scrollingElement = document.documentElement;
  6. if (!NodeList.prototype.forEach) NodeList.prototype.forEach = Array.prototype.forEach;
  7. if (!Element.prototype.matches) Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.webkitMatchesSelector;
  8. if (!Element.prototype.closest) Element.prototype.closest = function (s) {
  9. let el = this;
  10. do {
  11. if (el.matches(s)) return el;
  12. el = el.parentElement;
  13. } while (el);
  14. return null;
  15. };
  16. if (!Element.prototype.remove) Element.prototype.remove = function () { if (this.parentNode) this.parentNode.removeChild(this); };
  17. //</Polyfills>
  18. //<Utils>
  19. function xmlHttpRequestJson(req) {
  20. let json = req.response;
  21. if (req.responseType !== 'json') { //IE11
  22. try { json = JSON.parse(req.responseText); }
  23. catch (ex) { json = null; }
  24. }
  25. return json;
  26. }
  27. //</Utils>
  28. //<Global context>
  29. var context;
  30. (function parseJsonVars() {
  31. const jsonVars = document.getElementById('jsonVars'),
  32. json = JSON.parse(jsonVars.innerHTML);
  33. jsonVars.outerHTML = '';
  34. context = json.context;
  35. context.ajax_loading = false;
  36. context.i18n = json.i18n;
  37. context.shortcuts = json.shortcuts;
  38. context.urls = json.urls;
  39. context.icons = json.icons;
  40. context.icons.read = decodeURIComponent(context.icons.read);
  41. context.icons.unread = decodeURIComponent(context.icons.unread);
  42. }());
  43. //</Global context>
  44. function badAjax(reload) {
  45. openNotification(context.i18n.notif_request_failed, 'bad');
  46. if (reload) {
  47. setTimeout(function () { location.reload(); }, 2000);
  48. }
  49. return true;
  50. }
  51. function needsScroll(elem) {
  52. const winBottom = document.scrollingElement.scrollTop + document.scrollingElement.clientHeight,
  53. elemTop = elem.offsetParent.offsetTop + elem.offsetTop,
  54. elemBottom = elemTop + elem.offsetHeight;
  55. return (elemTop < document.scrollingElement.scrollTop || elemBottom > winBottom) ?
  56. elemTop - (document.scrollingElement.clientHeight / 2) : 0;
  57. }
  58. function str2int(str) {
  59. if (!str) {
  60. return 0;
  61. }
  62. return parseInt(str.replace(/\D/g, ''), 10) || 0;
  63. }
  64. function numberFormat(nStr) {
  65. if (nStr < 0) {
  66. return 0;
  67. }
  68. // http://www.mredkj.com/javascript/numberFormat.html
  69. nStr += '';
  70. const x = nStr.split('.'),
  71. x2 = x.length > 1 ? '.' + x[1] : '',
  72. rgx = /(\d+)(\d{3})/;
  73. let x1 = x[0];
  74. while (rgx.test(x1)) {
  75. x1 = x1.replace(rgx, '$1' + ' ' + '$2');
  76. }
  77. return x1 + x2;
  78. }
  79. function incLabel(p, inc, spaceAfter) {
  80. const i = str2int(p) + inc;
  81. return i > 0 ? ((spaceAfter ? '' : ' ') + '(' + numberFormat(i) + ')' + (spaceAfter ? ' ' : '')) : '';
  82. }
  83. function incUnreadsFeed(article, feed_id, nb) {
  84. //Update unread: feed
  85. let elem = document.getElementById(feed_id),
  86. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0,
  87. feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0;
  88. if (elem) {
  89. elem.setAttribute('data-unread', feed_unreads + nb);
  90. elem = elem.querySelector('.item-title');
  91. if (elem) {
  92. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  93. }
  94. }
  95. //Update unread: category
  96. elem = document.getElementById(feed_id).closest('.category');
  97. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  98. if (elem) {
  99. elem.setAttribute('data-unread', feed_unreads + nb);
  100. elem = elem.querySelector('.title');
  101. if (elem) {
  102. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  103. }
  104. }
  105. //Update unread: all
  106. if (feed_priority > 0) {
  107. elem = document.querySelector('#aside_feed .all .title');
  108. if (elem) {
  109. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  110. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  111. }
  112. }
  113. //Update unread: favourites
  114. if (article && article.closest('div').classList.contains('favorite')) {
  115. elem = document.querySelector('#aside_feed .favorites .title');
  116. if (elem) {
  117. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  118. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  119. }
  120. }
  121. let isCurrentView = false;
  122. // Update unread: title
  123. document.title = document.title.replace(/^((?:\([ 0-9]+\) )?)/, function (m, p1) {
  124. const feed = document.getElementById(feed_id);
  125. if (article || feed.closest('.active')) {
  126. isCurrentView = true;
  127. return incLabel(p1, nb, true);
  128. } else if (document.querySelector('.all.active')) {
  129. isCurrentView = feed_priority > 0;
  130. return incLabel(p1, feed_priority > 0 ? nb : 0, true);
  131. } else {
  132. return p1;
  133. }
  134. });
  135. return isCurrentView;
  136. }
  137. function incUnreadsTag(tag_id, nb) {
  138. let t = document.getElementById(tag_id);
  139. if (t) {
  140. let unreads = str2int(t.getAttribute('data-unread'));
  141. t.setAttribute('data-unread', unreads + nb);
  142. t.querySelector('.item-title').setAttribute('data-unread', numberFormat(unreads + nb));
  143. }
  144. t = document.querySelector('.category.tags .title');
  145. if (t) {
  146. let unreads = str2int(t.getAttribute('data-unread'));
  147. t.setAttribute('data-unread', numberFormat(unreads + nb));
  148. }
  149. }
  150. function removeArticle(div) {
  151. if (!div || div.classList.contains('not_read') || (context.auto_mark_article && div.classList.contains('active'))) {
  152. return;
  153. }
  154. let scrollTop = box_to_follow.scrollTop;
  155. let dirty = false;
  156. const p = div.previousElementSibling,
  157. n = div.nextElementSibling;
  158. if (p && p.classList.contains('day') && n && n.classList.contains('day')) {
  159. scrollTop -= p.offsetHeight;
  160. dirty = true;
  161. p.remove();
  162. }
  163. if (div.offsetHeight > 0 && div.offsetParent.offsetTop + div.offsetTop + div.offsetHeight < scrollTop) {
  164. scrollTop -= div.offsetHeight;
  165. dirty = true;
  166. }
  167. div.remove();
  168. if (dirty) {
  169. box_to_follow.scrollTop = scrollTop;
  170. }
  171. }
  172. var pending_entries = {},
  173. mark_read_queue = [];
  174. function send_mark_read_queue(queue, asRead, callback) {
  175. const req = new XMLHttpRequest();
  176. req.open('POST', '.?c=entry&a=read' + (asRead ? '' : '&is_read=0'), true);
  177. req.responseType = 'json';
  178. req.onerror = function (e) {
  179. for (let i = queue.length - 1; i >= 0; i--) {
  180. delete pending_entries['flux_' + queue[i]];
  181. }
  182. badAjax(this.status == 403);
  183. };
  184. req.onload = function (e) {
  185. if (this.status != 200) {
  186. return req.onerror(e);
  187. }
  188. const json = xmlHttpRequestJson(this);
  189. if (!json) {
  190. return req.onerror(e);
  191. }
  192. for (let i = queue.length - 1; i >= 0; i--) {
  193. const div = document.getElementById('flux_' + queue[i]),
  194. myIcons = context.icons;
  195. let inc = 0;
  196. if (div.classList.contains('not_read')) {
  197. div.classList.remove('not_read');
  198. div.querySelectorAll('a.read').forEach(function (a) {
  199. a.href = a.href.replace('&is_read=0', '') + '&is_read=1';
  200. });
  201. div.querySelectorAll('a.read > .icon').forEach(function (img) { img.outerHTML = myIcons.read; });
  202. inc--;
  203. if (context.auto_remove_article) {
  204. removeArticle(div);
  205. }
  206. } else {
  207. div.classList.add('not_read');
  208. div.classList.add('keep_unread'); //Split for IE11
  209. div.querySelectorAll('a.read').forEach(function (a) {
  210. a.href = a.href.replace('&is_read=1', '');
  211. });
  212. div.querySelectorAll('a.read > .icon').forEach(function (img) { img.outerHTML = myIcons.unread; });
  213. inc++;
  214. }
  215. let feed_link = div.querySelector('.website > a, a.website');
  216. if (feed_link) {
  217. const feed_url = feed_link.href,
  218. feed_id = feed_url.substr(feed_url.lastIndexOf('f_'));
  219. incUnreadsFeed(div, feed_id, inc);
  220. }
  221. delete pending_entries['flux_' + queue[i]];
  222. }
  223. faviconNbUnread();
  224. if (json.tags) {
  225. const tagIds = Object.keys(json.tags);
  226. for (let i = tagIds.length - 1; i >= 0; i--) {
  227. let tagId = tagIds[i];
  228. incUnreadsTag(tagId, (asRead ? -1 : 1) * json.tags[tagId].length);
  229. }
  230. }
  231. onScroll();
  232. if (callback) {
  233. callback();
  234. }
  235. };
  236. req.setRequestHeader('Content-Type', 'application/json');
  237. req.send(JSON.stringify({
  238. ajax: true,
  239. _csrf: context.csrf,
  240. id: queue,
  241. }));
  242. }
  243. var send_mark_read_queue_timeout = 0;
  244. function send_mark_queue_tick(callback) {
  245. send_mark_read_queue_timeout = 0;
  246. const queue = mark_read_queue.slice(0);
  247. mark_read_queue = [];
  248. send_mark_read_queue(queue, true, callback);
  249. }
  250. var delayedFunction = send_mark_queue_tick;
  251. function delayedClick(a) {
  252. if (a) {
  253. delayedFunction(function () { a.click(); });
  254. }
  255. }
  256. function mark_read(div, only_not_read, asBatch) {
  257. if (!div || !div.id || context.anonymous ||
  258. (only_not_read && !div.classList.contains('not_read'))) {
  259. return false;
  260. }
  261. if (pending_entries[div.id]) {
  262. return false;
  263. }
  264. pending_entries[div.id] = true;
  265. const asRead = div.classList.contains('not_read'),
  266. entryId = div.id.replace(/^flux_/, '');
  267. if (asRead && asBatch) {
  268. mark_read_queue.push(entryId);
  269. if (send_mark_read_queue_timeout == 0) {
  270. send_mark_read_queue_timeout = setTimeout(function () { send_mark_queue_tick(null); }, 1000);
  271. }
  272. } else {
  273. const queue = [ entryId ];
  274. send_mark_read_queue(queue, asRead);
  275. }
  276. }
  277. function mark_previous_read(div) {
  278. while (div) {
  279. mark_read(div, true, true);
  280. div = div.previousElementSibling;
  281. }
  282. }
  283. function mark_favorite(div) {
  284. if (!div) {
  285. return false;
  286. }
  287. let a = div.querySelector('a.bookmark'),
  288. url = a ? a.href : '';
  289. if (!url) {
  290. return false;
  291. }
  292. if (pending_entries[div.id]) {
  293. return false;
  294. }
  295. pending_entries[div.id] = true;
  296. const req = new XMLHttpRequest();
  297. req.open('POST', url, true);
  298. req.responseType = 'json';
  299. req.onerror = function (e) {
  300. delete pending_entries[div.id];
  301. badAjax(this.status == 403);
  302. };
  303. req.onload = function (e) {
  304. if (this.status != 200) {
  305. return req.onerror(e);
  306. }
  307. const json = xmlHttpRequestJson(this);
  308. if (!json) {
  309. return req.onerror(e);
  310. }
  311. let inc = 0;
  312. if (div.classList.contains('favorite')) {
  313. div.classList.remove('favorite');
  314. inc--;
  315. } else {
  316. div.classList.add('favorite');
  317. inc++;
  318. }
  319. div.querySelectorAll('a.bookmark').forEach(function (a) { a.href = json.url; });
  320. div.querySelectorAll('a.bookmark > .icon').forEach(function (img) { img.outerHTML = json.icon; });
  321. const favourites = document.querySelector('#aside_feed .favorites .title');
  322. if (favourites) {
  323. favourites.textContent = favourites.textContent.replace(/((?: \([ 0-9]+\))?\s*)$/, function (m, p1) {
  324. return incLabel(p1, inc, false);
  325. });
  326. }
  327. if (div.classList.contains('not_read')) {
  328. const elem = document.querySelector('#aside_feed .favorites .title'),
  329. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  330. if (elem) {
  331. elem.setAttribute('data-unread', numberFormat(feed_unreads + inc));
  332. }
  333. }
  334. delete pending_entries[div.id];
  335. };
  336. req.setRequestHeader('Content-Type', 'application/json');
  337. req.send(JSON.stringify({
  338. ajax: true,
  339. _csrf: context.csrf,
  340. }));
  341. }
  342. var freshrssOpenArticleEvent = document.createEvent('Event');
  343. freshrssOpenArticleEvent.initEvent('freshrss:openArticle', true, true);
  344. function toggleContent(new_active, old_active, skipping) {
  345. // If skipping, move current without activating or marking as read
  346. if (!new_active) {
  347. return;
  348. }
  349. if (context.does_lazyload && !skipping) {
  350. new_active.querySelectorAll('img[data-original], iframe[data-original]').forEach(function (el) {
  351. el.src = el.getAttribute('data-original');
  352. el.removeAttribute('data-original');
  353. });
  354. }
  355. if (old_active !== new_active) {
  356. if (!skipping) {
  357. new_active.classList.add('active');
  358. }
  359. new_active.classList.add('current');
  360. if (old_active) {
  361. old_active.classList.remove('active');
  362. old_active.classList.remove('current'); //Split for IE11
  363. if (context.auto_remove_article) {
  364. removeArticle(old_active);
  365. }
  366. }
  367. } else {
  368. new_active.classList.toggle('active');
  369. }
  370. const relative_move = context.current_view === 'global',
  371. box_to_move = relative_move ? document.getElementById('panel') : document.scrollingElement;
  372. if (context.sticky_post) { //Stick the article to the top when opened
  373. let prev_article = new_active.previousElementSibling,
  374. new_pos = new_active.offsetParent.offsetTop + new_active.offsetTop;
  375. if (prev_article && new_active.offsetTop - prev_article.offsetTop <= 150) {
  376. new_pos = prev_article.offsetParent.offsetTop + prev_article.offsetTop;
  377. if (relative_move) {
  378. new_pos -= box_to_move.offsetTop;
  379. }
  380. }
  381. if (skipping) {
  382. // when skipping, this feels more natural if it's not so near the top
  383. new_pos -= document.body.clientHeight / 4;
  384. }
  385. if (relative_move) {
  386. new_pos += box_to_move.scrollTop;
  387. }
  388. box_to_move.scrollTop = new_pos;
  389. }
  390. if (new_active.classList.contains('active') && !skipping) {
  391. if (context.auto_mark_article) {
  392. mark_read(new_active, true, true);
  393. }
  394. new_active.dispatchEvent(freshrssOpenArticleEvent);
  395. }
  396. onScroll();
  397. }
  398. function prev_entry(skipping) {
  399. const old_active = document.querySelector('.flux.current');
  400. let new_active = old_active;
  401. if (new_active) {
  402. do new_active = new_active.previousElementSibling;
  403. while (new_active && !new_active.classList.contains('flux'));
  404. if (!new_active) {
  405. prev_feed();
  406. }
  407. } else {
  408. new_active = document.querySelector('.flux');
  409. }
  410. toggleContent(new_active, old_active, skipping);
  411. }
  412. function next_entry(skipping) {
  413. const old_active = document.querySelector('.flux.current');
  414. let new_active = old_active;
  415. if (new_active) {
  416. do new_active = new_active.nextElementSibling;
  417. while (new_active && !new_active.classList.contains('flux'));
  418. if (!new_active) {
  419. next_feed();
  420. }
  421. } else {
  422. new_active = document.querySelector('.flux');
  423. }
  424. toggleContent(new_active, old_active, skipping);
  425. }
  426. function prev_feed() {
  427. let found = false;
  428. let adjacent = null;
  429. const feeds = document.querySelectorAll('#aside_feed .feed');
  430. for (let i = feeds.length - 1; i >= 0; i--) {
  431. const feed = feeds[i];
  432. if (feed.classList.contains('active')) {
  433. found = true;
  434. continue;
  435. }
  436. if (!found) {
  437. continue;
  438. }
  439. if (getComputedStyle(feed).display === 'none') {
  440. continue;
  441. }
  442. if (feed.dataset.unread != 0) {
  443. return delayedClick(feed.querySelector('a.item-title'));
  444. } else if (adjacent === null) {
  445. adjacent = feed;
  446. }
  447. }
  448. if (found) {
  449. delayedClick(adjacent.querySelector('a.item-title'));
  450. } else {
  451. last_feed();
  452. }
  453. }
  454. function next_feed() {
  455. let found = false;
  456. let adjacent = null;
  457. const feeds = document.querySelectorAll('#aside_feed .feed');
  458. for (let i = 0; i < feeds.length; i++) {
  459. const feed = feeds[i];
  460. if (feed.classList.contains('active')) {
  461. found = true;
  462. continue;
  463. }
  464. if (!found) {
  465. continue;
  466. }
  467. if (getComputedStyle(feed).display === 'none') {
  468. continue;
  469. }
  470. if (feed.dataset.unread != 0) {
  471. return delayedClick(feed.querySelector('a.item-title'));
  472. } else if (adjacent === null) {
  473. adjacent = feed;
  474. }
  475. }
  476. if (found) {
  477. delayedClick(adjacent.querySelector('a.item-title'));
  478. } else {
  479. first_feed();
  480. }
  481. }
  482. function first_feed() {
  483. const a = document.querySelector('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  484. delayedClick(a);
  485. }
  486. function last_feed() {
  487. const links = document.querySelectorAll('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  488. if (links && links.length > 0) {
  489. delayedClick(links[links.length - 1]);
  490. }
  491. }
  492. function prev_category() {
  493. const active_cat = document.querySelector('#aside_feed .category.active');
  494. if (active_cat) {
  495. let cat = active_cat;
  496. do cat = cat.previousElementSibling;
  497. while (cat && getComputedStyle(cat).display === 'none');
  498. if (cat) {
  499. delayedClick(cat.querySelector('a.title'));
  500. }
  501. } else {
  502. last_category();
  503. }
  504. }
  505. function next_category() {
  506. const active_cat = document.querySelector('#aside_feed .category.active');
  507. if (active_cat) {
  508. let cat = active_cat;
  509. do cat = cat.nextElementSibling;
  510. while (cat && getComputedStyle(cat).display === 'none');
  511. if (cat) {
  512. delayedClick(cat.querySelector('a.title'));
  513. }
  514. } else {
  515. first_category();
  516. }
  517. }
  518. function first_category() {
  519. const a = document.querySelector('#aside_feed .category:not([data-unread="0"]) a.title');
  520. delayedClick(a);
  521. }
  522. function last_category() {
  523. const links = document.querySelectorAll('#aside_feed .category:not([data-unread="0"]) a.title');
  524. if (links && links.length > 0) {
  525. delayedClick(links[links.length - 1]);
  526. }
  527. }
  528. function collapse_entry() {
  529. const flux_current = document.querySelector('.flux.current');
  530. toggleContent(flux_current, flux_current, false);
  531. }
  532. function toggle_media() {
  533. const media = document.querySelector('.flux.current video,.flux.current audio');
  534. if (media === null) {
  535. return;
  536. }
  537. if (media.paused) {
  538. media.play();
  539. } else {
  540. media.pause();
  541. }
  542. }
  543. function user_filter(key) {
  544. const filter = document.getElementById('dropdown-query'),
  545. filters = filter.parentElement.querySelectorAll('.dropdown-menu > .query > a');
  546. if (typeof key === 'undefined') {
  547. if (!filters.length) {
  548. return;
  549. }
  550. // Display the filter div
  551. location.hash = filter.id;
  552. // Force scrolling to the filter div
  553. const scroll = needsScroll(document.querySelector('.header'));
  554. if (scroll !== 0) {
  555. document.scrollingElement.scrollTop = scroll;
  556. }
  557. // Force the key value if there is only one action, so we can trigger it automatically
  558. if (filters.length === 1) {
  559. key = 1;
  560. } else {
  561. return;
  562. }
  563. }
  564. // Trigger selected share action
  565. key = parseInt(key);
  566. if (key <= filters.length) {
  567. filters[key - 1].click();
  568. }
  569. }
  570. function auto_share(key) {
  571. const share = document.querySelector('.flux.current.active .dropdown-target[id^="dropdown-share"]');
  572. if (!share) {
  573. return;
  574. }
  575. const shares = share.parentElement.querySelectorAll('.dropdown-menu .item a');
  576. if (typeof key === 'undefined') {
  577. // Display the share div
  578. location.hash = share.id;
  579. // Force scrolling to the share div
  580. const scrollTop = needsScroll(share.closest('.bottom'));
  581. if (scrollTop !== 0) {
  582. document.scrollingElement.scrollTop = scrollTop;
  583. }
  584. // Force the key value if there is only one action, so we can trigger it automatically
  585. if (shares.length === 1) {
  586. key = 1;
  587. } else {
  588. return;
  589. }
  590. }
  591. // Trigger selected share action and hide the share div
  592. key = parseInt(key);
  593. if (key <= shares.length) {
  594. shares[key - 1].click();
  595. share.parentElement.querySelector('.dropdown-menu .dropdown-close a').click();
  596. }
  597. }
  598. var box_to_follow;
  599. function onScroll() {
  600. if (!box_to_follow) {
  601. return;
  602. }
  603. if (context.auto_mark_scroll) {
  604. const minTop = 40 + box_to_follow.scrollTop;
  605. document.querySelectorAll('.not_read:not(.keep_unread)').forEach(function (div) {
  606. if (div.offsetHeight > 0 &&
  607. div.offsetParent.offsetTop + div.offsetTop + div.offsetHeight < minTop) {
  608. mark_read(div, true, true);
  609. }
  610. });
  611. }
  612. if (context.auto_load_more) {
  613. const pagination = document.getElementById('mark-read-pagination');
  614. if (pagination && box_to_follow.offsetHeight > 0 &&
  615. box_to_follow.scrollTop + box_to_follow.offsetHeight + (window.innerHeight / 2) >= pagination.offsetTop) {
  616. load_more_posts();
  617. }
  618. }
  619. }
  620. function init_posts() {
  621. if (context.auto_load_more || context.auto_mark_scroll || context.auto_remove_article) {
  622. box_to_follow = context.current_view === 'global' ? document.getElementById('panel') : document.scrollingElement;
  623. let lastScroll = 0, //Throttle
  624. timerId = 0;
  625. (box_to_follow === document.scrollingElement ? window : box_to_follow).onscroll = function () {
  626. clearTimeout(timerId);
  627. if (lastScroll + 500 < Date.now()) {
  628. lastScroll = Date.now();
  629. onScroll();
  630. } else {
  631. timerId = setTimeout(onScroll, 500);
  632. }
  633. };
  634. onScroll();
  635. }
  636. }
  637. function rememberOpenCategory(category_id, isOpen) {
  638. if (context.display_categories === 'remember') {
  639. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  640. if (isOpen) {
  641. open_categories[category_id] = true;
  642. } else {
  643. delete open_categories[category_id];
  644. }
  645. localStorage.setItem('FreshRSS_open_categories', JSON.stringify(open_categories));
  646. }
  647. }
  648. function openCategory(category_id) {
  649. const category_element = document.getElementById(category_id);
  650. category_element.querySelector('.tree-folder-items').classList.add('active');
  651. const img = category_element.querySelector('a.dropdown-toggle img');
  652. img.src = img.src.replace('/icons/down.', '/icons/up.');
  653. img.alt = '△';
  654. }
  655. function init_column_categories() {
  656. if (context.current_view !== 'normal' && context.current_view !== 'reader') {
  657. return;
  658. }
  659. //Restore sidebar scroll position
  660. document.getElementById('sidebar').scrollTop = +sessionStorage.getItem('FreshRSS_sidebar_scrollTop');
  661. //Restore open categories
  662. if (context.display_categories === 'remember') {
  663. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  664. Object.keys(open_categories).forEach(function (category_id) {
  665. openCategory(category_id);
  666. });
  667. }
  668. document.getElementById('aside_feed').onclick = function (ev) {
  669. let a = ev.target.closest('.tree-folder > .tree-folder-title > a.dropdown-toggle');
  670. if (a) {
  671. const img = a.querySelector('img');
  672. const category_id = a.closest('.category').id;
  673. if (img.alt === '▽') {
  674. img.src = img.src.replace('/icons/down.', '/icons/up.');
  675. img.alt = '△';
  676. rememberOpenCategory(category_id, true);
  677. } else {
  678. img.src = img.src.replace('/icons/up.', '/icons/down.');
  679. img.alt = '▽';
  680. rememberOpenCategory(category_id, false);
  681. }
  682. const ul = a.closest('li').querySelector('.tree-folder-items');
  683. let nbVisibleItems = 0;
  684. for (let i = ul.children.length - 1; i >= 0; i--) {
  685. if (ul.children[i].offsetHeight) {
  686. nbVisibleItems++;
  687. }
  688. }
  689. ul.classList.toggle('active');
  690. //CSS transition does not work on max-height:auto
  691. ul.style.maxHeight = ul.classList.contains('active') ? (nbVisibleItems * 4) + 'em' : 0;
  692. return false;
  693. }
  694. a = ev.target.closest('.tree-folder-items > .feed .dropdown-toggle');
  695. if (a) {
  696. const itemId = a.closest('.item').id,
  697. templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template',
  698. id = itemId.substr(2),
  699. feed_web = a.getAttribute('data-fweb'),
  700. div = a.parentElement,
  701. dropdownMenu = div.querySelector('.dropdown-menu'),
  702. template = document.getElementById(templateId)
  703. .innerHTML.replace(/------/g, id).replace('http://example.net/', feed_web);
  704. if (!dropdownMenu) {
  705. a.href = '#dropdown-' + id;
  706. div.querySelector('.dropdown-target').id = 'dropdown-' + id;
  707. div.insertAdjacentHTML('beforeend', template);
  708. const b = div.querySelector('button.confirm');
  709. if (b) {
  710. b.disabled = false;
  711. }
  712. } else if (getComputedStyle(dropdownMenu).display === 'none') {
  713. const id2 = div.closest('.item').id.substr(2);
  714. a.href = '#dropdown-' + id2;
  715. } else {
  716. a.href = '#close';
  717. }
  718. return true;
  719. }
  720. return true;
  721. };
  722. }
  723. function init_shortcuts() {
  724. Object.keys(context.shortcuts).forEach(function (k) {
  725. context.shortcuts[k] = (context.shortcuts[k] || '').toUpperCase();
  726. });
  727. document.body.onkeydown = function (ev) {
  728. if (ev.target.closest('input, textarea') ||
  729. ev.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey)) {
  730. return true;
  731. }
  732. const s = context.shortcuts;
  733. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  734. //IE11
  735. if (k === 'SPACEBAR') k = 'SPACE';
  736. else if (k === 'DEL') k = 'DELETE';
  737. else if (k === 'ESC') k = 'ESCAPE';
  738. if (location.hash.match(/^#dropdown-/)) {
  739. const n = parseInt(k);
  740. if (n) {
  741. if (location.hash === '#dropdown-query') {
  742. user_filter(n);
  743. } else {
  744. auto_share(n);
  745. }
  746. return false;
  747. }
  748. }
  749. if (k === s.next_entry) {
  750. if (ev.altKey) {
  751. next_category();
  752. } else if (ev.shiftKey) {
  753. next_feed();
  754. } else {
  755. next_entry(false);
  756. }
  757. return false;
  758. }
  759. if (k === s.prev_entry) {
  760. if (ev.altKey) {
  761. prev_category();
  762. } else if (ev.shiftKey) {
  763. prev_feed();
  764. } else {
  765. prev_entry(false);
  766. }
  767. return false;
  768. }
  769. if (k === s.mark_read) {
  770. if (ev.altKey) {
  771. mark_previous_read(document.querySelector('.flux.current'));
  772. } else if (ev.shiftKey) {
  773. document.querySelector('.nav_menu .read_all').click();
  774. } else { // Toggle the read state
  775. mark_read(document.querySelector('.flux.current'), false, false);
  776. }
  777. return false;
  778. }
  779. if (k === s.first_entry) {
  780. if (ev.altKey) {
  781. first_category();
  782. } else if (ev.shiftKey) {
  783. first_feed();
  784. } else {
  785. const old_active = document.querySelector('.flux.current'),
  786. first = document.querySelector('.flux');
  787. if (first.classList.contains('flux')) {
  788. toggleContent(first, old_active, false);
  789. }
  790. }
  791. return false;
  792. }
  793. if (k === s.last_entry) {
  794. if (ev.altKey) {
  795. last_category();
  796. } else if (ev.shiftKey) {
  797. last_feed();
  798. } else {
  799. const old_active = document.querySelector('.flux.current'),
  800. last = document.querySelector('.flux:last-of-type');
  801. if (last.classList.contains('flux')) {
  802. toggleContent(last, old_active, false);
  803. }
  804. }
  805. return false;
  806. }
  807. if (ev.altKey || ev.shiftKey) {
  808. return true;
  809. }
  810. if (k === s.mark_favorite) { // Toggle the favorite state
  811. mark_favorite(document.querySelector('.flux.current'));
  812. return false;
  813. }
  814. if (k === s.go_website) {
  815. if (context.auto_mark_site) {
  816. mark_read(document.querySelector('.flux.current'), true, false);
  817. }
  818. const newWindow = window.open();
  819. if (newWindow) {
  820. newWindow.opener = null;
  821. newWindow.location = document.querySelector('.flux.current a.go_website').href;
  822. }
  823. return false;
  824. }
  825. if (k === s.skip_next_entry) { next_entry(true); return false; }
  826. if (k === s.skip_prev_entry) { prev_entry(true); return false; }
  827. if (k === s.collapse_entry) { collapse_entry(); return false; }
  828. if (k === s.auto_share) { auto_share(); return false; }
  829. if (k === s.user_filter) { user_filter(); return false; }
  830. if (k === s.load_more) { load_more_posts(); return false; }
  831. if (k === s.close_dropdown) { location.hash = null; return false; }
  832. if (k === s.help) { window.open(context.urls.help); return false; }
  833. if (k === s.focus_search) { document.getElementById('search').focus(); return false; }
  834. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); return false; }
  835. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); return false; }
  836. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); return false; }
  837. if (k === s.rss_view) { delayedClick(document.querySelector('#nav_menu_views .view-rss')); return false; }
  838. if (k === s.toggle_media) { toggle_media(); return false;}
  839. return true;
  840. };
  841. }
  842. function init_stream(stream) {
  843. stream.onclick = function (ev) {
  844. let el = ev.target.closest('.flux a.read');
  845. if (el) {
  846. mark_read(el.closest('.flux'), false, false);
  847. return false;
  848. }
  849. el = ev.target.closest('.flux a.bookmark');
  850. if (el) {
  851. mark_favorite(el.closest('.flux'));
  852. return false;
  853. }
  854. el = ev.target.closest('.dynamictags');
  855. if (el) {
  856. loadDynamicTags(el);
  857. return true;
  858. }
  859. el = ev.target.closest('.item.title > a');
  860. if (el) { // Allow default control-click behaviour such as open in backround-tab
  861. return ev.ctrlKey;
  862. }
  863. el = ev.target.closest('.flux .content a');
  864. if (el) {
  865. if (!el.closest('div').classList.contains('author')) {
  866. el.target = '_blank';
  867. el.rel = 'noreferrer';
  868. }
  869. return true;
  870. }
  871. el = ev.target.closest('.item.share > a[href="#"]');
  872. if (el) { //Print
  873. const tmp_window = window.open();
  874. for (var i = 0; i < document.styleSheets.length; i++) {
  875. tmp_window.document.writeln('<link href="' + document.styleSheets[i].href + '" rel="stylesheet" type="text/css" />');
  876. }
  877. tmp_window.document.writeln(el.closest('.flux_content').querySelector('.content').innerHTML);
  878. tmp_window.document.close();
  879. tmp_window.focus();
  880. tmp_window.print();
  881. tmp_window.close();
  882. return false;
  883. }
  884. el = ev.target.closest('.item.share > a[href="POST"]');
  885. if (el) { //Share by POST
  886. const f = el.parentElement.querySelector('form');
  887. f.disabled = false;
  888. f.submit();
  889. return false;
  890. }
  891. el = ev.target.closest('.flux_header, .flux_content');
  892. if (el) { //flux_toggle
  893. if (ev.target.closest('.content, .item.website, .item.link, .dropdown-menu')) {
  894. return true;
  895. }
  896. if (!context.sides_close_article && ev.target.matches('div.flux_content')) {
  897. // setting for not-closing after clicking outside article area
  898. return false;
  899. }
  900. const old_active = document.querySelector('.flux.current'),
  901. new_active = el.parentNode;
  902. if (ev.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  903. if (context.auto_mark_article) {
  904. mark_read(new_active, true, false);
  905. }
  906. return true;
  907. }
  908. toggleContent(new_active, old_active, false);
  909. return false;
  910. }
  911. };
  912. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  913. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  914. return;
  915. }
  916. let el = ev.target.closest('.item.title > a');
  917. if (el) {
  918. if (ev.which == 1) {
  919. if (ev.ctrlKey) { //Control+click
  920. if (context.auto_mark_site) {
  921. mark_read(el.closest('.flux'), true, false);
  922. }
  923. } else {
  924. el.parentElement.click(); //Normal click, just toggle article.
  925. }
  926. } else if (ev.which == 2 && !ev.ctrlKey) { //Simple middle click: same behaviour as CTRL+click
  927. if (context.auto_mark_article) {
  928. const new_active = el.closest('.flux');
  929. mark_read(new_active, true, false);
  930. }
  931. }
  932. return;
  933. }
  934. if (context.auto_mark_site) {
  935. // catch mouseup instead of click so we can have the correct behaviour
  936. // with middle button click (scroll button).
  937. el = ev.target.closest('.flux .link > a');
  938. if (el) {
  939. if (ev.which == 3) {
  940. return;
  941. }
  942. mark_read(el.closest('.flux'), true, false);
  943. }
  944. }
  945. };
  946. stream.onchange = function (ev) {
  947. const checkboxTag = ev.target.closest('.checkboxTag');
  948. if (checkboxTag) { //Dynamic tags
  949. ev.stopPropagation();
  950. const isChecked = checkboxTag.checked,
  951. tagId = checkboxTag.name.replace(/^t_/, ''),
  952. tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.value : '',
  953. entry = checkboxTag.closest('div.flux'),
  954. entryId = entry.id.replace(/^flux_/, '');
  955. checkboxTag.disabled = true;
  956. const req = new XMLHttpRequest();
  957. req.open('POST', './?c=tag&a=tagEntry', true);
  958. req.responseType = 'json';
  959. req.onerror = function (e) {
  960. checkboxTag.checked = !isChecked;
  961. badAjax(this.status == 403);
  962. };
  963. req.onload = function (e) {
  964. if (this.status != 200) {
  965. return req.onerror(e);
  966. }
  967. if (entry.classList.contains('not_read')) {
  968. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  969. }
  970. };
  971. req.onloadend = function (e) {
  972. checkboxTag.disabled = false;
  973. if (tagId == 0) {
  974. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  975. }
  976. };
  977. req.setRequestHeader('Content-Type', 'application/json');
  978. req.send(JSON.stringify({
  979. _csrf: context.csrf,
  980. id_tag: tagId,
  981. name_tag: tagId == 0 ? tagName : '',
  982. id_entry: entryId,
  983. checked: isChecked,
  984. }));
  985. }
  986. };
  987. }
  988. function init_nav_entries() {
  989. const nav_entries = document.getElementById('nav_entries');
  990. if (nav_entries) {
  991. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  992. prev_entry(false);
  993. return false;
  994. };
  995. nav_entries.querySelector('.next_entry').onclick = function (e) {
  996. next_entry(false);
  997. return false;
  998. };
  999. nav_entries.querySelector('.up').onclick = function (e) {
  1000. const active_item = document.querySelector('.flux.current'),
  1001. windowTop = document.scrollingElement.scrollTop,
  1002. item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  1003. document.scrollingElement.scrollTop = windowTop > item_top ? item_top : 0;
  1004. return false;
  1005. };
  1006. }
  1007. }
  1008. function loadDynamicTags(div) {
  1009. div.classList.remove('dynamictags');
  1010. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1011. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  1012. const req = new XMLHttpRequest();
  1013. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  1014. req.responseType = 'json';
  1015. req.onerror = function (e) {
  1016. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1017. div.classList.add('dynamictags');
  1018. };
  1019. req.onload = function (e) {
  1020. if (this.status != 200) {
  1021. return req.onerror(e);
  1022. }
  1023. const json = xmlHttpRequestJson(this);
  1024. if (!json) {
  1025. return req.onerror(e);
  1026. }
  1027. let html = '<li class="item"><label><input class="checkboxTag" name="t_0" type="checkbox" /> <input type="text" name="newTag" /></label></li>';
  1028. if (json && json.length) {
  1029. for (let i = 0; i < json.length; i++) {
  1030. const tag = json[i];
  1031. html += '<li class="item"><label><input class="checkboxTag" name="t_' + tag.id + '" type="checkbox"' +
  1032. (tag.checked ? ' checked="checked"' : '') + '> ' + tag.name + '</label></li>';
  1033. }
  1034. }
  1035. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1036. };
  1037. req.send();
  1038. }
  1039. // <actualize>
  1040. var feed_processed = 0;
  1041. function updateFeed(feeds, feeds_count) {
  1042. const feed = feeds.pop();
  1043. if (!feed) {
  1044. return;
  1045. }
  1046. const req = new XMLHttpRequest();
  1047. req.open('POST', feed.url, true);
  1048. req.onloadend = function (e) {
  1049. if (this.status != 200) {
  1050. return badAjax(false);
  1051. }
  1052. feed_processed++;
  1053. const div = document.getElementById('actualizeProgress');
  1054. div.querySelector('.progress').innerHTML = feed_processed + ' / ' + feeds_count;
  1055. div.querySelector('.title').innerHTML = feed.title;
  1056. if (feed_processed === feeds_count) {
  1057. //Empty request to commit new articles
  1058. const req2 = new XMLHttpRequest();
  1059. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1060. req2.onloadend = function (e) {
  1061. delayedFunction(function () { location.reload(); });
  1062. };
  1063. req2.setRequestHeader('Content-Type', 'application/json');
  1064. req2.send(JSON.stringify({
  1065. _csrf: context.csrf,
  1066. noCommit: 0,
  1067. }));
  1068. } else {
  1069. updateFeed(feeds, feeds_count);
  1070. }
  1071. };
  1072. req.setRequestHeader('Content-Type', 'application/json');
  1073. req.send(JSON.stringify({
  1074. _csrf: context.csrf,
  1075. noCommit: 1,
  1076. }));
  1077. }
  1078. function init_actualize() {
  1079. let auto = false;
  1080. document.getElementById('actualize').onclick = function () {
  1081. if (context.ajax_loading) {
  1082. return false;
  1083. }
  1084. context.ajax_loading = true;
  1085. const req = new XMLHttpRequest();
  1086. req.open('POST', './?c=javascript&a=actualize', true);
  1087. req.responseType = 'json';
  1088. req.onload = function (e) {
  1089. if (this.status != 200) {
  1090. return badAjax(false);
  1091. }
  1092. const json = xmlHttpRequestJson(this);
  1093. if (!json) {
  1094. return badAjax(false);
  1095. }
  1096. if (auto && json.feeds.length < 1) {
  1097. auto = false;
  1098. context.ajax_loading = false;
  1099. return false;
  1100. }
  1101. if (json.feeds.length === 0) {
  1102. openNotification(json.feedback_no_refresh, 'good');
  1103. //Empty request to commit new articles
  1104. const req2 = new XMLHttpRequest();
  1105. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1106. req2.onloadend = function (e) {
  1107. context.ajax_loading = false;
  1108. };
  1109. req2.setRequestHeader('Content-Type', 'application/json');
  1110. req2.send(JSON.stringify({
  1111. _csrf: context.csrf,
  1112. noCommit: 0,
  1113. }));
  1114. return;
  1115. }
  1116. //Progress bar
  1117. const feeds_count = json.feeds.length;
  1118. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1119. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1120. feeds_count + '</span></div>');
  1121. for (let i = 10; i > 0; i--) {
  1122. updateFeed(json.feeds, feeds_count);
  1123. }
  1124. };
  1125. req.setRequestHeader('Content-Type', 'application/json');
  1126. req.send(JSON.stringify({
  1127. _csrf: context.csrf,
  1128. }));
  1129. return false;
  1130. };
  1131. if (context.auto_actualize_feeds) {
  1132. auto = true;
  1133. document.getElementById('actualize').click();
  1134. }
  1135. }
  1136. // </actualize>
  1137. // <notification>
  1138. var notification = null,
  1139. notification_interval = null,
  1140. notification_working = false;
  1141. function openNotification(msg, status) {
  1142. if (notification_working === true) {
  1143. return false;
  1144. }
  1145. notification_working = true;
  1146. notification.querySelector('.msg').innerHTML = msg;
  1147. notification.className = 'notification';
  1148. notification.classList.add(status);
  1149. notification_interval = setTimeout(closeNotification, 4000);
  1150. }
  1151. function closeNotification() {
  1152. notification.classList.add('closed');
  1153. clearInterval(notification_interval);
  1154. notification_working = false;
  1155. }
  1156. function init_notifications() {
  1157. notification = document.getElementById('notification');
  1158. notification.querySelector('a.close').onclick = function () {
  1159. closeNotification();
  1160. return false;
  1161. };
  1162. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1163. notification_working = true;
  1164. notification_interval = setTimeout(closeNotification, 4000);
  1165. }
  1166. }
  1167. // </notification>
  1168. // <popup>
  1169. let popup = null,
  1170. popup_iframe_container = null,
  1171. popup_iframe = null,
  1172. popup_txt = null,
  1173. popup_working = false;
  1174. function openPopupWithMessage(msg) {
  1175. if (popup_working === true) {
  1176. return false;
  1177. }
  1178. popup_working = true;
  1179. popup_txt.innerHTML = msg;
  1180. popup_txt.style.display = 'table-row';
  1181. popup.style.display = 'block';
  1182. }
  1183. function openPopupWithSource(source) {
  1184. if (popup_working === true) {
  1185. return false;
  1186. }
  1187. popup_working = true;
  1188. popup_iframe.src = source;
  1189. popup_iframe_container.style.display = 'table-row';
  1190. popup.style.display = 'block';
  1191. }
  1192. function closePopup() {
  1193. popup.style.display = 'none';
  1194. popup_iframe_container.style.display = 'none';
  1195. popup_txt.style.display = 'none';
  1196. popup_iframe.src = 'about:blank';
  1197. popup_working = false;
  1198. }
  1199. function init_popup() {
  1200. //Fetch elements.
  1201. popup = document.getElementById('popup');
  1202. popup_iframe_container = document.getElementById('popup-iframe-container');
  1203. popup_iframe = document.getElementById('popup-iframe');
  1204. popup_txt = document.getElementById('popup-txt');
  1205. //Configure close button.
  1206. document.getElementById('popup-close').addEventListener('click', function (ev) {
  1207. closePopup();
  1208. });
  1209. //Configure close-on-click.
  1210. window.addEventListener('click', function (ev) {
  1211. if (ev.target == popup) {
  1212. closePopup();
  1213. }
  1214. });
  1215. }
  1216. // </popup>
  1217. // <notifs html5>
  1218. var notifs_html5_permission = 'denied';
  1219. function notifs_html5_is_supported() {
  1220. return window.Notification !== undefined;
  1221. }
  1222. function notifs_html5_ask_permission() {
  1223. window.Notification.requestPermission(function () {
  1224. notifs_html5_permission = window.Notification.permission;
  1225. });
  1226. }
  1227. function notifs_html5_show(nb) {
  1228. if (notifs_html5_permission !== 'granted') {
  1229. return;
  1230. }
  1231. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1232. icon: '../themes/icons/favicon-256.png',
  1233. body: context.i18n.notif_body_articles.replace('%d', nb),
  1234. tag: 'freshRssNewArticles',
  1235. });
  1236. notification.onclick = function () {
  1237. delayedFunction(function() {
  1238. location.reload();
  1239. window.focus();
  1240. notification.close();
  1241. });
  1242. };
  1243. if (context.html5_notif_timeout !== 0) {
  1244. setTimeout(function () {
  1245. notification.close();
  1246. }, context.html5_notif_timeout * 1000);
  1247. }
  1248. }
  1249. function init_notifs_html5() {
  1250. if (!notifs_html5_is_supported()) {
  1251. return;
  1252. }
  1253. notifs_html5_permission = notifs_html5_ask_permission();
  1254. }
  1255. // </notifs html5>
  1256. function refreshUnreads() {
  1257. const req = new XMLHttpRequest();
  1258. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1259. req.responseType = 'json';
  1260. req.onload = function (e) {
  1261. const json = xmlHttpRequestJson(this);
  1262. if (!json) {
  1263. return badAjax(false);
  1264. }
  1265. const isAll = document.querySelector('.category.all.active');
  1266. let new_articles = false;
  1267. Object.keys(json.feeds).forEach(function (feed_id) {
  1268. const nbUnreads = json.feeds[feed_id];
  1269. feed_id = 'f_' + feed_id;
  1270. const elem = document.getElementById(feed_id),
  1271. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1272. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  1273. (nbUnreads - feed_unreads > 0)) {
  1274. const newArticle = document.getElementById('new-article');
  1275. newArticle.setAttribute('aria-hidden', 'false');
  1276. newArticle.style.display = 'block';
  1277. new_articles = true;
  1278. }
  1279. });
  1280. let nbUnreadTags = 0;
  1281. Object.keys(json.tags).forEach(function (tag_id) {
  1282. const nbUnreads = json.tags[tag_id];
  1283. nbUnreadTags += nbUnreads;
  1284. const tag = document.getElementById('t_' + tag_id);
  1285. if (tag) {
  1286. tag.setAttribute('data-unread', nbUnreads);
  1287. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1288. }
  1289. });
  1290. const tags = document.querySelector('.category.tags');
  1291. if (tags) {
  1292. tags.setAttribute('data-unread', nbUnreadTags);
  1293. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1294. }
  1295. const title = document.querySelector('.category.all .title'),
  1296. nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1297. if (nb_unreads > 0 && new_articles) {
  1298. faviconNbUnread(nb_unreads);
  1299. notifs_html5_show(nb_unreads);
  1300. }
  1301. };
  1302. req.send();
  1303. }
  1304. //<endless_mode>
  1305. var url_load_more = '',
  1306. load_more = false,
  1307. box_load_more = null;
  1308. function load_more_posts() {
  1309. if (load_more || !url_load_more || !box_load_more) {
  1310. return;
  1311. }
  1312. load_more = true;
  1313. document.getElementById('load_more').classList.add('loading');
  1314. const req = new XMLHttpRequest();
  1315. req.open('GET', url_load_more, true);
  1316. req.responseType = 'document';
  1317. req.onload = function (e) {
  1318. const html = this.response,
  1319. formPagination = document.getElementById('mark-read-pagination');
  1320. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1321. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1322. box_load_more.insertBefore(div, formPagination);
  1323. });
  1324. const paginationOld = formPagination.querySelector('.pagination'),
  1325. paginationNew = streamAdopted.querySelector('.pagination');
  1326. formPagination.replaceChild(paginationNew, paginationOld);
  1327. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1328. if (bigMarkAsRead) {
  1329. if (context.display_order === 'ASC') {
  1330. document.querySelector('#nav_menu_read_all .read_all').formAction = bigMarkAsRead.formAction;
  1331. } else {
  1332. bigMarkAsRead.formAction = document.querySelector('#nav_menu_read_all .read_all').formAction;
  1333. }
  1334. }
  1335. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1336. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1337. for (let i = ids.length - 1; i > 0; i--) { //Keep only the first
  1338. ids[i].remove();
  1339. }
  1340. });
  1341. init_load_more(box_load_more);
  1342. const div_load_more = document.getElementById('load_more');
  1343. if (bigMarkAsRead) {
  1344. bigMarkAsRead.removeAttribute('disabled');
  1345. }
  1346. if (div_load_more) {
  1347. div_load_more.classList.remove('loading');
  1348. }
  1349. load_more = false;
  1350. };
  1351. req.send();
  1352. }
  1353. var freshrssLoadMoreEvent = document.createEvent('Event');
  1354. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1355. function init_load_more(box) {
  1356. box_load_more = box;
  1357. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1358. const next_link = document.getElementById('load_more');
  1359. if (!next_link) {
  1360. // no more article to load
  1361. url_load_more = '';
  1362. return;
  1363. }
  1364. url_load_more = next_link.href;
  1365. next_link.onclick = function (e) {
  1366. load_more_posts();
  1367. return false;
  1368. };
  1369. }
  1370. //</endless_mode>
  1371. function init_confirm_action() {
  1372. document.body.onclick = function (ev) {
  1373. const b = ev.target.closest('.confirm');
  1374. if (b) {
  1375. let str_confirmation = this.getAttribute('data-str-confirm');
  1376. if (!str_confirmation) {
  1377. str_confirmation = context.i18n.confirmation_default;
  1378. }
  1379. return confirm(str_confirmation);
  1380. }
  1381. };
  1382. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1383. }
  1384. function faviconNbUnread(n) {
  1385. if (typeof n === 'undefined') {
  1386. const t = document.querySelector('.category.all .title');
  1387. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1388. }
  1389. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1390. const canvas = document.createElement('canvas'),
  1391. link = document.getElementById('favicon').cloneNode(true),
  1392. ratio = window.devicePixelRatio;
  1393. if (canvas.getContext && link) {
  1394. canvas.height = canvas.width = 16 * ratio;
  1395. const img = document.createElement('img');
  1396. img.onload = function () {
  1397. const ctx = canvas.getContext('2d');
  1398. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1399. if (n > 0) {
  1400. let text = '';
  1401. if (n < 1000) {
  1402. text = n;
  1403. } else if (n < 100000) {
  1404. text = Math.floor(n / 1000) + 'k';
  1405. } else {
  1406. text = 'E' + Math.floor(Math.log10(n));
  1407. }
  1408. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1409. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1410. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1411. ctx.fillStyle = '#F00';
  1412. ctx.fillText(text, 0, canvas.height - 1);
  1413. }
  1414. link.href = canvas.toDataURL('image/png');
  1415. document.querySelector('link[rel~=icon]').remove();
  1416. document.head.appendChild(link);
  1417. };
  1418. img.src = '../favicon.ico';
  1419. }
  1420. }
  1421. function removeFirstLoadSpinner() {
  1422. const first_load = document.getElementById('first_load');
  1423. if (first_load) {
  1424. first_load.remove();
  1425. }
  1426. }
  1427. function init_normal() {
  1428. const stream = document.getElementById('stream');
  1429. if (!stream) {
  1430. if (window.console) {
  1431. console.log('FreshRSS waiting for content…');
  1432. }
  1433. setTimeout(init_normal, 100);
  1434. return;
  1435. }
  1436. init_column_categories();
  1437. init_stream(stream);
  1438. init_shortcuts();
  1439. init_actualize();
  1440. faviconNbUnread();
  1441. window.onbeforeunload = function (e) {
  1442. const sidebar = document.getElementById('sidebar');
  1443. if (sidebar) { //Save sidebar scroll position
  1444. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1445. }
  1446. if (mark_read_queue && mark_read_queue.length > 0) {
  1447. return false;
  1448. }
  1449. };
  1450. }
  1451. function init_beforeDOM() {
  1452. document.scrollingElement.scrollTop = 0;
  1453. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1454. init_normal();
  1455. }
  1456. }
  1457. function init_afterDOM() {
  1458. removeFirstLoadSpinner();
  1459. init_notifications();
  1460. init_popup();
  1461. init_confirm_action();
  1462. const stream = document.getElementById('stream');
  1463. if (stream) {
  1464. init_load_more(stream);
  1465. init_posts();
  1466. init_nav_entries();
  1467. init_notifs_html5();
  1468. setTimeout(faviconNbUnread, 1000);
  1469. setInterval(refreshUnreads, 120000);
  1470. }
  1471. if (window.console) {
  1472. console.log('FreshRSS main init done.');
  1473. }
  1474. }
  1475. init_beforeDOM(); //Can be called before DOM is fully loaded
  1476. if (document.readyState && document.readyState !== 'loading') {
  1477. init_afterDOM();
  1478. } else {
  1479. if (window.console) {
  1480. console.log('FreshRSS waiting for DOMContentLoaded…');
  1481. }
  1482. document.addEventListener('DOMContentLoaded', init_afterDOM, false);
  1483. }
  1484. // @license-end