main.js 43 KB

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