main.js 23 KB

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