main.js 34 KB

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