main.js 35 KB

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