main.js 16 KB

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