main.js 16 KB

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