main.js 42 KB

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