main.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. $(".flux.current").toggleClass("active");
  313. }
  314. function auto_share(key) {
  315. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  316. var shares = share.siblings('.dropdown-menu').find('.item a');
  317. if (typeof key === "undefined") {
  318. if (!share.length) {
  319. return;
  320. }
  321. // Display the share div
  322. window.location.hash = share.attr('id');
  323. // Force scrolling to the share div
  324. var scroll = needsScroll(share.closest('.bottom'));
  325. if (scroll !== 0) {
  326. $('html,body').scrollTop(scroll);
  327. }
  328. // Force the key value if there is only one action, so we can trigger it automatically
  329. if (shares.length === 1) {
  330. key = 1;
  331. } else {
  332. return;
  333. }
  334. }
  335. // Trigger selected share action and hide the share div
  336. key = parseInt(key);
  337. if (key <= shares.length) {
  338. shares[key - 1].click();
  339. share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click();
  340. }
  341. }
  342. function inMarkViewport(flux, box_to_follow, relative_follow) {
  343. var top = flux.position().top;
  344. if (relative_follow) {
  345. top += box_to_follow.scrollTop();
  346. }
  347. var height = flux.height(),
  348. begin = top + 3 * height / 4,
  349. bot = Math.min(begin + 75, top + height),
  350. windowTop = box_to_follow.scrollTop(),
  351. windowBot = windowTop + box_to_follow.height() / 2;
  352. return (windowBot >= begin && bot >= windowBot);
  353. }
  354. function init_lazyload() {
  355. if ($.fn.lazyload) {
  356. if (is_global_mode()) {
  357. $(".flux_content img").lazyload({
  358. container: $("#panel")
  359. });
  360. } else {
  361. $(".flux_content img").lazyload();
  362. }
  363. }
  364. }
  365. function init_posts() {
  366. init_lazyload();
  367. var box_to_follow = $(window),
  368. relative_follow = false;
  369. if (is_global_mode()) {
  370. box_to_follow = $("#panel");
  371. relative_follow = true;
  372. }
  373. if (auto_mark_scroll) {
  374. box_to_follow.scroll(function () {
  375. $('.not_read:visible').each(function () {
  376. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow, relative_follow)) {
  377. mark_read($(this), true);
  378. }
  379. });
  380. });
  381. }
  382. if (auto_load_more) {
  383. box_to_follow.scroll(function () {
  384. var load_more = $("#load_more");
  385. if (!load_more.is(':visible')) {
  386. return;
  387. }
  388. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  389. load_more_top = load_more.position().top;
  390. if (relative_follow) {
  391. load_more_top += box_to_follow.scrollTop();
  392. }
  393. if (boxBot >= load_more_top) {
  394. load_more_posts();
  395. }
  396. });
  397. box_to_follow.scroll();
  398. }
  399. }
  400. function init_column_categories() {
  401. if (!is_normal_mode()) {
  402. return;
  403. }
  404. $('#aside_flux').on('click', '.category>a.dropdown-toggle', function () {
  405. $(this).children().each(function() {
  406. if (this.alt === '▽') {
  407. this.src = this.src.replace('/icons/down.', '/icons/up.');
  408. this.alt = '△';
  409. } else {
  410. this.src = this.src.replace('/icons/up.', '/icons/down.');
  411. this.alt = '▽';
  412. }
  413. });
  414. $(this).parent().next(".feeds").slideToggle();
  415. return false;
  416. });
  417. $('#aside_flux').on('click', '.feeds .dropdown-toggle', function () {
  418. if ($(this).nextAll('.dropdown-menu').length === 0) {
  419. var feed_id = $(this).closest('li').attr('id').substr(2),
  420. feed_web = $(this).data('fweb'),
  421. template = $('#feed_config_template').html().replace(/!!!!!!/g, feed_id).replace('http://example.net/', feed_web);
  422. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  423. }
  424. });
  425. }
  426. function init_shortcuts() {
  427. if (!(window.shortcut && window.shortcuts)) {
  428. if (window.console) {
  429. console.log('FreshRSS waiting for sortcut.js…');
  430. }
  431. window.setTimeout(init_shortcuts, 50);
  432. return;
  433. }
  434. // Touches de manipulation
  435. shortcut.add(shortcuts.mark_read, function () {
  436. // on marque comme lu ou non lu
  437. var active = $(".flux.current");
  438. mark_read(active, false);
  439. }, {
  440. 'disable_in_input': true
  441. });
  442. shortcut.add("shift+" + shortcuts.mark_read, function () {
  443. // on marque tout comme lu
  444. var url = $(".nav_menu a.read_all").attr("href");
  445. redirect(url, false);
  446. }, {
  447. 'disable_in_input': true
  448. });
  449. shortcut.add(shortcuts.mark_favorite, function () {
  450. // on marque comme favori ou non favori
  451. var active = $(".flux.current");
  452. mark_favorite(active);
  453. }, {
  454. 'disable_in_input': true
  455. });
  456. shortcut.add(shortcuts.collapse_entry, function () {
  457. collapse_entry();
  458. }, {
  459. 'disable_in_input': true
  460. });
  461. shortcut.add(shortcuts.auto_share, function () {
  462. auto_share();
  463. }, {
  464. 'disable_in_input': true
  465. });
  466. for(var i = 1; i < 10; i++){
  467. shortcut.add(i.toString(), function (e) {
  468. auto_share(String.fromCharCode(e.keyCode));
  469. }, {
  470. 'disable_in_input': true
  471. });
  472. }
  473. // Touches de navigation pour les articles
  474. shortcut.add(shortcuts.prev_entry, prev_entry, {
  475. 'disable_in_input': true
  476. });
  477. shortcut.add(shortcuts.first_entry, function () {
  478. var old_active = $(".flux.current"),
  479. first = $(".flux:first");
  480. if (first.hasClass("flux")) {
  481. toggleContent(first, old_active);
  482. }
  483. }, {
  484. 'disable_in_input': true
  485. });
  486. shortcut.add(shortcuts.next_entry, next_entry, {
  487. 'disable_in_input': true
  488. });
  489. shortcut.add(shortcuts.last_entry, function () {
  490. var old_active = $(".flux.current"),
  491. last = $(".flux:last");
  492. if (last.hasClass("flux")) {
  493. toggleContent(last, old_active);
  494. }
  495. }, {
  496. 'disable_in_input': true
  497. });
  498. // Touches de navigation pour les flux
  499. shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, {
  500. 'disable_in_input': true
  501. });
  502. shortcut.add("shift+" + shortcuts.next_entry, next_feed, {
  503. 'disable_in_input': true
  504. });
  505. shortcut.add("shift+" + shortcuts.first_entry, first_feed, {
  506. 'disable_in_input': true
  507. });
  508. shortcut.add("shift+" + shortcuts.last_entry, last_feed, {
  509. 'disable_in_input': true
  510. });
  511. // Touches de navigation pour les categories
  512. shortcut.add("alt+" + shortcuts.prev_entry, prev_category, {
  513. 'disable_in_input': true
  514. });
  515. shortcut.add("alt+" + shortcuts.next_entry, next_category, {
  516. 'disable_in_input': true
  517. });
  518. shortcut.add("alt+" + shortcuts.first_entry, first_category, {
  519. 'disable_in_input': true
  520. });
  521. shortcut.add("alt+" + shortcuts.last_entry, last_category, {
  522. 'disable_in_input': true
  523. });
  524. shortcut.add(shortcuts.go_website, function () {
  525. var url_website = $('.flux.current > .flux_header > .title > a').attr("href");
  526. if (auto_mark_site) {
  527. $(".flux.current").each(function () {
  528. mark_read($(this), true);
  529. });
  530. }
  531. redirect(url_website, true);
  532. }, {
  533. 'disable_in_input': true
  534. });
  535. shortcut.add(shortcuts.load_more, function () {
  536. load_more_posts();
  537. }, {
  538. 'disable_in_input': true
  539. });
  540. shortcut.add(shortcuts.focus_search, function () {
  541. focus_search();
  542. }, {
  543. 'disable_in_input': true
  544. });
  545. }
  546. function init_stream(divStream) {
  547. divStream.on('click', '.flux_header,.flux_content', function (e) { //flux_toggle
  548. if ($(e.target).closest('.content, .item.website, .item.link').length > 0) {
  549. return;
  550. }
  551. var old_active = $(".flux.current"),
  552. new_active = $(this).parent();
  553. isCollapsed = true;
  554. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  555. if (auto_mark_article) {
  556. mark_read(new_active, true);
  557. }
  558. return true;
  559. }
  560. toggleContent(new_active, old_active);
  561. });
  562. divStream.on('click', '.flux a.read', function () {
  563. var active = $(this).parents(".flux");
  564. mark_read(active, false);
  565. return false;
  566. });
  567. divStream.on('click', '.flux a.bookmark', function () {
  568. var active = $(this).parents(".flux");
  569. mark_favorite(active);
  570. return false;
  571. });
  572. divStream.on('click', '.item.title > a', function (e) {
  573. if (e.ctrlKey) {
  574. return true; //Allow default control-click behaviour such as open in backround-tab
  575. }
  576. $(this).parent().click(); //Will perform toggle flux_content
  577. return false;
  578. });
  579. divStream.on('click', '.flux .content a', function () {
  580. $(this).attr('target', '_blank');
  581. });
  582. if (auto_mark_site) {
  583. divStream.on('click', '.flux .link > a', function () {
  584. mark_read($(this).parent().parent().parent(), true);
  585. });
  586. }
  587. }
  588. function init_nav_entries() {
  589. var $nav_entries = $('#nav_entries');
  590. $nav_entries.find('.previous_entry').click(function () {
  591. prev_entry();
  592. return false;
  593. });
  594. $nav_entries.find('.next_entry').click(function () {
  595. next_entry();
  596. return false;
  597. });
  598. $nav_entries.find('.up').click(function () {
  599. var active_item = $(".flux.current"),
  600. windowTop = $(window).scrollTop(),
  601. item_top = active_item.position().top;
  602. if (windowTop > item_top) {
  603. $("html,body").scrollTop(item_top);
  604. } else {
  605. $("html,body").scrollTop(0);
  606. }
  607. return false;
  608. });
  609. }
  610. function init_actualize() {
  611. var auto = false;
  612. $("#actualize").click(function () {
  613. if (ajax_loading) {
  614. return false;
  615. }
  616. ajax_loading = true;
  617. $.getScript('./?c=javascript&a=actualize').done(function () {
  618. if (auto && feed_count < 1) {
  619. auto = false;
  620. ajax_loading = false;
  621. return false;
  622. }
  623. updateFeeds();
  624. });
  625. return false;
  626. });
  627. if (auto_actualize_feeds) {
  628. auto = true;
  629. $("#actualize").click();
  630. }
  631. }
  632. // <notification>
  633. var notification = null,
  634. notification_interval = null,
  635. notification_working = false;
  636. function openNotification(msg, status) {
  637. if (notification_working === true) {
  638. return false;
  639. }
  640. notification_working = true;
  641. notification.removeClass();
  642. notification.addClass("notification");
  643. notification.addClass(status);
  644. notification.find(".msg").html(msg);
  645. notification.fadeIn(300);
  646. notification_interval = window.setInterval(closeNotification, 4000);
  647. }
  648. function closeNotification() {
  649. notification.fadeOut(600, function() {
  650. notification.removeClass();
  651. notification.addClass('closed');
  652. window.clearInterval(notification_interval);
  653. notification_working = false;
  654. });
  655. }
  656. function init_notifications() {
  657. notification = $("#notification");
  658. notification.find("a.close").click(function () {
  659. closeNotification();
  660. return false;
  661. });
  662. if (notification.find(".msg").html().length > 0) {
  663. notification_working = true;
  664. notification_interval = window.setInterval(closeNotification, 4000);
  665. }
  666. }
  667. // </notification>
  668. function refreshUnreads() {
  669. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  670. var isAll = $('.category.all > .active').length > 0;
  671. $.each(data, function(feed_id, nbUnreads) {
  672. feed_id = 'f_' + feed_id;
  673. var elem = $('#' + feed_id + '>.feed').get(0),
  674. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  675. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  676. (nbUnreads - feed_unreads > 0)) {
  677. $('#new-article').show();
  678. };
  679. });
  680. });
  681. }
  682. //<endless_mode>
  683. var url_load_more = "",
  684. load_more = false,
  685. box_load_more = null;
  686. function load_more_posts() {
  687. if (load_more || url_load_more === '' || box_load_more === null) {
  688. return;
  689. }
  690. load_more = true;
  691. $('#load_more').addClass('loading');
  692. $.get(url_load_more, function (data) {
  693. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  694. $('.pagination').replaceWith($('.pagination', data));
  695. if (display_order === 'ASC') {
  696. $('#nav_menu_read_all>a').attr('href', $('#bigMarkAsRead').attr('href'));
  697. } else {
  698. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  699. }
  700. $('[id^=day_]').each(function (i) {
  701. var ids = $('[id="' + this.id + '"]');
  702. if (ids.length > 1) {
  703. $('[id="' + this.id + '"]:gt(0)').remove();
  704. }
  705. });
  706. init_load_more(box_load_more);
  707. init_lazyload();
  708. $('#load_more').removeClass('loading');
  709. load_more = false;
  710. });
  711. }
  712. function focus_search() {
  713. $('#search').focus();
  714. }
  715. function init_load_more(box) {
  716. box_load_more = box;
  717. var $next_link = $("#load_more");
  718. if (!$next_link.length) {
  719. // no more article to load
  720. url_load_more = "";
  721. return;
  722. }
  723. url_load_more = $next_link.attr("href");
  724. var $prefetch = $('#prefetch');
  725. if ($prefetch.attr('href') !== url_load_more) {
  726. $prefetch.attr('rel', 'next'); //Remove prefetch
  727. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  728. $prefetch.attr('href', url_load_more);
  729. }
  730. $next_link.click(function () {
  731. load_more_posts();
  732. return false;
  733. });
  734. }
  735. //</endless_mode>
  736. //<Web login form>
  737. function poormanSalt() { //If crypto.getRandomValues is not available
  738. var text = '$2a$04$',
  739. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  740. for (var i = 22; i > 0; i--) {
  741. text += base.charAt(Math.floor(Math.random() * 64));
  742. }
  743. return text;
  744. }
  745. function init_loginForm() {
  746. var $loginForm = $('#loginForm');
  747. if ($loginForm.length === 0) {
  748. return;
  749. }
  750. if (!(window.dcodeIO)) {
  751. if (window.console) {
  752. console.log('FreshRSS waiting for bcrypt.js…');
  753. }
  754. window.setTimeout(init_loginForm, 100);
  755. return;
  756. }
  757. $loginForm.on('submit', function() {
  758. $('#loginButton').attr('disabled', '');
  759. var success = false;
  760. $.ajax({
  761. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  762. dataType: 'json',
  763. async: false
  764. }).done(function (data) {
  765. if (data.salt1 == '' || data.nonce == '') {
  766. alert('Invalid user!');
  767. } else {
  768. try {
  769. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  770. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  771. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  772. $('#challenge').val(c);
  773. if (s == '' || c == '') {
  774. alert('Crypto error!');
  775. } else {
  776. success = true;
  777. }
  778. } catch (e) {
  779. alert('Crypto exception! ' + e);
  780. }
  781. }
  782. }).fail(function() {
  783. alert('Communication error!');
  784. });
  785. $('#loginButton').removeAttr('disabled');
  786. return success;
  787. });
  788. }
  789. //</Web login form>
  790. //<persona>
  791. function init_persona() {
  792. if (!(navigator.id)) {
  793. if (window.console) {
  794. console.log('FreshRSS waiting for Persona…');
  795. }
  796. window.setTimeout(init_persona, 100);
  797. return;
  798. }
  799. $('a.signin').click(function() {
  800. navigator.id.request();
  801. return false;
  802. });
  803. $('a.signout').click(function() {
  804. navigator.id.logout();
  805. return false;
  806. });
  807. navigator.id.watch({
  808. loggedInUser: current_user_mail,
  809. onlogin: function(assertion) {
  810. // A user has logged in! Here you need to:
  811. // 1. Send the assertion to your backend for verification and to create a session.
  812. // 2. Update your UI.
  813. $.ajax ({
  814. type: 'POST',
  815. url: url_login,
  816. data: {assertion: assertion},
  817. success: function(res, status, xhr) {
  818. /*if (res.status === 'failure') {
  819. alert (res_obj.reason);
  820. } else*/ if (res.status === 'okay') {
  821. location.href = url_freshrss;
  822. }
  823. },
  824. error: function(res, status, xhr) {
  825. alert("Login failure: " + res);
  826. }
  827. });
  828. },
  829. onlogout: function() {
  830. // A user has logged out! Here you need to:
  831. // Tear down the user's session by redirecting the user or making a call to your backend.
  832. // Also, make sure loggedInUser will get set to null on the next page load.
  833. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  834. $.ajax ({
  835. type: 'POST',
  836. url: url_logout,
  837. success: function(res, status, xhr) {
  838. location.href = url_freshrss;
  839. },
  840. error: function(res, status, xhr) {
  841. //alert("logout failure" + res);
  842. }
  843. });
  844. }
  845. });
  846. }
  847. //</persona>
  848. function init_confirm_action() {
  849. $('.confirm').click(function () {
  850. return confirm(str_confirmation);
  851. });
  852. }
  853. function init_print_action() {
  854. $('.item.share > a[href="#"]').click(function () {
  855. var content = "<html><head><style>"
  856. + "body { font-family: Serif; text-align: justify; }"
  857. + "a { color: #000; text-decoration: none; }"
  858. + "a:after { content: ' [' attr(href) ']'}"
  859. + "</style></head><body>"
  860. + $(".flux.current .content").html()
  861. + "</body></html>";
  862. var tmp_window = window.open();
  863. tmp_window.document.writeln(content);
  864. tmp_window.document.close();
  865. tmp_window.focus();
  866. tmp_window.print();
  867. tmp_window.close();
  868. return false;
  869. });
  870. }
  871. function init_share_observers() {
  872. shares = $('.form-group:not(".form-actions")').length;
  873. $('.share.add').on('click', function(e) {
  874. var opt = $(this).siblings('select').find(':selected');
  875. var row = $(this).parents('form').data(opt.data('form'));
  876. row = row.replace('##label##', opt.html(), 'g');
  877. row = row.replace('##type##', opt.val(), 'g');
  878. row = row.replace('##help##', opt.data('help'), 'g');
  879. row = row.replace('##key##', shares, 'g');
  880. $(this).parents('.form-group').before(row);
  881. shares++;
  882. return false;
  883. });
  884. }
  885. function init_remove_observers() {
  886. $('.post').on('click', 'a.remove', function(e) {
  887. var remove_what = $(this).attr('data-remove');
  888. if (remove_what !== undefined) {
  889. var remove_obj = $('#' + remove_what);
  890. remove_obj.remove();
  891. }
  892. return false;
  893. });
  894. }
  895. function init_feed_observers() {
  896. $('select[id="category"]').on('change', function() {
  897. var detail = $('#new_category_name').parent();
  898. if ($(this).val() === 'nc') {
  899. detail.show();
  900. detail.find('input').focus();
  901. } else {
  902. detail.hide();
  903. }
  904. });
  905. }
  906. function init_password_observers() {
  907. $('input[type="password"] + a.btn.toggle-password').on('click', function(e) {
  908. var button = $(this);
  909. var passwordField = $(this).siblings('input[type="password"]');
  910. passwordField.attr('type', 'text');
  911. button.addClass('active');
  912. setTimeout(function() {
  913. passwordField.attr('type', 'password');
  914. button.removeClass('active');
  915. }, 2000);
  916. return false;
  917. });
  918. }
  919. function init_all() {
  920. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  921. if (window.console) {
  922. console.log('FreshRSS waiting for JS…');
  923. }
  924. window.setTimeout(init_all, 50);
  925. return;
  926. }
  927. init_notifications();
  928. switch (authType) {
  929. case 'form':
  930. init_loginForm();
  931. break;
  932. case 'persona':
  933. init_persona();
  934. break;
  935. }
  936. init_confirm_action();
  937. $stream = $('#stream');
  938. if ($stream.length > 0) {
  939. init_actualize();
  940. init_column_categories();
  941. init_load_more($stream);
  942. init_posts();
  943. init_stream($stream);
  944. init_nav_entries();
  945. init_shortcuts();
  946. init_print_action();
  947. window.setInterval(refreshUnreads, 120000);
  948. } else {
  949. init_share_observers();
  950. init_remove_observers();
  951. init_feed_observers();
  952. init_password_observers();
  953. }
  954. if (window.console) {
  955. console.log('FreshRSS init done.');
  956. }
  957. }
  958. if (document.readyState && document.readyState !== 'loading') {
  959. if (window.console) {
  960. console.log('FreshRSS immediate init…');
  961. }
  962. init_all();
  963. } else if (document.addEventListener) {
  964. document.addEventListener('DOMContentLoaded', function () {
  965. if (window.console) {
  966. console.log('FreshRSS waiting for DOMContentLoaded…');
  967. }
  968. init_all();
  969. }, false);
  970. }