main.js 42 KB

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