main.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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 > .flux_header > .title > 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. shortcut.add(shortcuts.focus_search, function () {
  534. focus_search();
  535. }, {
  536. 'disable_in_input': true
  537. });
  538. }
  539. function init_stream(divStream) {
  540. divStream.on('click', '.flux_header,.flux_content', function (e) { //flux_toggle
  541. if ($(e.target).closest('.content, .item.website, .item.link').length > 0) {
  542. return;
  543. }
  544. var old_active = $(".flux.current"),
  545. new_active = $(this).parent();
  546. isCollapsed = true;
  547. if (e.target.tagName.toUpperCase() === 'A') { //Leave real links alone
  548. if (auto_mark_article) {
  549. mark_read(new_active, true);
  550. }
  551. return true;
  552. }
  553. toggleContent(new_active, old_active);
  554. });
  555. divStream.on('click', '.flux a.read', function () {
  556. var active = $(this).parents(".flux");
  557. mark_read(active, false);
  558. return false;
  559. });
  560. divStream.on('click', '.flux a.bookmark', function () {
  561. var active = $(this).parents(".flux");
  562. mark_favorite(active);
  563. return false;
  564. });
  565. divStream.on('click', '.item.title > a', function (e) {
  566. if (e.ctrlKey) {
  567. return true; //Allow default control-click behaviour such as open in backround-tab
  568. }
  569. $(this).parent().click(); //Will perform toggle flux_content
  570. return false;
  571. });
  572. divStream.on('click', '.flux .content a', function () {
  573. $(this).attr('target', '_blank');
  574. });
  575. if (auto_mark_site) {
  576. divStream.on('click', '.flux .link > a', function () {
  577. mark_read($(this).parent().parent().parent(), true);
  578. });
  579. }
  580. }
  581. function init_nav_entries() {
  582. var $nav_entries = $('#nav_entries');
  583. $nav_entries.find('.previous_entry').click(function () {
  584. prev_entry();
  585. return false;
  586. });
  587. $nav_entries.find('.next_entry').click(function () {
  588. next_entry();
  589. return false;
  590. });
  591. $nav_entries.find('.up').click(function () {
  592. var active_item = $(".flux.current"),
  593. windowTop = $(window).scrollTop(),
  594. item_top = active_item.position().top;
  595. if (windowTop > item_top) {
  596. $("html,body").scrollTop(item_top);
  597. } else {
  598. $("html,body").scrollTop(0);
  599. }
  600. return false;
  601. });
  602. }
  603. function init_actualize() {
  604. var auto = false;
  605. $("#actualize").click(function () {
  606. $.getScript('./?c=javascript&a=actualize').done(function () {
  607. if (auto && feed_count < 1) {
  608. auto = false;
  609. return;
  610. }
  611. updateFeeds();
  612. });
  613. return false;
  614. });
  615. if (auto_actualize_feeds) {
  616. auto = true;
  617. $("#actualize").click();
  618. }
  619. }
  620. // <notification>
  621. var notification = null,
  622. notification_interval = null,
  623. notification_working = false;
  624. function openNotification(msg, status) {
  625. if (notification_working === true) {
  626. return false;
  627. }
  628. notification_working = true;
  629. notification.removeClass();
  630. notification.addClass("notification");
  631. notification.addClass(status);
  632. notification.find(".msg").html(msg);
  633. notification.fadeIn(300);
  634. notification_interval = window.setInterval(closeNotification, 4000);
  635. }
  636. function closeNotification() {
  637. notification.fadeOut(600, function() {
  638. notification.removeClass();
  639. notification.addClass('closed');
  640. window.clearInterval(notification_interval);
  641. notification_working = false;
  642. });
  643. }
  644. function init_notifications() {
  645. notification = $("#notification");
  646. notification.find("a.close").click(function () {
  647. closeNotification();
  648. return false;
  649. });
  650. if (notification.find(".msg").html().length > 0) {
  651. notification_working = true;
  652. notification_interval = window.setInterval(closeNotification, 4000);
  653. }
  654. }
  655. // </notification>
  656. function refreshUnreads() {
  657. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  658. var isAll = $('.category.all > .active').length > 0;
  659. $.each(data, function(feed_id, nbUnreads) {
  660. feed_id = 'f_' + feed_id;
  661. var elem = $('#' + feed_id + '>.feed').get(0),
  662. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  663. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  664. (nbUnreads - feed_unreads > 0)) {
  665. $('#new-article').show();
  666. };
  667. });
  668. });
  669. }
  670. //<endless_mode>
  671. var url_load_more = "",
  672. load_more = false,
  673. box_load_more = null;
  674. function load_more_posts() {
  675. if (load_more || url_load_more === '' || box_load_more === null) {
  676. return;
  677. }
  678. load_more = true;
  679. $('#load_more').addClass('loading');
  680. $.get(url_load_more, function (data) {
  681. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  682. $('.pagination').replaceWith($('.pagination', data));
  683. if (display_order === 'ASC') {
  684. $('#nav_menu_read_all>a').attr('href', $('#bigMarkAsRead').attr('href'));
  685. } else {
  686. $('#bigMarkAsRead').attr('href', $('#nav_menu_read_all>a').attr('href'));
  687. }
  688. $('[id^=day_]').each(function (i) {
  689. var ids = $('[id="' + this.id + '"]');
  690. if (ids.length > 1) {
  691. $('[id="' + this.id + '"]:gt(0)').remove();
  692. }
  693. });
  694. init_load_more(box_load_more);
  695. init_lazyload();
  696. $('#load_more').removeClass('loading');
  697. load_more = false;
  698. });
  699. }
  700. function focus_search() {
  701. $('#search').focus();
  702. }
  703. function init_load_more(box) {
  704. box_load_more = box;
  705. var $next_link = $("#load_more");
  706. if (!$next_link.length) {
  707. // no more article to load
  708. url_load_more = "";
  709. return;
  710. }
  711. url_load_more = $next_link.attr("href");
  712. var $prefetch = $('#prefetch');
  713. if ($prefetch.attr('href') !== url_load_more) {
  714. $prefetch.attr('rel', 'next'); //Remove prefetch
  715. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  716. $prefetch.attr('href', url_load_more);
  717. }
  718. $next_link.click(function () {
  719. load_more_posts();
  720. return false;
  721. });
  722. }
  723. //</endless_mode>
  724. //<Web login form>
  725. function poormanSalt() { //If crypto.getRandomValues is not available
  726. var text = '$2a$04$',
  727. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  728. for (var i = 22; i > 0; i--) {
  729. text += base.charAt(Math.floor(Math.random() * 64));
  730. }
  731. return text;
  732. }
  733. function init_loginForm() {
  734. var $loginForm = $('#loginForm');
  735. if ($loginForm.length === 0) {
  736. return;
  737. }
  738. if (!(window.dcodeIO)) {
  739. if (window.console) {
  740. console.log('FreshRSS waiting for bcrypt.js…');
  741. }
  742. window.setTimeout(init_loginForm, 100);
  743. return;
  744. }
  745. $loginForm.on('submit', function() {
  746. $('#loginButton').attr('disabled', '');
  747. var success = false;
  748. $.ajax({
  749. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  750. dataType: 'json',
  751. async: false
  752. }).done(function (data) {
  753. if (data.salt1 == '' || data.nonce == '') {
  754. alert('Invalid user!');
  755. } else {
  756. try {
  757. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  758. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  759. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? 4 : poormanSalt());
  760. $('#challenge').val(c);
  761. if (s == '' || c == '') {
  762. alert('Crypto error!');
  763. } else {
  764. success = true;
  765. }
  766. } catch (e) {
  767. alert('Crypto exception! ' + e);
  768. }
  769. }
  770. }).fail(function() {
  771. alert('Communication error!');
  772. });
  773. $('#loginButton').removeAttr('disabled');
  774. return success;
  775. });
  776. }
  777. //</Web login form>
  778. //<persona>
  779. function init_persona() {
  780. if (!(navigator.id)) {
  781. if (window.console) {
  782. console.log('FreshRSS waiting for Persona…');
  783. }
  784. window.setTimeout(init_persona, 100);
  785. return;
  786. }
  787. $('a.signin').click(function() {
  788. navigator.id.request();
  789. return false;
  790. });
  791. $('a.signout').click(function() {
  792. navigator.id.logout();
  793. return false;
  794. });
  795. navigator.id.watch({
  796. loggedInUser: current_user_mail,
  797. onlogin: function(assertion) {
  798. // A user has logged in! Here you need to:
  799. // 1. Send the assertion to your backend for verification and to create a session.
  800. // 2. Update your UI.
  801. $.ajax ({
  802. type: 'POST',
  803. url: url_login,
  804. data: {assertion: assertion},
  805. success: function(res, status, xhr) {
  806. /*if (res.status === 'failure') {
  807. alert (res_obj.reason);
  808. } else*/ if (res.status === 'okay') {
  809. location.href = url_freshrss;
  810. }
  811. },
  812. error: function(res, status, xhr) {
  813. alert("Login failure: " + res);
  814. }
  815. });
  816. },
  817. onlogout: function() {
  818. // A user has logged out! Here you need to:
  819. // Tear down the user's session by redirecting the user or making a call to your backend.
  820. // Also, make sure loggedInUser will get set to null on the next page load.
  821. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  822. $.ajax ({
  823. type: 'POST',
  824. url: url_logout,
  825. success: function(res, status, xhr) {
  826. location.href = url_freshrss;
  827. },
  828. error: function(res, status, xhr) {
  829. //alert("logout failure" + res);
  830. }
  831. });
  832. }
  833. });
  834. }
  835. //</persona>
  836. function init_confirm_action() {
  837. $('.confirm').click(function () {
  838. return confirm(str_confirmation);
  839. });
  840. }
  841. function init_print_action() {
  842. $('.item.share > a[href="#"]').click(function () {
  843. var content = "<html><head><style>"
  844. + "body { font-family: Serif; text-align: justify; }"
  845. + "a { color: #000; text-decoration: none; }"
  846. + "a:after { content: ' [' attr(href) ']'}"
  847. + "</style></head><body>"
  848. + $(".flux.current .content").html()
  849. + "</body></html>";
  850. var tmp_window = window.open();
  851. tmp_window.document.writeln(content);
  852. tmp_window.document.close();
  853. tmp_window.focus();
  854. tmp_window.print();
  855. tmp_window.close();
  856. return false;
  857. });
  858. }
  859. function init_share_observers() {
  860. shares = $('.form-group:not(".form-actions")').length;
  861. $('.post').on('click', '.share.remove', function(e) {
  862. e.preventDefault();
  863. $(this).parents('.form-group').remove();
  864. });
  865. $('.share.add').on('click', function(e) {
  866. var opt = $(this).siblings('select').find(':selected');
  867. var row = $(this).parents('form').data(opt.data('form'));
  868. row = row.replace('##label##', opt.html(), 'g');
  869. row = row.replace('##type##', opt.val(), 'g');
  870. row = row.replace('##help##', opt.data('help'), 'g');
  871. row = row.replace('##key##', shares, 'g');
  872. $(this).parents('.form-group').before(row);
  873. shares++;
  874. return false;
  875. });
  876. }
  877. function init_queries_observers() {
  878. $('.post').on('click', '.query.remove', function(e) {
  879. e.preventDefault();
  880. $(this).parents('.form-group').remove();
  881. });
  882. }
  883. function init_feed_observers() {
  884. $('select[id="category"]').on('change', function() {
  885. var detail = $('#new_category_name').parent();
  886. if ($(this).val() === 'nc') {
  887. detail.show();
  888. detail.find('input').focus();
  889. } else {
  890. detail.hide();
  891. }
  892. });
  893. }
  894. function init_password_observers() {
  895. $('input[type="password"] + a.btn.toggle-password').on('click', function(e) {
  896. var button = $(this);
  897. var passwordField = $(this).siblings('input[type="password"]');
  898. passwordField.attr('type', 'text');
  899. button.addClass('active');
  900. setTimeout(function() {
  901. passwordField.attr('type', 'password');
  902. button.removeClass('active');
  903. }, 2000);
  904. return false;
  905. });
  906. }
  907. function init_all() {
  908. if (!(window.$ && window.url_freshrss && ((!full_lazyload) || $.fn.lazyload))) {
  909. if (window.console) {
  910. console.log('FreshRSS waiting for JS…');
  911. }
  912. window.setTimeout(init_all, 50);
  913. return;
  914. }
  915. init_notifications();
  916. switch (authType) {
  917. case 'form':
  918. init_loginForm();
  919. break;
  920. case 'persona':
  921. init_persona();
  922. break;
  923. }
  924. init_confirm_action();
  925. $stream = $('#stream');
  926. if ($stream.length > 0) {
  927. init_actualize();
  928. init_column_categories();
  929. init_load_more($stream);
  930. init_posts();
  931. init_stream($stream);
  932. init_nav_entries();
  933. init_shortcuts();
  934. init_print_action();
  935. window.setInterval(refreshUnreads, 120000);
  936. } else {
  937. init_share_observers();
  938. init_queries_observers();
  939. init_feed_observers();
  940. init_password_observers();
  941. }
  942. if (window.console) {
  943. console.log('FreshRSS init done.');
  944. }
  945. }
  946. if (document.readyState && document.readyState !== 'loading') {
  947. if (window.console) {
  948. console.log('FreshRSS immediate init…');
  949. }
  950. init_all();
  951. } else if (document.addEventListener) {
  952. document.addEventListener('DOMContentLoaded', function () {
  953. if (window.console) {
  954. console.log('FreshRSS waiting for DOMContentLoaded…');
  955. }
  956. init_all();
  957. }, false);
  958. }