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