main.js 21 KB

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