main.js 44 KB

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