main.js 41 KB

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