main.js 35 KB

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