main.js 42 KB

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