main.js 41 KB

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