main.js 34 KB

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