main.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  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 init_column_categories() {
  638. if (context.current_view !== 'normal' && context.current_view !== 'reader') {
  639. return;
  640. }
  641. document.getElementById('aside_feed').onclick = function (ev) {
  642. let a = ev.target.closest('.tree-folder > .tree-folder-title > a.dropdown-toggle');
  643. if (a) {
  644. const img = a.querySelector('img');
  645. if (img.alt === '▽') {
  646. img.src = img.src.replace('/icons/down.', '/icons/up.');
  647. img.alt = '△';
  648. } else {
  649. img.src = img.src.replace('/icons/up.', '/icons/down.');
  650. img.alt = '▽';
  651. }
  652. const ul = a.closest('li').querySelector('.tree-folder-items');
  653. let nbVisibleItems = 0;
  654. for (let i = ul.children.length - 1; i >= 0; i--) {
  655. if (ul.children[i].offsetHeight) {
  656. nbVisibleItems++;
  657. }
  658. }
  659. ul.classList.toggle('active');
  660. //CSS transition does not work on max-height:auto
  661. ul.style.maxHeight = ul.classList.contains('active') ? (nbVisibleItems * 4) + 'em' : 0;
  662. return false;
  663. }
  664. a = ev.target.closest('.tree-folder-items > .feed .dropdown-toggle');
  665. if (a) {
  666. const itemId = a.closest('.item').id,
  667. templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template',
  668. id = itemId.substr(2),
  669. feed_web = a.getAttribute('data-fweb'),
  670. div = a.parentElement,
  671. dropdownMenu = div.querySelector('.dropdown-menu'),
  672. template = document.getElementById(templateId)
  673. .innerHTML.replace(/------/g, id).replace('http://example.net/', feed_web);
  674. if (!dropdownMenu) {
  675. a.href = '#dropdown-' + id;
  676. div.querySelector('.dropdown-target').id = 'dropdown-' + id;
  677. div.insertAdjacentHTML('beforeend', template);
  678. const b = div.querySelector('button.confirm');
  679. if (b) {
  680. b.disabled = false;
  681. }
  682. } else if (getComputedStyle(dropdownMenu).display === 'none') {
  683. const id2 = div.closest('.item').id.substr(2);
  684. a.href = '#dropdown-' + id2;
  685. } else {
  686. a.href = '#close';
  687. }
  688. return true;
  689. }
  690. return true;
  691. };
  692. }
  693. function init_shortcuts() {
  694. Object.keys(context.shortcuts).forEach(function (k) {
  695. context.shortcuts[k] = (context.shortcuts[k] || '').toUpperCase();
  696. });
  697. document.body.onkeydown = function (ev) {
  698. if (ev.target.closest('input, textarea') ||
  699. ev.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey)) {
  700. return true;
  701. }
  702. const s = context.shortcuts;
  703. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  704. //IE11
  705. if (k === 'SPACEBAR') k = 'SPACE';
  706. else if (k === 'DEL') k = 'DELETE';
  707. else if (k === 'ESC') k = 'ESCAPE';
  708. if (location.hash.match(/^#dropdown-/)) {
  709. const n = parseInt(k);
  710. if (n) {
  711. if (location.hash === '#dropdown-query') {
  712. user_filter(n);
  713. } else {
  714. auto_share(n);
  715. }
  716. return false;
  717. }
  718. }
  719. if (k === s.next_entry) {
  720. if (ev.altKey) {
  721. next_category();
  722. } else if (ev.shiftKey) {
  723. next_feed();
  724. } else {
  725. next_entry(false);
  726. }
  727. return false;
  728. }
  729. if (k === s.prev_entry) {
  730. if (ev.altKey) {
  731. prev_category();
  732. } else if (ev.shiftKey) {
  733. prev_feed();
  734. } else {
  735. prev_entry(false);
  736. }
  737. return false;
  738. }
  739. if (k === s.mark_read) {
  740. if (ev.altKey) {
  741. mark_previous_read(document.querySelector('.flux.current'));
  742. } else if (ev.shiftKey) {
  743. document.querySelector('.nav_menu .read_all').click();
  744. } else { // Toggle the read state
  745. mark_read(document.querySelector('.flux.current'), false, false);
  746. }
  747. return false;
  748. }
  749. if (k === s.first_entry) {
  750. if (ev.altKey) {
  751. first_category();
  752. } else if (ev.shiftKey) {
  753. first_feed();
  754. } else {
  755. const old_active = document.querySelector('.flux.current'),
  756. first = document.querySelector('.flux');
  757. if (first.classList.contains('flux')) {
  758. toggleContent(first, old_active, false);
  759. }
  760. }
  761. return false;
  762. }
  763. if (k === s.last_entry) {
  764. if (ev.altKey) {
  765. last_category();
  766. } else if (ev.shiftKey) {
  767. last_feed();
  768. } else {
  769. const old_active = document.querySelector('.flux.current'),
  770. last = document.querySelector('.flux:last-of-type');
  771. if (last.classList.contains('flux')) {
  772. toggleContent(last, old_active, false);
  773. }
  774. }
  775. return false;
  776. }
  777. if (ev.altKey || ev.shiftKey) {
  778. return true;
  779. }
  780. if (k === s.mark_favorite) { // Toggle the favorite state
  781. mark_favorite(document.querySelector('.flux.current'));
  782. return false;
  783. }
  784. if (k === s.go_website) {
  785. if (context.auto_mark_site) {
  786. mark_read(document.querySelector('.flux.current'), true, false);
  787. }
  788. const newWindow = window.open();
  789. if (newWindow) {
  790. newWindow.opener = null;
  791. newWindow.location = document.querySelector('.flux.current a.go_website').href;
  792. }
  793. return false;
  794. }
  795. if (k === s.skip_next_entry) { next_entry(true); return false; }
  796. if (k === s.skip_prev_entry) { prev_entry(true); return false; }
  797. if (k === s.collapse_entry) { collapse_entry(); return false; }
  798. if (k === s.auto_share) { auto_share(); return false; }
  799. if (k === s.user_filter) { user_filter(); return false; }
  800. if (k === s.load_more) { load_more_posts(); return false; }
  801. if (k === s.close_dropdown) { location.hash = null; return false; }
  802. if (k === s.help) { window.open(context.urls.help); return false; }
  803. if (k === s.focus_search) { document.getElementById('search').focus(); return false; }
  804. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); return false; }
  805. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); return false; }
  806. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); return false; }
  807. if (k === s.rss_view) { delayedClick(document.querySelector('#nav_menu_views .view-rss')); return false; }
  808. if (k === s.toggle_media) { toggle_media(); return false;}
  809. return true;
  810. };
  811. }
  812. function init_stream(stream) {
  813. stream.onclick = function (ev) {
  814. let el = ev.target.closest('.flux a.read');
  815. if (el) {
  816. mark_read(el.closest('.flux'), false, false);
  817. return false;
  818. }
  819. el = ev.target.closest('.flux a.bookmark');
  820. if (el) {
  821. mark_favorite(el.closest('.flux'));
  822. return false;
  823. }
  824. el = ev.target.closest('.dynamictags');
  825. if (el) {
  826. loadDynamicTags(el);
  827. return true;
  828. }
  829. el = ev.target.closest('.item.title > a');
  830. if (el) { // Allow default control-click behaviour such as open in backround-tab
  831. return ev.ctrlKey;
  832. }
  833. el = ev.target.closest('.flux .content a');
  834. if (el) {
  835. if (!el.closest('div').classList.contains('author')) {
  836. el.target = '_blank';
  837. el.rel = 'noreferrer';
  838. }
  839. return true;
  840. }
  841. el = ev.target.closest('.item.share > a[href="#"]');
  842. if (el) { //Print
  843. const tmp_window = window.open();
  844. for (var i = 0; i < document.styleSheets.length; i++) {
  845. tmp_window.document.writeln('<link href="' + document.styleSheets[i].href + '" rel="stylesheet" type="text/css" />');
  846. }
  847. tmp_window.document.writeln(el.closest('.flux_content').querySelector('.content').innerHTML);
  848. tmp_window.document.close();
  849. tmp_window.focus();
  850. tmp_window.print();
  851. tmp_window.close();
  852. return false;
  853. }
  854. el = ev.target.closest('.item.share > a[href="POST"]');
  855. if (el) { //Share by POST
  856. const f = el.parentElement.querySelector('form');
  857. f.disabled = false;
  858. f.submit();
  859. return false;
  860. }
  861. el = ev.target.closest('.flux_header, .flux_content');
  862. if (el) { //flux_toggle
  863. if (ev.target.closest('.content, .item.website, .item.link, .dropdown-menu')) {
  864. return true;
  865. }
  866. if (!context.sides_close_article && ev.target.matches('div.flux_content')) {
  867. // setting for not-closing after clicking outside article area
  868. return false;
  869. }
  870. const old_active = document.querySelector('.flux.current'),
  871. new_active = el.parentNode;
  872. if (ev.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  873. if (context.auto_mark_article) {
  874. mark_read(new_active, true, false);
  875. }
  876. return true;
  877. }
  878. toggleContent(new_active, old_active, false);
  879. return false;
  880. }
  881. };
  882. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  883. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  884. return;
  885. }
  886. let el = ev.target.closest('.item.title > a');
  887. if (el) {
  888. if (ev.which == 1) {
  889. if (ev.ctrlKey) { //Control+click
  890. if (context.auto_mark_site) {
  891. mark_read(el.closest('.flux'), true, false);
  892. }
  893. } else {
  894. el.parentElement.click(); //Normal click, just toggle article.
  895. }
  896. } else if (ev.which == 2 && !ev.ctrlKey) { //Simple middle click: same behaviour as CTRL+click
  897. if (context.auto_mark_article) {
  898. const new_active = el.closest('.flux');
  899. mark_read(new_active, true, false);
  900. }
  901. }
  902. return;
  903. }
  904. if (context.auto_mark_site) {
  905. // catch mouseup instead of click so we can have the correct behaviour
  906. // with middle button click (scroll button).
  907. el = ev.target.closest('.flux .link > a');
  908. if (el) {
  909. if (ev.which == 3) {
  910. return;
  911. }
  912. mark_read(el.closest('.flux'), true, false);
  913. }
  914. }
  915. };
  916. stream.onchange = function (ev) {
  917. const checkboxTag = ev.target.closest('.checkboxTag');
  918. if (checkboxTag) { //Dynamic tags
  919. ev.stopPropagation();
  920. const isChecked = checkboxTag.checked,
  921. tagId = checkboxTag.name.replace(/^t_/, ''),
  922. tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.value : '',
  923. entry = checkboxTag.closest('div.flux'),
  924. entryId = entry.id.replace(/^flux_/, '');
  925. checkboxTag.disabled = true;
  926. const req = new XMLHttpRequest();
  927. req.open('POST', './?c=tag&a=tagEntry', true);
  928. req.responseType = 'json';
  929. req.onerror = function (e) {
  930. checkboxTag.checked = !isChecked;
  931. badAjax(this.status == 403);
  932. };
  933. req.onload = function (e) {
  934. if (this.status != 200) {
  935. return req.onerror(e);
  936. }
  937. if (entry.classList.contains('not_read')) {
  938. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  939. }
  940. };
  941. req.onloadend = function (e) {
  942. checkboxTag.disabled = false;
  943. if (tagId == 0) {
  944. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  945. }
  946. };
  947. req.setRequestHeader('Content-Type', 'application/json');
  948. req.send(JSON.stringify({
  949. _csrf: context.csrf,
  950. id_tag: tagId,
  951. name_tag: tagId == 0 ? tagName : '',
  952. id_entry: entryId,
  953. checked: isChecked,
  954. }));
  955. }
  956. };
  957. }
  958. function init_nav_entries() {
  959. const nav_entries = document.getElementById('nav_entries');
  960. if (nav_entries) {
  961. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  962. prev_entry(false);
  963. return false;
  964. };
  965. nav_entries.querySelector('.next_entry').onclick = function (e) {
  966. next_entry(false);
  967. return false;
  968. };
  969. nav_entries.querySelector('.up').onclick = function (e) {
  970. const active_item = document.querySelector('.flux.current'),
  971. windowTop = document.scrollingElement.scrollTop,
  972. item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  973. document.scrollingElement.scrollTop = windowTop > item_top ? item_top : 0;
  974. return false;
  975. };
  976. }
  977. }
  978. function loadDynamicTags(div) {
  979. div.classList.remove('dynamictags');
  980. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  981. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  982. const req = new XMLHttpRequest();
  983. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  984. req.responseType = 'json';
  985. req.onerror = function (e) {
  986. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  987. div.classList.add('dynamictags');
  988. };
  989. req.onload = function (e) {
  990. if (this.status != 200) {
  991. return req.onerror(e);
  992. }
  993. const json = xmlHttpRequestJson(this);
  994. if (!json) {
  995. return req.onerror(e);
  996. }
  997. let html = '<li class="item"><label><input class="checkboxTag" name="t_0" type="checkbox" /> <input type="text" name="newTag" /></label></li>';
  998. if (json && json.length) {
  999. for (let i = 0; i < json.length; i++) {
  1000. const tag = json[i];
  1001. html += '<li class="item"><label><input class="checkboxTag" name="t_' + tag.id + '" type="checkbox"' +
  1002. (tag.checked ? ' checked="checked"' : '') + '> ' + tag.name + '</label></li>';
  1003. }
  1004. }
  1005. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1006. };
  1007. req.send();
  1008. }
  1009. // <actualize>
  1010. var feed_processed = 0;
  1011. function updateFeed(feeds, feeds_count) {
  1012. const feed = feeds.pop();
  1013. if (!feed) {
  1014. return;
  1015. }
  1016. const req = new XMLHttpRequest();
  1017. req.open('POST', feed.url, true);
  1018. req.onloadend = function (e) {
  1019. if (this.status != 200) {
  1020. return badAjax(false);
  1021. }
  1022. feed_processed++;
  1023. const div = document.getElementById('actualizeProgress');
  1024. div.querySelector('.progress').innerHTML = feed_processed + ' / ' + feeds_count;
  1025. div.querySelector('.title').innerHTML = feed.title;
  1026. if (feed_processed === feeds_count) {
  1027. //Empty request to commit new articles
  1028. const req2 = new XMLHttpRequest();
  1029. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1030. req2.onloadend = function (e) {
  1031. delayedFunction(function () { location.reload(); });
  1032. };
  1033. req2.setRequestHeader('Content-Type', 'application/json');
  1034. req2.send(JSON.stringify({
  1035. _csrf: context.csrf,
  1036. noCommit: 0,
  1037. }));
  1038. } else {
  1039. updateFeed(feeds, feeds_count);
  1040. }
  1041. };
  1042. req.setRequestHeader('Content-Type', 'application/json');
  1043. req.send(JSON.stringify({
  1044. _csrf: context.csrf,
  1045. noCommit: 1,
  1046. }));
  1047. }
  1048. function init_actualize() {
  1049. let auto = false;
  1050. document.getElementById('actualize').onclick = function () {
  1051. if (context.ajax_loading) {
  1052. return false;
  1053. }
  1054. context.ajax_loading = true;
  1055. const req = new XMLHttpRequest();
  1056. req.open('POST', './?c=javascript&a=actualize', true);
  1057. req.responseType = 'json';
  1058. req.onload = function (e) {
  1059. if (this.status != 200) {
  1060. return badAjax(false);
  1061. }
  1062. const json = xmlHttpRequestJson(this);
  1063. if (!json) {
  1064. return badAjax(false);
  1065. }
  1066. if (auto && json.feeds.length < 1) {
  1067. auto = false;
  1068. context.ajax_loading = false;
  1069. return false;
  1070. }
  1071. if (json.feeds.length === 0) {
  1072. openNotification(json.feedback_no_refresh, 'good');
  1073. //Empty request to commit new articles
  1074. const req2 = new XMLHttpRequest();
  1075. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1076. req2.onloadend = function (e) {
  1077. context.ajax_loading = false;
  1078. };
  1079. req2.setRequestHeader('Content-Type', 'application/json');
  1080. req2.send(JSON.stringify({
  1081. _csrf: context.csrf,
  1082. noCommit: 0,
  1083. }));
  1084. return;
  1085. }
  1086. //Progress bar
  1087. const feeds_count = json.feeds.length;
  1088. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1089. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1090. feeds_count + '</span></div>');
  1091. for (let i = 10; i > 0; i--) {
  1092. updateFeed(json.feeds, feeds_count);
  1093. }
  1094. };
  1095. req.setRequestHeader('Content-Type', 'application/json');
  1096. req.send(JSON.stringify({
  1097. _csrf: context.csrf,
  1098. }));
  1099. return false;
  1100. };
  1101. if (context.auto_actualize_feeds) {
  1102. auto = true;
  1103. document.getElementById('actualize').click();
  1104. }
  1105. }
  1106. // </actualize>
  1107. // <notification>
  1108. var notification = null,
  1109. notification_interval = null,
  1110. notification_working = false;
  1111. function openNotification(msg, status) {
  1112. if (notification_working === true) {
  1113. return false;
  1114. }
  1115. notification_working = true;
  1116. notification.querySelector('.msg').innerHTML = msg;
  1117. notification.className = 'notification';
  1118. notification.classList.add(status);
  1119. notification_interval = setTimeout(closeNotification, 4000);
  1120. }
  1121. function closeNotification() {
  1122. notification.classList.add('closed');
  1123. clearInterval(notification_interval);
  1124. notification_working = false;
  1125. }
  1126. function init_notifications() {
  1127. notification = document.getElementById('notification');
  1128. notification.querySelector('a.close').onclick = function () {
  1129. closeNotification();
  1130. return false;
  1131. };
  1132. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1133. notification_working = true;
  1134. notification_interval = setTimeout(closeNotification, 4000);
  1135. }
  1136. }
  1137. // </notification>
  1138. // <popup>
  1139. let popup = null,
  1140. popup_iframe_container = null,
  1141. popup_iframe = null,
  1142. popup_txt = null,
  1143. popup_working = false;
  1144. function openPopupWithMessage(msg) {
  1145. if (popup_working === true) {
  1146. return false;
  1147. }
  1148. popup_working = true;
  1149. popup_txt.innerHTML = msg;
  1150. popup_txt.style.display = 'table-row';
  1151. popup.style.display = 'block';
  1152. }
  1153. function openPopupWithSource(source) {
  1154. if (popup_working === true) {
  1155. return false;
  1156. }
  1157. popup_working = true;
  1158. popup_iframe.src = source;
  1159. popup_iframe_container.style.display = 'table-row';
  1160. popup.style.display = 'block';
  1161. }
  1162. function closePopup() {
  1163. popup.style.display = 'none';
  1164. popup_iframe_container.style.display = 'none';
  1165. popup_txt.style.display = 'none';
  1166. popup_iframe.src = 'about:blank';
  1167. popup_working = false;
  1168. }
  1169. function init_popup() {
  1170. //Fetch elements.
  1171. popup = document.getElementById('popup');
  1172. popup_iframe_container = document.getElementById('popup-iframe-container');
  1173. popup_iframe = document.getElementById('popup-iframe');
  1174. popup_txt = document.getElementById('popup-txt');
  1175. //Configure close button.
  1176. document.getElementById('popup-close').addEventListener('click', function (ev) {
  1177. closePopup();
  1178. });
  1179. //Configure close-on-click.
  1180. window.addEventListener('click', function (ev) {
  1181. if (ev.target == popup) {
  1182. closePopup();
  1183. }
  1184. });
  1185. }
  1186. // </popup>
  1187. // <notifs html5>
  1188. var notifs_html5_permission = 'denied';
  1189. function notifs_html5_is_supported() {
  1190. return window.Notification !== undefined;
  1191. }
  1192. function notifs_html5_ask_permission() {
  1193. window.Notification.requestPermission(function () {
  1194. notifs_html5_permission = window.Notification.permission;
  1195. });
  1196. }
  1197. function notifs_html5_show(nb) {
  1198. if (notifs_html5_permission !== 'granted') {
  1199. return;
  1200. }
  1201. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1202. icon: '../themes/icons/favicon-256.png',
  1203. body: context.i18n.notif_body_articles.replace('%d', nb),
  1204. tag: 'freshRssNewArticles',
  1205. });
  1206. notification.onclick = function () {
  1207. delayedFunction(function() {
  1208. location.reload();
  1209. window.focus();
  1210. notification.close();
  1211. });
  1212. };
  1213. if (context.html5_notif_timeout !== 0) {
  1214. setTimeout(function () {
  1215. notification.close();
  1216. }, context.html5_notif_timeout * 1000);
  1217. }
  1218. }
  1219. function init_notifs_html5() {
  1220. if (!notifs_html5_is_supported()) {
  1221. return;
  1222. }
  1223. notifs_html5_permission = notifs_html5_ask_permission();
  1224. }
  1225. // </notifs html5>
  1226. function refreshUnreads() {
  1227. const req = new XMLHttpRequest();
  1228. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1229. req.responseType = 'json';
  1230. req.onload = function (e) {
  1231. const json = xmlHttpRequestJson(this);
  1232. if (!json) {
  1233. return badAjax(false);
  1234. }
  1235. const isAll = document.querySelector('.category.all.active');
  1236. let new_articles = false;
  1237. Object.keys(json.feeds).forEach(function (feed_id) {
  1238. const nbUnreads = json.feeds[feed_id];
  1239. feed_id = 'f_' + feed_id;
  1240. const elem = document.getElementById(feed_id),
  1241. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1242. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  1243. (nbUnreads - feed_unreads > 0)) {
  1244. const newArticle = document.getElementById('new-article');
  1245. newArticle.setAttribute('aria-hidden', 'false');
  1246. newArticle.style.display = 'block';
  1247. new_articles = true;
  1248. }
  1249. });
  1250. let nbUnreadTags = 0;
  1251. Object.keys(json.tags).forEach(function (tag_id) {
  1252. const nbUnreads = json.tags[tag_id];
  1253. nbUnreadTags += nbUnreads;
  1254. const tag = document.getElementById('t_' + tag_id);
  1255. if (tag) {
  1256. tag.setAttribute('data-unread', nbUnreads);
  1257. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1258. }
  1259. });
  1260. const tags = document.querySelector('.category.tags');
  1261. if (tags) {
  1262. tags.setAttribute('data-unread', nbUnreadTags);
  1263. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1264. }
  1265. const title = document.querySelector('.category.all .title'),
  1266. nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1267. if (nb_unreads > 0 && new_articles) {
  1268. faviconNbUnread(nb_unreads);
  1269. notifs_html5_show(nb_unreads);
  1270. }
  1271. };
  1272. req.send();
  1273. }
  1274. //<endless_mode>
  1275. var url_load_more = '',
  1276. load_more = false,
  1277. box_load_more = null;
  1278. function load_more_posts() {
  1279. if (load_more || !url_load_more || !box_load_more) {
  1280. return;
  1281. }
  1282. load_more = true;
  1283. document.getElementById('load_more').classList.add('loading');
  1284. const req = new XMLHttpRequest();
  1285. req.open('GET', url_load_more, true);
  1286. req.responseType = 'document';
  1287. req.onload = function (e) {
  1288. const html = this.response,
  1289. formPagination = document.getElementById('mark-read-pagination');
  1290. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1291. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1292. box_load_more.insertBefore(div, formPagination);
  1293. });
  1294. const paginationOld = formPagination.querySelector('.pagination'),
  1295. paginationNew = streamAdopted.querySelector('.pagination');
  1296. formPagination.replaceChild(paginationNew, paginationOld);
  1297. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1298. if (bigMarkAsRead) {
  1299. if (context.display_order === 'ASC') {
  1300. document.querySelector('#nav_menu_read_all .read_all').formAction = bigMarkAsRead.formAction;
  1301. } else {
  1302. bigMarkAsRead.formAction = document.querySelector('#nav_menu_read_all .read_all').formAction;
  1303. }
  1304. }
  1305. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1306. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1307. for (let i = ids.length - 1; i > 0; i--) { //Keep only the first
  1308. ids[i].remove();
  1309. }
  1310. });
  1311. init_load_more(box_load_more);
  1312. const div_load_more = document.getElementById('load_more');
  1313. if (bigMarkAsRead) {
  1314. bigMarkAsRead.removeAttribute('disabled');
  1315. }
  1316. if (div_load_more) {
  1317. div_load_more.classList.remove('loading');
  1318. }
  1319. load_more = false;
  1320. };
  1321. req.send();
  1322. }
  1323. var freshrssLoadMoreEvent = document.createEvent('Event');
  1324. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1325. function init_load_more(box) {
  1326. box_load_more = box;
  1327. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1328. const next_link = document.getElementById('load_more');
  1329. if (!next_link) {
  1330. // no more article to load
  1331. url_load_more = '';
  1332. return;
  1333. }
  1334. url_load_more = next_link.href;
  1335. next_link.onclick = function (e) {
  1336. load_more_posts();
  1337. return false;
  1338. };
  1339. }
  1340. //</endless_mode>
  1341. function init_confirm_action() {
  1342. document.body.onclick = function (ev) {
  1343. const b = ev.target.closest('.confirm');
  1344. if (b) {
  1345. let str_confirmation = this.getAttribute('data-str-confirm');
  1346. if (!str_confirmation) {
  1347. str_confirmation = context.i18n.confirmation_default;
  1348. }
  1349. return confirm(str_confirmation);
  1350. }
  1351. };
  1352. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1353. }
  1354. function faviconNbUnread(n) {
  1355. if (typeof n === 'undefined') {
  1356. const t = document.querySelector('.category.all .title');
  1357. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1358. }
  1359. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1360. const canvas = document.createElement('canvas'),
  1361. link = document.getElementById('favicon').cloneNode(true),
  1362. ratio = window.devicePixelRatio;
  1363. if (canvas.getContext && link) {
  1364. canvas.height = canvas.width = 16 * ratio;
  1365. const img = document.createElement('img');
  1366. img.onload = function () {
  1367. const ctx = canvas.getContext('2d');
  1368. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1369. if (n > 0) {
  1370. let text = '';
  1371. if (n < 1000) {
  1372. text = n;
  1373. } else if (n < 100000) {
  1374. text = Math.floor(n / 1000) + 'k';
  1375. } else {
  1376. text = 'E' + Math.floor(Math.log10(n));
  1377. }
  1378. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1379. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1380. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1381. ctx.fillStyle = '#F00';
  1382. ctx.fillText(text, 0, canvas.height - 1);
  1383. }
  1384. link.href = canvas.toDataURL('image/png');
  1385. document.querySelector('link[rel~=icon]').remove();
  1386. document.head.appendChild(link);
  1387. };
  1388. img.src = '../favicon.ico';
  1389. }
  1390. }
  1391. function removeFirstLoadSpinner() {
  1392. const first_load = document.getElementById('first_load');
  1393. if (first_load) {
  1394. first_load.remove();
  1395. }
  1396. }
  1397. function init_normal() {
  1398. const stream = document.getElementById('stream');
  1399. if (!stream) {
  1400. if (window.console) {
  1401. console.log('FreshRSS waiting for content…');
  1402. }
  1403. setTimeout(init_normal, 100);
  1404. return;
  1405. }
  1406. init_column_categories();
  1407. init_stream(stream);
  1408. init_shortcuts();
  1409. init_actualize();
  1410. faviconNbUnread();
  1411. window.onbeforeunload = function (e) {
  1412. if (mark_read_queue && mark_read_queue.length > 0) {
  1413. return false;
  1414. }
  1415. };
  1416. }
  1417. function init_beforeDOM() {
  1418. document.scrollingElement.scrollTop = 0;
  1419. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1420. init_normal();
  1421. }
  1422. }
  1423. function init_afterDOM() {
  1424. removeFirstLoadSpinner();
  1425. init_notifications();
  1426. init_popup();
  1427. init_confirm_action();
  1428. const stream = document.getElementById('stream');
  1429. if (stream) {
  1430. init_load_more(stream);
  1431. init_posts();
  1432. init_nav_entries();
  1433. init_notifs_html5();
  1434. setTimeout(faviconNbUnread, 1000);
  1435. setInterval(refreshUnreads, 120000);
  1436. }
  1437. if (window.console) {
  1438. console.log('FreshRSS main init done.');
  1439. }
  1440. }
  1441. init_beforeDOM(); //Can be called before DOM is fully loaded
  1442. if (document.readyState && document.readyState !== 'loading') {
  1443. init_afterDOM();
  1444. } else {
  1445. if (window.console) {
  1446. console.log('FreshRSS waiting for DOMContentLoaded…');
  1447. }
  1448. document.addEventListener('DOMContentLoaded', init_afterDOM, false);
  1449. }
  1450. // @license-end