main.js 32 KB

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