main.js 27 KB

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