main.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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 scrollAsRead(box_to_follow) {
  400. var minTop = 40 + (context.current_view === 'global' ? box_to_follow.offset().top : box_to_follow.scrollTop());
  401. $('.not_read:visible').each(function () {
  402. var $this = $(this);
  403. if ($this.offset().top + $this.height() < minTop) {
  404. mark_read($this, true);
  405. }
  406. });
  407. }
  408. function init_posts() {
  409. var box_to_follow = context.current_view === 'global' ? $("#panel") : $(window);
  410. if (context.auto_mark_scroll) {
  411. var lastScroll = 0, //Throttle
  412. timerId = 0;
  413. box_to_follow.scroll(function () {
  414. window.clearTimeout(timerId);
  415. if (lastScroll + 500 < Date.now()) {
  416. lastScroll = Date.now();
  417. scrollAsRead(box_to_follow);
  418. } else {
  419. timerId = window.setTimeout(function() {
  420. scrollAsRead(box_to_follow);
  421. }, 500);
  422. }
  423. });
  424. }
  425. if (context.auto_load_more) {
  426. box_to_follow.scroll(function () {
  427. var load_more = $("#load_more");
  428. if (!load_more.is(':visible')) {
  429. return;
  430. }
  431. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  432. load_more_top = load_more.offset().top;
  433. if (boxBot >= load_more_top) {
  434. load_more_posts();
  435. }
  436. });
  437. box_to_follow.scroll();
  438. }
  439. }
  440. function inject_script(name) {
  441. var script = document.createElement('script');
  442. script.async = 'async';
  443. script.defer = 'defer';
  444. script.src = '../scripts/' + name;
  445. document.head.appendChild(script);
  446. }
  447. function init_sticky_column() {
  448. if (!window.$ || !window.$.fn.stick_in_parent) {
  449. if (window.console) {
  450. console.log('FreshRSS waiting for Sticky-kit…');
  451. }
  452. window.setTimeout(init_sticky_column, 200);
  453. return;
  454. }
  455. if ($('.toggle_aside').css('display') === 'none') {
  456. $('#aside_feed .tree').stick_in_parent({parent:'#aside_feed'});
  457. }
  458. }
  459. function init_column_categories() {
  460. if (context.current_view !== 'normal') {
  461. return;
  462. }
  463. $('#aside_feed').on('click', '.tree-folder>.tree-folder-title>a.dropdown-toggle', function () {
  464. $(this).children().each(function() {
  465. if (this.alt === '▽') {
  466. this.src = this.src.replace('/icons/down.', '/icons/up.');
  467. this.alt = '△';
  468. } else {
  469. this.src = this.src.replace('/icons/up.', '/icons/down.');
  470. this.alt = '▽';
  471. }
  472. });
  473. $(this).parent().next(".tree-folder-items").slideToggle(300 , function() { $(document.body).trigger("sticky_kit:recalc"); });
  474. return false;
  475. });
  476. $('#aside_feed').on('click', '.tree-folder-items .item .dropdown-toggle', function () {
  477. if ($(this).nextAll('.dropdown-menu').length === 0) {
  478. var feed_id = $(this).closest('.item').attr('id').substr(2),
  479. feed_web = $(this).data('fweb'),
  480. template = $('#feed_config_template').html().replace(/------/g, feed_id).replace('http://example.net/', feed_web);
  481. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  482. }
  483. });
  484. init_sticky_column();
  485. }
  486. function init_shortcuts() {
  487. if (!(window.shortcut && window.shortcuts)) {
  488. if (window.console) {
  489. console.log('FreshRSS waiting for sortcut.js…');
  490. }
  491. window.setTimeout(init_shortcuts, 200);
  492. return;
  493. }
  494. // Touches de manipulation
  495. shortcut.add(shortcuts.mark_read, function () {
  496. // on marque comme lu ou non lu
  497. var active = $(".flux.current");
  498. mark_read(active, false);
  499. }, {
  500. 'disable_in_input': true
  501. });
  502. shortcut.add("shift+" + shortcuts.mark_read, function () {
  503. // on marque tout comme lu
  504. $(".nav_menu .read_all").click();
  505. }, {
  506. 'disable_in_input': true
  507. });
  508. shortcut.add(shortcuts.mark_favorite, function () {
  509. // on marque comme favori ou non favori
  510. var active = $(".flux.current");
  511. mark_favorite(active);
  512. }, {
  513. 'disable_in_input': true
  514. });
  515. shortcut.add(shortcuts.collapse_entry, function () {
  516. collapse_entry();
  517. }, {
  518. 'disable_in_input': true
  519. });
  520. shortcut.add(shortcuts.auto_share, function () {
  521. auto_share();
  522. }, {
  523. 'disable_in_input': true
  524. });
  525. shortcut.add(shortcuts.user_filter, function () {
  526. user_filter();
  527. }, {
  528. 'disable_in_input': true
  529. });
  530. function addShortcut(evt) {
  531. if ($('#dropdown-query').siblings('.dropdown-menu').is(':visible')) {
  532. user_filter(String.fromCharCode(evt.keyCode));
  533. } else {
  534. auto_share(String.fromCharCode(evt.keyCode));
  535. }
  536. }
  537. for(var i = 1; i < 10; i++) {
  538. shortcut.add(i.toString(), addShortcut, {
  539. 'disable_in_input': true
  540. });
  541. }
  542. // Touches de navigation pour les articles
  543. shortcut.add(shortcuts.prev_entry, prev_entry, {
  544. 'disable_in_input': true
  545. });
  546. shortcut.add(shortcuts.first_entry, function () {
  547. var old_active = $(".flux.current"),
  548. first = $(".flux:first");
  549. if (first.hasClass("flux")) {
  550. toggleContent(first, old_active);
  551. }
  552. }, {
  553. 'disable_in_input': true
  554. });
  555. shortcut.add(shortcuts.next_entry, next_entry, {
  556. 'disable_in_input': true
  557. });
  558. shortcut.add(shortcuts.last_entry, function () {
  559. var old_active = $(".flux.current"),
  560. last = $(".flux:last");
  561. if (last.hasClass("flux")) {
  562. toggleContent(last, old_active);
  563. }
  564. }, {
  565. 'disable_in_input': true
  566. });
  567. // Touches de navigation pour les flux
  568. shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, {
  569. 'disable_in_input': true
  570. });
  571. shortcut.add("shift+" + shortcuts.next_entry, next_feed, {
  572. 'disable_in_input': true
  573. });
  574. shortcut.add("shift+" + shortcuts.first_entry, first_feed, {
  575. 'disable_in_input': true
  576. });
  577. shortcut.add("shift+" + shortcuts.last_entry, last_feed, {
  578. 'disable_in_input': true
  579. });
  580. // Touches de navigation pour les categories
  581. shortcut.add("alt+" + shortcuts.prev_entry, prev_category, {
  582. 'disable_in_input': true
  583. });
  584. shortcut.add("alt+" + shortcuts.next_entry, next_category, {
  585. 'disable_in_input': true
  586. });
  587. shortcut.add("alt+" + shortcuts.first_entry, first_category, {
  588. 'disable_in_input': true
  589. });
  590. shortcut.add("alt+" + shortcuts.last_entry, last_category, {
  591. 'disable_in_input': true
  592. });
  593. shortcut.add(shortcuts.go_website, function () {
  594. var url_website = $('.flux.current > .flux_header > .title > a').attr("href");
  595. if (context.auto_mark_site) {
  596. $(".flux.current").each(function () {
  597. mark_read($(this), true);
  598. });
  599. }
  600. redirect(url_website, true);
  601. }, {
  602. 'disable_in_input': true
  603. });
  604. shortcut.add(shortcuts.load_more, function () {
  605. load_more_posts();
  606. }, {
  607. 'disable_in_input': true
  608. });
  609. shortcut.add(shortcuts.focus_search, function () {
  610. focus_search();
  611. }, {
  612. 'disable_in_input': true
  613. });
  614. shortcut.add(shortcuts.help, function () {
  615. redirect(url.help, true);
  616. }, {
  617. 'disable_in_input': true
  618. });
  619. shortcut.add(shortcuts.close_dropdown, function () {
  620. window.location.hash = null;
  621. }, {
  622. 'disable_in_input': true
  623. });
  624. }
  625. function init_stream(divStream) {
  626. divStream.on('click', '.flux_header,.flux_content', function (e) { //flux_toggle
  627. if ($(e.target).closest('.content, .item.website, .item.link').length > 0) {
  628. return;
  629. }
  630. var old_active = $(".flux.current"),
  631. new_active = $(this).parent();
  632. isCollapsed = true;
  633. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  634. if (context.auto_mark_article) {
  635. mark_read(new_active, true);
  636. }
  637. return true;
  638. }
  639. toggleContent(new_active, old_active);
  640. });
  641. divStream.on('click', '.flux a.read', function () {
  642. var active = $(this).parents(".flux");
  643. if (context.auto_remove_article && active.hasClass('not_read')) {
  644. auto_remove(active);
  645. }
  646. mark_read(active, false);
  647. return false;
  648. });
  649. divStream.on('click', '.flux a.bookmark', function () {
  650. var active = $(this).parents(".flux");
  651. mark_favorite(active);
  652. return false;
  653. });
  654. divStream.on('click', '.item.title > a', function (e) {
  655. // Allow default control-click behaviour such as open in backround-tab.
  656. return e.ctrlKey;
  657. });
  658. divStream.on('mouseup', '.item.title > a', function (e) {
  659. // Mouseup enables us to catch middle click.
  660. if (e.ctrlKey) {
  661. // CTRL+click, it will be manage by previous rule.
  662. return;
  663. }
  664. if (e.which == 2) {
  665. // If middle click, we want same behaviour as CTRL+click.
  666. var ev = jQuery.Event("click");
  667. ev.ctrlKey = true;
  668. $(this).trigger(ev);
  669. } else if(e.which == 1) {
  670. // Normal click, just toggle article.
  671. $(this).parent().click();
  672. }
  673. });
  674. divStream.on('click', '.flux .content a', function () {
  675. $(this).attr('target', '_blank').attr('rel', 'noreferrer');
  676. });
  677. if (context.auto_mark_site) {
  678. // catch mouseup instead of click so we can have the correct behaviour
  679. // with middle button click (scroll button).
  680. divStream.on('mouseup', '.flux .link > a', function (e) {
  681. if (e.which == 3) {
  682. return;
  683. }
  684. mark_read($(this).parents(".flux"), true);
  685. });
  686. }
  687. }
  688. function init_nav_entries() {
  689. var $nav_entries = $('#nav_entries');
  690. $nav_entries.find('.previous_entry').click(function () {
  691. prev_entry();
  692. return false;
  693. });
  694. $nav_entries.find('.next_entry').click(function () {
  695. next_entry();
  696. return false;
  697. });
  698. $nav_entries.find('.up').click(function () {
  699. var active_item = $(".flux.current"),
  700. windowTop = $(window).scrollTop(),
  701. item_top = active_item.offset().top;
  702. if (windowTop > item_top) {
  703. $("html,body").scrollTop(item_top);
  704. } else {
  705. $("html,body").scrollTop(0);
  706. }
  707. return false;
  708. });
  709. }
  710. // <actualize>
  711. var feed_processed = 0;
  712. function updateFeed(feeds, feeds_count) {
  713. var feed = feeds.pop();
  714. if (!feed) {
  715. return;
  716. }
  717. $.ajax({
  718. type: 'POST',
  719. url: feed.url,
  720. data : {
  721. _csrf: context.csrf,
  722. },
  723. }).always(function (data) {
  724. feed_processed++;
  725. $("#actualizeProgress .progress").html(feed_processed + " / " + feeds_count);
  726. $("#actualizeProgress .title").html(feed.title);
  727. if (feed_processed === feeds_count) {
  728. window.location.reload();
  729. } else {
  730. updateFeed(feeds, feeds_count);
  731. }
  732. });
  733. }
  734. function init_actualize() {
  735. var auto = false;
  736. $("#actualize").click(function () {
  737. if (ajax_loading) {
  738. return false;
  739. }
  740. ajax_loading = true;
  741. $.getJSON('./?c=javascript&a=actualize').done(function (data) {
  742. if (auto && data.feeds.length < 1) {
  743. auto = false;
  744. ajax_loading = false;
  745. return false;
  746. }
  747. if (data.feeds.length === 0) {
  748. openNotification(data.feedback_no_refresh, "good");
  749. ajax_loading = false;
  750. return;
  751. }
  752. //Progress bar
  753. var feeds_count = data.feeds.length;
  754. $('body').after('<div id="actualizeProgress" class="notification good">' + data.feedback_actualize +
  755. '<br /><span class="title">/</span><br /><span class="progress">0 / ' + feeds_count +
  756. '</span></div>');
  757. for (var i = 10; i > 0; i--) {
  758. updateFeed(data.feeds, feeds_count);
  759. }
  760. });
  761. return false;
  762. });
  763. if (context.auto_actualize_feeds) {
  764. auto = true;
  765. $("#actualize").click();
  766. }
  767. }
  768. // </actualize>
  769. // <notification>
  770. var notification = null,
  771. notification_interval = null,
  772. notification_working = false;
  773. function openNotification(msg, status) {
  774. if (notification_working === true) {
  775. return false;
  776. }
  777. notification_working = true;
  778. notification.removeClass();
  779. notification.addClass("notification");
  780. notification.addClass(status);
  781. notification.find(".msg").html(msg);
  782. notification.fadeIn(300);
  783. notification_interval = window.setTimeout(closeNotification, 4000);
  784. }
  785. function closeNotification() {
  786. notification.fadeOut(600, function() {
  787. notification.removeClass();
  788. notification.addClass('closed');
  789. window.clearInterval(notification_interval);
  790. notification_working = false;
  791. });
  792. }
  793. function init_notifications() {
  794. notification = $("#notification");
  795. notification.find("a.close").click(function () {
  796. closeNotification();
  797. return false;
  798. });
  799. if (notification.find(".msg").html().length > 0) {
  800. notification_working = true;
  801. notification_interval = window.setTimeout(closeNotification, 4000);
  802. }
  803. }
  804. // </notification>
  805. // <notifs html5>
  806. var notifs_html5_permission = 'denied';
  807. function notifs_html5_is_supported() {
  808. return window.Notification !== undefined;
  809. }
  810. function notifs_html5_ask_permission() {
  811. window.Notification.requestPermission(function () {
  812. notifs_html5_permission = window.Notification.permission;
  813. });
  814. }
  815. function notifs_html5_show(nb) {
  816. if (notifs_html5_permission !== "granted") {
  817. return;
  818. }
  819. var notification = new window.Notification(i18n.notif_title_articles, {
  820. icon: "../themes/icons/favicon-256.png",
  821. body: i18n.notif_body_articles.replace('%d', nb),
  822. tag: "freshRssNewArticles"
  823. });
  824. notification.onclick = function() {
  825. window.location.reload();
  826. };
  827. if (context.html5_notif_timeout !== 0) {
  828. setTimeout(function() {
  829. notification.close();
  830. }, context.html5_notif_timeout * 1000);
  831. }
  832. }
  833. function init_notifs_html5() {
  834. if (!notifs_html5_is_supported()) {
  835. return;
  836. }
  837. notifs_html5_permission = notifs_html5_ask_permission();
  838. }
  839. // </notifs html5>
  840. function refreshUnreads() {
  841. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  842. var isAll = $('.category.all.active').length > 0,
  843. new_articles = false;
  844. $.each(data, function(feed_id, nbUnreads) {
  845. feed_id = 'f_' + feed_id;
  846. var elem = $('#' + feed_id).get(0),
  847. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  848. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  849. (nbUnreads - feed_unreads > 0)) {
  850. $('#new-article').attr('aria-hidden', 'false').show();
  851. new_articles = true;
  852. }
  853. });
  854. var nb_unreads = str2int($('.category.all .title').attr('data-unread'));
  855. if (nb_unreads > 0 && new_articles) {
  856. faviconNbUnread(nb_unreads);
  857. notifs_html5_show(nb_unreads);
  858. }
  859. });
  860. }
  861. //<endless_mode>
  862. var url_load_more = "",
  863. load_more = false,
  864. box_load_more = null;
  865. function load_more_posts() {
  866. if (load_more || url_load_more === '' || box_load_more === null) {
  867. return;
  868. }
  869. load_more = true;
  870. $('#load_more').addClass('loading');
  871. $.get(url_load_more, function (data) {
  872. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  873. $('.pagination').replaceWith($('.pagination', data));
  874. if (context.display_order === 'ASC') {
  875. $('#nav_menu_read_all .read_all').attr(
  876. 'formaction', $('#bigMarkAsRead').attr('formaction')
  877. );
  878. } else {
  879. $('#bigMarkAsRead').attr(
  880. 'formaction', $('#nav_menu_read_all .read_all').attr('formaction')
  881. );
  882. }
  883. $('[id^=day_]').each(function (i) {
  884. var ids = $('[id="' + this.id + '"]');
  885. if (ids.length > 1) {
  886. $('[id="' + this.id + '"]:gt(0)').remove();
  887. }
  888. });
  889. init_load_more(box_load_more);
  890. $('#load_more').removeClass('loading');
  891. load_more = false;
  892. $(document.body).trigger('sticky_kit:recalc');
  893. });
  894. }
  895. function focus_search() {
  896. $('#search').focus();
  897. }
  898. var freshrssLoadMoreEvent = document.createEvent('Event');
  899. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  900. function init_load_more(box) {
  901. box_load_more = box;
  902. document.body.dispatchEvent(freshrssLoadMoreEvent);
  903. var $next_link = $("#load_more");
  904. if (!$next_link.length) {
  905. // no more article to load
  906. url_load_more = "";
  907. return;
  908. }
  909. url_load_more = $next_link.attr("href");
  910. var $prefetch = $('#prefetch');
  911. if ($prefetch.attr('href') !== url_load_more) {
  912. $prefetch.attr('rel', 'next'); //Remove prefetch
  913. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  914. $prefetch.attr('href', url_load_more);
  915. }
  916. $next_link.click(function () {
  917. load_more_posts();
  918. return false;
  919. });
  920. }
  921. //</endless_mode>
  922. //<crypto form (Web login)>
  923. function poormanSalt() { //If crypto.getRandomValues is not available
  924. var text = '$2a$04$',
  925. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  926. for (var i = 22; i > 0; i--) {
  927. text += base.charAt(Math.floor(Math.random() * 64));
  928. }
  929. return text;
  930. }
  931. function init_crypto_form() {
  932. /* globals dcodeIO */
  933. var $crypto_form = $('#crypto-form');
  934. if ($crypto_form.length === 0) {
  935. return;
  936. }
  937. if (!(window.dcodeIO)) {
  938. if (window.console) {
  939. console.log('FreshRSS waiting for bcrypt.js…');
  940. }
  941. window.setTimeout(init_crypto_form, 100);
  942. return;
  943. }
  944. $crypto_form.on('submit', function() {
  945. var $submit_button = $(this).find('button[type="submit"]');
  946. $submit_button.attr('disabled', '');
  947. var success = false;
  948. $.ajax({
  949. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  950. dataType: 'json',
  951. async: false
  952. }).done(function (data) {
  953. if (!data.salt1 || !data.nonce) {
  954. openNotification('Invalid user!', 'bad');
  955. } else {
  956. try {
  957. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  958. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  959. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  960. $('#challenge').val(c);
  961. if (!s || !c) {
  962. openNotification('Crypto error!', 'bad');
  963. } else {
  964. success = true;
  965. }
  966. } catch (e) {
  967. openNotification('Crypto exception! ' + e, 'bad');
  968. }
  969. }
  970. }).fail(function() {
  971. openNotification('Communication error!', 'bad');
  972. });
  973. $submit_button.removeAttr('disabled');
  974. return success;
  975. });
  976. }
  977. //</crypto form (Web login)>
  978. function init_confirm_action() {
  979. $('body').on('click', '.confirm', function () {
  980. var str_confirmation = $(this).attr('data-str-confirm');
  981. if (!str_confirmation) {
  982. str_confirmation = i18n.confirmation_default;
  983. }
  984. return confirm(str_confirmation);
  985. });
  986. }
  987. function init_print_action() {
  988. $('.item.share > a[href="#"]').click(function () {
  989. var content = "<html><head><style>" +
  990. "body { font-family: Serif; text-align: justify; }" +
  991. "a { color: #000; text-decoration: none; }" +
  992. "a:after { content: ' [' attr(href) ']'}" +
  993. "</style></head><body>" +
  994. $(".flux.current .content").html() +
  995. "</body></html>";
  996. var tmp_window = window.open();
  997. tmp_window.document.writeln(content);
  998. tmp_window.document.close();
  999. tmp_window.focus();
  1000. tmp_window.print();
  1001. tmp_window.close();
  1002. return false;
  1003. });
  1004. }
  1005. function init_share_observers() {
  1006. shares = $('.group-share').length;
  1007. $('.share.add').on('click', function(e) {
  1008. var opt = $(this).siblings('select').find(':selected');
  1009. var row = $(this).parents('form').data(opt.data('form'));
  1010. row = row.replace(/##label##/g, opt.html().trim());
  1011. row = row.replace(/##type##/g, opt.val());
  1012. row = row.replace(/##help##/g, opt.data('help'));
  1013. row = row.replace(/##key##/g, shares);
  1014. $(this).parents('.form-group').before(row);
  1015. shares++;
  1016. return false;
  1017. });
  1018. }
  1019. function init_stats_observers() {
  1020. $('.select-change').on('change', function(e) {
  1021. redirect($(this).find(':selected').data('url'));
  1022. });
  1023. }
  1024. function init_remove_observers() {
  1025. $('.post').on('click', 'a.remove', function(e) {
  1026. var remove_what = $(this).attr('data-remove');
  1027. if (remove_what !== undefined) {
  1028. var remove_obj = $('#' + remove_what);
  1029. remove_obj.remove();
  1030. }
  1031. return false;
  1032. });
  1033. }
  1034. function init_feed_observers() {
  1035. $('select[id="category"]').on('change', function() {
  1036. var detail = $('#new_category_name').parent();
  1037. if ($(this).val() === 'nc') {
  1038. detail.attr('aria-hidden', 'false').show();
  1039. detail.find('input').focus();
  1040. } else {
  1041. detail.attr('aria-hidden', 'true').hide();
  1042. }
  1043. });
  1044. }
  1045. function init_password_observers() {
  1046. $('.toggle-password').on('mousedown', function(e) {
  1047. var button = $(this);
  1048. var passwordField = $('#' + button.attr('data-toggle'));
  1049. passwordField.attr('type', 'text');
  1050. button.addClass('active');
  1051. return false;
  1052. }).on('mouseup', function(e) {
  1053. var button = $(this);
  1054. var passwordField = $('#' + button.attr('data-toggle'));
  1055. passwordField.attr('type', 'password');
  1056. button.removeClass('active');
  1057. return false;
  1058. });
  1059. }
  1060. function faviconNbUnread(n) {
  1061. if (typeof n === 'undefined') {
  1062. n = str2int($('.category.all .title').attr('data-unread'));
  1063. }
  1064. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1065. var canvas = document.createElement('canvas'),
  1066. link = document.getElementById('favicon').cloneNode(true);
  1067. if (canvas.getContext && link) {
  1068. canvas.height = canvas.width = 16;
  1069. var img = document.createElement('img');
  1070. img.onload = function () {
  1071. var ctx = canvas.getContext('2d');
  1072. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1073. if (n > 0) {
  1074. var text = '';
  1075. if (n < 1000) {
  1076. text = n;
  1077. } else if (n < 100000) {
  1078. text = Math.floor(n / 1000) + 'k';
  1079. } else {
  1080. text = 'E' + Math.floor(Math.log10(n));
  1081. }
  1082. ctx.font = 'bold 9px "Arial", sans-serif';
  1083. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1084. ctx.fillRect(0, 7, ctx.measureText(text).width, 9);
  1085. ctx.fillStyle = '#F00';
  1086. ctx.fillText(text, 0, canvas.height - 1);
  1087. }
  1088. link.href = canvas.toDataURL('image/png');
  1089. $('link[rel~=icon]').remove();
  1090. document.head.appendChild(link);
  1091. };
  1092. img.src = '../favicon.ico';
  1093. }
  1094. }
  1095. function init_slider_observers() {
  1096. var slider = $('#slider'),
  1097. closer = $('#close-slider');
  1098. if (slider.length < 1) {
  1099. return;
  1100. }
  1101. $('.post').on('click', '.open-slider', function() {
  1102. if (ajax_loading) {
  1103. return false;
  1104. }
  1105. ajax_loading = true;
  1106. var url_slide = $(this).attr('href');
  1107. $.ajax({
  1108. type: 'GET',
  1109. url: url_slide,
  1110. data : { ajax: true }
  1111. }).done(function (data) {
  1112. slider.html(data);
  1113. closer.addClass('active');
  1114. slider.addClass('active');
  1115. ajax_loading = false;
  1116. });
  1117. return false;
  1118. });
  1119. closer.on('click', function() {
  1120. closer.removeClass('active');
  1121. slider.removeClass('active');
  1122. return false;
  1123. });
  1124. }
  1125. function init_configuration_alert() {
  1126. $(window).on('submit', function(e) {
  1127. window.hasSubmit = true;
  1128. });
  1129. $(window).on('beforeunload', function(e) {
  1130. if (window.hasSubmit) {
  1131. return;
  1132. }
  1133. var fields = $("[data-leave-validation]");
  1134. for (var i = 0; i < fields.length; i++) {
  1135. if ($(fields[i]).attr('type') === 'checkbox' || $(fields[i]).attr('type') === 'radio') {
  1136. // The use of != is done on purpose to check boolean against integer
  1137. if ($(fields[i]).is(':checked') != $(fields[i]).attr('data-leave-validation')) {
  1138. return false;
  1139. }
  1140. } else {
  1141. if ($(fields[i]).attr('data-leave-validation') !== $(fields[i]).val()) {
  1142. return false;
  1143. }
  1144. }
  1145. }
  1146. return;
  1147. });
  1148. }
  1149. function init_subscription() {
  1150. $('body').on('click', '.bookmarkClick', function (e) {
  1151. return false;
  1152. });
  1153. }
  1154. function parseJsonVars() {
  1155. var jsonVars = document.getElementById('jsonVars'),
  1156. json = JSON.parse(jsonVars.innerHTML);
  1157. jsonVars.outerHTML = '';
  1158. window.context = json.context;
  1159. window.shortcuts = json.shortcuts;
  1160. window.url = json.url;
  1161. window.i18n = json.i18n;
  1162. window.icons = json.icons;
  1163. }
  1164. function init_normal() {
  1165. $stream = $('#stream');
  1166. if ($stream.length < 1) {
  1167. if (window.console) {
  1168. console.log('FreshRSS waiting for content…');
  1169. }
  1170. window.setTimeout(init_normal, 100);
  1171. return;
  1172. }
  1173. init_column_categories();
  1174. init_stream($stream);
  1175. init_shortcuts();
  1176. init_actualize();
  1177. faviconNbUnread();
  1178. }
  1179. function init_beforeDOM() {
  1180. if (!window.$) {
  1181. if (window.console) {
  1182. console.log('FreshRSS waiting for jQuery…');
  1183. }
  1184. window.setTimeout(init_beforeDOM, 100);
  1185. return;
  1186. }
  1187. init_confirm_action();
  1188. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1189. inject_script('jquery.sticky-kit.min.js');
  1190. init_normal();
  1191. }
  1192. }
  1193. function init_afterDOM() {
  1194. if (!window.$) {
  1195. if (window.console) {
  1196. console.log('FreshRSS waiting again for jQuery…');
  1197. }
  1198. window.setTimeout(init_afterDOM, 100);
  1199. return;
  1200. }
  1201. init_notifications();
  1202. $stream = $('#stream');
  1203. if ($stream.length > 0) {
  1204. init_load_more($stream);
  1205. init_posts();
  1206. init_nav_entries();
  1207. init_print_action();
  1208. init_notifs_html5();
  1209. window.setInterval(refreshUnreads, 120000);
  1210. } else {
  1211. init_subscription();
  1212. init_crypto_form();
  1213. init_share_observers();
  1214. init_remove_observers();
  1215. init_feed_observers();
  1216. init_password_observers();
  1217. init_stats_observers();
  1218. init_slider_observers();
  1219. init_configuration_alert();
  1220. }
  1221. if (window.console) {
  1222. console.log('FreshRSS init done.');
  1223. }
  1224. }
  1225. parseJsonVars();
  1226. init_beforeDOM(); //Can be called before DOM is fully loaded
  1227. if (document.readyState && document.readyState !== 'loading') {
  1228. init_afterDOM();
  1229. } else if (document.addEventListener) {
  1230. document.addEventListener('DOMContentLoaded', function () {
  1231. if (window.console) {
  1232. console.log('FreshRSS waiting for DOMContentLoaded…');
  1233. }
  1234. init_afterDOM();
  1235. }, false);
  1236. }