main.js 24 KB

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