main.js 26 KB

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