main.js 40 KB

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