main.js 47 KB

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