main.js 32 KB

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