main.js 43 KB

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