main.js 34 KB

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