main.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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 init_column_categories() {
  638. if (context.current_view !== 'normal' && context.current_view !== 'reader') {
  639. return;
  640. }
  641. //Restore sidebar scroll position
  642. document.getElementById('sidebar').scrollTop = +sessionStorage.getItem('FreshRSS_sidebar_scrollTop');
  643. document.getElementById('aside_feed').onclick = function (ev) {
  644. let a = ev.target.closest('.tree-folder > .tree-folder-title > a.dropdown-toggle');
  645. if (a) {
  646. const img = a.querySelector('img');
  647. if (img.alt === '▽') {
  648. img.src = img.src.replace('/icons/down.', '/icons/up.');
  649. img.alt = '△';
  650. } else {
  651. img.src = img.src.replace('/icons/up.', '/icons/down.');
  652. img.alt = '▽';
  653. }
  654. const ul = a.closest('li').querySelector('.tree-folder-items');
  655. let nbVisibleItems = 0;
  656. for (let i = ul.children.length - 1; i >= 0; i--) {
  657. if (ul.children[i].offsetHeight) {
  658. nbVisibleItems++;
  659. }
  660. }
  661. ul.classList.toggle('active');
  662. //CSS transition does not work on max-height:auto
  663. ul.style.maxHeight = ul.classList.contains('active') ? (nbVisibleItems * 4) + 'em' : 0;
  664. return false;
  665. }
  666. a = ev.target.closest('.tree-folder-items > .feed .dropdown-toggle');
  667. if (a) {
  668. const itemId = a.closest('.item').id,
  669. templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template',
  670. id = itemId.substr(2),
  671. feed_web = a.getAttribute('data-fweb'),
  672. div = a.parentElement,
  673. dropdownMenu = div.querySelector('.dropdown-menu'),
  674. template = document.getElementById(templateId)
  675. .innerHTML.replace(/------/g, id).replace('http://example.net/', feed_web);
  676. if (!dropdownMenu) {
  677. a.href = '#dropdown-' + id;
  678. div.querySelector('.dropdown-target').id = 'dropdown-' + id;
  679. div.insertAdjacentHTML('beforeend', template);
  680. const b = div.querySelector('button.confirm');
  681. if (b) {
  682. b.disabled = false;
  683. }
  684. } else if (getComputedStyle(dropdownMenu).display === 'none') {
  685. const id2 = div.closest('.item').id.substr(2);
  686. a.href = '#dropdown-' + id2;
  687. } else {
  688. a.href = '#close';
  689. }
  690. return true;
  691. }
  692. return true;
  693. };
  694. }
  695. function init_shortcuts() {
  696. Object.keys(context.shortcuts).forEach(function (k) {
  697. context.shortcuts[k] = (context.shortcuts[k] || '').toUpperCase();
  698. });
  699. document.body.onkeydown = function (ev) {
  700. if (ev.target.closest('input, textarea') ||
  701. ev.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey)) {
  702. return true;
  703. }
  704. const s = context.shortcuts;
  705. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  706. //IE11
  707. if (k === 'SPACEBAR') k = 'SPACE';
  708. else if (k === 'DEL') k = 'DELETE';
  709. else if (k === 'ESC') k = 'ESCAPE';
  710. if (location.hash.match(/^#dropdown-/)) {
  711. const n = parseInt(k);
  712. if (n) {
  713. if (location.hash === '#dropdown-query') {
  714. user_filter(n);
  715. } else {
  716. auto_share(n);
  717. }
  718. return false;
  719. }
  720. }
  721. if (k === s.next_entry) {
  722. if (ev.altKey) {
  723. next_category();
  724. } else if (ev.shiftKey) {
  725. next_feed();
  726. } else {
  727. next_entry(false);
  728. }
  729. return false;
  730. }
  731. if (k === s.prev_entry) {
  732. if (ev.altKey) {
  733. prev_category();
  734. } else if (ev.shiftKey) {
  735. prev_feed();
  736. } else {
  737. prev_entry(false);
  738. }
  739. return false;
  740. }
  741. if (k === s.mark_read) {
  742. if (ev.altKey) {
  743. mark_previous_read(document.querySelector('.flux.current'));
  744. } else if (ev.shiftKey) {
  745. document.querySelector('.nav_menu .read_all').click();
  746. } else { // Toggle the read state
  747. mark_read(document.querySelector('.flux.current'), false, false);
  748. }
  749. return false;
  750. }
  751. if (k === s.first_entry) {
  752. if (ev.altKey) {
  753. first_category();
  754. } else if (ev.shiftKey) {
  755. first_feed();
  756. } else {
  757. const old_active = document.querySelector('.flux.current'),
  758. first = document.querySelector('.flux');
  759. if (first.classList.contains('flux')) {
  760. toggleContent(first, old_active, false);
  761. }
  762. }
  763. return false;
  764. }
  765. if (k === s.last_entry) {
  766. if (ev.altKey) {
  767. last_category();
  768. } else if (ev.shiftKey) {
  769. last_feed();
  770. } else {
  771. const old_active = document.querySelector('.flux.current'),
  772. last = document.querySelector('.flux:last-of-type');
  773. if (last.classList.contains('flux')) {
  774. toggleContent(last, old_active, false);
  775. }
  776. }
  777. return false;
  778. }
  779. if (ev.altKey || ev.shiftKey) {
  780. return true;
  781. }
  782. if (k === s.mark_favorite) { // Toggle the favorite state
  783. mark_favorite(document.querySelector('.flux.current'));
  784. return false;
  785. }
  786. if (k === s.go_website) {
  787. if (context.auto_mark_site) {
  788. mark_read(document.querySelector('.flux.current'), true, false);
  789. }
  790. const newWindow = window.open();
  791. if (newWindow) {
  792. newWindow.opener = null;
  793. newWindow.location = document.querySelector('.flux.current a.go_website').href;
  794. }
  795. return false;
  796. }
  797. if (k === s.skip_next_entry) { next_entry(true); return false; }
  798. if (k === s.skip_prev_entry) { prev_entry(true); return false; }
  799. if (k === s.collapse_entry) { collapse_entry(); return false; }
  800. if (k === s.auto_share) { auto_share(); return false; }
  801. if (k === s.user_filter) { user_filter(); return false; }
  802. if (k === s.load_more) { load_more_posts(); return false; }
  803. if (k === s.close_dropdown) { location.hash = null; return false; }
  804. if (k === s.help) { window.open(context.urls.help); return false; }
  805. if (k === s.focus_search) { document.getElementById('search').focus(); return false; }
  806. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); return false; }
  807. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); return false; }
  808. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); return false; }
  809. if (k === s.rss_view) { delayedClick(document.querySelector('#nav_menu_views .view-rss')); return false; }
  810. if (k === s.toggle_media) { toggle_media(); return false;}
  811. return true;
  812. };
  813. }
  814. function init_stream(stream) {
  815. stream.onclick = function (ev) {
  816. let el = ev.target.closest('.flux a.read');
  817. if (el) {
  818. mark_read(el.closest('.flux'), false, false);
  819. return false;
  820. }
  821. el = ev.target.closest('.flux a.bookmark');
  822. if (el) {
  823. mark_favorite(el.closest('.flux'));
  824. return false;
  825. }
  826. el = ev.target.closest('.dynamictags');
  827. if (el) {
  828. loadDynamicTags(el);
  829. return true;
  830. }
  831. el = ev.target.closest('.item.title > a');
  832. if (el) { // Allow default control-click behaviour such as open in backround-tab
  833. return ev.ctrlKey;
  834. }
  835. el = ev.target.closest('.flux .content a');
  836. if (el) {
  837. if (!el.closest('div').classList.contains('author')) {
  838. el.target = '_blank';
  839. el.rel = 'noreferrer';
  840. }
  841. return true;
  842. }
  843. el = ev.target.closest('.item.share > a[href="#"]');
  844. if (el) { //Print
  845. const tmp_window = window.open();
  846. for (var i = 0; i < document.styleSheets.length; i++) {
  847. tmp_window.document.writeln('<link href="' + document.styleSheets[i].href + '" rel="stylesheet" type="text/css" />');
  848. }
  849. tmp_window.document.writeln(el.closest('.flux_content').querySelector('.content').innerHTML);
  850. tmp_window.document.close();
  851. tmp_window.focus();
  852. tmp_window.print();
  853. tmp_window.close();
  854. return false;
  855. }
  856. el = ev.target.closest('.item.share > a[href="POST"]');
  857. if (el) { //Share by POST
  858. const f = el.parentElement.querySelector('form');
  859. f.disabled = false;
  860. f.submit();
  861. return false;
  862. }
  863. el = ev.target.closest('.flux_header, .flux_content');
  864. if (el) { //flux_toggle
  865. if (ev.target.closest('.content, .item.website, .item.link, .dropdown-menu')) {
  866. return true;
  867. }
  868. if (!context.sides_close_article && ev.target.matches('div.flux_content')) {
  869. // setting for not-closing after clicking outside article area
  870. return false;
  871. }
  872. const old_active = document.querySelector('.flux.current'),
  873. new_active = el.parentNode;
  874. if (ev.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  875. if (context.auto_mark_article) {
  876. mark_read(new_active, true, false);
  877. }
  878. return true;
  879. }
  880. toggleContent(new_active, old_active, false);
  881. return false;
  882. }
  883. };
  884. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  885. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  886. return;
  887. }
  888. let el = ev.target.closest('.item.title > a');
  889. if (el) {
  890. if (ev.which == 1) {
  891. if (ev.ctrlKey) { //Control+click
  892. if (context.auto_mark_site) {
  893. mark_read(el.closest('.flux'), true, false);
  894. }
  895. } else {
  896. el.parentElement.click(); //Normal click, just toggle article.
  897. }
  898. } else if (ev.which == 2 && !ev.ctrlKey) { //Simple middle click: same behaviour as CTRL+click
  899. if (context.auto_mark_article) {
  900. const new_active = el.closest('.flux');
  901. mark_read(new_active, true, false);
  902. }
  903. }
  904. return;
  905. }
  906. if (context.auto_mark_site) {
  907. // catch mouseup instead of click so we can have the correct behaviour
  908. // with middle button click (scroll button).
  909. el = ev.target.closest('.flux .link > a');
  910. if (el) {
  911. if (ev.which == 3) {
  912. return;
  913. }
  914. mark_read(el.closest('.flux'), true, false);
  915. }
  916. }
  917. };
  918. stream.onchange = function (ev) {
  919. const checkboxTag = ev.target.closest('.checkboxTag');
  920. if (checkboxTag) { //Dynamic tags
  921. ev.stopPropagation();
  922. const isChecked = checkboxTag.checked,
  923. tagId = checkboxTag.name.replace(/^t_/, ''),
  924. tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.value : '',
  925. entry = checkboxTag.closest('div.flux'),
  926. entryId = entry.id.replace(/^flux_/, '');
  927. checkboxTag.disabled = true;
  928. const req = new XMLHttpRequest();
  929. req.open('POST', './?c=tag&a=tagEntry', true);
  930. req.responseType = 'json';
  931. req.onerror = function (e) {
  932. checkboxTag.checked = !isChecked;
  933. badAjax(this.status == 403);
  934. };
  935. req.onload = function (e) {
  936. if (this.status != 200) {
  937. return req.onerror(e);
  938. }
  939. if (entry.classList.contains('not_read')) {
  940. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  941. }
  942. };
  943. req.onloadend = function (e) {
  944. checkboxTag.disabled = false;
  945. if (tagId == 0) {
  946. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  947. }
  948. };
  949. req.setRequestHeader('Content-Type', 'application/json');
  950. req.send(JSON.stringify({
  951. _csrf: context.csrf,
  952. id_tag: tagId,
  953. name_tag: tagId == 0 ? tagName : '',
  954. id_entry: entryId,
  955. checked: isChecked,
  956. }));
  957. }
  958. };
  959. }
  960. function init_nav_entries() {
  961. const nav_entries = document.getElementById('nav_entries');
  962. if (nav_entries) {
  963. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  964. prev_entry(false);
  965. return false;
  966. };
  967. nav_entries.querySelector('.next_entry').onclick = function (e) {
  968. next_entry(false);
  969. return false;
  970. };
  971. nav_entries.querySelector('.up').onclick = function (e) {
  972. const active_item = document.querySelector('.flux.current'),
  973. windowTop = document.scrollingElement.scrollTop,
  974. item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  975. document.scrollingElement.scrollTop = windowTop > item_top ? item_top : 0;
  976. return false;
  977. };
  978. }
  979. }
  980. function loadDynamicTags(div) {
  981. div.classList.remove('dynamictags');
  982. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  983. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  984. const req = new XMLHttpRequest();
  985. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  986. req.responseType = 'json';
  987. req.onerror = function (e) {
  988. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  989. div.classList.add('dynamictags');
  990. };
  991. req.onload = function (e) {
  992. if (this.status != 200) {
  993. return req.onerror(e);
  994. }
  995. const json = xmlHttpRequestJson(this);
  996. if (!json) {
  997. return req.onerror(e);
  998. }
  999. let html = '<li class="item"><label><input class="checkboxTag" name="t_0" type="checkbox" /> <input type="text" name="newTag" /></label></li>';
  1000. if (json && json.length) {
  1001. for (let i = 0; i < json.length; i++) {
  1002. const tag = json[i];
  1003. html += '<li class="item"><label><input class="checkboxTag" name="t_' + tag.id + '" type="checkbox"' +
  1004. (tag.checked ? ' checked="checked"' : '') + '> ' + tag.name + '</label></li>';
  1005. }
  1006. }
  1007. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1008. };
  1009. req.send();
  1010. }
  1011. // <actualize>
  1012. var feed_processed = 0;
  1013. function updateFeed(feeds, feeds_count) {
  1014. const feed = feeds.pop();
  1015. if (!feed) {
  1016. return;
  1017. }
  1018. const req = new XMLHttpRequest();
  1019. req.open('POST', feed.url, true);
  1020. req.onloadend = function (e) {
  1021. if (this.status != 200) {
  1022. return badAjax(false);
  1023. }
  1024. feed_processed++;
  1025. const div = document.getElementById('actualizeProgress');
  1026. div.querySelector('.progress').innerHTML = feed_processed + ' / ' + feeds_count;
  1027. div.querySelector('.title').innerHTML = feed.title;
  1028. if (feed_processed === feeds_count) {
  1029. //Empty request to commit new articles
  1030. const req2 = new XMLHttpRequest();
  1031. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1032. req2.onloadend = function (e) {
  1033. delayedFunction(function () { location.reload(); });
  1034. };
  1035. req2.setRequestHeader('Content-Type', 'application/json');
  1036. req2.send(JSON.stringify({
  1037. _csrf: context.csrf,
  1038. noCommit: 0,
  1039. }));
  1040. } else {
  1041. updateFeed(feeds, feeds_count);
  1042. }
  1043. };
  1044. req.setRequestHeader('Content-Type', 'application/json');
  1045. req.send(JSON.stringify({
  1046. _csrf: context.csrf,
  1047. noCommit: 1,
  1048. }));
  1049. }
  1050. function init_actualize() {
  1051. let auto = false;
  1052. document.getElementById('actualize').onclick = function () {
  1053. if (context.ajax_loading) {
  1054. return false;
  1055. }
  1056. context.ajax_loading = true;
  1057. const req = new XMLHttpRequest();
  1058. req.open('POST', './?c=javascript&a=actualize', true);
  1059. req.responseType = 'json';
  1060. req.onload = function (e) {
  1061. if (this.status != 200) {
  1062. return badAjax(false);
  1063. }
  1064. const json = xmlHttpRequestJson(this);
  1065. if (!json) {
  1066. return badAjax(false);
  1067. }
  1068. if (auto && json.feeds.length < 1) {
  1069. auto = false;
  1070. context.ajax_loading = false;
  1071. return false;
  1072. }
  1073. if (json.feeds.length === 0) {
  1074. openNotification(json.feedback_no_refresh, 'good');
  1075. //Empty request to commit new articles
  1076. const req2 = new XMLHttpRequest();
  1077. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1078. req2.onloadend = function (e) {
  1079. context.ajax_loading = false;
  1080. };
  1081. req2.setRequestHeader('Content-Type', 'application/json');
  1082. req2.send(JSON.stringify({
  1083. _csrf: context.csrf,
  1084. noCommit: 0,
  1085. }));
  1086. return;
  1087. }
  1088. //Progress bar
  1089. const feeds_count = json.feeds.length;
  1090. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1091. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1092. feeds_count + '</span></div>');
  1093. for (let i = 10; i > 0; i--) {
  1094. updateFeed(json.feeds, feeds_count);
  1095. }
  1096. };
  1097. req.setRequestHeader('Content-Type', 'application/json');
  1098. req.send(JSON.stringify({
  1099. _csrf: context.csrf,
  1100. }));
  1101. return false;
  1102. };
  1103. if (context.auto_actualize_feeds) {
  1104. auto = true;
  1105. document.getElementById('actualize').click();
  1106. }
  1107. }
  1108. // </actualize>
  1109. // <notification>
  1110. var notification = null,
  1111. notification_interval = null,
  1112. notification_working = false;
  1113. function openNotification(msg, status) {
  1114. if (notification_working === true) {
  1115. return false;
  1116. }
  1117. notification_working = true;
  1118. notification.querySelector('.msg').innerHTML = msg;
  1119. notification.className = 'notification';
  1120. notification.classList.add(status);
  1121. notification_interval = setTimeout(closeNotification, 4000);
  1122. }
  1123. function closeNotification() {
  1124. notification.classList.add('closed');
  1125. clearInterval(notification_interval);
  1126. notification_working = false;
  1127. }
  1128. function init_notifications() {
  1129. notification = document.getElementById('notification');
  1130. notification.querySelector('a.close').onclick = function () {
  1131. closeNotification();
  1132. return false;
  1133. };
  1134. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1135. notification_working = true;
  1136. notification_interval = setTimeout(closeNotification, 4000);
  1137. }
  1138. }
  1139. // </notification>
  1140. // <popup>
  1141. let popup = null,
  1142. popup_iframe_container = null,
  1143. popup_iframe = null,
  1144. popup_txt = null,
  1145. popup_working = false;
  1146. function openPopupWithMessage(msg) {
  1147. if (popup_working === true) {
  1148. return false;
  1149. }
  1150. popup_working = true;
  1151. popup_txt.innerHTML = msg;
  1152. popup_txt.style.display = 'table-row';
  1153. popup.style.display = 'block';
  1154. }
  1155. function openPopupWithSource(source) {
  1156. if (popup_working === true) {
  1157. return false;
  1158. }
  1159. popup_working = true;
  1160. popup_iframe.src = source;
  1161. popup_iframe_container.style.display = 'table-row';
  1162. popup.style.display = 'block';
  1163. }
  1164. function closePopup() {
  1165. popup.style.display = 'none';
  1166. popup_iframe_container.style.display = 'none';
  1167. popup_txt.style.display = 'none';
  1168. popup_iframe.src = 'about:blank';
  1169. popup_working = false;
  1170. }
  1171. function init_popup() {
  1172. //Fetch elements.
  1173. popup = document.getElementById('popup');
  1174. popup_iframe_container = document.getElementById('popup-iframe-container');
  1175. popup_iframe = document.getElementById('popup-iframe');
  1176. popup_txt = document.getElementById('popup-txt');
  1177. //Configure close button.
  1178. document.getElementById('popup-close').addEventListener('click', function (ev) {
  1179. closePopup();
  1180. });
  1181. //Configure close-on-click.
  1182. window.addEventListener('click', function (ev) {
  1183. if (ev.target == popup) {
  1184. closePopup();
  1185. }
  1186. });
  1187. }
  1188. // </popup>
  1189. // <notifs html5>
  1190. var notifs_html5_permission = 'denied';
  1191. function notifs_html5_is_supported() {
  1192. return window.Notification !== undefined;
  1193. }
  1194. function notifs_html5_ask_permission() {
  1195. window.Notification.requestPermission(function () {
  1196. notifs_html5_permission = window.Notification.permission;
  1197. });
  1198. }
  1199. function notifs_html5_show(nb) {
  1200. if (notifs_html5_permission !== 'granted') {
  1201. return;
  1202. }
  1203. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1204. icon: '../themes/icons/favicon-256.png',
  1205. body: context.i18n.notif_body_articles.replace('%d', nb),
  1206. tag: 'freshRssNewArticles',
  1207. });
  1208. notification.onclick = function () {
  1209. delayedFunction(function() {
  1210. location.reload();
  1211. window.focus();
  1212. notification.close();
  1213. });
  1214. };
  1215. if (context.html5_notif_timeout !== 0) {
  1216. setTimeout(function () {
  1217. notification.close();
  1218. }, context.html5_notif_timeout * 1000);
  1219. }
  1220. }
  1221. function init_notifs_html5() {
  1222. if (!notifs_html5_is_supported()) {
  1223. return;
  1224. }
  1225. notifs_html5_permission = notifs_html5_ask_permission();
  1226. }
  1227. // </notifs html5>
  1228. function refreshUnreads() {
  1229. const req = new XMLHttpRequest();
  1230. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1231. req.responseType = 'json';
  1232. req.onload = function (e) {
  1233. const json = xmlHttpRequestJson(this);
  1234. if (!json) {
  1235. return badAjax(false);
  1236. }
  1237. const isAll = document.querySelector('.category.all.active');
  1238. let new_articles = false;
  1239. Object.keys(json.feeds).forEach(function (feed_id) {
  1240. const nbUnreads = json.feeds[feed_id];
  1241. feed_id = 'f_' + feed_id;
  1242. const elem = document.getElementById(feed_id),
  1243. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1244. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  1245. (nbUnreads - feed_unreads > 0)) {
  1246. const newArticle = document.getElementById('new-article');
  1247. newArticle.setAttribute('aria-hidden', 'false');
  1248. newArticle.style.display = 'block';
  1249. new_articles = true;
  1250. }
  1251. });
  1252. let nbUnreadTags = 0;
  1253. Object.keys(json.tags).forEach(function (tag_id) {
  1254. const nbUnreads = json.tags[tag_id];
  1255. nbUnreadTags += nbUnreads;
  1256. const tag = document.getElementById('t_' + tag_id);
  1257. if (tag) {
  1258. tag.setAttribute('data-unread', nbUnreads);
  1259. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1260. }
  1261. });
  1262. const tags = document.querySelector('.category.tags');
  1263. if (tags) {
  1264. tags.setAttribute('data-unread', nbUnreadTags);
  1265. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1266. }
  1267. const title = document.querySelector('.category.all .title'),
  1268. nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1269. if (nb_unreads > 0 && new_articles) {
  1270. faviconNbUnread(nb_unreads);
  1271. notifs_html5_show(nb_unreads);
  1272. }
  1273. };
  1274. req.send();
  1275. }
  1276. //<endless_mode>
  1277. var url_load_more = '',
  1278. load_more = false,
  1279. box_load_more = null;
  1280. function load_more_posts() {
  1281. if (load_more || !url_load_more || !box_load_more) {
  1282. return;
  1283. }
  1284. load_more = true;
  1285. document.getElementById('load_more').classList.add('loading');
  1286. const req = new XMLHttpRequest();
  1287. req.open('GET', url_load_more, true);
  1288. req.responseType = 'document';
  1289. req.onload = function (e) {
  1290. const html = this.response,
  1291. formPagination = document.getElementById('mark-read-pagination');
  1292. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1293. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1294. box_load_more.insertBefore(div, formPagination);
  1295. });
  1296. const paginationOld = formPagination.querySelector('.pagination'),
  1297. paginationNew = streamAdopted.querySelector('.pagination');
  1298. formPagination.replaceChild(paginationNew, paginationOld);
  1299. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1300. if (bigMarkAsRead) {
  1301. if (context.display_order === 'ASC') {
  1302. document.querySelector('#nav_menu_read_all .read_all').formAction = bigMarkAsRead.formAction;
  1303. } else {
  1304. bigMarkAsRead.formAction = document.querySelector('#nav_menu_read_all .read_all').formAction;
  1305. }
  1306. }
  1307. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1308. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1309. for (let i = ids.length - 1; i > 0; i--) { //Keep only the first
  1310. ids[i].remove();
  1311. }
  1312. });
  1313. init_load_more(box_load_more);
  1314. const div_load_more = document.getElementById('load_more');
  1315. if (bigMarkAsRead) {
  1316. bigMarkAsRead.removeAttribute('disabled');
  1317. }
  1318. if (div_load_more) {
  1319. div_load_more.classList.remove('loading');
  1320. }
  1321. load_more = false;
  1322. };
  1323. req.send();
  1324. }
  1325. var freshrssLoadMoreEvent = document.createEvent('Event');
  1326. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1327. function init_load_more(box) {
  1328. box_load_more = box;
  1329. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1330. const next_link = document.getElementById('load_more');
  1331. if (!next_link) {
  1332. // no more article to load
  1333. url_load_more = '';
  1334. return;
  1335. }
  1336. url_load_more = next_link.href;
  1337. next_link.onclick = function (e) {
  1338. load_more_posts();
  1339. return false;
  1340. };
  1341. }
  1342. //</endless_mode>
  1343. function init_confirm_action() {
  1344. document.body.onclick = function (ev) {
  1345. const b = ev.target.closest('.confirm');
  1346. if (b) {
  1347. let str_confirmation = this.getAttribute('data-str-confirm');
  1348. if (!str_confirmation) {
  1349. str_confirmation = context.i18n.confirmation_default;
  1350. }
  1351. return confirm(str_confirmation);
  1352. }
  1353. };
  1354. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1355. }
  1356. function faviconNbUnread(n) {
  1357. if (typeof n === 'undefined') {
  1358. const t = document.querySelector('.category.all .title');
  1359. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1360. }
  1361. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1362. const canvas = document.createElement('canvas'),
  1363. link = document.getElementById('favicon').cloneNode(true),
  1364. ratio = window.devicePixelRatio;
  1365. if (canvas.getContext && link) {
  1366. canvas.height = canvas.width = 16 * ratio;
  1367. const img = document.createElement('img');
  1368. img.onload = function () {
  1369. const ctx = canvas.getContext('2d');
  1370. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1371. if (n > 0) {
  1372. let text = '';
  1373. if (n < 1000) {
  1374. text = n;
  1375. } else if (n < 100000) {
  1376. text = Math.floor(n / 1000) + 'k';
  1377. } else {
  1378. text = 'E' + Math.floor(Math.log10(n));
  1379. }
  1380. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1381. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1382. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1383. ctx.fillStyle = '#F00';
  1384. ctx.fillText(text, 0, canvas.height - 1);
  1385. }
  1386. link.href = canvas.toDataURL('image/png');
  1387. document.querySelector('link[rel~=icon]').remove();
  1388. document.head.appendChild(link);
  1389. };
  1390. img.src = '../favicon.ico';
  1391. }
  1392. }
  1393. function removeFirstLoadSpinner() {
  1394. const first_load = document.getElementById('first_load');
  1395. if (first_load) {
  1396. first_load.remove();
  1397. }
  1398. }
  1399. function init_normal() {
  1400. const stream = document.getElementById('stream');
  1401. if (!stream) {
  1402. if (window.console) {
  1403. console.log('FreshRSS waiting for content…');
  1404. }
  1405. setTimeout(init_normal, 100);
  1406. return;
  1407. }
  1408. init_column_categories();
  1409. init_stream(stream);
  1410. init_shortcuts();
  1411. init_actualize();
  1412. faviconNbUnread();
  1413. window.onbeforeunload = function (e) {
  1414. const sidebar = document.getElementById('sidebar');
  1415. if (sidebar) { //Save sidebar scroll position
  1416. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1417. }
  1418. if (mark_read_queue && mark_read_queue.length > 0) {
  1419. return false;
  1420. }
  1421. };
  1422. }
  1423. function init_beforeDOM() {
  1424. document.scrollingElement.scrollTop = 0;
  1425. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1426. init_normal();
  1427. }
  1428. }
  1429. function init_afterDOM() {
  1430. removeFirstLoadSpinner();
  1431. init_notifications();
  1432. init_popup();
  1433. init_confirm_action();
  1434. const stream = document.getElementById('stream');
  1435. if (stream) {
  1436. init_load_more(stream);
  1437. init_posts();
  1438. init_nav_entries();
  1439. init_notifs_html5();
  1440. setTimeout(faviconNbUnread, 1000);
  1441. setInterval(refreshUnreads, 120000);
  1442. }
  1443. if (window.console) {
  1444. console.log('FreshRSS main init done.');
  1445. }
  1446. }
  1447. init_beforeDOM(); //Can be called before DOM is fully loaded
  1448. if (document.readyState && document.readyState !== 'loading') {
  1449. init_afterDOM();
  1450. } else {
  1451. if (window.console) {
  1452. console.log('FreshRSS waiting for DOMContentLoaded…');
  1453. }
  1454. document.addEventListener('DOMContentLoaded', init_afterDOM, false);
  1455. }
  1456. // @license-end