main.js 17 KB

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