main.js 27 KB

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