main.js 43 KB

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