main.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. "use strict";
  2. var $stream = null,
  3. isCollapsed = true;
  4. function is_normal_mode() {
  5. return $stream.hasClass('normal');
  6. }
  7. function is_global_mode() {
  8. return $stream.hasClass('global');
  9. }
  10. function redirect(url, new_tab) {
  11. if (url) {
  12. if (new_tab) {
  13. window.open(url);
  14. } else {
  15. location.href = url;
  16. }
  17. }
  18. }
  19. function incLabel(p, inc) {
  20. var i = (parseInt(p.replace(/\D/g, ''), 10) || 0) + inc;
  21. return i > 0 ? ' (' + i + ')' : '';
  22. }
  23. function incUnreadsFeed(article, feed_id, nb) {
  24. //Update unread: feed
  25. var elem = $('#' + feed_id + '>.feed').get(0),
  26. feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread'), 10) || 0) : 0,
  27. feed_priority = elem ? (parseInt(elem.getAttribute('data-priority'), 10) || 0) : 0;
  28. if (elem) {
  29. elem.setAttribute('data-unread', Math.max(0, feed_unreads + nb));
  30. }
  31. //Update unread: category
  32. elem = $('#' + feed_id).parent().prevAll('.category').children(':first').get(0);
  33. feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread'), 10) || 0) : 0;
  34. if (elem) {
  35. elem.setAttribute('data-unread', Math.max(0, feed_unreads + nb));
  36. }
  37. //Update unread: all
  38. if (feed_priority > 0) {
  39. elem = $('#aside_flux .all').children(':first').get(0);
  40. if (elem) {
  41. feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread'), 10) || 0) : 0;
  42. elem.setAttribute('data-unread', Math.max(0, feed_unreads + nb));
  43. }
  44. }
  45. //Update unread: favourites
  46. if (article && article.closest('div').hasClass('favorite')) {
  47. elem = $('#aside_flux .favorites').children(':first').get(0);
  48. if (elem) {
  49. feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread'), 10) || 0) : 0;
  50. elem.setAttribute('data-unread', Math.max(0, feed_unreads + nb));
  51. }
  52. }
  53. //Update unread: title
  54. document.title = document.title.replace(/((?: \(\d+\))?)( · .*?)((?: \(\d+\))?)$/, function (m, p1, p2, p3) {
  55. return incLabel(p1, nb) + p2 + incLabel(p3, feed_priority > 0 ? nb : 0);
  56. });
  57. }
  58. function mark_read(active, only_not_read) {
  59. if (active[0] === undefined || (only_not_read === true && !active.hasClass("not_read"))) {
  60. return false;
  61. }
  62. var url = active.find("a.read").attr("href");
  63. if (url === undefined) {
  64. return false;
  65. }
  66. $.ajax({
  67. type: 'POST',
  68. url: url,
  69. data : { ajax: true }
  70. }).done(function (data) {
  71. var $r = active.find("a.read").attr("href", data.url),
  72. inc = 0;
  73. if (active.hasClass("not_read")) {
  74. active.removeClass("not_read");
  75. inc--;
  76. } else if (only_not_read !== true || active.hasClass("not_read")) {
  77. active.addClass("not_read");
  78. inc++;
  79. }
  80. $r.find('.icon').replaceWith(data.icon);
  81. var feed_url = active.find(".website>a").attr("href"),
  82. feed_id = feed_url.substr(feed_url.lastIndexOf('f_'));
  83. incUnreadsFeed(active, feed_id, inc);
  84. });
  85. }
  86. function mark_favorite(active) {
  87. if (active[0] === undefined) {
  88. return false;
  89. }
  90. var url = active.find("a.bookmark").attr("href");
  91. if (url === undefined) {
  92. return false;
  93. }
  94. $.ajax({
  95. type: 'POST',
  96. url: url,
  97. data : { ajax: true }
  98. }).done(function (data) {
  99. var $b = active.find("a.bookmark").attr("href", data.url),
  100. inc = 0;
  101. if (active.hasClass("favorite")) {
  102. active.removeClass("favorite");
  103. inc--;
  104. } else {
  105. active.addClass("favorite").find('.bookmark');
  106. inc++;
  107. }
  108. $b.find('.icon').replaceWith(data.icon);
  109. var favourites = $('.favorites>a').contents().last().get(0);
  110. if (favourites && favourites.textContent) {
  111. favourites.textContent = favourites.textContent.replace(/((?: \(\d+\))?\s*)$/, function (m, p1) {
  112. return incLabel(p1, inc);
  113. });
  114. }
  115. if (active.closest('div').hasClass('not_read')) {
  116. var elem = $('#aside_flux .favorites').children(':first').get(0),
  117. feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread'), 10) || 0) : 0;
  118. elem.setAttribute('data-unread', Math.max(0, feed_unreads + inc));
  119. }
  120. });
  121. }
  122. function toggleContent(new_active, old_active) {
  123. if (does_lazyload) {
  124. new_active.find('img[data-original], iframe[data-original]').each(function () {
  125. this.setAttribute('src', this.getAttribute('data-original'));
  126. this.removeAttribute('data-original');
  127. });
  128. }
  129. old_active.removeClass("active").removeClass("current");
  130. if (old_active[0] !== new_active[0]) {
  131. if (isCollapsed) {
  132. new_active.addClass("active");
  133. }
  134. new_active.addClass("current");
  135. }
  136. var box_to_move = "html,body",
  137. relative_move = false;
  138. if (is_global_mode()) {
  139. box_to_move = "#panel";
  140. relative_move = true;
  141. }
  142. var new_pos = new_active.position().top,
  143. old_scroll = $(box_to_move).scrollTop();
  144. if (hide_posts) {
  145. new_pos = new_active.position().top;
  146. old_scroll = $(box_to_move).scrollTop();
  147. if (relative_move) {
  148. new_pos += old_scroll;
  149. }
  150. if (old_active[0] !== new_active[0]) {
  151. new_active.children(".flux_content").first().each(function () {
  152. $(box_to_move).scrollTop(new_pos).scrollTop();
  153. });
  154. }
  155. } else {
  156. if (relative_move) {
  157. new_pos += old_scroll;
  158. }
  159. $(box_to_move).scrollTop(new_pos).scrollTop();
  160. }
  161. if (auto_mark_article) {
  162. mark_read(new_active, true);
  163. }
  164. }
  165. function prev_entry() {
  166. var old_active = $(".flux.current"),
  167. last_active = $(".flux:last"),
  168. new_active = old_active.prevAll(".flux:first");
  169. if (new_active.hasClass("flux")) {
  170. toggleContent(new_active, old_active);
  171. } else if (old_active[0] === undefined && new_active[0] === undefined) {
  172. toggleContent(last_active, old_active);
  173. }
  174. }
  175. function next_entry() {
  176. var old_active = $(".flux.current"),
  177. first_active = $(".flux:first"),
  178. last_active = $(".flux:last"),
  179. new_active = old_active.nextAll(".flux:first");
  180. if (new_active.hasClass("flux")) {
  181. toggleContent(new_active, old_active);
  182. } else if (old_active[0] === undefined && new_active[0] === undefined) {
  183. toggleContent(first_active, old_active);
  184. }
  185. if ((!auto_load_more) && (last_active.attr("id") === new_active.attr("id"))) {
  186. load_more_posts();
  187. }
  188. }
  189. function collapse_entry() {
  190. isCollapsed = !isCollapsed;
  191. $(".flux.current").toggleClass("active");
  192. }
  193. function auto_share() {
  194. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  195. if (share.length) {
  196. window.location.hash = share.attr('id');
  197. }
  198. }
  199. function inMarkViewport(flux, box_to_follow, relative_follow) {
  200. var top = flux.position().top;
  201. if (relative_follow) {
  202. top += box_to_follow.scrollTop();
  203. }
  204. var height = flux.height(),
  205. begin = top + 3 * height / 4,
  206. bot = Math.min(begin + 75, top + height),
  207. windowTop = box_to_follow.scrollTop(),
  208. windowBot = windowTop + box_to_follow.height() / 2;
  209. return (windowBot >= begin && bot >= windowBot);
  210. }
  211. function init_lazyload() {
  212. if ($.fn.lazyload) {
  213. if (is_global_mode()) {
  214. $(".flux_content img").lazyload({
  215. container: $("#panel")
  216. });
  217. } else {
  218. $(".flux_content img").lazyload();
  219. }
  220. }
  221. }
  222. function init_posts() {
  223. init_lazyload();
  224. var box_to_follow = $(window),
  225. relative_follow = false;
  226. if (is_global_mode()) {
  227. box_to_follow = $("#panel");
  228. relative_follow = true;
  229. }
  230. if (auto_mark_scroll) {
  231. box_to_follow.scroll(function () {
  232. $('.not_read:visible').each(function () {
  233. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow, relative_follow)) {
  234. mark_read($(this), true);
  235. }
  236. });
  237. });
  238. }
  239. if (auto_load_more) {
  240. box_to_follow.scroll(function () {
  241. var load_more = $("#load_more");
  242. if (!load_more.is(':visible')) {
  243. return;
  244. }
  245. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  246. load_more_top = load_more.position().top;
  247. if (relative_follow) {
  248. load_more_top += box_to_follow.scrollTop();
  249. }
  250. if (boxBot >= load_more_top) {
  251. load_more_posts();
  252. }
  253. });
  254. }
  255. }
  256. function init_column_categories() {
  257. if (!is_normal_mode()) {
  258. return;
  259. }
  260. $('#aside_flux').on('click', '.category>a.dropdown-toggle', function () {
  261. $(this).children().each(function() {
  262. if (this.alt === '▽') {
  263. this.src = this.src.replace('/icons/down.', '/icons/up.');
  264. this.alt = '△';
  265. } else {
  266. this.src = this.src.replace('/icons/up.', '/icons/down.');
  267. this.alt = '▽';
  268. }
  269. });
  270. $(this).parent().next(".feeds").slideToggle();
  271. return false;
  272. });
  273. }
  274. function init_shortcuts() {
  275. if (!(window.shortcut && window.shortcuts)) {
  276. if (window.console) {
  277. console.log('FreshRSS waiting for sortcut.js…');
  278. }
  279. window.setTimeout(init_shortcuts, 50);
  280. return;
  281. }
  282. // Touches de manipulation
  283. shortcut.add(shortcuts.mark_read, function () {
  284. // on marque comme lu ou non lu
  285. var active = $(".flux.current");
  286. mark_read(active, false);
  287. }, {
  288. 'disable_in_input': true
  289. });
  290. shortcut.add("shift+" + shortcuts.mark_read, function () {
  291. // on marque tout comme lu
  292. var url = $(".nav_menu a.read_all").attr("href");
  293. redirect(url, false);
  294. }, {
  295. 'disable_in_input': true
  296. });
  297. shortcut.add(shortcuts.mark_favorite, function () {
  298. // on marque comme favori ou non favori
  299. var active = $(".flux.current");
  300. mark_favorite(active);
  301. }, {
  302. 'disable_in_input': true
  303. });
  304. shortcut.add(shortcuts.collapse_entry, function () {
  305. collapse_entry();
  306. }, {
  307. 'disable_in_input': true
  308. });
  309. shortcut.add(shortcuts.auto_share, function () {
  310. auto_share();
  311. }, {
  312. 'disable_in_input': true
  313. });
  314. // Touches de navigation
  315. shortcut.add(shortcuts.prev_entry, prev_entry, {
  316. 'disable_in_input': true
  317. });
  318. shortcut.add("shift+" + shortcuts.prev_entry, function () {
  319. var old_active = $(".flux.current"),
  320. first = $(".flux:first");
  321. if (first.hasClass("flux")) {
  322. toggleContent(first, old_active);
  323. }
  324. }, {
  325. 'disable_in_input': true
  326. });
  327. shortcut.add(shortcuts.next_entry, next_entry, {
  328. 'disable_in_input': true
  329. });
  330. shortcut.add("shift+" + shortcuts.next_entry, function () {
  331. var old_active = $(".flux.current"),
  332. last = $(".flux:last");
  333. if (last.hasClass("flux")) {
  334. toggleContent(last, old_active);
  335. }
  336. }, {
  337. 'disable_in_input': true
  338. });
  339. shortcut.add(shortcuts.go_website, function () {
  340. var url_website = $(".flux.active .link a").attr("href");
  341. if (auto_mark_site) {
  342. $(".flux.current").each(function () {
  343. mark_read($(this), true);
  344. });
  345. }
  346. redirect(url_website, true);
  347. }, {
  348. 'disable_in_input': true
  349. });
  350. shortcut.add(shortcuts.load_more, function () {
  351. load_more_posts();
  352. }, {
  353. 'disable_in_input': true
  354. });
  355. }
  356. function init_stream_delegates(divStream) {
  357. divStream.on('click', '.flux_header', function (e) { //flux_header_toggle
  358. if ($(e.target).closest('.item.website > a').length > 0) {
  359. return;
  360. }
  361. var old_active = $(".flux.current"),
  362. new_active = $(this).parent();
  363. isCollapsed = true;
  364. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  365. if (auto_mark_article) {
  366. mark_read(new_active, true);
  367. }
  368. return true;
  369. }
  370. toggleContent(new_active, old_active);
  371. });
  372. divStream.on('click', '.flux a.read', function () {
  373. var active = $(this).parents(".flux");
  374. mark_read(active, false);
  375. return false;
  376. });
  377. divStream.on('click', '.flux a.bookmark', function () {
  378. var active = $(this).parents(".flux");
  379. mark_favorite(active);
  380. return false;
  381. });
  382. divStream.on('click', '.item.title>a', function (e) {
  383. if (e.ctrlKey) {
  384. return true; //Allow default control-click behaviour such as open in backround-tab
  385. }
  386. $(this).parent().click(); //Will perform toggle flux_content
  387. return false;
  388. });
  389. divStream.on('click', '.flux .content a', function () {
  390. $(this).attr('target', '_blank');
  391. });
  392. if (auto_mark_site) {
  393. divStream.on('click', '.flux .link a', function () {
  394. mark_read($(this).parent().parent().parent(), true);
  395. });
  396. }
  397. }
  398. function init_nav_entries() {
  399. var $nav_entries = $('#nav_entries');
  400. $nav_entries.find('.previous_entry').click(function () {
  401. prev_entry();
  402. return false;
  403. });
  404. $nav_entries.find('.next_entry').click(function () {
  405. next_entry();
  406. return false;
  407. });
  408. $nav_entries.find('.up').click(function () {
  409. var active_item = $(".flux.current"),
  410. windowTop = $(window).scrollTop(),
  411. item_top = active_item.position().top;
  412. if (windowTop > item_top) {
  413. $("html,body").scrollTop(item_top);
  414. } else {
  415. $("html,body").scrollTop(0);
  416. }
  417. return false;
  418. });
  419. }
  420. function init_templates() {
  421. $('#aside_flux').on('click', '.feeds .dropdown-toggle', function () {
  422. if ($(this).nextAll('.dropdown-menu').length === 0) {
  423. var feed_id = $(this).closest('li').attr('id').substr(2),
  424. feed_web = $(this).data('fweb'),
  425. template = $('#feed_config_template').html().replace(/!!!!!!/g, feed_id).replace('http://example.net/', feed_web);
  426. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  427. }
  428. });
  429. }
  430. function init_actualize() {
  431. $("#actualize").click(function () {
  432. $.getScript('./?c=javascript&a=actualize').done(function () {
  433. updateFeeds();
  434. });
  435. return false;
  436. });
  437. if(auto_actualize_feeds) {
  438. $.getScript('./?c=javascript&a=actualize').done(function () {
  439. updateFeeds();
  440. });
  441. }
  442. }
  443. function closeNotification() {
  444. $(".notification").fadeOut(600, function () {
  445. $(".notification").remove();
  446. });
  447. }
  448. function init_notifications() {
  449. var notif = $(".notification");
  450. if (notif[0] !== undefined) {
  451. window.setInterval(closeNotification, 4000);
  452. notif.find("a.close").click(function () {
  453. closeNotification();
  454. return false;
  455. });
  456. }
  457. }
  458. function refreshUnreads() {
  459. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  460. $.each(data, function(feed_id, nbUnreads) {
  461. feed_id = 'f_' + feed_id;
  462. var elem = $('#' + feed_id + '>.feed').get(0),
  463. feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread'), 10) || 0) : 0;
  464. incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads);
  465. });
  466. });
  467. }
  468. //<endless_mode>
  469. var url_load_more = "",
  470. load_more = false,
  471. box_load_more = null;
  472. function load_more_posts() {
  473. if (load_more || url_load_more === '' || box_load_more === null) {
  474. return;
  475. }
  476. load_more = true;
  477. $('#load_more').addClass('loading');
  478. $.get(url_load_more, function (data) {
  479. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  480. $('.pagination').replaceWith($('.pagination', data));
  481. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  482. $('[id^=day_]').each(function (i) {
  483. var ids = $('[id="' + this.id + '"]');
  484. if (ids.length > 1) {
  485. $('[id="' + this.id + '"]:gt(0)').remove();
  486. }
  487. });
  488. init_load_more(box_load_more);
  489. init_lazyload();
  490. $('#load_more').removeClass('loading');
  491. load_more = false;
  492. });
  493. }
  494. function init_load_more(box) {
  495. var $next_link = $("#load_more");
  496. if (!$next_link.length) {
  497. // no more article to load
  498. url_load_more = "";
  499. return;
  500. }
  501. box_load_more = box;
  502. url_load_more = $next_link.attr("href");
  503. var $prefetch = $('#prefetch');
  504. if ($prefetch.attr('href') !== url_load_more) {
  505. $prefetch.attr('rel', 'next'); //Remove prefetch
  506. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  507. $prefetch.attr('href', url_load_more);
  508. }
  509. $next_link.click(function () {
  510. load_more_posts();
  511. return false;
  512. });
  513. }
  514. //</endless_mode>
  515. //<persona>
  516. function init_persona() {
  517. if (!(navigator.id)) {
  518. if (window.console) {
  519. console.log('FreshRSS waiting for Persona…');
  520. }
  521. window.setTimeout(init_persona, 100);
  522. return;
  523. }
  524. $('a.signin').click(function() {
  525. navigator.id.request();
  526. return false;
  527. });
  528. $('a.signout').click(function() {
  529. navigator.id.logout();
  530. return false;
  531. });
  532. navigator.id.watch({
  533. loggedInUser: current_user_mail,
  534. onlogin: function(assertion) {
  535. // A user has logged in! Here you need to:
  536. // 1. Send the assertion to your backend for verification and to create a session.
  537. // 2. Update your UI.
  538. $.ajax ({
  539. type: 'POST',
  540. url: url_login,
  541. data: {assertion: assertion},
  542. success: function(res, status, xhr) {
  543. /*if (res.status === 'failure') {
  544. alert (res_obj.reason);
  545. } else*/ if (res.status === 'okay') {
  546. location.href = url_freshrss;
  547. }
  548. },
  549. error: function(res, status, xhr) {
  550. alert("Login failure: " + res);
  551. }
  552. });
  553. },
  554. onlogout: function() {
  555. // A user has logged out! Here you need to:
  556. // Tear down the user's session by redirecting the user or making a call to your backend.
  557. // Also, make sure loggedInUser will get set to null on the next page load.
  558. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  559. $.ajax ({
  560. type: 'POST',
  561. url: url_logout,
  562. success: function(res, status, xhr) {
  563. location.href = url_freshrss;
  564. },
  565. error: function(res, status, xhr) {
  566. //alert("logout failure" + res);
  567. }
  568. });
  569. }
  570. });
  571. }
  572. //</persona>
  573. function init_confirm_action() {
  574. $('.confirm').click(function () {
  575. return confirm(str_confirmation);
  576. });
  577. }
  578. function init_print_action() {
  579. $('.print-article').click(function () {
  580. var content = "<html><head><style>"
  581. + "body { font-family: Serif; text-align: justify; }"
  582. + "a { color: #000; text-decoration: none; }"
  583. + "a:after { content: ' [' attr(href) ']'}"
  584. + "</style></head><body>"
  585. + $(".flux.current .content").html()
  586. + "</body></html>";
  587. var tmp_window = window.open();
  588. tmp_window.document.writeln(content);
  589. tmp_window.document.close();
  590. tmp_window.focus();
  591. tmp_window.print();
  592. tmp_window.close();
  593. return false;
  594. });
  595. }
  596. function init_all() {
  597. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  598. if (window.console) {
  599. console.log('FreshRSS waiting for JS…');
  600. }
  601. window.setTimeout(init_all, 50);
  602. return;
  603. }
  604. $stream = $('#stream');
  605. init_posts();
  606. init_column_categories();
  607. if (load_shortcuts) {
  608. init_shortcuts();
  609. }
  610. init_stream_delegates($stream);
  611. init_nav_entries();
  612. init_templates();
  613. init_notifications();
  614. init_actualize();
  615. init_load_more($stream);
  616. if (use_persona) {
  617. init_persona();
  618. }
  619. init_confirm_action();
  620. init_print_action();
  621. window.setInterval(refreshUnreads, 120000);
  622. if (window.console) {
  623. console.log('FreshRSS init done.');
  624. }
  625. }
  626. if (document.readyState && document.readyState !== 'loading') {
  627. if (window.console) {
  628. console.log('FreshRSS immediate init…');
  629. }
  630. init_all();
  631. } else if (document.addEventListener) {
  632. document.addEventListener('DOMContentLoaded', function () {
  633. if (window.console) {
  634. console.log('FreshRSS waiting for DOMContentLoaded…');
  635. }
  636. init_all();
  637. }, false);
  638. }