main.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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 next_unread_entry(skipping) {
  431. const old_active = document.querySelector('.flux.current');
  432. let new_active = old_active;
  433. if (new_active) {
  434. do new_active = new_active.nextElementSibling;
  435. while (new_active && !new_active.classList.contains('not_read'));
  436. if (!new_active) {
  437. next_feed();
  438. }
  439. } else {
  440. new_active = document.querySelector('.not_read');
  441. }
  442. toggleContent(new_active, old_active, skipping);
  443. }
  444. function prev_feed() {
  445. let found = false;
  446. let adjacent = null;
  447. const feeds = document.querySelectorAll('#aside_feed .feed');
  448. for (let i = feeds.length - 1; i >= 0; i--) {
  449. const feed = feeds[i];
  450. if (feed.classList.contains('active')) {
  451. found = true;
  452. continue;
  453. }
  454. if (!found) {
  455. continue;
  456. }
  457. if (getComputedStyle(feed).display === 'none') {
  458. continue;
  459. }
  460. if (feed.dataset.unread != 0) {
  461. return delayedClick(feed.querySelector('a.item-title'));
  462. } else if (adjacent === null) {
  463. adjacent = feed;
  464. }
  465. }
  466. if (found) {
  467. delayedClick(adjacent.querySelector('a.item-title'));
  468. } else {
  469. last_feed();
  470. }
  471. }
  472. function next_feed() {
  473. let found = false;
  474. let adjacent = null;
  475. const feeds = document.querySelectorAll('#aside_feed .feed');
  476. for (let i = 0; i < feeds.length; i++) {
  477. const feed = feeds[i];
  478. if (feed.classList.contains('active')) {
  479. found = true;
  480. continue;
  481. }
  482. if (!found) {
  483. continue;
  484. }
  485. if (getComputedStyle(feed).display === 'none') {
  486. continue;
  487. }
  488. if (feed.dataset.unread != 0) {
  489. return delayedClick(feed.querySelector('a.item-title'));
  490. } else if (adjacent === null) {
  491. adjacent = feed;
  492. }
  493. }
  494. if (found) {
  495. delayedClick(adjacent.querySelector('a.item-title'));
  496. } else {
  497. first_feed();
  498. }
  499. }
  500. function first_feed() {
  501. const a = document.querySelector('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  502. delayedClick(a);
  503. }
  504. function last_feed() {
  505. const links = document.querySelectorAll('#aside_feed .category.active .feed:not([data-unread="0"]) a.item-title');
  506. if (links && links.length > 0) {
  507. delayedClick(links[links.length - 1]);
  508. }
  509. }
  510. function prev_category() {
  511. const active_cat = document.querySelector('#aside_feed .category.active');
  512. if (active_cat) {
  513. let cat = active_cat;
  514. do cat = cat.previousElementSibling;
  515. while (cat && getComputedStyle(cat).display === 'none');
  516. if (cat) {
  517. delayedClick(cat.querySelector('a.title'));
  518. }
  519. } else {
  520. last_category();
  521. }
  522. }
  523. function next_category() {
  524. const active_cat = document.querySelector('#aside_feed .category.active');
  525. if (active_cat) {
  526. let cat = active_cat;
  527. do cat = cat.nextElementSibling;
  528. while (cat && getComputedStyle(cat).display === 'none');
  529. if (cat) {
  530. delayedClick(cat.querySelector('a.title'));
  531. }
  532. } else {
  533. first_category();
  534. }
  535. }
  536. function next_unread_category() {
  537. const active_cat = document.querySelector('#aside_feed .category.active');
  538. if (active_cat) {
  539. let cat = active_cat;
  540. do cat = cat.nextElementSibling;
  541. while (cat && cat.getAttribute('data-unread') <= 0);
  542. if (cat) {
  543. delayedClick(cat.querySelector('a.title'));
  544. }
  545. } else {
  546. first_category();
  547. }
  548. }
  549. function first_category() {
  550. const a = document.querySelector('#aside_feed .category:not([data-unread="0"]) a.title');
  551. delayedClick(a);
  552. }
  553. function last_category() {
  554. const links = document.querySelectorAll('#aside_feed .category:not([data-unread="0"]) a.title');
  555. if (links && links.length > 0) {
  556. delayedClick(links[links.length - 1]);
  557. }
  558. }
  559. function collapse_entry() {
  560. const flux_current = document.querySelector('.flux.current');
  561. toggleContent(flux_current, flux_current, false);
  562. }
  563. function toggle_media() {
  564. const media = document.querySelector('.flux.current video,.flux.current audio');
  565. if (media === null) {
  566. return;
  567. }
  568. if (media.paused) {
  569. media.play();
  570. } else {
  571. media.pause();
  572. }
  573. }
  574. function user_filter(key) {
  575. const filter = document.getElementById('dropdown-query'),
  576. filters = filter.parentElement.querySelectorAll('.dropdown-menu > .query > a');
  577. if (typeof key === 'undefined') {
  578. if (!filters.length) {
  579. return;
  580. }
  581. // Display the filter div
  582. location.hash = filter.id;
  583. // Force scrolling to the filter div
  584. const scroll = needsScroll(document.querySelector('.header'));
  585. if (scroll !== 0) {
  586. document.scrollingElement.scrollTop = scroll;
  587. }
  588. // Force the key value if there is only one action, so we can trigger it automatically
  589. if (filters.length === 1) {
  590. key = 1;
  591. } else {
  592. return;
  593. }
  594. }
  595. // Trigger selected share action
  596. key = parseInt(key);
  597. if (key <= filters.length) {
  598. filters[key - 1].click();
  599. }
  600. }
  601. function auto_share(key) {
  602. const share = document.querySelector('.flux.current.active .dropdown-target[id^="dropdown-share"]');
  603. if (!share) {
  604. return;
  605. }
  606. const shares = share.parentElement.querySelectorAll('.dropdown-menu .item a');
  607. if (typeof key === 'undefined') {
  608. // Display the share div
  609. location.hash = share.id;
  610. // Force scrolling to the share div
  611. const scrollTop = needsScroll(share.closest('.bottom'));
  612. if (scrollTop !== 0) {
  613. document.scrollingElement.scrollTop = scrollTop;
  614. }
  615. // Force the key value if there is only one action, so we can trigger it automatically
  616. if (shares.length === 1) {
  617. key = 1;
  618. } else {
  619. return;
  620. }
  621. }
  622. // Trigger selected share action and hide the share div
  623. key = parseInt(key);
  624. if (key <= shares.length) {
  625. shares[key - 1].click();
  626. share.parentElement.querySelector('.dropdown-menu .dropdown-close a').click();
  627. }
  628. }
  629. var box_to_follow;
  630. function onScroll() {
  631. if (!box_to_follow) {
  632. return;
  633. }
  634. if (context.auto_mark_scroll) {
  635. const minTop = 40 + box_to_follow.scrollTop;
  636. document.querySelectorAll('.not_read:not(.keep_unread)').forEach(function (div) {
  637. if (div.offsetHeight > 0 &&
  638. div.offsetParent.offsetTop + div.offsetTop + div.offsetHeight < minTop) {
  639. mark_read(div, true, true);
  640. }
  641. });
  642. }
  643. if (context.auto_load_more) {
  644. const pagination = document.getElementById('mark-read-pagination');
  645. if (pagination && box_to_follow.offsetHeight > 0 &&
  646. box_to_follow.scrollTop + box_to_follow.offsetHeight + (window.innerHeight / 2) >= pagination.offsetTop) {
  647. load_more_posts();
  648. }
  649. }
  650. }
  651. function init_posts() {
  652. if (context.auto_load_more || context.auto_mark_scroll || context.auto_remove_article) {
  653. box_to_follow = context.current_view === 'global' ? document.getElementById('panel') : document.scrollingElement;
  654. let lastScroll = 0, //Throttle
  655. timerId = 0;
  656. (box_to_follow === document.scrollingElement ? window : box_to_follow).onscroll = function () {
  657. clearTimeout(timerId);
  658. if (lastScroll + 500 < Date.now()) {
  659. lastScroll = Date.now();
  660. onScroll();
  661. } else {
  662. timerId = setTimeout(onScroll, 500);
  663. }
  664. };
  665. onScroll();
  666. }
  667. }
  668. function rememberOpenCategory(category_id, isOpen) {
  669. if (context.display_categories === 'remember') {
  670. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  671. if (isOpen) {
  672. open_categories[category_id] = true;
  673. } else {
  674. delete open_categories[category_id];
  675. }
  676. localStorage.setItem('FreshRSS_open_categories', JSON.stringify(open_categories));
  677. }
  678. }
  679. function openCategory(category_id) {
  680. const category_element = document.getElementById(category_id);
  681. category_element.querySelector('.tree-folder-items').classList.add('active');
  682. const img = category_element.querySelector('a.dropdown-toggle img');
  683. img.src = img.src.replace('/icons/down.', '/icons/up.');
  684. img.alt = '△';
  685. }
  686. function init_column_categories() {
  687. if (context.current_view !== 'normal' && context.current_view !== 'reader') {
  688. return;
  689. }
  690. //Restore sidebar scroll position
  691. document.getElementById('sidebar').scrollTop = +sessionStorage.getItem('FreshRSS_sidebar_scrollTop');
  692. //Restore open categories
  693. if (context.display_categories === 'remember') {
  694. const open_categories = JSON.parse(localStorage.getItem('FreshRSS_open_categories') || '{}');
  695. Object.keys(open_categories).forEach(function (category_id) {
  696. openCategory(category_id);
  697. });
  698. }
  699. document.getElementById('aside_feed').onclick = function (ev) {
  700. let a = ev.target.closest('.tree-folder > .tree-folder-title > a.dropdown-toggle');
  701. if (a) {
  702. const img = a.querySelector('img');
  703. const category_id = a.closest('.category').id;
  704. if (img.alt === '▽') {
  705. img.src = img.src.replace('/icons/down.', '/icons/up.');
  706. img.alt = '△';
  707. rememberOpenCategory(category_id, true);
  708. } else {
  709. img.src = img.src.replace('/icons/up.', '/icons/down.');
  710. img.alt = '▽';
  711. rememberOpenCategory(category_id, false);
  712. }
  713. const ul = a.closest('li').querySelector('.tree-folder-items');
  714. let nbVisibleItems = 0;
  715. for (let i = ul.children.length - 1; i >= 0; i--) {
  716. if (ul.children[i].offsetHeight) {
  717. nbVisibleItems++;
  718. }
  719. }
  720. ul.classList.toggle('active');
  721. //CSS transition does not work on max-height:auto
  722. ul.style.maxHeight = ul.classList.contains('active') ? (nbVisibleItems * 4) + 'em' : 0;
  723. return false;
  724. }
  725. a = ev.target.closest('.tree-folder-items > .feed .dropdown-toggle');
  726. if (a) {
  727. const itemId = a.closest('.item').id,
  728. templateId = itemId.substring(0, 2) === 't_' ? 'tag_config_template' : 'feed_config_template',
  729. id = itemId.substr(2),
  730. feed_web = a.getAttribute('data-fweb'),
  731. div = a.parentElement,
  732. dropdownMenu = div.querySelector('.dropdown-menu'),
  733. template = document.getElementById(templateId)
  734. .innerHTML.replace(/------/g, id).replace('http://example.net/', feed_web);
  735. if (!dropdownMenu) {
  736. a.href = '#dropdown-' + id;
  737. div.querySelector('.dropdown-target').id = 'dropdown-' + id;
  738. div.insertAdjacentHTML('beforeend', template);
  739. const b = div.querySelector('button.confirm');
  740. if (b) {
  741. b.disabled = false;
  742. }
  743. } else if (getComputedStyle(dropdownMenu).display === 'none') {
  744. const id2 = div.closest('.item').id.substr(2);
  745. a.href = '#dropdown-' + id2;
  746. } else {
  747. a.href = '#close';
  748. }
  749. return true;
  750. }
  751. return true;
  752. };
  753. }
  754. function init_shortcuts() {
  755. Object.keys(context.shortcuts).forEach(function (k) {
  756. context.shortcuts[k] = (context.shortcuts[k] || '').toUpperCase();
  757. });
  758. document.addEventListener('keydown', ev => {
  759. if (ev.target.closest('input, textarea') ||
  760. ev.ctrlKey || ev.metaKey || (ev.altKey && ev.shiftKey)) {
  761. return true;
  762. }
  763. const s = context.shortcuts;
  764. let k = (ev.key.trim() || ev.code || 'Space').toUpperCase();
  765. //IE11
  766. if (k === 'SPACEBAR') k = 'SPACE';
  767. else if (k === 'DEL') k = 'DELETE';
  768. else if (k === 'ESC') k = 'ESCAPE';
  769. if (location.hash.match(/^#dropdown-/)) {
  770. const n = parseInt(k);
  771. if (n) {
  772. if (location.hash === '#dropdown-query') {
  773. user_filter(n);
  774. } else {
  775. auto_share(n);
  776. }
  777. return false;
  778. }
  779. }
  780. if (k === s.next_entry) {
  781. if (ev.altKey) {
  782. next_category();
  783. } else if (ev.shiftKey) {
  784. next_feed();
  785. } else {
  786. next_entry(false);
  787. }
  788. return false;
  789. }
  790. if (k === s.next_unread_entry) {
  791. if (ev.altKey) {
  792. next_unread_category();
  793. } else if (ev.shiftKey) {
  794. next_feed();
  795. } else {
  796. next_unread_entry(false);
  797. }
  798. return false;
  799. }
  800. if (k === s.prev_entry) {
  801. if (ev.altKey) {
  802. prev_category();
  803. } else if (ev.shiftKey) {
  804. prev_feed();
  805. } else {
  806. prev_entry(false);
  807. }
  808. return false;
  809. }
  810. if (k === s.mark_read) {
  811. if (ev.altKey) {
  812. mark_previous_read(document.querySelector('.flux.current'));
  813. } else if (ev.shiftKey) {
  814. document.querySelector('.nav_menu .read_all').click();
  815. } else { // Toggle the read state
  816. mark_read(document.querySelector('.flux.current'), false, false);
  817. }
  818. return false;
  819. }
  820. if (k === s.first_entry) {
  821. if (ev.altKey) {
  822. first_category();
  823. } else if (ev.shiftKey) {
  824. first_feed();
  825. } else {
  826. const old_active = document.querySelector('.flux.current'),
  827. first = document.querySelector('.flux');
  828. if (first.classList.contains('flux')) {
  829. toggleContent(first, old_active, false);
  830. }
  831. }
  832. return false;
  833. }
  834. if (k === s.last_entry) {
  835. if (ev.altKey) {
  836. last_category();
  837. } else if (ev.shiftKey) {
  838. last_feed();
  839. } else {
  840. const old_active = document.querySelector('.flux.current'),
  841. last = document.querySelector('.flux:last-of-type');
  842. if (last.classList.contains('flux')) {
  843. toggleContent(last, old_active, false);
  844. }
  845. }
  846. return false;
  847. }
  848. if (ev.altKey || ev.shiftKey) {
  849. return true;
  850. }
  851. if (k === s.mark_favorite) { // Toggle the favorite state
  852. mark_favorite(document.querySelector('.flux.current'));
  853. return false;
  854. }
  855. if (k === s.go_website) {
  856. if (context.auto_mark_site) {
  857. mark_read(document.querySelector('.flux.current'), true, false);
  858. }
  859. const newWindow = window.open();
  860. if (newWindow) {
  861. newWindow.opener = null;
  862. newWindow.location = document.querySelector('.flux.current a.go_website').href;
  863. }
  864. return false;
  865. }
  866. if (k === s.skip_next_entry) { next_entry(true); return false; }
  867. if (k === s.skip_prev_entry) { prev_entry(true); return false; }
  868. if (k === s.collapse_entry) { collapse_entry(); return false; }
  869. if (k === s.auto_share) { auto_share(); return false; }
  870. if (k === s.user_filter) { user_filter(); return false; }
  871. if (k === s.load_more) { load_more_posts(); return false; }
  872. if (k === s.close_dropdown) { location.hash = null; return false; }
  873. if (k === s.help) { window.open(context.urls.help); return false; }
  874. if (k === s.focus_search) { document.getElementById('search').focus(); return false; }
  875. if (k === s.normal_view) { delayedClick(document.querySelector('#nav_menu_views .view-normal')); return false; }
  876. if (k === s.reading_view) { delayedClick(document.querySelector('#nav_menu_views .view-reader')); return false; }
  877. if (k === s.global_view) { delayedClick(document.querySelector('#nav_menu_views .view-global')); return false; }
  878. if (k === s.rss_view) { delayedClick(document.querySelector('#nav_menu_views .view-rss')); return false; }
  879. if (k === s.toggle_media) { toggle_media(); return false; }
  880. return true;
  881. });
  882. }
  883. function init_stream(stream) {
  884. stream.onclick = function (ev) {
  885. let el = ev.target.closest('.flux a.read');
  886. if (el) {
  887. mark_read(el.closest('.flux'), false, false);
  888. return false;
  889. }
  890. el = ev.target.closest('.flux a.bookmark');
  891. if (el) {
  892. mark_favorite(el.closest('.flux'));
  893. return false;
  894. }
  895. el = ev.target.closest('.dynamictags');
  896. if (el) {
  897. loadDynamicTags(el);
  898. return true;
  899. }
  900. el = ev.target.closest('.item.title > a');
  901. if (el) { // Allow default control-click behaviour such as open in backround-tab
  902. return ev.ctrlKey;
  903. }
  904. el = ev.target.closest('.flux .content a');
  905. if (el) {
  906. if (!el.closest('div').classList.contains('author')) {
  907. el.target = '_blank';
  908. el.rel = 'noreferrer';
  909. }
  910. return true;
  911. }
  912. el = ev.target.closest('.item.share > a[data-type="print"]');
  913. if (el) { //Print
  914. const tmp_window = window.open();
  915. for (var i = 0; i < document.styleSheets.length; i++) {
  916. tmp_window.document.writeln('<link href="' + document.styleSheets[i].href + '" rel="stylesheet" type="text/css" />');
  917. }
  918. tmp_window.document.writeln(el.closest('.flux_content').querySelector('.content').innerHTML);
  919. tmp_window.document.close();
  920. tmp_window.focus();
  921. tmp_window.print();
  922. tmp_window.close();
  923. return false;
  924. }
  925. el = ev.target.closest('.item.share > a[data-type="clipboard"]');
  926. if (el && navigator.clipboard) { //Clipboard
  927. navigator.clipboard.writeText(el.href);
  928. return false;
  929. }
  930. el = ev.target.closest('.item.share > a[href="POST"]');
  931. if (el) { //Share by POST
  932. const f = el.parentElement.querySelector('form');
  933. f.disabled = false;
  934. f.submit();
  935. return false;
  936. }
  937. el = ev.target.closest('.flux_header, .flux_content');
  938. if (el) { //flux_toggle
  939. if (ev.target.closest('.content, .item.website, .item.link, .dropdown-menu')) {
  940. return true;
  941. }
  942. if (!context.sides_close_article && ev.target.matches('div.flux_content')) {
  943. // setting for not-closing after clicking outside article area
  944. return false;
  945. }
  946. const old_active = document.querySelector('.flux.current'),
  947. new_active = el.parentNode;
  948. if (ev.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  949. if (context.auto_mark_article) {
  950. mark_read(new_active, true, false);
  951. }
  952. return true;
  953. }
  954. toggleContent(new_active, old_active, false);
  955. return false;
  956. }
  957. };
  958. stream.onmouseup = function (ev) { // Mouseup enables us to catch middle click, and control+click in IE/Edge
  959. if (ev.altKey || ev.metaKey || ev.shiftKey) {
  960. return;
  961. }
  962. let el = ev.target.closest('.item.title > a');
  963. if (el) {
  964. if (ev.which == 1) {
  965. if (ev.ctrlKey) { //Control+click
  966. if (context.auto_mark_site) {
  967. mark_read(el.closest('.flux'), true, false);
  968. }
  969. } else {
  970. el.parentElement.click(); //Normal click, just toggle article.
  971. }
  972. } else if (ev.which == 2 && !ev.ctrlKey) { //Simple middle click: same behaviour as CTRL+click
  973. if (context.auto_mark_article) {
  974. const new_active = el.closest('.flux');
  975. mark_read(new_active, true, false);
  976. }
  977. }
  978. return;
  979. }
  980. if (context.auto_mark_site) {
  981. // catch mouseup instead of click so we can have the correct behaviour
  982. // with middle button click (scroll button).
  983. el = ev.target.closest('.flux .link > a');
  984. if (el) {
  985. if (ev.which == 3) {
  986. return;
  987. }
  988. mark_read(el.closest('.flux'), true, false);
  989. }
  990. }
  991. };
  992. stream.onchange = function (ev) {
  993. const checkboxTag = ev.target.closest('.checkboxTag');
  994. if (checkboxTag) { //Dynamic tags
  995. ev.stopPropagation();
  996. const isChecked = checkboxTag.checked,
  997. tagId = checkboxTag.name.replace(/^t_/, ''),
  998. tagName = checkboxTag.nextElementSibling ? checkboxTag.nextElementSibling.value : '',
  999. entry = checkboxTag.closest('div.flux'),
  1000. entryId = entry.id.replace(/^flux_/, '');
  1001. checkboxTag.disabled = true;
  1002. const req = new XMLHttpRequest();
  1003. req.open('POST', './?c=tag&a=tagEntry', true);
  1004. req.responseType = 'json';
  1005. req.onerror = function (e) {
  1006. checkboxTag.checked = !isChecked;
  1007. badAjax(this.status == 403);
  1008. };
  1009. req.onload = function (e) {
  1010. if (this.status != 200) {
  1011. return req.onerror(e);
  1012. }
  1013. if (entry.classList.contains('not_read')) {
  1014. incUnreadsTag('t_' + tagId, isChecked ? 1 : -1);
  1015. }
  1016. };
  1017. req.onloadend = function (e) {
  1018. checkboxTag.disabled = false;
  1019. if (tagId == 0) {
  1020. loadDynamicTags(checkboxTag.closest('div.dropdown'));
  1021. }
  1022. };
  1023. req.setRequestHeader('Content-Type', 'application/json');
  1024. req.send(JSON.stringify({
  1025. _csrf: context.csrf,
  1026. id_tag: tagId,
  1027. name_tag: tagId == 0 ? tagName : '',
  1028. id_entry: entryId,
  1029. checked: isChecked,
  1030. }));
  1031. }
  1032. };
  1033. }
  1034. function init_nav_entries() {
  1035. const nav_entries = document.getElementById('nav_entries');
  1036. if (nav_entries) {
  1037. nav_entries.querySelector('.previous_entry').onclick = function (e) {
  1038. prev_entry(false);
  1039. return false;
  1040. };
  1041. nav_entries.querySelector('.next_entry').onclick = function (e) {
  1042. next_entry(false);
  1043. return false;
  1044. };
  1045. nav_entries.querySelector('.up').onclick = function (e) {
  1046. const active_item = (document.querySelector('.flux.current') || document.querySelector('.flux')),
  1047. windowTop = document.scrollingElement.scrollTop,
  1048. item_top = active_item.offsetParent.offsetTop + active_item.offsetTop;
  1049. document.scrollingElement.scrollTop = windowTop > item_top ? item_top : 0;
  1050. return false;
  1051. };
  1052. }
  1053. }
  1054. function loadDynamicTags(div) {
  1055. div.classList.remove('dynamictags');
  1056. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1057. const entryId = div.closest('div.flux').id.replace(/^flux_/, '');
  1058. const req = new XMLHttpRequest();
  1059. req.open('GET', './?c=tag&a=getTagsForEntry&id_entry=' + entryId, true);
  1060. req.responseType = 'json';
  1061. req.onerror = function (e) {
  1062. div.querySelectorAll('li.item').forEach(function (li) { li.remove(); });
  1063. div.classList.add('dynamictags');
  1064. };
  1065. req.onload = function (e) {
  1066. if (this.status != 200) {
  1067. return req.onerror(e);
  1068. }
  1069. const json = xmlHttpRequestJson(this);
  1070. if (!json) {
  1071. return req.onerror(e);
  1072. }
  1073. let html = '<li class="item"><label><input class="checkboxTag" name="t_0" type="checkbox" /> <input type="text" name="newTag" /></label></li>';
  1074. if (json && json.length) {
  1075. for (let i = 0; i < json.length; i++) {
  1076. const tag = json[i];
  1077. html += '<li class="item"><label><input class="checkboxTag" name="t_' + tag.id + '" type="checkbox"' +
  1078. (tag.checked ? ' checked="checked"' : '') + '> ' + tag.name + '</label></li>';
  1079. }
  1080. }
  1081. div.querySelector('.dropdown-menu').insertAdjacentHTML('beforeend', html);
  1082. };
  1083. req.send();
  1084. }
  1085. // <actualize>
  1086. var feed_processed = 0;
  1087. function updateFeed(feeds, feeds_count) {
  1088. const feed = feeds.pop();
  1089. if (!feed) {
  1090. return;
  1091. }
  1092. const req = new XMLHttpRequest();
  1093. req.open('POST', feed.url, true);
  1094. req.onloadend = function (e) {
  1095. if (this.status != 200) {
  1096. return badAjax(false);
  1097. }
  1098. feed_processed++;
  1099. const div = document.getElementById('actualizeProgress');
  1100. div.querySelector('.progress').innerHTML = feed_processed + ' / ' + feeds_count;
  1101. div.querySelector('.title').innerHTML = feed.title;
  1102. if (feed_processed === feeds_count) {
  1103. //Empty request to commit new articles
  1104. const req2 = new XMLHttpRequest();
  1105. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1106. req2.onloadend = function (e) {
  1107. delayedFunction(function () { location.reload(); });
  1108. };
  1109. req2.setRequestHeader('Content-Type', 'application/json');
  1110. req2.send(JSON.stringify({
  1111. _csrf: context.csrf,
  1112. noCommit: 0,
  1113. }));
  1114. } else {
  1115. updateFeed(feeds, feeds_count);
  1116. }
  1117. };
  1118. req.setRequestHeader('Content-Type', 'application/json');
  1119. req.send(JSON.stringify({
  1120. _csrf: context.csrf,
  1121. noCommit: 1,
  1122. }));
  1123. }
  1124. function init_actualize() {
  1125. let auto = false;
  1126. const actualize = document.getElementById('actualize');
  1127. if (!actualize) {
  1128. return;
  1129. }
  1130. actualize.onclick = function () {
  1131. if (context.ajax_loading) {
  1132. return false;
  1133. }
  1134. context.ajax_loading = true;
  1135. const req = new XMLHttpRequest();
  1136. req.open('POST', './?c=javascript&a=actualize', true);
  1137. req.responseType = 'json';
  1138. req.onload = function (e) {
  1139. if (this.status != 200) {
  1140. return badAjax(false);
  1141. }
  1142. const json = xmlHttpRequestJson(this);
  1143. if (!json) {
  1144. return badAjax(false);
  1145. }
  1146. if (auto && json.feeds.length < 1) {
  1147. auto = false;
  1148. context.ajax_loading = false;
  1149. return false;
  1150. }
  1151. if (json.feeds.length === 0) {
  1152. openNotification(json.feedback_no_refresh, 'good');
  1153. //Empty request to commit new articles
  1154. const req2 = new XMLHttpRequest();
  1155. req2.open('POST', './?c=feed&a=actualize&id=-1&ajax=1', true);
  1156. req2.onloadend = function (e) {
  1157. context.ajax_loading = false;
  1158. };
  1159. req2.setRequestHeader('Content-Type', 'application/json');
  1160. req2.send(JSON.stringify({
  1161. _csrf: context.csrf,
  1162. noCommit: 0,
  1163. }));
  1164. return;
  1165. }
  1166. //Progress bar
  1167. const feeds_count = json.feeds.length;
  1168. document.body.insertAdjacentHTML('beforeend', '<div id="actualizeProgress" class="notification good">' +
  1169. json.feedback_actualize + '<br /><span class="title">/</span><br /><span class="progress">0 / ' +
  1170. feeds_count + '</span></div>');
  1171. for (let i = 10; i > 0; i--) {
  1172. updateFeed(json.feeds, feeds_count);
  1173. }
  1174. };
  1175. req.setRequestHeader('Content-Type', 'application/json');
  1176. req.send(JSON.stringify({
  1177. _csrf: context.csrf,
  1178. }));
  1179. return false;
  1180. };
  1181. if (context.auto_actualize_feeds) {
  1182. auto = true;
  1183. actualize.click();
  1184. }
  1185. }
  1186. // </actualize>
  1187. // <notification>
  1188. var notification = null,
  1189. notification_interval = null,
  1190. notification_working = false;
  1191. function openNotification(msg, status) {
  1192. if (notification_working === true) {
  1193. return false;
  1194. }
  1195. notification_working = true;
  1196. notification.querySelector('.msg').innerHTML = msg;
  1197. notification.className = 'notification';
  1198. notification.classList.add(status);
  1199. notification_interval = setTimeout(closeNotification, 4000);
  1200. }
  1201. function closeNotification() {
  1202. notification.classList.add('closed');
  1203. clearInterval(notification_interval);
  1204. notification_working = false;
  1205. }
  1206. function init_notifications() {
  1207. notification = document.getElementById('notification');
  1208. notification.querySelector('a.close').onclick = function () {
  1209. closeNotification();
  1210. return false;
  1211. };
  1212. if (notification.querySelector('.msg').innerHTML.length > 0) {
  1213. notification_working = true;
  1214. notification_interval = setTimeout(closeNotification, 4000);
  1215. }
  1216. }
  1217. // </notification>
  1218. // <popup>
  1219. let popup = null,
  1220. popup_iframe_container = null,
  1221. popup_iframe = null,
  1222. popup_txt = null,
  1223. popup_working = false;
  1224. function openPopupWithMessage(msg) {
  1225. if (popup_working === true) {
  1226. return false;
  1227. }
  1228. popup_working = true;
  1229. popup_txt.innerHTML = msg;
  1230. popup_txt.style.display = 'table-row';
  1231. popup.style.display = 'block';
  1232. }
  1233. function openPopupWithSource(source) {
  1234. if (popup_working === true) {
  1235. return false;
  1236. }
  1237. popup_working = true;
  1238. popup_iframe.src = source;
  1239. popup_iframe_container.style.display = 'table-row';
  1240. popup.style.display = 'block';
  1241. }
  1242. function closePopup() {
  1243. popup.style.display = 'none';
  1244. popup_iframe_container.style.display = 'none';
  1245. popup_txt.style.display = 'none';
  1246. popup_iframe.src = 'about:blank';
  1247. popup_working = false;
  1248. }
  1249. function init_popup() {
  1250. //Fetch elements.
  1251. popup = document.getElementById('popup');
  1252. popup_iframe_container = document.getElementById('popup-iframe-container');
  1253. popup_iframe = document.getElementById('popup-iframe');
  1254. popup_txt = document.getElementById('popup-txt');
  1255. //Configure close button.
  1256. document.getElementById('popup-close').addEventListener('click', function (ev) {
  1257. closePopup();
  1258. });
  1259. //Configure close-on-click.
  1260. window.addEventListener('click', function (ev) {
  1261. if (ev.target == popup) {
  1262. closePopup();
  1263. }
  1264. });
  1265. }
  1266. // </popup>
  1267. // <notifs html5>
  1268. var notifs_html5_permission = 'denied';
  1269. function notifs_html5_is_supported() {
  1270. return window.Notification !== undefined;
  1271. }
  1272. function notifs_html5_ask_permission() {
  1273. window.Notification.requestPermission(function () {
  1274. notifs_html5_permission = window.Notification.permission;
  1275. });
  1276. }
  1277. function notifs_html5_show(nb) {
  1278. if (notifs_html5_permission !== 'granted') {
  1279. return;
  1280. }
  1281. const notification = new window.Notification(context.i18n.notif_title_articles, {
  1282. icon: '../themes/icons/favicon-256-padding.png',
  1283. body: context.i18n.notif_body_articles.replace('%d', nb),
  1284. tag: 'freshRssNewArticles',
  1285. });
  1286. notification.onclick = function () {
  1287. delayedFunction(function() {
  1288. location.reload();
  1289. window.focus();
  1290. notification.close();
  1291. });
  1292. };
  1293. if (context.html5_notif_timeout !== 0) {
  1294. setTimeout(function () {
  1295. notification.close();
  1296. }, context.html5_notif_timeout * 1000);
  1297. }
  1298. }
  1299. function init_notifs_html5() {
  1300. if (!notifs_html5_is_supported()) {
  1301. return;
  1302. }
  1303. notifs_html5_permission = notifs_html5_ask_permission();
  1304. }
  1305. // </notifs html5>
  1306. function refreshUnreads() {
  1307. const req = new XMLHttpRequest();
  1308. req.open('GET', './?c=javascript&a=nbUnreadsPerFeed', true);
  1309. req.responseType = 'json';
  1310. req.onload = function (e) {
  1311. const json = xmlHttpRequestJson(this);
  1312. if (!json) {
  1313. return badAjax(false);
  1314. }
  1315. const isAll = document.querySelector('.category.all.active');
  1316. let new_articles = false;
  1317. Object.keys(json.feeds).forEach(function (feed_id) {
  1318. const nbUnreads = json.feeds[feed_id];
  1319. feed_id = 'f_' + feed_id;
  1320. const elem = document.getElementById(feed_id),
  1321. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  1322. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  1323. (nbUnreads - feed_unreads > 0)) {
  1324. const newArticle = document.getElementById('new-article');
  1325. newArticle.setAttribute('aria-hidden', 'false');
  1326. newArticle.style.display = 'block';
  1327. new_articles = true;
  1328. }
  1329. });
  1330. let nbUnreadTags = 0;
  1331. Object.keys(json.tags).forEach(function (tag_id) {
  1332. const nbUnreads = json.tags[tag_id];
  1333. nbUnreadTags += nbUnreads;
  1334. const tag = document.getElementById('t_' + tag_id);
  1335. if (tag) {
  1336. tag.setAttribute('data-unread', nbUnreads);
  1337. tag.querySelector('.item-title').setAttribute('data-unread', numberFormat(nbUnreads));
  1338. }
  1339. });
  1340. const tags = document.querySelector('.category.tags');
  1341. if (tags) {
  1342. tags.setAttribute('data-unread', nbUnreadTags);
  1343. tags.querySelector('.title').setAttribute('data-unread', numberFormat(nbUnreadTags));
  1344. }
  1345. const title = document.querySelector('.category.all .title'),
  1346. nb_unreads = title ? str2int(title.getAttribute('data-unread')) : 0;
  1347. if (nb_unreads > 0 && new_articles) {
  1348. faviconNbUnread(nb_unreads);
  1349. notifs_html5_show(nb_unreads);
  1350. }
  1351. };
  1352. req.send();
  1353. }
  1354. //<endless_mode>
  1355. var url_load_more = '',
  1356. load_more = false,
  1357. box_load_more = null;
  1358. function load_more_posts() {
  1359. if (load_more || !url_load_more || !box_load_more) {
  1360. return;
  1361. }
  1362. load_more = true;
  1363. document.getElementById('load_more').classList.add('loading');
  1364. const req = new XMLHttpRequest();
  1365. req.open('GET', url_load_more, true);
  1366. req.responseType = 'document';
  1367. req.onload = function (e) {
  1368. const html = this.response,
  1369. formPagination = document.getElementById('mark-read-pagination');
  1370. const streamAdopted = document.adoptNode(html.getElementById('stream'));
  1371. streamAdopted.querySelectorAll('.flux, .day').forEach(function (div) {
  1372. box_load_more.insertBefore(div, formPagination);
  1373. });
  1374. const paginationOld = formPagination.querySelector('.pagination'),
  1375. paginationNew = streamAdopted.querySelector('.pagination');
  1376. formPagination.replaceChild(paginationNew, paginationOld);
  1377. const bigMarkAsRead = document.getElementById('bigMarkAsRead');
  1378. if (bigMarkAsRead) {
  1379. if (context.display_order === 'ASC') {
  1380. document.querySelector('#nav_menu_read_all .read_all').formAction = bigMarkAsRead.formAction;
  1381. } else {
  1382. bigMarkAsRead.formAction = document.querySelector('#nav_menu_read_all .read_all').formAction;
  1383. }
  1384. }
  1385. document.querySelectorAll('[id^=day_]').forEach(function (div) {
  1386. const ids = document.querySelectorAll('[id="' + div.id + '"]');
  1387. for (let i = ids.length - 1; i > 0; i--) { //Keep only the first
  1388. ids[i].remove();
  1389. }
  1390. });
  1391. init_load_more(box_load_more);
  1392. const div_load_more = document.getElementById('load_more');
  1393. if (bigMarkAsRead) {
  1394. bigMarkAsRead.removeAttribute('disabled');
  1395. }
  1396. if (div_load_more) {
  1397. div_load_more.classList.remove('loading');
  1398. }
  1399. load_more = false;
  1400. };
  1401. req.send();
  1402. }
  1403. var freshrssLoadMoreEvent = document.createEvent('Event');
  1404. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1405. function init_load_more(box) {
  1406. box_load_more = box;
  1407. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1408. const next_link = document.getElementById('load_more');
  1409. if (!next_link) {
  1410. // no more article to load
  1411. url_load_more = '';
  1412. return;
  1413. }
  1414. url_load_more = next_link.href;
  1415. next_link.onclick = function (e) {
  1416. load_more_posts();
  1417. return false;
  1418. };
  1419. }
  1420. //</endless_mode>
  1421. function init_confirm_action() {
  1422. document.body.onclick = function (ev) {
  1423. const b = ev.target.closest('.confirm');
  1424. if (b) {
  1425. let str_confirmation = this.getAttribute('data-str-confirm');
  1426. if (!str_confirmation) {
  1427. str_confirmation = context.i18n.confirmation_default;
  1428. }
  1429. return confirm(str_confirmation);
  1430. }
  1431. };
  1432. document.querySelectorAll('button.confirm').forEach(function (b) { b.disabled = false; });
  1433. }
  1434. function faviconNbUnread(n) {
  1435. if (typeof n === 'undefined') {
  1436. const t = document.querySelector('.category.all .title');
  1437. n = t ? str2int(t.getAttribute('data-unread')) : 0;
  1438. }
  1439. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1440. const canvas = document.createElement('canvas'),
  1441. link = document.getElementById('favicon').cloneNode(true),
  1442. ratio = window.devicePixelRatio;
  1443. if (canvas.getContext && link) {
  1444. canvas.height = canvas.width = 16 * ratio;
  1445. const img = document.createElement('img');
  1446. img.onload = function () {
  1447. const ctx = canvas.getContext('2d');
  1448. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1449. if (n > 0) {
  1450. let text = '';
  1451. if (n < 1000) {
  1452. text = n;
  1453. } else if (n < 100000) {
  1454. text = Math.floor(n / 1000) + 'k';
  1455. } else {
  1456. text = 'E' + Math.floor(Math.log10(n));
  1457. }
  1458. ctx.font = 'bold ' + 9 * ratio + 'px "Arial", sans-serif';
  1459. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1460. ctx.fillRect(0, 7 * ratio, ctx.measureText(text).width, 9 * ratio);
  1461. ctx.fillStyle = '#F00';
  1462. ctx.fillText(text, 0, canvas.height - 1);
  1463. }
  1464. link.href = canvas.toDataURL('image/png');
  1465. document.querySelector('link[rel~=icon]').remove();
  1466. document.head.appendChild(link);
  1467. };
  1468. img.src = '../favicon.ico';
  1469. }
  1470. }
  1471. function removeFirstLoadSpinner() {
  1472. const first_load = document.getElementById('first_load');
  1473. if (first_load) {
  1474. first_load.remove();
  1475. }
  1476. }
  1477. function init_normal() {
  1478. const stream = document.getElementById('stream');
  1479. if (!stream) {
  1480. if (window.console) {
  1481. console.log('FreshRSS waiting for content…');
  1482. }
  1483. setTimeout(init_normal, 100);
  1484. return;
  1485. }
  1486. init_column_categories();
  1487. init_stream(stream);
  1488. init_shortcuts();
  1489. init_actualize();
  1490. faviconNbUnread();
  1491. window.onbeforeunload = function (e) {
  1492. const sidebar = document.getElementById('sidebar');
  1493. if (sidebar) { //Save sidebar scroll position
  1494. sessionStorage.setItem('FreshRSS_sidebar_scrollTop', sidebar.scrollTop);
  1495. }
  1496. if (mark_read_queue && mark_read_queue.length > 0) {
  1497. return false;
  1498. }
  1499. };
  1500. }
  1501. function init_beforeDOM() {
  1502. document.scrollingElement.scrollTop = 0;
  1503. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1504. init_normal();
  1505. }
  1506. }
  1507. function init_afterDOM() {
  1508. removeFirstLoadSpinner();
  1509. init_notifications();
  1510. init_popup();
  1511. init_confirm_action();
  1512. const stream = document.getElementById('stream');
  1513. if (stream) {
  1514. init_load_more(stream);
  1515. init_posts();
  1516. init_nav_entries();
  1517. init_notifs_html5();
  1518. setTimeout(faviconNbUnread, 1000);
  1519. setInterval(refreshUnreads, 120000);
  1520. }
  1521. if (window.console) {
  1522. console.log('FreshRSS main init done.');
  1523. }
  1524. }
  1525. init_beforeDOM(); //Can be called before DOM is fully loaded
  1526. if (document.readyState && document.readyState !== 'loading') {
  1527. init_afterDOM();
  1528. } else {
  1529. if (window.console) {
  1530. console.log('FreshRSS waiting for DOMContentLoaded…');
  1531. }
  1532. document.addEventListener('DOMContentLoaded', init_afterDOM, false);
  1533. }
  1534. // @license-end