main.js 16 KB

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