main.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. active_feed.prev().find('a.feed').each(function(){this.click();});
  245. } else {
  246. last_feed();
  247. }
  248. }
  249. function next_feed() {
  250. var active_feed = $("#aside_flux .feeds li.active");
  251. if (active_feed.length > 0) {
  252. active_feed.next().find('a.feed').each(function(){this.click();});
  253. } else {
  254. first_feed();
  255. }
  256. }
  257. function first_feed() {
  258. var feed = $("#aside_flux .feeds.active li:first");
  259. if (feed.length > 0) {
  260. feed.find('a')[1].click();
  261. }
  262. }
  263. function last_feed() {
  264. var feed = $("#aside_flux .feeds.active li:last");
  265. if (feed.length > 0) {
  266. feed.find('a')[1].click();
  267. }
  268. }
  269. function prev_category() {
  270. var active_cat = $("#aside_flux .category.stick.active");
  271. if (active_cat.length > 0) {
  272. var prev_cat = active_cat.parent('li').prev().find('.category.stick a.btn');
  273. if (prev_cat.length > 0) {
  274. prev_cat[0].click();
  275. }
  276. } else {
  277. last_category();
  278. }
  279. return;
  280. }
  281. function next_category() {
  282. var active_cat = $("#aside_flux .category.stick.active");
  283. if (active_cat.length > 0) {
  284. var next_cat = active_cat.parent('li').next().find('.category.stick a.btn');
  285. if (next_cat.length > 0) {
  286. next_cat[0].click();
  287. }
  288. } else {
  289. first_category();
  290. }
  291. return;
  292. }
  293. function first_category() {
  294. var cat = $("#aside_flux .category.stick:first");
  295. if (cat.length > 0) {
  296. cat.find('a.btn')[0].click();
  297. }
  298. }
  299. function last_category() {
  300. var cat = $("#aside_flux .category.stick:last");
  301. if (cat.length > 0) {
  302. cat.find('a.btn')[0].click();
  303. }
  304. }
  305. function collapse_entry() {
  306. isCollapsed = !isCollapsed;
  307. $(".flux.current").toggleClass("active");
  308. }
  309. function auto_share(key) {
  310. var share = $(".flux.current.active").find('.dropdown-target[id^="dropdown-share"]');
  311. var shares = share.siblings('.dropdown-menu').find('.item a');
  312. if (typeof key === "undefined") {
  313. if (!share.length) {
  314. return;
  315. }
  316. // Display the share div
  317. window.location.hash = share.attr('id');
  318. // Force scrolling to the share div
  319. var scroll = needsScroll(share.closest('.bottom'));
  320. if (scroll !== 0) {
  321. $('html,body').scrollTop(scroll);
  322. }
  323. // Force the key value if there is only one action, so we can trigger it automatically
  324. if (shares.length === 1) {
  325. key = 1;
  326. } else {
  327. return;
  328. }
  329. }
  330. // Trigger selected share action and hide the share div
  331. key = parseInt(key);
  332. if (key <= shares.length) {
  333. shares[key - 1].click();
  334. share.siblings('.dropdown-menu').find('.dropdown-close a')[0].click();
  335. }
  336. }
  337. function inMarkViewport(flux, box_to_follow, relative_follow) {
  338. var top = flux.position().top;
  339. if (relative_follow) {
  340. top += box_to_follow.scrollTop();
  341. }
  342. var height = flux.height(),
  343. begin = top + 3 * height / 4,
  344. bot = Math.min(begin + 75, top + height),
  345. windowTop = box_to_follow.scrollTop(),
  346. windowBot = windowTop + box_to_follow.height() / 2;
  347. return (windowBot >= begin && bot >= windowBot);
  348. }
  349. function init_lazyload() {
  350. if ($.fn.lazyload) {
  351. if (is_global_mode()) {
  352. $(".flux_content img").lazyload({
  353. container: $("#panel")
  354. });
  355. } else {
  356. $(".flux_content img").lazyload();
  357. }
  358. }
  359. }
  360. function init_posts() {
  361. init_lazyload();
  362. var box_to_follow = $(window),
  363. relative_follow = false;
  364. if (is_global_mode()) {
  365. box_to_follow = $("#panel");
  366. relative_follow = true;
  367. }
  368. if (auto_mark_scroll) {
  369. box_to_follow.scroll(function () {
  370. $('.not_read:visible').each(function () {
  371. if ($(this).children(".flux_content").is(':visible') && inMarkViewport($(this), box_to_follow, relative_follow)) {
  372. mark_read($(this), true);
  373. }
  374. });
  375. });
  376. }
  377. if (auto_load_more) {
  378. box_to_follow.scroll(function () {
  379. var load_more = $("#load_more");
  380. if (!load_more.is(':visible')) {
  381. return;
  382. }
  383. var boxBot = box_to_follow.scrollTop() + box_to_follow.height(),
  384. load_more_top = load_more.position().top;
  385. if (relative_follow) {
  386. load_more_top += box_to_follow.scrollTop();
  387. }
  388. if (boxBot >= load_more_top) {
  389. load_more_posts();
  390. }
  391. });
  392. }
  393. }
  394. function init_column_categories() {
  395. if (!is_normal_mode()) {
  396. return;
  397. }
  398. $('#aside_flux').on('click', '.category>a.dropdown-toggle', function () {
  399. $(this).children().each(function() {
  400. if (this.alt === '▽') {
  401. this.src = this.src.replace('/icons/down.', '/icons/up.');
  402. this.alt = '△';
  403. } else {
  404. this.src = this.src.replace('/icons/up.', '/icons/down.');
  405. this.alt = '▽';
  406. }
  407. });
  408. $(this).parent().next(".feeds").slideToggle();
  409. return false;
  410. });
  411. $('#aside_flux').on('click', '.feeds .dropdown-toggle', function () {
  412. if ($(this).nextAll('.dropdown-menu').length === 0) {
  413. var feed_id = $(this).closest('li').attr('id').substr(2),
  414. feed_web = $(this).data('fweb'),
  415. template = $('#feed_config_template').html().replace(/!!!!!!/g, feed_id).replace('http://example.net/', feed_web);
  416. $(this).attr('href', '#dropdown-' + feed_id).prev('.dropdown-target').attr('id', 'dropdown-' + feed_id).parent().append(template);
  417. }
  418. });
  419. }
  420. function init_shortcuts() {
  421. if (!(window.shortcut && window.shortcuts)) {
  422. if (window.console) {
  423. console.log('FreshRSS waiting for sortcut.js…');
  424. }
  425. window.setTimeout(init_shortcuts, 50);
  426. return;
  427. }
  428. // Touches de manipulation
  429. shortcut.add(shortcuts.mark_read, function () {
  430. // on marque comme lu ou non lu
  431. var active = $(".flux.current");
  432. mark_read(active, false);
  433. }, {
  434. 'disable_in_input': true
  435. });
  436. shortcut.add("shift+" + shortcuts.mark_read, function () {
  437. // on marque tout comme lu
  438. var url = $(".nav_menu a.read_all").attr("href");
  439. redirect(url, false);
  440. }, {
  441. 'disable_in_input': true
  442. });
  443. shortcut.add(shortcuts.mark_favorite, function () {
  444. // on marque comme favori ou non favori
  445. var active = $(".flux.current");
  446. mark_favorite(active);
  447. }, {
  448. 'disable_in_input': true
  449. });
  450. shortcut.add(shortcuts.collapse_entry, function () {
  451. collapse_entry();
  452. }, {
  453. 'disable_in_input': true
  454. });
  455. shortcut.add(shortcuts.auto_share, function () {
  456. auto_share();
  457. }, {
  458. 'disable_in_input': true
  459. });
  460. for(var i = 1; i < 10; i++){
  461. shortcut.add(i.toString(), function (e) {
  462. auto_share(String.fromCharCode(e.keyCode));
  463. }, {
  464. 'disable_in_input': true
  465. });
  466. }
  467. // Touches de navigation pour les articles
  468. shortcut.add(shortcuts.prev_entry, prev_entry, {
  469. 'disable_in_input': true
  470. });
  471. shortcut.add(shortcuts.first_entry, function () {
  472. var old_active = $(".flux.current"),
  473. first = $(".flux:first");
  474. if (first.hasClass("flux")) {
  475. toggleContent(first, old_active);
  476. }
  477. }, {
  478. 'disable_in_input': true
  479. });
  480. shortcut.add(shortcuts.next_entry, next_entry, {
  481. 'disable_in_input': true
  482. });
  483. shortcut.add(shortcuts.last_entry, function () {
  484. var old_active = $(".flux.current"),
  485. last = $(".flux:last");
  486. if (last.hasClass("flux")) {
  487. toggleContent(last, old_active);
  488. }
  489. }, {
  490. 'disable_in_input': true
  491. });
  492. // Touches de navigation pour les flux
  493. shortcut.add("shift+" + shortcuts.prev_entry, prev_feed, {
  494. 'disable_in_input': true
  495. });
  496. shortcut.add("shift+" + shortcuts.next_entry, next_feed, {
  497. 'disable_in_input': true
  498. });
  499. shortcut.add("shift+" + shortcuts.first_entry, first_feed, {
  500. 'disable_in_input': true
  501. });
  502. shortcut.add("shift+" + shortcuts.last_entry, last_feed, {
  503. 'disable_in_input': true
  504. });
  505. // Touches de navigation pour les categories
  506. shortcut.add("alt+" + shortcuts.prev_entry, prev_category, {
  507. 'disable_in_input': true
  508. });
  509. shortcut.add("alt+" + shortcuts.next_entry, next_category, {
  510. 'disable_in_input': true
  511. });
  512. shortcut.add("alt+" + shortcuts.first_entry, first_category, {
  513. 'disable_in_input': true
  514. });
  515. shortcut.add("alt+" + shortcuts.last_entry, last_category, {
  516. 'disable_in_input': true
  517. });
  518. shortcut.add(shortcuts.go_website, function () {
  519. var url_website = $(".flux.current .link a").attr("href");
  520. if (auto_mark_site) {
  521. $(".flux.current").each(function () {
  522. mark_read($(this), true);
  523. });
  524. }
  525. redirect(url_website, true);
  526. }, {
  527. 'disable_in_input': true
  528. });
  529. shortcut.add(shortcuts.load_more, function () {
  530. load_more_posts();
  531. }, {
  532. 'disable_in_input': true
  533. });
  534. }
  535. function init_stream(divStream) {
  536. divStream.on('click', '.flux_header', function (e) { //flux_header_toggle
  537. if ($(e.target).closest('.item.website, .item.link').length > 0) {
  538. return;
  539. }
  540. var old_active = $(".flux.current"),
  541. new_active = $(this).parent();
  542. isCollapsed = true;
  543. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  544. if (auto_mark_article) {
  545. mark_read(new_active, true);
  546. }
  547. return true;
  548. }
  549. toggleContent(new_active, old_active);
  550. });
  551. divStream.on('click', '.flux a.read', function () {
  552. var active = $(this).parents(".flux");
  553. mark_read(active, false);
  554. return false;
  555. });
  556. divStream.on('click', '.flux a.bookmark', function () {
  557. var active = $(this).parents(".flux");
  558. mark_favorite(active);
  559. return false;
  560. });
  561. divStream.on('click', '.item.title > a', function (e) {
  562. if (e.ctrlKey) {
  563. return true; //Allow default control-click behaviour such as open in backround-tab
  564. }
  565. $(this).parent().click(); //Will perform toggle flux_content
  566. return false;
  567. });
  568. divStream.on('click', '.flux .content a', function () {
  569. $(this).attr('target', '_blank');
  570. });
  571. if (auto_mark_site) {
  572. divStream.on('click', '.flux .link > a', function () {
  573. mark_read($(this).parent().parent().parent(), true);
  574. });
  575. }
  576. }
  577. function init_nav_entries() {
  578. var $nav_entries = $('#nav_entries');
  579. $nav_entries.find('.previous_entry').click(function () {
  580. prev_entry();
  581. return false;
  582. });
  583. $nav_entries.find('.next_entry').click(function () {
  584. next_entry();
  585. return false;
  586. });
  587. $nav_entries.find('.up').click(function () {
  588. var active_item = $(".flux.current"),
  589. windowTop = $(window).scrollTop(),
  590. item_top = active_item.position().top;
  591. if (windowTop > item_top) {
  592. $("html,body").scrollTop(item_top);
  593. } else {
  594. $("html,body").scrollTop(0);
  595. }
  596. return false;
  597. });
  598. }
  599. function init_actualize() {
  600. var auto = false;
  601. $("#actualize").click(function () {
  602. $.getScript('./?c=javascript&a=actualize').done(function () {
  603. if (auto && feed_count < 1) {
  604. auto = false;
  605. return;
  606. }
  607. updateFeeds();
  608. });
  609. return false;
  610. });
  611. if (auto_actualize_feeds) {
  612. auto = true;
  613. $("#actualize").click();
  614. }
  615. }
  616. // <notification>
  617. var notification = null,
  618. notification_interval = null,
  619. notification_working = false;
  620. function openNotification(msg, status) {
  621. if (notification_working === true) {
  622. return false;
  623. }
  624. notification_working = true;
  625. notification.removeClass();
  626. notification.addClass(status);
  627. notification.find(".msg").html(msg);
  628. notification.fadeIn(300);
  629. notification_interval = window.setInterval(closeNotification, 4000);
  630. }
  631. function closeNotification() {
  632. notification.fadeOut(600, function() {
  633. notification.removeClass();
  634. notification.addClass('closed');
  635. window.clearInterval(notification_interval);
  636. notification_working = false;
  637. });
  638. }
  639. function init_notifications() {
  640. notification = $("#notification");
  641. notification.find("a.close").click(function () {
  642. closeNotification();
  643. return false;
  644. });
  645. if (notification.find(".msg").html().length > 0) {
  646. notification_working = true;
  647. notification_interval = window.setInterval(closeNotification, 4000);
  648. }
  649. }
  650. // </notification>
  651. function refreshUnreads() {
  652. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  653. var isAll = $('.category.all > .active').length > 0;
  654. $.each(data, function(feed_id, nbUnreads) {
  655. feed_id = 'f_' + feed_id;
  656. var elem = $('#' + feed_id + '>.feed').get(0),
  657. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  658. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  659. (nbUnreads - feed_unreads > 0)) {
  660. $('#new-article').show();
  661. };
  662. });
  663. });
  664. }
  665. //<endless_mode>
  666. var url_load_more = "",
  667. load_more = false,
  668. box_load_more = null;
  669. function load_more_posts() {
  670. if (load_more || url_load_more === '' || box_load_more === null) {
  671. return;
  672. }
  673. load_more = true;
  674. $('#load_more').addClass('loading');
  675. $.get(url_load_more, function (data) {
  676. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  677. $('.pagination').replaceWith($('.pagination', data));
  678. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  679. $('[id^=day_]').each(function (i) {
  680. var ids = $('[id="' + this.id + '"]');
  681. if (ids.length > 1) {
  682. $('[id="' + this.id + '"]:gt(0)').remove();
  683. }
  684. });
  685. init_load_more(box_load_more);
  686. init_lazyload();
  687. $('#load_more').removeClass('loading');
  688. load_more = false;
  689. });
  690. }
  691. function init_load_more(box) {
  692. box_load_more = box;
  693. var $next_link = $("#load_more");
  694. if (!$next_link.length) {
  695. // no more article to load
  696. url_load_more = "";
  697. return;
  698. }
  699. url_load_more = $next_link.attr("href");
  700. var $prefetch = $('#prefetch');
  701. if ($prefetch.attr('href') !== url_load_more) {
  702. $prefetch.attr('rel', 'next'); //Remove prefetch
  703. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  704. $prefetch.attr('href', url_load_more);
  705. }
  706. $next_link.click(function () {
  707. load_more_posts();
  708. return false;
  709. });
  710. }
  711. //</endless_mode>
  712. //<Web login form>
  713. function poormanSalt() { //If crypto.getRandomValues is not available
  714. var text = '$2a$04$',
  715. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  716. for (var i = 22; i > 0; i--) {
  717. text += base.charAt(Math.floor(Math.random() * 64));
  718. }
  719. return text;
  720. }
  721. function init_loginForm() {
  722. var $loginForm = $('#loginForm');
  723. if ($loginForm.length === 0) {
  724. return;
  725. }
  726. if (!(window.dcodeIO)) {
  727. if (window.console) {
  728. console.log('FreshRSS waiting for bcrypt.js…');
  729. }
  730. window.setTimeout(init_loginForm, 100);
  731. return;
  732. }
  733. $loginForm.on('submit', function() {
  734. $('#loginButton').attr('disabled', '');
  735. var success = false;
  736. $.ajax({
  737. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  738. dataType: 'json',
  739. async: false
  740. }).done(function (data) {
  741. if (data.salt1 == '' || data.nonce == '') {
  742. alert('Invalid user!');
  743. } else {
  744. try {
  745. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  746. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  747. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  748. $('#challenge').val(c);
  749. if (s == '' || c == '') {
  750. alert('Crypto error!');
  751. } else {
  752. success = true;
  753. }
  754. } catch (e) {
  755. alert('Crypto exception! ' + e);
  756. }
  757. }
  758. }).fail(function() {
  759. alert('Communication error!');
  760. });
  761. $('#loginButton').removeAttr('disabled');
  762. return success;
  763. });
  764. }
  765. //</Web login form>
  766. //<persona>
  767. function init_persona() {
  768. if (!(navigator.id)) {
  769. if (window.console) {
  770. console.log('FreshRSS waiting for Persona…');
  771. }
  772. window.setTimeout(init_persona, 100);
  773. return;
  774. }
  775. $('a.signin').click(function() {
  776. navigator.id.request();
  777. return false;
  778. });
  779. $('a.signout').click(function() {
  780. navigator.id.logout();
  781. return false;
  782. });
  783. navigator.id.watch({
  784. loggedInUser: current_user_mail,
  785. onlogin: function(assertion) {
  786. // A user has logged in! Here you need to:
  787. // 1. Send the assertion to your backend for verification and to create a session.
  788. // 2. Update your UI.
  789. $.ajax ({
  790. type: 'POST',
  791. url: url_login,
  792. data: {assertion: assertion},
  793. success: function(res, status, xhr) {
  794. /*if (res.status === 'failure') {
  795. alert (res_obj.reason);
  796. } else*/ if (res.status === 'okay') {
  797. location.href = url_freshrss;
  798. }
  799. },
  800. error: function(res, status, xhr) {
  801. alert("Login failure: " + res);
  802. }
  803. });
  804. },
  805. onlogout: function() {
  806. // A user has logged out! Here you need to:
  807. // Tear down the user's session by redirecting the user or making a call to your backend.
  808. // Also, make sure loggedInUser will get set to null on the next page load.
  809. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  810. $.ajax ({
  811. type: 'POST',
  812. url: url_logout,
  813. success: function(res, status, xhr) {
  814. location.href = url_freshrss;
  815. },
  816. error: function(res, status, xhr) {
  817. //alert("logout failure" + res);
  818. }
  819. });
  820. }
  821. });
  822. }
  823. //</persona>
  824. function init_confirm_action() {
  825. $('.confirm').click(function () {
  826. return confirm(str_confirmation);
  827. });
  828. }
  829. function init_print_action() {
  830. $('.print-article').click(function () {
  831. var content = "<html><head><style>"
  832. + "body { font-family: Serif; text-align: justify; }"
  833. + "a { color: #000; text-decoration: none; }"
  834. + "a:after { content: ' [' attr(href) ']'}"
  835. + "</style></head><body>"
  836. + $(".flux.current .content").html()
  837. + "</body></html>";
  838. var tmp_window = window.open();
  839. tmp_window.document.writeln(content);
  840. tmp_window.document.close();
  841. tmp_window.focus();
  842. tmp_window.print();
  843. tmp_window.close();
  844. return false;
  845. });
  846. }
  847. function init_all() {
  848. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  849. if (window.console) {
  850. console.log('FreshRSS waiting for JS…');
  851. }
  852. window.setTimeout(init_all, 50);
  853. return;
  854. }
  855. init_notifications();
  856. switch (authType) {
  857. case 'form':
  858. init_loginForm();
  859. break;
  860. case 'persona':
  861. init_persona();
  862. break;
  863. }
  864. init_confirm_action();
  865. $stream = $('#stream');
  866. if ($stream.length > 0) {
  867. init_actualize();
  868. init_column_categories();
  869. init_load_more($stream);
  870. init_posts();
  871. init_stream($stream);
  872. init_nav_entries();
  873. init_shortcuts();
  874. init_print_action();
  875. window.setInterval(refreshUnreads, 120000);
  876. }
  877. if (window.console) {
  878. console.log('FreshRSS init done.');
  879. }
  880. }
  881. if (document.readyState && document.readyState !== 'loading') {
  882. if (window.console) {
  883. console.log('FreshRSS immediate init…');
  884. }
  885. init_all();
  886. } else if (document.addEventListener) {
  887. document.addEventListener('DOMContentLoaded', function () {
  888. if (window.console) {
  889. console.log('FreshRSS waiting for DOMContentLoaded…');
  890. }
  891. init_all();
  892. }, false);
  893. }