main.js 25 KB

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