main.js 21 KB

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