main.js 46 KB

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