4
0

main.js 40 KB

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