main.js 23 KB

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