main.js 27 KB

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