main.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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[data-type="print"]');
  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[data-type="clipboard"]');
  885. if (el && navigator.clipboard) { //Clipboard
  886. navigator.clipboard.writeText(el.href);
  887. return false;
  888. }
  889. el = ev.target.closest('.item.share > a[href="POST"]');
  890. if (el) { //Share by POST
  891. const f = el.parentElement.querySelector('form');
  892. f.disabled = false;
  893. f.submit();
  894. return false;
  895. }
  896. el = ev.target.closest('.flux_header, .flux_content');
  897. if (el) { //flux_toggle
  898. if (ev.target.closest('.content, .item.website, .item.link, .dropdown-menu')) {
  899. return true;
  900. }
  901. if (!context.sides_close_article && ev.target.matches('div.flux_content')) {
  902. // setting for not-closing after clicking outside article area
  903. return false;
  904. }
  905. const old_active = document.querySelector('.flux.current'),
  906. new_active = el.parentNode;
  907. if (ev.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  908. if (context.auto_mark_article) {
  909. mark_read(new_active, true, false);
  910. }
  911. return true;
  912. }
  913. toggleContent(new_active, old_active, false);
  914. return false;
  915. }
  916. };
  917. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  918. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  919. return;
  920. }
  921. let el = ev.target.closest('.item.title > a');
  922. if (el) {
  923. if (ev.which == 1) {
  924. if (ev.ctrlKey) { //Control+click
  925. if (context.auto_mark_site) {
  926. mark_read(el.closest('.flux'), true, false);
  927. }
  928. } else {
  929. el.parentElement.click(); //Normal click, just toggle article.
  930. }
  931. } else if (ev.which == 2 && !ev.ctrlKey) { //Simple middle click: same behaviour as CTRL+click
  932. if (context.auto_mark_article) {
  933. const new_active = el.closest('.flux');
  934. mark_read(new_active, true, false);
  935. }
  936. }
  937. return;
  938. }
  939. if (context.auto_mark_site) {
  940. // catch mouseup instead of click so we can have the correct behaviour
  941. // with middle button click (scroll button).
  942. el = ev.target.closest('.flux .link > a');
  943. if (el) {
  944. if (ev.which == 3) {
  945. return;
  946. }
  947. mark_read(el.closest('.flux'), true, false);
  948. }
  949. }
  950. };
  951. stream.onchange = function (ev) {
  952. const checkboxTag = ev.target.closest('.checkboxTag');
  953. if (checkboxTag) { //Dynamic tags
  954. ev.stopPropagation();
  955. const isChecked = checkboxTag.checked,
  956. tagId = checkboxTag.name.replace(/^t_/, ''),
  957. tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.value : '',
  958. entry = checkboxTag.closest('div.flux'),
  959. entryId = entry.id.replace(/^flux_/, '');
  960. checkboxTag.disabled = true;
  961. const req = new XMLHttpRequest();
  962. req.open('POST', './?c=tag&a=tagEntry', true);
  963. req.responseType = 'json';
  964. req.onerror = function (e) {
  965. checkboxTag.checked = !isChecked;
  966. badAjax(this.status == 403);
  967. };
  968. req.onload = function (e) {
  969. if (this.status != 200) {
  970. return req.onerror(e);
  971. }
  972. if (entry.classList.contains('not_read')) {
  973. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  974. }
  975. };
  976. req.onloadend = function (e) {
  977. checkboxTag.disabled = false;
  978. if (tagId == 0) {
  979. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  980. }
  981. };
  982. req.setRequestHeader('Content-Type', 'application/json');
  983. req.send(JSON.stringify({
  984. _csrf: context.csrf,
  985. id_tag: tagId,
  986. name_tag: tagId == 0 ? tagName : '',
  987. id_entry: entryId,
  988. checked: isChecked,
  989. }));
  990. }
  991. };
  992. }
  993. function init_nav_entries() {
  994. const nav_entries = document.getElementById('nav_entries');
  995. if (nav_entries) {
  996. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  997. prev_entry(false);
  998. return false;
  999. };
  1000. nav_entries.querySelector('.next_entry').onclick = function (e) {
  1001. next_entry(false);
  1002. return false;
  1003. };
  1004. nav_entries.querySelector('.up').onclick = function (e) {
  1005. const active_item = document.querySelector('.flux.current'),
  1006. windowTop = document.scrollingElement.scrollTop,
  1007. item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  1008. document.scrollingElement.scrollTop = windowTop > item_top ? item_top : 0;
  1009. return false;
  1010. };
  1011. }
  1012. }
  1013. function loadDynamicTags(div) {
  1014. div.classList.remove('dynamictags');
  1015. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1016. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  1017. const req = new XMLHttpRequest();
  1018. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  1019. req.responseType = 'json';
  1020. req.onerror = function (e) {
  1021. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1022. div.classList.add('dynamictags');
  1023. };
  1024. req.onload = function (e) {
  1025. if (this.status != 200) {
  1026. return req.onerror(e);
  1027. }
  1028. const json = xmlHttpRequestJson(this);
  1029. if (!json) {
  1030. return req.onerror(e);
  1031. }
  1032. let html = '<li class="item"><label><input class="checkboxTag" name="t_0" type="checkbox" /> <input type="text" name="newTag" /></label></li>';
  1033. if (json && json.length) {
  1034. for (let i = 0; i < json.length; i++) {
  1035. const tag = json[i];
  1036. html += '<li class="item"><label><input class="checkboxTag" name="t_' + tag.id + '" type="checkbox"' +
  1037. (tag.checked ? ' checked="checked"' : '') + '> ' + tag.name + '</label></li>';
  1038. }
  1039. }
  1040. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1041. };
  1042. req.send();
  1043. }
  1044. // <actualize>
  1045. var feed_processed = 0;
  1046. function updateFeed(feeds, feeds_count) {
  1047. const feed = feeds.pop();
  1048. if (!feed) {
  1049. return;
  1050. }
  1051. const req = new XMLHttpRequest();
  1052. req.open('POST', feed.url, true);
  1053. req.onloadend = function (e) {
  1054. if (this.status != 200) {
  1055. return badAjax(false);
  1056. }
  1057. feed_processed++;
  1058. const div = document.getElementById('actualizeProgress');
  1059. div.querySelector('.progress').innerHTML = feed_processed + ' / ' + feeds_count;
  1060. div.querySelector('.title').innerHTML = feed.title;
  1061. if (feed_processed === feeds_count) {
  1062. //Empty request to commit new articles
  1063. const req2 = new XMLHttpRequest();
  1064. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1065. req2.onloadend = function (e) {
  1066. delayedFunction(function () { location.reload(); });
  1067. };
  1068. req2.setRequestHeader('Content-Type', 'application/json');
  1069. req2.send(JSON.stringify({
  1070. _csrf: context.csrf,
  1071. noCommit: 0,
  1072. }));
  1073. } else {
  1074. updateFeed(feeds, feeds_count);
  1075. }
  1076. };
  1077. req.setRequestHeader('Content-Type', 'application/json');
  1078. req.send(JSON.stringify({
  1079. _csrf: context.csrf,
  1080. noCommit: 1,
  1081. }));
  1082. }
  1083. function init_actualize() {
  1084. let auto = false;
  1085. const actualize = document.getElementById('actualize');
  1086. if (!actualize) {
  1087. return;
  1088. }
  1089. actualize.onclick = function () {
  1090. if (context.ajax_loading) {
  1091. return false;
  1092. }
  1093. context.ajax_loading = true;
  1094. const req = new XMLHttpRequest();
  1095. req.open('POST', './?c=javascript&a=actualize', true);
  1096. req.responseType = 'json';
  1097. req.onload = function (e) {
  1098. if (this.status != 200) {
  1099. return badAjax(false);
  1100. }
  1101. const json = xmlHttpRequestJson(this);
  1102. if (!json) {
  1103. return badAjax(false);
  1104. }
  1105. if (auto && json.feeds.length < 1) {
  1106. auto = false;
  1107. context.ajax_loading = false;
  1108. return false;
  1109. }
  1110. if (json.feeds.length === 0) {
  1111. openNotification(json.feedback_no_refresh, 'good');
  1112. //Empty request to commit new articles
  1113. const req2 = new XMLHttpRequest();
  1114. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1115. req2.onloadend = function (e) {
  1116. context.ajax_loading = false;
  1117. };
  1118. req2.setRequestHeader('Content-Type', 'application/json');
  1119. req2.send(JSON.stringify({
  1120. _csrf: context.csrf,
  1121. noCommit: 0,
  1122. }));
  1123. return;
  1124. }
  1125. //Progress bar
  1126. const feeds_count = json.feeds.length;
  1127. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1128. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1129. feeds_count + '</span></div>');
  1130. for (let i = 10; i > 0; i--) {
  1131. updateFeed(json.feeds, feeds_count);
  1132. }
  1133. };
  1134. req.setRequestHeader('Content-Type', 'application/json');
  1135. req.send(JSON.stringify({
  1136. _csrf: context.csrf,
  1137. }));
  1138. return false;
  1139. };
  1140. if (context.auto_actualize_feeds) {
  1141. auto = true;
  1142. actualize.click();
  1143. }
  1144. }
  1145. // </actualize>
  1146. // <notification>
  1147. var notification = null,
  1148. notification_interval = null,
  1149. notification_working = false;
  1150. function openNotification(msg, status) {
  1151. if (notification_working === true) {
  1152. return false;
  1153. }
  1154. notification_working = true;
  1155. notification.querySelector('.msg').innerHTML = msg;
  1156. notification.className = 'notification';
  1157. notification.classList.add(status);
  1158. notification_interval = setTimeout(closeNotification, 4000);
  1159. }
  1160. function closeNotification() {
  1161. notification.classList.add('closed');
  1162. clearInterval(notification_interval);
  1163. notification_working = false;
  1164. }
  1165. function init_notifications() {
  1166. notification = document.getElementById('notification');
  1167. notification.querySelector('a.close').onclick = function () {
  1168. closeNotification();
  1169. return false;
  1170. };
  1171. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1172. notification_working = true;
  1173. notification_interval = setTimeout(closeNotification, 4000);
  1174. }
  1175. }
  1176. // </notification>
  1177. // <popup>
  1178. let popup = null,
  1179. popup_iframe_container = null,
  1180. popup_iframe = null,
  1181. popup_txt = null,
  1182. popup_working = false;
  1183. function openPopupWithMessage(msg) {
  1184. if (popup_working === true) {
  1185. return false;
  1186. }
  1187. popup_working = true;
  1188. popup_txt.innerHTML = msg;
  1189. popup_txt.style.display = 'table-row';
  1190. popup.style.display = 'block';
  1191. }
  1192. function openPopupWithSource(source) {
  1193. if (popup_working === true) {
  1194. return false;
  1195. }
  1196. popup_working = true;
  1197. popup_iframe.src = source;
  1198. popup_iframe_container.style.display = 'table-row';
  1199. popup.style.display = 'block';
  1200. }
  1201. function closePopup() {
  1202. popup.style.display = 'none';
  1203. popup_iframe_container.style.display = 'none';
  1204. popup_txt.style.display = 'none';
  1205. popup_iframe.src = 'about:blank';
  1206. popup_working = false;
  1207. }
  1208. function init_popup() {
  1209. //Fetch elements.
  1210. popup = document.getElementById('popup');
  1211. popup_iframe_container = document.getElementById('popup-iframe-container');
  1212. popup_iframe = document.getElementById('popup-iframe');
  1213. popup_txt = document.getElementById('popup-txt');
  1214. //Configure close button.
  1215. document.getElementById('popup-close').addEventListener('click', function (ev) {
  1216. closePopup();
  1217. });
  1218. //Configure close-on-click.
  1219. window.addEventListener('click', function (ev) {
  1220. if (ev.target == popup) {
  1221. closePopup();
  1222. }
  1223. });
  1224. }
  1225. // </popup>
  1226. // <notifs html5>
  1227. var notifs_html5_permission = 'denied';
  1228. function notifs_html5_is_supported() {
  1229. return window.Notification !== undefined;
  1230. }
  1231. function notifs_html5_ask_permission() {
  1232. window.Notification.requestPermission(function () {
  1233. notifs_html5_permission = window.Notification.permission;
  1234. });
  1235. }
  1236. function notifs_html5_show(nb) {
  1237. if (notifs_html5_permission !== 'granted') {
  1238. return;
  1239. }
  1240. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1241. icon: '../themes/icons/favicon-256.png',
  1242. body: context.i18n.notif_body_articles.replace('%d', nb),
  1243. tag: 'freshRssNewArticles',
  1244. });
  1245. notification.onclick = function () {
  1246. delayedFunction(function() {
  1247. location.reload();
  1248. window.focus();
  1249. notification.close();
  1250. });
  1251. };
  1252. if (context.html5_notif_timeout !== 0) {
  1253. setTimeout(function () {
  1254. notification.close();
  1255. }, context.html5_notif_timeout * 1000);
  1256. }
  1257. }
  1258. function init_notifs_html5() {
  1259. if (!notifs_html5_is_supported()) {
  1260. return;
  1261. }
  1262. notifs_html5_permission = notifs_html5_ask_permission();
  1263. }
  1264. // </notifs html5>
  1265. function refreshUnreads() {
  1266. const req = new XMLHttpRequest();
  1267. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1268. req.responseType = 'json';
  1269. req.onload = function (e) {
  1270. const json = xmlHttpRequestJson(this);
  1271. if (!json) {
  1272. return badAjax(false);
  1273. }
  1274. const isAll = document.querySelector('.category.all.active');
  1275. let new_articles = false;
  1276. Object.keys(json.feeds).forEach(function (feed_id) {
  1277. const nbUnreads = json.feeds[feed_id];
  1278. feed_id = 'f_' + feed_id;
  1279. const elem = document.getElementById(feed_id),
  1280. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1281. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  1282. (nbUnreads - feed_unreads > 0)) {
  1283. const newArticle = document.getElementById('new-article');
  1284. newArticle.setAttribute('aria-hidden', 'false');
  1285. newArticle.style.display = 'block';
  1286. new_articles = true;
  1287. }
  1288. });
  1289. let nbUnreadTags = 0;
  1290. Object.keys(json.tags).forEach(function (tag_id) {
  1291. const nbUnreads = json.tags[tag_id];
  1292. nbUnreadTags += nbUnreads;
  1293. const tag = document.getElementById('t_' + tag_id);
  1294. if (tag) {
  1295. tag.setAttribute('data-unread', nbUnreads);
  1296. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1297. }
  1298. });
  1299. const tags = document.querySelector('.category.tags');
  1300. if (tags) {
  1301. tags.setAttribute('data-unread', nbUnreadTags);
  1302. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1303. }
  1304. const title = document.querySelector('.category.all .title'),
  1305. nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1306. if (nb_unreads > 0 && new_articles) {
  1307. faviconNbUnread(nb_unreads);
  1308. notifs_html5_show(nb_unreads);
  1309. }
  1310. };
  1311. req.send();
  1312. }
  1313. //<endless_mode>
  1314. var url_load_more = '',
  1315. load_more = false,
  1316. box_load_more = null;
  1317. function load_more_posts() {
  1318. if (load_more || !url_load_more || !box_load_more) {
  1319. return;
  1320. }
  1321. load_more = true;
  1322. document.getElementById('load_more').classList.add('loading');
  1323. const req = new XMLHttpRequest();
  1324. req.open('GET', url_load_more, true);
  1325. req.responseType = 'document';
  1326. req.onload = function (e) {
  1327. const html = this.response,
  1328. formPagination = document.getElementById('mark-read-pagination');
  1329. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1330. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1331. box_load_more.insertBefore(div, formPagination);
  1332. });
  1333. const paginationOld = formPagination.querySelector('.pagination'),
  1334. paginationNew = streamAdopted.querySelector('.pagination');
  1335. formPagination.replaceChild(paginationNew, paginationOld);
  1336. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1337. if (bigMarkAsRead) {
  1338. if (context.display_order === 'ASC') {
  1339. document.querySelector('#nav_menu_read_all .read_all').formAction = bigMarkAsRead.formAction;
  1340. } else {
  1341. bigMarkAsRead.formAction = document.querySelector('#nav_menu_read_all .read_all').formAction;
  1342. }
  1343. }
  1344. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1345. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1346. for (let i = ids.length - 1; i > 0; i--) { //Keep only the first
  1347. ids[i].remove();
  1348. }
  1349. });
  1350. init_load_more(box_load_more);
  1351. const div_load_more = document.getElementById('load_more');
  1352. if (bigMarkAsRead) {
  1353. bigMarkAsRead.removeAttribute('disabled');
  1354. }
  1355. if (div_load_more) {
  1356. div_load_more.classList.remove('loading');
  1357. }
  1358. load_more = false;
  1359. };
  1360. req.send();
  1361. }
  1362. var freshrssLoadMoreEvent = document.createEvent('Event');
  1363. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1364. function init_load_more(box) {
  1365. box_load_more = box;
  1366. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1367. const next_link = document.getElementById('load_more');
  1368. if (!next_link) {
  1369. // no more article to load
  1370. url_load_more = '';
  1371. return;
  1372. }
  1373. url_load_more = next_link.href;
  1374. next_link.onclick = function (e) {
  1375. load_more_posts();
  1376. return false;
  1377. };
  1378. }
  1379. //</endless_mode>
  1380. function init_confirm_action() {
  1381. document.body.onclick = function (ev) {
  1382. const b = ev.target.closest('.confirm');
  1383. if (b) {
  1384. let str_confirmation = this.getAttribute('data-str-confirm');
  1385. if (!str_confirmation) {
  1386. str_confirmation = context.i18n.confirmation_default;
  1387. }
  1388. return confirm(str_confirmation);
  1389. }
  1390. };
  1391. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1392. }
  1393. function faviconNbUnread(n) {
  1394. if (typeof n === 'undefined') {
  1395. const t = document.querySelector('.category.all .title');
  1396. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1397. }
  1398. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1399. const canvas = document.createElement('canvas'),
  1400. link = document.getElementById('favicon').cloneNode(true),
  1401. ratio = window.devicePixelRatio;
  1402. if (canvas.getContext && link) {
  1403. canvas.height = canvas.width = 16 * ratio;
  1404. const img = document.createElement('img');
  1405. img.onload = function () {
  1406. const ctx = canvas.getContext('2d');
  1407. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1408. if (n > 0) {
  1409. let text = '';
  1410. if (n < 1000) {
  1411. text = n;
  1412. } else if (n < 100000) {
  1413. text = Math.floor(n / 1000) + 'k';
  1414. } else {
  1415. text = 'E' + Math.floor(Math.log10(n));
  1416. }
  1417. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1418. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1419. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1420. ctx.fillStyle = '#F00';
  1421. ctx.fillText(text, 0, canvas.height - 1);
  1422. }
  1423. link.href = canvas.toDataURL('image/png');
  1424. document.querySelector('link[rel~=icon]').remove();
  1425. document.head.appendChild(link);
  1426. };
  1427. img.src = '../favicon.ico';
  1428. }
  1429. }
  1430. function removeFirstLoadSpinner() {
  1431. const first_load = document.getElementById('first_load');
  1432. if (first_load) {
  1433. first_load.remove();
  1434. }
  1435. }
  1436. function init_normal() {
  1437. const stream = document.getElementById('stream');
  1438. if (!stream) {
  1439. if (window.console) {
  1440. console.log('FreshRSS waiting for content…');
  1441. }
  1442. setTimeout(init_normal, 100);
  1443. return;
  1444. }
  1445. init_column_categories();
  1446. init_stream(stream);
  1447. init_shortcuts();
  1448. init_actualize();
  1449. faviconNbUnread();
  1450. window.onbeforeunload = function (e) {
  1451. const sidebar = document.getElementById('sidebar');
  1452. if (sidebar) { //Save sidebar scroll position
  1453. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1454. }
  1455. if (mark_read_queue && mark_read_queue.length > 0) {
  1456. return false;
  1457. }
  1458. };
  1459. }
  1460. function init_beforeDOM() {
  1461. document.scrollingElement.scrollTop = 0;
  1462. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1463. init_normal();
  1464. }
  1465. }
  1466. function init_afterDOM() {
  1467. removeFirstLoadSpinner();
  1468. init_notifications();
  1469. init_popup();
  1470. init_confirm_action();
  1471. const stream = document.getElementById('stream');
  1472. if (stream) {
  1473. init_load_more(stream);
  1474. init_posts();
  1475. init_nav_entries();
  1476. init_notifs_html5();
  1477. setTimeout(faviconNbUnread, 1000);
  1478. setInterval(refreshUnreads, 120000);
  1479. }
  1480. if (window.console) {
  1481. console.log('FreshRSS main init done.');
  1482. }
  1483. }
  1484. init_beforeDOM(); //Can be called before DOM is fully loaded
  1485. if (document.readyState && document.readyState !== 'loading') {
  1486. init_afterDOM();
  1487. } else {
  1488. if (window.console) {
  1489. console.log('FreshRSS waiting for DOMContentLoaded…');
  1490. }
  1491. document.addEventListener('DOMContentLoaded', init_afterDOM, false);
  1492. }
  1493. // @license-end