main.js 21 KB

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