main.js 40 KB

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