main.js 47 KB

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