main.js 31 KB

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