main.js 20 KB

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