main.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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. mark_read(active, false);
  617. return false;
  618. });
  619. divStream.on('click', '.flux a.bookmark', function () {
  620. var active = $(this).parents(".flux");
  621. mark_favorite(active);
  622. return false;
  623. });
  624. divStream.on('click', '.item.title > a', function (e) {
  625. // Allow default control-click behaviour such as open in backround-tab.
  626. return e.ctrlKey;
  627. });
  628. divStream.on('mouseup', '.item.title > a', function (e) {
  629. // Mouseup enables us to catch middle click.
  630. if (e.ctrlKey) {
  631. // CTRL+click, it will be manage by previous rule.
  632. return;
  633. }
  634. if (e.which == 2) {
  635. // If middle click, we want same behaviour as CTRL+click.
  636. var e = jQuery.Event("click");
  637. e.ctrlKey = true;
  638. $(this).trigger(e);
  639. } else if(e.which == 1) {
  640. // Normal click, just toggle article.
  641. $(this).parent().click();
  642. }
  643. });
  644. divStream.on('click', '.flux .content a', function () {
  645. $(this).attr('target', '_blank');
  646. });
  647. if (context['auto_mark_site']) {
  648. // catch mouseup instead of click so we can have the correct behaviour
  649. // with middle button click (scroll button).
  650. divStream.on('mouseup', '.flux .link > a', function (e) {
  651. if (e.which == 3) {
  652. return;
  653. }
  654. mark_read($(this).parents(".flux"), true);
  655. });
  656. }
  657. }
  658. function init_nav_entries() {
  659. var $nav_entries = $('#nav_entries');
  660. $nav_entries.find('.previous_entry').click(function () {
  661. prev_entry();
  662. return false;
  663. });
  664. $nav_entries.find('.next_entry').click(function () {
  665. next_entry();
  666. return false;
  667. });
  668. $nav_entries.find('.up').click(function () {
  669. var active_item = $(".flux.current"),
  670. windowTop = $(window).scrollTop(),
  671. item_top = active_item.position().top;
  672. if (windowTop > item_top) {
  673. $("html,body").scrollTop(item_top);
  674. } else {
  675. $("html,body").scrollTop(0);
  676. }
  677. return false;
  678. });
  679. }
  680. function init_actualize() {
  681. var auto = false;
  682. $("#actualize").click(function () {
  683. if (ajax_loading) {
  684. return false;
  685. }
  686. ajax_loading = true;
  687. $.getScript('./?c=javascript&a=actualize').done(function () {
  688. if (auto && feed_count < 1) {
  689. auto = false;
  690. ajax_loading = false;
  691. return false;
  692. }
  693. updateFeeds();
  694. });
  695. return false;
  696. });
  697. if (context['auto_actualize_feeds']) {
  698. auto = true;
  699. $("#actualize").click();
  700. }
  701. }
  702. // <notification>
  703. var notification = null,
  704. notification_interval = null,
  705. notification_working = false;
  706. function openNotification(msg, status) {
  707. if (notification_working === true) {
  708. return false;
  709. }
  710. notification_working = true;
  711. notification.removeClass();
  712. notification.addClass("notification");
  713. notification.addClass(status);
  714. notification.find(".msg").html(msg);
  715. notification.fadeIn(300);
  716. notification_interval = window.setTimeout(closeNotification, 4000);
  717. }
  718. function closeNotification() {
  719. notification.fadeOut(600, function() {
  720. notification.removeClass();
  721. notification.addClass('closed');
  722. window.clearInterval(notification_interval);
  723. notification_working = false;
  724. });
  725. }
  726. function init_notifications() {
  727. notification = $("#notification");
  728. notification.find("a.close").click(function () {
  729. closeNotification();
  730. return false;
  731. });
  732. if (notification.find(".msg").html().length > 0) {
  733. notification_working = true;
  734. notification_interval = window.setTimeout(closeNotification, 4000);
  735. }
  736. }
  737. // </notification>
  738. // <notifs html5>
  739. var notifs_html5_permission = 'denied';
  740. function notifs_html5_is_supported() {
  741. return window.Notification !== undefined;
  742. }
  743. function notifs_html5_ask_permission() {
  744. window.Notification.requestPermission(function () {
  745. notifs_html5_permission = window.Notification.permission;
  746. });
  747. }
  748. function notifs_html5_show(nb) {
  749. if (notifs_html5_permission !== "granted") {
  750. return
  751. }
  752. var notification = new window.Notification(i18n['notif_title_articles'], {
  753. icon: "../themes/icons/favicon-256.png",
  754. body: i18n['notif_body_articles'].replace("\d", nb),
  755. tag: "freshRssNewArticles"
  756. });
  757. notification.onclick = function() {
  758. window.location.reload();
  759. }
  760. if (context['html5_notif_timeout'] !== 0){
  761. setTimeout(function() {
  762. notification.close();
  763. }, context['html5_notif_timeout'] * 1000);
  764. }
  765. }
  766. function init_notifs_html5() {
  767. if (!notifs_html5_is_supported()) {
  768. return;
  769. }
  770. notifs_html5_permission = notifs_html5_ask_permission();
  771. }
  772. // </notifs html5>
  773. function refreshUnreads() {
  774. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  775. var isAll = $('.category.all.active').length > 0,
  776. new_articles = false;
  777. $.each(data, function(feed_id, nbUnreads) {
  778. feed_id = 'f_' + feed_id;
  779. var elem = $('#' + feed_id).get(0),
  780. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  781. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  782. (nbUnreads - feed_unreads > 0)) {
  783. $('#new-article').show();
  784. new_articles = true;
  785. };
  786. });
  787. var nb_unreads = str2int($('.category.all .title').attr('data-unread'));
  788. if (nb_unreads > 0 && new_articles) {
  789. faviconNbUnread(nb_unreads);
  790. notifs_html5_show(nb_unreads);
  791. }
  792. });
  793. }
  794. //<endless_mode>
  795. var url_load_more = "",
  796. load_more = false,
  797. box_load_more = null;
  798. function load_more_posts() {
  799. if (load_more || url_load_more === '' || box_load_more === null) {
  800. return;
  801. }
  802. load_more = true;
  803. $('#load_more').addClass('loading');
  804. $.get(url_load_more, function (data) {
  805. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  806. $('.pagination').replaceWith($('.pagination', data));
  807. if (context['display_order'] === 'ASC') {
  808. $('#nav_menu_read_all > .read_all').attr(
  809. 'formaction', $('#bigMarkAsRead').attr('formaction')
  810. );
  811. } else {
  812. $('#bigMarkAsRead').attr(
  813. 'formaction', $('#nav_menu_read_all > .read_all').attr('formaction')
  814. );
  815. }
  816. $('[id^=day_]').each(function (i) {
  817. var ids = $('[id="' + this.id + '"]');
  818. if (ids.length > 1) {
  819. $('[id="' + this.id + '"]:gt(0)').remove();
  820. }
  821. });
  822. init_load_more(box_load_more);
  823. $('#load_more').removeClass('loading');
  824. load_more = false;
  825. });
  826. }
  827. function focus_search() {
  828. $('#search').focus();
  829. }
  830. function init_load_more(box) {
  831. box_load_more = box;
  832. if (!context['does_lazyload']) {
  833. $('img[postpone], audio[postpone], iframe[postpone], video[postpone]').each(function () {
  834. this.removeAttribute('postpone');
  835. });
  836. }
  837. var $next_link = $("#load_more");
  838. if (!$next_link.length) {
  839. // no more article to load
  840. url_load_more = "";
  841. return;
  842. }
  843. url_load_more = $next_link.attr("href");
  844. var $prefetch = $('#prefetch');
  845. if ($prefetch.attr('href') !== url_load_more) {
  846. $prefetch.attr('rel', 'next'); //Remove prefetch
  847. $.ajax({url: url_load_more, ifModified: true }); //TODO: Try to find a less agressive solution
  848. $prefetch.attr('href', url_load_more);
  849. }
  850. $next_link.click(function () {
  851. load_more_posts();
  852. return false;
  853. });
  854. }
  855. //</endless_mode>
  856. //<crypto form (Web login)>
  857. function poormanSalt() { //If crypto.getRandomValues is not available
  858. var text = '$2a$04$',
  859. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  860. for (var i = 22; i > 0; i--) {
  861. text += base.charAt(Math.floor(Math.random() * 64));
  862. }
  863. return text;
  864. }
  865. function init_crypto_form() {
  866. var $crypto_form = $('#crypto-form');
  867. if ($crypto_form.length === 0) {
  868. return;
  869. }
  870. if (!(window.dcodeIO)) {
  871. if (window.console) {
  872. console.log('FreshRSS waiting for bcrypt.js…');
  873. }
  874. window.setTimeout(init_crypto_form, 100);
  875. return;
  876. }
  877. $crypto_form.on('submit', function() {
  878. var $submit_button = $(this).find('button[type="submit"]');
  879. $submit_button.attr('disabled', '');
  880. var success = false;
  881. $.ajax({
  882. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  883. dataType: 'json',
  884. async: false
  885. }).done(function (data) {
  886. if (data.salt1 == '' || data.nonce == '') {
  887. openNotification('Invalid user!', 'bad');
  888. } else {
  889. try {
  890. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  891. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  892. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  893. $('#challenge').val(c);
  894. if (s == '' || c == '') {
  895. openNotification('Crypto error!', 'bad');
  896. } else {
  897. success = true;
  898. }
  899. } catch (e) {
  900. openNotification('Crypto exception! ' + e, 'bad');
  901. }
  902. }
  903. }).fail(function() {
  904. openNotification('Communication error!', 'bad');
  905. });
  906. $submit_button.removeAttr('disabled');
  907. return success;
  908. });
  909. }
  910. //</crypto form (Web login)>
  911. function init_confirm_action() {
  912. $('body').on('click', '.confirm', function () {
  913. var str_confirmation = $(this).attr('data-str-confirm');
  914. if (!str_confirmation) {
  915. str_confirmation = i18n['confirmation_default'];
  916. }
  917. return confirm(str_confirmation);
  918. });
  919. }
  920. function init_print_action() {
  921. $('.item.share > a[href="#"]').click(function () {
  922. var content = "<html><head><style>"
  923. + "body { font-family: Serif; text-align: justify; }"
  924. + "a { color: #000; text-decoration: none; }"
  925. + "a:after { content: ' [' attr(href) ']'}"
  926. + "</style></head><body>"
  927. + $(".flux.current .content").html()
  928. + "</body></html>";
  929. var tmp_window = window.open();
  930. tmp_window.document.writeln(content);
  931. tmp_window.document.close();
  932. tmp_window.focus();
  933. tmp_window.print();
  934. tmp_window.close();
  935. return false;
  936. });
  937. }
  938. function init_share_observers() {
  939. shares = $('.form-group:not(".form-actions")').length;
  940. $('.share.add').on('click', function(e) {
  941. var opt = $(this).siblings('select').find(':selected');
  942. var row = $(this).parents('form').data(opt.data('form'));
  943. row = row.replace('##label##', opt.html(), 'g');
  944. row = row.replace('##type##', opt.val(), 'g');
  945. row = row.replace('##help##', opt.data('help'), 'g');
  946. row = row.replace('##key##', shares, 'g');
  947. $(this).parents('.form-group').before(row);
  948. shares++;
  949. return false;
  950. });
  951. }
  952. function init_stats_observers() {
  953. $('.select-change').on('change', function(e) {
  954. redirect($(this).find(':selected').data('url'));
  955. });
  956. }
  957. function init_remove_observers() {
  958. $('.post').on('click', 'a.remove', function(e) {
  959. var remove_what = $(this).attr('data-remove');
  960. if (remove_what !== undefined) {
  961. var remove_obj = $('#' + remove_what);
  962. remove_obj.remove();
  963. }
  964. return false;
  965. });
  966. }
  967. function init_feed_observers() {
  968. $('select[id="category"]').on('change', function() {
  969. var detail = $('#new_category_name').parent();
  970. if ($(this).val() === 'nc') {
  971. detail.show();
  972. detail.find('input').focus();
  973. } else {
  974. detail.hide();
  975. }
  976. });
  977. }
  978. function init_password_observers() {
  979. $('input[type="password"] + a.btn.toggle-password').on('click', function(e) {
  980. var button = $(this);
  981. var passwordField = $(this).siblings('input[type="password"]');
  982. passwordField.attr('type', 'text');
  983. button.addClass('active');
  984. setTimeout(function() {
  985. passwordField.attr('type', 'password');
  986. button.removeClass('active');
  987. }, 2000);
  988. return false;
  989. });
  990. }
  991. function faviconNbUnread(n) {
  992. if (typeof n === 'undefined') {
  993. n = str2int($('.category.all .title').attr('data-unread'));
  994. }
  995. //http://remysharp.com/2010/08/24/dynamic-favicons/
  996. var canvas = document.createElement('canvas'),
  997. link = document.getElementById('favicon').cloneNode(true);
  998. if (canvas.getContext && link) {
  999. canvas.height = canvas.width = 16;
  1000. var img = document.createElement('img');
  1001. img.onload = function () {
  1002. var ctx = canvas.getContext('2d');
  1003. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1004. if (n > 0) {
  1005. var text = '';
  1006. if (n < 1000) {
  1007. text = n;
  1008. } else if (n < 100000) {
  1009. text = Math.floor(n / 1000) + 'k';
  1010. } else {
  1011. text = 'E' + Math.floor(Math.log10(n));
  1012. }
  1013. ctx.font = 'bold 9px "Arial", sans-serif';
  1014. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1015. ctx.fillRect(0, 7, ctx.measureText(text).width, 9);
  1016. ctx.fillStyle = '#F00';
  1017. ctx.fillText(text, 0, canvas.height - 1);
  1018. }
  1019. link.href = canvas.toDataURL('image/png');
  1020. $('link[rel~=icon]').remove();
  1021. document.head.appendChild(link);
  1022. };
  1023. img.src = '../favicon.ico';
  1024. }
  1025. }
  1026. function init_slider_observers() {
  1027. var slider = $('#slider'),
  1028. closer = $('#close-slider');
  1029. if (slider.length < 1) {
  1030. return;
  1031. }
  1032. $('.post').on('click', '.open-slider', function() {
  1033. if (ajax_loading) {
  1034. return false;
  1035. }
  1036. ajax_loading = true;
  1037. var url_slide = $(this).attr('href');
  1038. $.ajax({
  1039. type: 'GET',
  1040. url: url_slide,
  1041. data : { ajax: true }
  1042. }).done(function (data) {
  1043. slider.html(data);
  1044. closer.addClass('active');
  1045. slider.addClass('active');
  1046. ajax_loading = false;
  1047. });
  1048. return false;
  1049. });
  1050. closer.on('click', function() {
  1051. closer.removeClass('active');
  1052. slider.removeClass('active');
  1053. return false;
  1054. });
  1055. }
  1056. function init_all() {
  1057. if (!(window.$ && window.context)) {
  1058. if (window.console) {
  1059. console.log('FreshRSS waiting for JS…');
  1060. }
  1061. window.setTimeout(init_all, 50);
  1062. return;
  1063. }
  1064. init_notifications();
  1065. init_confirm_action();
  1066. $stream = $('#stream');
  1067. if ($stream.length > 0) {
  1068. init_actualize();
  1069. init_column_categories();
  1070. init_load_more($stream);
  1071. init_posts();
  1072. init_stream($stream);
  1073. init_nav_entries();
  1074. init_shortcuts();
  1075. faviconNbUnread();
  1076. init_print_action();
  1077. init_notifs_html5();
  1078. window.setInterval(refreshUnreads, 120000);
  1079. } else {
  1080. init_crypto_form();
  1081. init_share_observers();
  1082. init_remove_observers();
  1083. init_feed_observers();
  1084. init_password_observers();
  1085. init_stats_observers();
  1086. init_slider_observers();
  1087. }
  1088. if (window.console) {
  1089. console.log('FreshRSS init done.');
  1090. }
  1091. }
  1092. if (document.readyState && document.readyState !== 'loading') {
  1093. if (window.console) {
  1094. console.log('FreshRSS immediate init…');
  1095. }
  1096. init_all();
  1097. } else if (document.addEventListener) {
  1098. document.addEventListener('DOMContentLoaded', function () {
  1099. if (window.console) {
  1100. console.log('FreshRSS waiting for DOMContentLoaded…');
  1101. }
  1102. init_all();
  1103. }, false);
  1104. }