main.js 42 KB

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