main.js 16 KB

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