main.js 41 KB

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