main.js 42 KB

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