main.js 46 KB

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