main.js 17 KB

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