main.js 35 KB

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