main.js 31 KB

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