main.js 15 KB

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