main.js 20 KB

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