main.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 prev_feed() {
  216. if ($('li.active').length > 0) {
  217. var pf = $('li.active').prev().find('a.feed');
  218. if (pf.length > 0) {
  219. pf[0].click();
  220. }
  221. } else {
  222. first_feed();
  223. }
  224. }
  225. function next_feed() {
  226. if ($('li.active').length > 0) {
  227. var nf = $('li.active').next().find('a.feed');
  228. if (nf.length > 0) {
  229. nf[0].click();
  230. }
  231. } else {
  232. last_feed();
  233. }
  234. }
  235. function first_feed() {
  236. $('.feeds.active li').first().find('a')[1].click();
  237. }
  238. function last_feed() {
  239. $('.feeds.active li').last().find('a')[1].click();
  240. }
  241. function prev_category() {
  242. if ($('div.active').length > 0) {
  243. var pc = $('div.active').parent('li').prev().find('div.stick a.btn');
  244. if (pc.length > 0) {
  245. pc[0].click();
  246. return;
  247. }
  248. } else {
  249. first_category();
  250. }
  251. }
  252. function next_category() {
  253. if ($('div.active').length > 0) {
  254. var nc = $('div.active').parent('li').next().find('div.stick a.btn');
  255. if (nc.length > 0) {
  256. nc[0].click();
  257. }
  258. } else {
  259. last_category();
  260. }
  261. }
  262. function first_category() {
  263. $('div.category.stick').first().find('a.btn')[0].click();
  264. }
  265. function last_category() {
  266. $('div.category.stick').last().find('a.btn')[0].click();
  267. }
  268. function collapse_entry() {
  269. isCollapsed = !isCollapsed;
  270. $(".flux.current").toggleClass("active");
  271. }
  272. function auto_share(key) {
  273. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  274. var shares = share.siblings('.dropdown-menu').find('.item a');
  275. if (typeof key === "undefined") {
  276. if (!share.length) {
  277. return;
  278. }
  279. // Display the share div
  280. window.location.hash = share.attr('id');
  281. // Force the key value if there is only one action, so we can trigger it automatically
  282. if (shares.length === 1) {
  283. key = 1;
  284. } else {
  285. return;
  286. }
  287. }
  288. // Trigger selected share action and hide the share div
  289. key = parseInt(key);
  290. if (key <= shares.length) {
  291. shares[key - 1].click();
  292. share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click();
  293. }
  294. }
  295. function inMarkViewport(flux, box_to_follow, relative_follow) {
  296. var top = flux.position().top;
  297. if (relative_follow) {
  298. top += box_to_follow.scrollTop();
  299. }
  300. var height = flux.height(),
  301. begin = top + 3 * height / 4,
  302. bot = Math.min(begin + 75, top + height),
  303. windowTop = box_to_follow.scrollTop(),
  304. windowBot = windowTop + box_to_follow.height() / 2;
  305. return (windowBot >= begin && bot >= windowBot);
  306. }
  307. function init_lazyload() {
  308. if ($.fn.lazyload) {
  309. if (is_global_mode()) {
  310. $(".flux_content img").lazyload({
  311. container: $("#panel")
  312. });
  313. } else {
  314. $(".flux_content img").lazyload();
  315. }
  316. }
  317. }
  318. function init_posts() {
  319. init_lazyload();
  320. var box_to_follow = $(window),
  321. relative_follow = false;
  322. if (is_global_mode()) {
  323. box_to_follow = $("#panel");
  324. relative_follow = true;
  325. }
  326. if (auto_mark_scroll) {
  327. box_to_follow.scroll(function () {
  328. $('.not_read:visible').each(function () {
  329. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow, relative_follow)) {
  330. mark_read($(this), true);
  331. }
  332. });
  333. });
  334. }
  335. if (auto_load_more) {
  336. box_to_follow.scroll(function () {
  337. var load_more = $("#load_more");
  338. if (!load_more.is(':visible')) {
  339. return;
  340. }
  341. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  342. load_more_top = load_more.position().top;
  343. if (relative_follow) {
  344. load_more_top += box_to_follow.scrollTop();
  345. }
  346. if (boxBot >= load_more_top) {
  347. load_more_posts();
  348. }
  349. });
  350. }
  351. }
  352. function init_column_categories() {
  353. if (!is_normal_mode()) {
  354. return;
  355. }
  356. $('#aside_flux').on('click', '.category>a.dropdown-toggle', function () {
  357. $(this).children().each(function() {
  358. if (this.alt === '▽') {
  359. this.src = this.src.replace('/icons/down.', '/icons/up.');
  360. this.alt = '△';
  361. } else {
  362. this.src = this.src.replace('/icons/up.', '/icons/down.');
  363. this.alt = '▽';
  364. }
  365. });
  366. $(this).parent().next(".feeds").slideToggle();
  367. return false;
  368. });
  369. $('#aside_flux').on('click', '.feeds .dropdown-toggle', function () {
  370. if ($(this).nextAll('.dropdown-menu').length === 0) {
  371. var feed_id = $(this).closest('li').attr('id').substr(2),
  372. feed_web = $(this).data('fweb'),
  373. template = $('#feed_config_template').html().replace(/!!!!!!/g, feed_id).replace('http://example.net/', feed_web);
  374. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  375. }
  376. });
  377. }
  378. function init_shortcuts() {
  379. if (!(window.shortcut && window.shortcuts)) {
  380. if (window.console) {
  381. console.log('FreshRSS waiting for sortcut.js…');
  382. }
  383. window.setTimeout(init_shortcuts, 50);
  384. return;
  385. }
  386. // Touches de manipulation
  387. shortcut.add(shortcuts.mark_read, function () {
  388. // on marque comme lu ou non lu
  389. var active = $(".flux.current");
  390. mark_read(active, false);
  391. }, {
  392. 'disable_in_input': true
  393. });
  394. shortcut.add("shift+" + shortcuts.mark_read, function () {
  395. // on marque tout comme lu
  396. var url = $(".nav_menu a.read_all").attr("href");
  397. redirect(url, false);
  398. }, {
  399. 'disable_in_input': true
  400. });
  401. shortcut.add(shortcuts.mark_favorite, function () {
  402. // on marque comme favori ou non favori
  403. var active = $(".flux.current");
  404. mark_favorite(active);
  405. }, {
  406. 'disable_in_input': true
  407. });
  408. shortcut.add(shortcuts.collapse_entry, function () {
  409. collapse_entry();
  410. }, {
  411. 'disable_in_input': true
  412. });
  413. shortcut.add(shortcuts.auto_share, function () {
  414. auto_share();
  415. }, {
  416. 'disable_in_input': true
  417. });
  418. for(var i = 1; i < 10; i++){
  419. shortcut.add(i.toString(), function (e) {
  420. auto_share(String.fromCharCode(e.keyCode));
  421. }, {
  422. 'disable_in_input': true
  423. });
  424. }
  425. // Touches de navigation pour les articles
  426. shortcut.add(shortcuts.prev_entry, prev_entry, {
  427. 'disable_in_input': true
  428. });
  429. shortcut.add(shortcuts.first_entry, function () {
  430. var old_active = $(".flux.current"),
  431. first = $(".flux:first");
  432. if (first.hasClass("flux")) {
  433. toggleContent(first, old_active);
  434. }
  435. }, {
  436. 'disable_in_input': true
  437. });
  438. shortcut.add(shortcuts.next_entry, next_entry, {
  439. 'disable_in_input': true
  440. });
  441. shortcut.add(shortcuts.last_entry, function () {
  442. var old_active = $(".flux.current"),
  443. last = $(".flux:last");
  444. if (last.hasClass("flux")) {
  445. toggleContent(last, old_active);
  446. }
  447. }, {
  448. 'disable_in_input': true
  449. });
  450. // Touches de navigation pour les flux
  451. shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, {
  452. 'disable_in_input': true
  453. });
  454. shortcut.add("shift+" + shortcuts.next_entry, next_feed, {
  455. 'disable_in_input': true
  456. });
  457. shortcut.add("shift+" + shortcuts.first_entry, first_feed, {
  458. 'disable_in_input': true
  459. });
  460. shortcut.add("shift+" + shortcuts.last_entry, last_feed, {
  461. 'disable_in_input': true
  462. });
  463. // Touches de navigation pour les categories
  464. shortcut.add("ctrl+" + shortcuts.prev_entry, prev_category, {
  465. 'disable_in_input': true
  466. });
  467. shortcut.add("ctrl+" + shortcuts.next_entry, next_category, {
  468. 'disable_in_input': true
  469. });
  470. shortcut.add("ctrl+" + shortcuts.first_entry, first_category, {
  471. 'disable_in_input': true
  472. });
  473. shortcut.add("ctrl+" + shortcuts.last_entry, last_category, {
  474. 'disable_in_input': true
  475. });
  476. shortcut.add(shortcuts.go_website, function () {
  477. var url_website = $(".flux.active .link a").attr("href");
  478. if (auto_mark_site) {
  479. $(".flux.current").each(function () {
  480. mark_read($(this), true);
  481. });
  482. }
  483. redirect(url_website, true);
  484. }, {
  485. 'disable_in_input': true
  486. });
  487. shortcut.add(shortcuts.load_more, function () {
  488. load_more_posts();
  489. }, {
  490. 'disable_in_input': true
  491. });
  492. }
  493. function init_stream(divStream) {
  494. divStream.on('click', '.flux_header', function (e) { //flux_header_toggle
  495. if ($(e.target).closest('.item.website > a').length > 0) {
  496. return;
  497. }
  498. var old_active = $(".flux.current"),
  499. new_active = $(this).parent();
  500. isCollapsed = true;
  501. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  502. if (auto_mark_article) {
  503. mark_read(new_active, true);
  504. }
  505. return true;
  506. }
  507. toggleContent(new_active, old_active);
  508. });
  509. divStream.on('click', '.flux a.read', function () {
  510. var active = $(this).parents(".flux");
  511. mark_read(active, false);
  512. return false;
  513. });
  514. divStream.on('click', '.flux a.bookmark', function () {
  515. var active = $(this).parents(".flux");
  516. mark_favorite(active);
  517. return false;
  518. });
  519. divStream.on('click', '.item.title>a', function (e) {
  520. if (e.ctrlKey) {
  521. return true; //Allow default control-click behaviour such as open in backround-tab
  522. }
  523. $(this).parent().click(); //Will perform toggle flux_content
  524. return false;
  525. });
  526. divStream.on('click', '.flux .content a', function () {
  527. $(this).attr('target', '_blank');
  528. });
  529. if (auto_mark_site) {
  530. divStream.on('click', '.flux .link a', function () {
  531. mark_read($(this).parent().parent().parent(), true);
  532. });
  533. }
  534. }
  535. function init_nav_entries() {
  536. var $nav_entries = $('#nav_entries');
  537. $nav_entries.find('.previous_entry').click(function () {
  538. prev_entry();
  539. return false;
  540. });
  541. $nav_entries.find('.next_entry').click(function () {
  542. next_entry();
  543. return false;
  544. });
  545. $nav_entries.find('.up').click(function () {
  546. var active_item = $(".flux.current"),
  547. windowTop = $(window).scrollTop(),
  548. item_top = active_item.position().top;
  549. if (windowTop > item_top) {
  550. $("html,body").scrollTop(item_top);
  551. } else {
  552. $("html,body").scrollTop(0);
  553. }
  554. return false;
  555. });
  556. }
  557. function init_actualize() {
  558. $("#actualize").click(function () {
  559. $.getScript('./?c=javascript&a=actualize').done(function () {
  560. updateFeeds();
  561. });
  562. return false;
  563. });
  564. if(auto_actualize_feeds) {
  565. $.getScript('./?c=javascript&a=actualize').done(function () {
  566. updateFeeds();
  567. });
  568. }
  569. }
  570. function closeNotification() {
  571. $(".notification").fadeOut(600, function () {
  572. $(".notification").remove();
  573. });
  574. }
  575. function init_notifications() {
  576. var notif = $(".notification");
  577. if (notif.length > 0) {
  578. window.setInterval(closeNotification, 4000);
  579. notif.find("a.close").click(function () {
  580. closeNotification();
  581. return false;
  582. });
  583. }
  584. }
  585. function refreshUnreads() {
  586. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  587. var isAll = $('.category.all > .active').length > 0;
  588. $.each(data, function(feed_id, nbUnreads) {
  589. feed_id = 'f_' + feed_id;
  590. var elem = $('#' + feed_id + '>.feed').get(0),
  591. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  592. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  593. (nbUnreads - feed_unreads > 0)) {
  594. $('#new-article').show();
  595. };
  596. });
  597. });
  598. }
  599. //<endless_mode>
  600. var url_load_more = "",
  601. load_more = false,
  602. box_load_more = null;
  603. function load_more_posts() {
  604. if (load_more || url_load_more === '' || box_load_more === null) {
  605. return;
  606. }
  607. load_more = true;
  608. $('#load_more').addClass('loading');
  609. $.get(url_load_more, function (data) {
  610. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  611. $('.pagination').replaceWith($('.pagination', data));
  612. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  613. $('[id^=day_]').each(function (i) {
  614. var ids = $('[id="' + this.id + '"]');
  615. if (ids.length > 1) {
  616. $('[id="' + this.id + '"]:gt(0)').remove();
  617. }
  618. });
  619. init_load_more(box_load_more);
  620. init_lazyload();
  621. $('#load_more').removeClass('loading');
  622. load_more = false;
  623. });
  624. }
  625. function init_load_more(box) {
  626. box_load_more = box;
  627. var $next_link = $("#load_more");
  628. if (!$next_link.length) {
  629. // no more article to load
  630. url_load_more = "";
  631. return;
  632. }
  633. url_load_more = $next_link.attr("href");
  634. var $prefetch = $('#prefetch');
  635. if ($prefetch.attr('href') !== url_load_more) {
  636. $prefetch.attr('rel', 'next'); //Remove prefetch
  637. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  638. $prefetch.attr('href', url_load_more);
  639. }
  640. $next_link.click(function () {
  641. load_more_posts();
  642. return false;
  643. });
  644. }
  645. //</endless_mode>
  646. //<Web login form>
  647. function poormanSalt() { //If crypto.getRandomValues is not available
  648. var text = '$2a$04$',
  649. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  650. for (var i = 22; i > 0; i--) {
  651. text += base.charAt(Math.floor(Math.random() * 64));
  652. }
  653. return text;
  654. }
  655. function init_loginForm() {
  656. var $loginForm = $('#loginForm');
  657. if ($loginForm.length === 0) {
  658. return;
  659. }
  660. if (!(window.dcodeIO)) {
  661. if (window.console) {
  662. console.log('FreshRSS waiting for bcrypt.js…');
  663. }
  664. window.setTimeout(init_loginForm, 100);
  665. return;
  666. }
  667. $loginForm.on('submit', function() {
  668. $('#loginButton').attr('disabled', '');
  669. var success = false;
  670. $.ajax({
  671. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  672. dataType: 'json',
  673. async: false
  674. }).done(function (data) {
  675. if (data.salt1 == '' || data.nonce == '') {
  676. alert('Invalid user!');
  677. } else {
  678. try {
  679. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  680. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  681. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  682. $('#challenge').val(c);
  683. if (s == '' || c == '') {
  684. alert('Crypto error!');
  685. } else {
  686. success = true;
  687. }
  688. } catch (e) {
  689. alert('Crypto exception! ' + e);
  690. }
  691. }
  692. }).fail(function() {
  693. alert('Communication error!');
  694. });
  695. $('#loginButton').removeAttr('disabled');
  696. return success;
  697. });
  698. }
  699. //</Web login form>
  700. //<persona>
  701. function init_persona() {
  702. if (!(navigator.id)) {
  703. if (window.console) {
  704. console.log('FreshRSS waiting for Persona…');
  705. }
  706. window.setTimeout(init_persona, 100);
  707. return;
  708. }
  709. $('a.signin').click(function() {
  710. navigator.id.request();
  711. return false;
  712. });
  713. $('a.signout').click(function() {
  714. navigator.id.logout();
  715. return false;
  716. });
  717. navigator.id.watch({
  718. loggedInUser: current_user_mail,
  719. onlogin: function(assertion) {
  720. // A user has logged in! Here you need to:
  721. // 1. Send the assertion to your backend for verification and to create a session.
  722. // 2. Update your UI.
  723. $.ajax ({
  724. type: 'POST',
  725. url: url_login,
  726. data: {assertion: assertion},
  727. success: function(res, status, xhr) {
  728. /*if (res.status === 'failure') {
  729. alert (res_obj.reason);
  730. } else*/ if (res.status === 'okay') {
  731. location.href = url_freshrss;
  732. }
  733. },
  734. error: function(res, status, xhr) {
  735. alert("Login failure: " + res);
  736. }
  737. });
  738. },
  739. onlogout: function() {
  740. // A user has logged out! Here you need to:
  741. // Tear down the user's session by redirecting the user or making a call to your backend.
  742. // Also, make sure loggedInUser will get set to null on the next page load.
  743. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  744. $.ajax ({
  745. type: 'POST',
  746. url: url_logout,
  747. success: function(res, status, xhr) {
  748. location.href = url_freshrss;
  749. },
  750. error: function(res, status, xhr) {
  751. //alert("logout failure" + res);
  752. }
  753. });
  754. }
  755. });
  756. }
  757. //</persona>
  758. function init_confirm_action() {
  759. $('.confirm').click(function () {
  760. return confirm(str_confirmation);
  761. });
  762. }
  763. function init_print_action() {
  764. $('.print-article').click(function () {
  765. var content = "<html><head><style>"
  766. + "body { font-family: Serif; text-align: justify; }"
  767. + "a { color: #000; text-decoration: none; }"
  768. + "a:after { content: ' [' attr(href) ']'}"
  769. + "</style></head><body>"
  770. + $(".flux.current .content").html()
  771. + "</body></html>";
  772. var tmp_window = window.open();
  773. tmp_window.document.writeln(content);
  774. tmp_window.document.close();
  775. tmp_window.focus();
  776. tmp_window.print();
  777. tmp_window.close();
  778. return false;
  779. });
  780. }
  781. function init_all() {
  782. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  783. if (window.console) {
  784. console.log('FreshRSS waiting for JS…');
  785. }
  786. window.setTimeout(init_all, 50);
  787. return;
  788. }
  789. init_notifications();
  790. switch (authType) {
  791. case 'form':
  792. init_loginForm();
  793. break;
  794. case 'persona':
  795. init_persona();
  796. break;
  797. }
  798. init_confirm_action();
  799. $stream = $('#stream');
  800. if ($stream.length > 0) {
  801. init_actualize();
  802. init_column_categories();
  803. init_load_more($stream);
  804. init_posts();
  805. init_stream($stream);
  806. init_nav_entries();
  807. init_shortcuts();
  808. init_print_action();
  809. window.setInterval(refreshUnreads, 120000);
  810. }
  811. if (window.console) {
  812. console.log('FreshRSS init done.');
  813. }
  814. }
  815. if (document.readyState && document.readyState !== 'loading') {
  816. if (window.console) {
  817. console.log('FreshRSS immediate init…');
  818. }
  819. init_all();
  820. } else if (document.addEventListener) {
  821. document.addEventListener('DOMContentLoaded', function () {
  822. if (window.console) {
  823. console.log('FreshRSS waiting for DOMContentLoaded…');
  824. }
  825. init_all();
  826. }, false);
  827. }