main.js 35 KB

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