main.js 15 KB

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