main.js 41 KB

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