main.js 21 KB

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