main.js 46 KB

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