main.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. old_active.removeClass("active");
  181. if (new_active.length === 0) {
  182. return;
  183. }
  184. old_active.removeClass("current");
  185. if (does_lazyload) {
  186. new_active.find('img[data-original], iframe[data-original]').each(function () {
  187. this.setAttribute('src', this.getAttribute('data-original'));
  188. this.removeAttribute('data-original');
  189. });
  190. }
  191. if (old_active[0] !== new_active[0]) {
  192. if (isCollapsed) {
  193. new_active.addClass("active");
  194. }
  195. new_active.addClass("current");
  196. }
  197. var box_to_move = "html,body",
  198. relative_move = false;
  199. if (is_global_mode()) {
  200. box_to_move = "#panel";
  201. relative_move = true;
  202. }
  203. var new_pos = new_active.position().top,
  204. old_scroll = $(box_to_move).scrollTop();
  205. if (sticky_post) {
  206. if (hide_posts) {
  207. new_pos = new_active.position().top;
  208. old_scroll = $(box_to_move).scrollTop();
  209. if (relative_move) {
  210. new_pos += old_scroll;
  211. }
  212. if (old_active[0] !== new_active[0]) {
  213. new_active.children(".flux_content").first().each(function () {
  214. $(box_to_move).scrollTop(new_pos).scrollTop();
  215. });
  216. }
  217. } else {
  218. if (relative_move) {
  219. new_pos += old_scroll;
  220. }
  221. $(box_to_move).scrollTop(new_pos).scrollTop();
  222. }
  223. }
  224. if (auto_mark_article) {
  225. mark_read(new_active, true);
  226. }
  227. }
  228. function prev_entry() {
  229. var old_active = $(".flux.current"),
  230. new_active = old_active.length === 0 ? $(".flux:last") : old_active.prevAll(".flux:first");
  231. toggleContent(new_active, old_active);
  232. }
  233. function next_entry() {
  234. var old_active = $(".flux.current"),
  235. new_active = old_active.length === 0 ? $(".flux:first") : old_active.nextAll(".flux:first");
  236. toggleContent(new_active, old_active);
  237. if (!auto_load_more) {
  238. var last_active = $(".flux:last");
  239. if (last_active.attr("id") === new_active.attr("id")) {
  240. load_more_posts();
  241. }
  242. }
  243. }
  244. function prev_feed() {
  245. var active_feed = $("#aside_flux .feeds li.active");
  246. if (active_feed.length > 0) {
  247. active_feed.prev().find('a.feed').each(function(){this.click();});
  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. active_feed.next().find('a.feed').each(function(){this.click();});
  256. } else {
  257. first_feed();
  258. }
  259. }
  260. function first_feed() {
  261. var feed = $("#aside_flux .feeds.active li:first");
  262. if (feed.length > 0) {
  263. feed.find('a')[1].click();
  264. }
  265. }
  266. function last_feed() {
  267. var feed = $("#aside_flux .feeds.active li:last");
  268. if (feed.length > 0) {
  269. feed.find('a')[1].click();
  270. }
  271. }
  272. function prev_category() {
  273. var active_cat = $("#aside_flux .category.stick.active");
  274. if (active_cat.length > 0) {
  275. var prev_cat = active_cat.parent('li').prev().find('.category.stick a.btn');
  276. if (prev_cat.length > 0) {
  277. prev_cat[0].click();
  278. }
  279. } else {
  280. last_category();
  281. }
  282. return;
  283. }
  284. function next_category() {
  285. var active_cat = $("#aside_flux .category.stick.active");
  286. if (active_cat.length > 0) {
  287. var next_cat = active_cat.parent('li').next().find('.category.stick a.btn');
  288. if (next_cat.length > 0) {
  289. next_cat[0].click();
  290. }
  291. } else {
  292. first_category();
  293. }
  294. return;
  295. }
  296. function first_category() {
  297. var cat = $("#aside_flux .category.stick:first");
  298. if (cat.length > 0) {
  299. cat.find('a.btn')[0].click();
  300. }
  301. }
  302. function last_category() {
  303. var cat = $("#aside_flux .category.stick:last");
  304. if (cat.length > 0) {
  305. cat.find('a.btn')[0].click();
  306. }
  307. }
  308. function collapse_entry() {
  309. isCollapsed = !isCollapsed;
  310. $(".flux.current").toggleClass("active");
  311. }
  312. function auto_share(key) {
  313. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  314. var shares = share.siblings('.dropdown-menu').find('.item a');
  315. if (typeof key === "undefined") {
  316. if (!share.length) {
  317. return;
  318. }
  319. // Display the share div
  320. window.location.hash = share.attr('id');
  321. // Force scrolling to the share div
  322. var scroll = needsScroll(share.closest('.bottom'));
  323. if (scroll !== 0) {
  324. $('html,body').scrollTop(scroll);
  325. }
  326. // Force the key value if there is only one action, so we can trigger it automatically
  327. if (shares.length === 1) {
  328. key = 1;
  329. } else {
  330. return;
  331. }
  332. }
  333. // Trigger selected share action and hide the share div
  334. key = parseInt(key);
  335. if (key <= shares.length) {
  336. shares[key - 1].click();
  337. share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click();
  338. }
  339. }
  340. function inMarkViewport(flux, box_to_follow, relative_follow) {
  341. var top = flux.position().top;
  342. if (relative_follow) {
  343. top += box_to_follow.scrollTop();
  344. }
  345. var height = flux.height(),
  346. begin = top + 3 * height / 4,
  347. bot = Math.min(begin + 75, top + height),
  348. windowTop = box_to_follow.scrollTop(),
  349. windowBot = windowTop + box_to_follow.height() / 2;
  350. return (windowBot >= begin && bot >= windowBot);
  351. }
  352. function init_lazyload() {
  353. if ($.fn.lazyload) {
  354. if (is_global_mode()) {
  355. $(".flux_content img").lazyload({
  356. container: $("#panel")
  357. });
  358. } else {
  359. $(".flux_content img").lazyload();
  360. }
  361. }
  362. }
  363. function init_posts() {
  364. init_lazyload();
  365. var box_to_follow = $(window),
  366. relative_follow = false;
  367. if (is_global_mode()) {
  368. box_to_follow = $("#panel");
  369. relative_follow = true;
  370. }
  371. if (auto_mark_scroll) {
  372. box_to_follow.scroll(function () {
  373. $('.not_read:visible').each(function () {
  374. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow, relative_follow)) {
  375. mark_read($(this), true);
  376. }
  377. });
  378. });
  379. }
  380. if (auto_load_more) {
  381. box_to_follow.scroll(function () {
  382. var load_more = $("#load_more");
  383. if (!load_more.is(':visible')) {
  384. return;
  385. }
  386. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  387. load_more_top = load_more.position().top;
  388. if (relative_follow) {
  389. load_more_top += box_to_follow.scrollTop();
  390. }
  391. if (boxBot >= load_more_top) {
  392. load_more_posts();
  393. }
  394. });
  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 .link 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. }
  538. function init_stream(divStream) {
  539. divStream.on('click', '.flux_header', function (e) { //flux_header_toggle
  540. if ($(e.target).closest('.item.website, .item.link').length > 0) {
  541. return;
  542. }
  543. var old_active = $(".flux.current"),
  544. new_active = $(this).parent();
  545. isCollapsed = true;
  546. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  547. if (auto_mark_article) {
  548. mark_read(new_active, true);
  549. }
  550. return true;
  551. }
  552. toggleContent(new_active, old_active);
  553. });
  554. divStream.on('click', '.flux a.read', function () {
  555. var active = $(this).parents(".flux");
  556. mark_read(active, false);
  557. return false;
  558. });
  559. divStream.on('click', '.flux a.bookmark', function () {
  560. var active = $(this).parents(".flux");
  561. mark_favorite(active);
  562. return false;
  563. });
  564. divStream.on('click', '.item.title > a', function (e) {
  565. if (e.ctrlKey) {
  566. return true; //Allow default control-click behaviour such as open in backround-tab
  567. }
  568. $(this).parent().click(); //Will perform toggle flux_content
  569. return false;
  570. });
  571. divStream.on('click', '.flux .content a', function () {
  572. $(this).attr('target', '_blank');
  573. });
  574. if (auto_mark_site) {
  575. divStream.on('click', '.flux .link > a', function () {
  576. mark_read($(this).parent().parent().parent(), true);
  577. });
  578. }
  579. }
  580. function init_nav_entries() {
  581. var $nav_entries = $('#nav_entries');
  582. $nav_entries.find('.previous_entry').click(function () {
  583. prev_entry();
  584. return false;
  585. });
  586. $nav_entries.find('.next_entry').click(function () {
  587. next_entry();
  588. return false;
  589. });
  590. $nav_entries.find('.up').click(function () {
  591. var active_item = $(".flux.current"),
  592. windowTop = $(window).scrollTop(),
  593. item_top = active_item.position().top;
  594. if (windowTop > item_top) {
  595. $("html,body").scrollTop(item_top);
  596. } else {
  597. $("html,body").scrollTop(0);
  598. }
  599. return false;
  600. });
  601. }
  602. function init_actualize() {
  603. var auto = false;
  604. $("#actualize").click(function () {
  605. $.getScript('./?c=javascript&a=actualize').done(function () {
  606. if (auto && feed_count < 1) {
  607. auto = false;
  608. return;
  609. }
  610. updateFeeds();
  611. });
  612. return false;
  613. });
  614. if (auto_actualize_feeds) {
  615. auto = true;
  616. $("#actualize").click();
  617. }
  618. }
  619. // <notification>
  620. var notification = null,
  621. notification_interval = null,
  622. notification_working = false;
  623. function openNotification(msg, status) {
  624. if (notification_working === true) {
  625. return false;
  626. }
  627. notification_working = true;
  628. notification.removeClass();
  629. notification.addClass(status);
  630. notification.find(".msg").html(msg);
  631. notification.fadeIn(300);
  632. notification_interval = window.setInterval(closeNotification, 4000);
  633. }
  634. function closeNotification() {
  635. notification.fadeOut(600, function() {
  636. notification.removeClass();
  637. notification.addClass('closed');
  638. window.clearInterval(notification_interval);
  639. notification_working = false;
  640. });
  641. }
  642. function init_notifications() {
  643. notification = $("#notification");
  644. notification.find("a.close").click(function () {
  645. closeNotification();
  646. return false;
  647. });
  648. if (notification.find(".msg").html().length > 0) {
  649. notification_working = true;
  650. notification_interval = window.setInterval(closeNotification, 4000);
  651. }
  652. }
  653. // </notification>
  654. function refreshUnreads() {
  655. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  656. var isAll = $('.category.all > .active').length > 0;
  657. $.each(data, function(feed_id, nbUnreads) {
  658. feed_id = 'f_' + feed_id;
  659. var elem = $('#' + feed_id + '>.feed').get(0),
  660. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  661. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  662. (nbUnreads - feed_unreads > 0)) {
  663. $('#new-article').show();
  664. };
  665. });
  666. });
  667. }
  668. //<endless_mode>
  669. var url_load_more = "",
  670. load_more = false,
  671. box_load_more = null;
  672. function load_more_posts() {
  673. if (load_more || url_load_more === '' || box_load_more === null) {
  674. return;
  675. }
  676. load_more = true;
  677. $('#load_more').addClass('loading');
  678. $.get(url_load_more, function (data) {
  679. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  680. $('.pagination').replaceWith($('.pagination', data));
  681. if (display_order === 'ASC') {
  682. $('#nav_menu_read_all>a').attr('href', $('#bigMarkAsRead').attr('href'));
  683. } else {
  684. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  685. }
  686. $('[id^=day_]').each(function (i) {
  687. var ids = $('[id="' + this.id + '"]');
  688. if (ids.length > 1) {
  689. $('[id="' + this.id + '"]:gt(0)').remove();
  690. }
  691. });
  692. init_load_more(box_load_more);
  693. init_lazyload();
  694. $('#load_more').removeClass('loading');
  695. load_more = false;
  696. });
  697. }
  698. function init_load_more(box) {
  699. box_load_more = box;
  700. var $next_link = $("#load_more");
  701. if (!$next_link.length) {
  702. // no more article to load
  703. url_load_more = "";
  704. return;
  705. }
  706. url_load_more = $next_link.attr("href");
  707. var $prefetch = $('#prefetch');
  708. if ($prefetch.attr('href') !== url_load_more) {
  709. $prefetch.attr('rel', 'next'); //Remove prefetch
  710. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  711. $prefetch.attr('href', url_load_more);
  712. }
  713. $next_link.click(function () {
  714. load_more_posts();
  715. return false;
  716. });
  717. }
  718. //</endless_mode>
  719. //<Web login form>
  720. function poormanSalt() { //If crypto.getRandomValues is not available
  721. var text = '$2a$04$',
  722. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  723. for (var i = 22; i > 0; i--) {
  724. text += base.charAt(Math.floor(Math.random() * 64));
  725. }
  726. return text;
  727. }
  728. function init_loginForm() {
  729. var $loginForm = $('#loginForm');
  730. if ($loginForm.length === 0) {
  731. return;
  732. }
  733. if (!(window.dcodeIO)) {
  734. if (window.console) {
  735. console.log('FreshRSS waiting for bcrypt.js…');
  736. }
  737. window.setTimeout(init_loginForm, 100);
  738. return;
  739. }
  740. $loginForm.on('submit', function() {
  741. $('#loginButton').attr('disabled', '');
  742. var success = false;
  743. $.ajax({
  744. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  745. dataType: 'json',
  746. async: false
  747. }).done(function (data) {
  748. if (data.salt1 == '' || data.nonce == '') {
  749. alert('Invalid user!');
  750. } else {
  751. try {
  752. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  753. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  754. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  755. $('#challenge').val(c);
  756. if (s == '' || c == '') {
  757. alert('Crypto error!');
  758. } else {
  759. success = true;
  760. }
  761. } catch (e) {
  762. alert('Crypto exception! ' + e);
  763. }
  764. }
  765. }).fail(function() {
  766. alert('Communication error!');
  767. });
  768. $('#loginButton').removeAttr('disabled');
  769. return success;
  770. });
  771. }
  772. //</Web login form>
  773. //<persona>
  774. function init_persona() {
  775. if (!(navigator.id)) {
  776. if (window.console) {
  777. console.log('FreshRSS waiting for Persona…');
  778. }
  779. window.setTimeout(init_persona, 100);
  780. return;
  781. }
  782. $('a.signin').click(function() {
  783. navigator.id.request();
  784. return false;
  785. });
  786. $('a.signout').click(function() {
  787. navigator.id.logout();
  788. return false;
  789. });
  790. navigator.id.watch({
  791. loggedInUser: current_user_mail,
  792. onlogin: function(assertion) {
  793. // A user has logged in! Here you need to:
  794. // 1. Send the assertion to your backend for verification and to create a session.
  795. // 2. Update your UI.
  796. $.ajax ({
  797. type: 'POST',
  798. url: url_login,
  799. data: {assertion: assertion},
  800. success: function(res, status, xhr) {
  801. /*if (res.status === 'failure') {
  802. alert (res_obj.reason);
  803. } else*/ if (res.status === 'okay') {
  804. location.href = url_freshrss;
  805. }
  806. },
  807. error: function(res, status, xhr) {
  808. alert("Login failure: " + res);
  809. }
  810. });
  811. },
  812. onlogout: function() {
  813. // A user has logged out! Here you need to:
  814. // Tear down the user's session by redirecting the user or making a call to your backend.
  815. // Also, make sure loggedInUser will get set to null on the next page load.
  816. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  817. $.ajax ({
  818. type: 'POST',
  819. url: url_logout,
  820. success: function(res, status, xhr) {
  821. location.href = url_freshrss;
  822. },
  823. error: function(res, status, xhr) {
  824. //alert("logout failure" + res);
  825. }
  826. });
  827. }
  828. });
  829. }
  830. //</persona>
  831. function init_confirm_action() {
  832. $('.confirm').click(function () {
  833. return confirm(str_confirmation);
  834. });
  835. }
  836. function init_print_action() {
  837. $('.item.share > a[href="#"]').click(function () {
  838. var content = "<html><head><style>"
  839. + "body { font-family: Serif; text-align: justify; }"
  840. + "a { color: #000; text-decoration: none; }"
  841. + "a:after { content: ' [' attr(href) ']'}"
  842. + "</style></head><body>"
  843. + $(".flux.current .content").html()
  844. + "</body></html>";
  845. var tmp_window = window.open();
  846. tmp_window.document.writeln(content);
  847. tmp_window.document.close();
  848. tmp_window.focus();
  849. tmp_window.print();
  850. tmp_window.close();
  851. return false;
  852. });
  853. }
  854. function init_share_observers() {
  855. shares = $('.form-group:not(".form-actions")').length;
  856. $('.post').on('click', '.share.remove', function(e){
  857. e.preventDefault();
  858. $(this).parents('.form-group').remove();
  859. });
  860. $('.share.add').on('click',function(e){
  861. e.preventDefault();
  862. var opt = $(this).siblings('select').find(':selected');
  863. var row = $(this).parents('form').data(opt.data('form'));
  864. row = row.replace('##label##', opt.html(), 'g');
  865. row = row.replace('##type##', opt.val(), 'g');
  866. row = row.replace('##help##', opt.data('help'), 'g');
  867. row = row.replace('##key##', shares, 'g');
  868. $(this).parents('.form-actions').before(row);
  869. shares++;
  870. });
  871. };
  872. function init_all() {
  873. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  874. if (window.console) {
  875. console.log('FreshRSS waiting for JS…');
  876. }
  877. window.setTimeout(init_all, 50);
  878. return;
  879. }
  880. init_notifications();
  881. switch (authType) {
  882. case 'form':
  883. init_loginForm();
  884. break;
  885. case 'persona':
  886. init_persona();
  887. break;
  888. }
  889. init_confirm_action();
  890. $stream = $('#stream');
  891. if ($stream.length > 0) {
  892. init_actualize();
  893. init_column_categories();
  894. init_load_more($stream);
  895. init_posts();
  896. init_stream($stream);
  897. init_nav_entries();
  898. init_shortcuts();
  899. init_print_action();
  900. window.setInterval(refreshUnreads, 120000);
  901. } else {
  902. init_share_observers();
  903. }
  904. if (window.console) {
  905. console.log('FreshRSS init done.');
  906. }
  907. }
  908. if (document.readyState && document.readyState !== 'loading') {
  909. if (window.console) {
  910. console.log('FreshRSS immediate init…');
  911. }
  912. init_all();
  913. } else if (document.addEventListener) {
  914. document.addEventListener('DOMContentLoaded', function () {
  915. if (window.console) {
  916. console.log('FreshRSS waiting for DOMContentLoaded…');
  917. }
  918. init_all();
  919. }, false);
  920. }