main.js 16 KB

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