main.js 32 KB

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