main.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 && feed_count > 0) {
  588. $("#actualize").click();
  589. }
  590. }
  591. // <notification>
  592. var notification = null,
  593. notification_interval = null,
  594. notification_working = false;
  595. function openNotification(msg, status) {
  596. if (notification_working === true) {
  597. return false;
  598. }
  599. notification_working = true;
  600. notification.removeClass();
  601. notification.addClass(status);
  602. notification.find(".msg").html(msg);
  603. notification.fadeIn(300);
  604. notification_interval = window.setInterval(closeNotification, 4000);
  605. }
  606. function closeNotification() {
  607. notification.fadeOut(600, function() {
  608. notification.removeClass();
  609. notification.addClass('closed');
  610. window.clearInterval(notification_interval);
  611. notification_working = false;
  612. });
  613. }
  614. function init_notifications() {
  615. notification = $("#notification");
  616. notification.find("a.close").click(function () {
  617. closeNotification();
  618. return false;
  619. });
  620. if (notification.find(".msg").html().length > 0) {
  621. notification_working = true;
  622. notification_interval = window.setInterval(closeNotification, 4000);
  623. }
  624. }
  625. // </notification>
  626. function refreshUnreads() {
  627. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  628. var isAll = $('.category.all > .active').length > 0;
  629. $.each(data, function(feed_id, nbUnreads) {
  630. feed_id = 'f_' + feed_id;
  631. var elem = $('#' + feed_id + '>.feed').get(0),
  632. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  633. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  634. (nbUnreads - feed_unreads > 0)) {
  635. $('#new-article').show();
  636. };
  637. });
  638. });
  639. }
  640. //<endless_mode>
  641. var url_load_more = "",
  642. load_more = false,
  643. box_load_more = null;
  644. function load_more_posts() {
  645. if (load_more || url_load_more === '' || box_load_more === null) {
  646. return;
  647. }
  648. load_more = true;
  649. $('#load_more').addClass('loading');
  650. $.get(url_load_more, function (data) {
  651. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  652. $('.pagination').replaceWith($('.pagination', data));
  653. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  654. $('[id^=day_]').each(function (i) {
  655. var ids = $('[id="' + this.id + '"]');
  656. if (ids.length > 1) {
  657. $('[id="' + this.id + '"]:gt(0)').remove();
  658. }
  659. });
  660. init_load_more(box_load_more);
  661. init_lazyload();
  662. $('#load_more').removeClass('loading');
  663. load_more = false;
  664. });
  665. }
  666. function init_load_more(box) {
  667. box_load_more = box;
  668. var $next_link = $("#load_more");
  669. if (!$next_link.length) {
  670. // no more article to load
  671. url_load_more = "";
  672. return;
  673. }
  674. url_load_more = $next_link.attr("href");
  675. var $prefetch = $('#prefetch');
  676. if ($prefetch.attr('href') !== url_load_more) {
  677. $prefetch.attr('rel', 'next'); //Remove prefetch
  678. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  679. $prefetch.attr('href', url_load_more);
  680. }
  681. $next_link.click(function () {
  682. load_more_posts();
  683. return false;
  684. });
  685. }
  686. //</endless_mode>
  687. //<Web login form>
  688. function poormanSalt() { //If crypto.getRandomValues is not available
  689. var text = '$2a$04$',
  690. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  691. for (var i = 22; i > 0; i--) {
  692. text += base.charAt(Math.floor(Math.random() * 64));
  693. }
  694. return text;
  695. }
  696. function init_loginForm() {
  697. var $loginForm = $('#loginForm');
  698. if ($loginForm.length === 0) {
  699. return;
  700. }
  701. if (!(window.dcodeIO)) {
  702. if (window.console) {
  703. console.log('FreshRSS waiting for bcrypt.js…');
  704. }
  705. window.setTimeout(init_loginForm, 100);
  706. return;
  707. }
  708. $loginForm.on('submit', function() {
  709. $('#loginButton').attr('disabled', '');
  710. var success = false;
  711. $.ajax({
  712. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  713. dataType: 'json',
  714. async: false
  715. }).done(function (data) {
  716. if (data.salt1 == '' || data.nonce == '') {
  717. alert('Invalid user!');
  718. } else {
  719. try {
  720. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  721. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  722. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  723. $('#challenge').val(c);
  724. if (s == '' || c == '') {
  725. alert('Crypto error!');
  726. } else {
  727. success = true;
  728. }
  729. } catch (e) {
  730. alert('Crypto exception! ' + e);
  731. }
  732. }
  733. }).fail(function() {
  734. alert('Communication error!');
  735. });
  736. $('#loginButton').removeAttr('disabled');
  737. return success;
  738. });
  739. }
  740. //</Web login form>
  741. //<persona>
  742. function init_persona() {
  743. if (!(navigator.id)) {
  744. if (window.console) {
  745. console.log('FreshRSS waiting for Persona…');
  746. }
  747. window.setTimeout(init_persona, 100);
  748. return;
  749. }
  750. $('a.signin').click(function() {
  751. navigator.id.request();
  752. return false;
  753. });
  754. $('a.signout').click(function() {
  755. navigator.id.logout();
  756. return false;
  757. });
  758. navigator.id.watch({
  759. loggedInUser: current_user_mail,
  760. onlogin: function(assertion) {
  761. // A user has logged in! Here you need to:
  762. // 1. Send the assertion to your backend for verification and to create a session.
  763. // 2. Update your UI.
  764. $.ajax ({
  765. type: 'POST',
  766. url: url_login,
  767. data: {assertion: assertion},
  768. success: function(res, status, xhr) {
  769. /*if (res.status === 'failure') {
  770. alert (res_obj.reason);
  771. } else*/ if (res.status === 'okay') {
  772. location.href = url_freshrss;
  773. }
  774. },
  775. error: function(res, status, xhr) {
  776. alert("Login failure: " + res);
  777. }
  778. });
  779. },
  780. onlogout: function() {
  781. // A user has logged out! Here you need to:
  782. // Tear down the user's session by redirecting the user or making a call to your backend.
  783. // Also, make sure loggedInUser will get set to null on the next page load.
  784. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  785. $.ajax ({
  786. type: 'POST',
  787. url: url_logout,
  788. success: function(res, status, xhr) {
  789. location.href = url_freshrss;
  790. },
  791. error: function(res, status, xhr) {
  792. //alert("logout failure" + res);
  793. }
  794. });
  795. }
  796. });
  797. }
  798. //</persona>
  799. function init_confirm_action() {
  800. $('.confirm').click(function () {
  801. return confirm(str_confirmation);
  802. });
  803. }
  804. function init_print_action() {
  805. $('.print-article').click(function () {
  806. var content = "<html><head><style>"
  807. + "body { font-family: Serif; text-align: justify; }"
  808. + "a { color: #000; text-decoration: none; }"
  809. + "a:after { content: ' [' attr(href) ']'}"
  810. + "</style></head><body>"
  811. + $(".flux.current .content").html()
  812. + "</body></html>";
  813. var tmp_window = window.open();
  814. tmp_window.document.writeln(content);
  815. tmp_window.document.close();
  816. tmp_window.focus();
  817. tmp_window.print();
  818. tmp_window.close();
  819. return false;
  820. });
  821. }
  822. function init_all() {
  823. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  824. if (window.console) {
  825. console.log('FreshRSS waiting for JS…');
  826. }
  827. window.setTimeout(init_all, 50);
  828. return;
  829. }
  830. init_notifications();
  831. switch (authType) {
  832. case 'form':
  833. init_loginForm();
  834. break;
  835. case 'persona':
  836. init_persona();
  837. break;
  838. }
  839. init_confirm_action();
  840. $stream = $('#stream');
  841. if ($stream.length > 0) {
  842. init_actualize();
  843. init_column_categories();
  844. init_load_more($stream);
  845. init_posts();
  846. init_stream($stream);
  847. init_nav_entries();
  848. init_shortcuts();
  849. init_print_action();
  850. window.setInterval(refreshUnreads, 120000);
  851. }
  852. if (window.console) {
  853. console.log('FreshRSS init done.');
  854. }
  855. }
  856. if (document.readyState && document.readyState !== 'loading') {
  857. if (window.console) {
  858. console.log('FreshRSS immediate init…');
  859. }
  860. init_all();
  861. } else if (document.addEventListener) {
  862. document.addEventListener('DOMContentLoaded', function () {
  863. if (window.console) {
  864. console.log('FreshRSS waiting for DOMContentLoaded…');
  865. }
  866. init_all();
  867. }, false);
  868. }