main.js 27 KB

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