main.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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('.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: 1,
  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. $.ajax({ //Empty request to commit new articles
  842. type: 'POST',
  843. url: './?c=feed&a=actualize&id=-1&ajax=1',
  844. data: {
  845. _csrf: context.csrf,
  846. noCommit: 0,
  847. },
  848. }).always(function (data) {
  849. window.location.reload();
  850. });
  851. } else {
  852. updateFeed(feeds, feeds_count);
  853. }
  854. });
  855. }
  856. function init_actualize() {
  857. var auto = false;
  858. $("#actualize").click(function () {
  859. if (ajax_loading) {
  860. return false;
  861. }
  862. ajax_loading = true;
  863. $.getJSON('./?c=javascript&a=actualize').done(function (data) {
  864. if (auto && data.feeds.length < 1) {
  865. auto = false;
  866. ajax_loading = false;
  867. return false;
  868. }
  869. if (data.feeds.length === 0) {
  870. openNotification(data.feedback_no_refresh, "good");
  871. $.ajax({ //Empty request to force refresh server database cache
  872. type: 'POST',
  873. url: './?c=feed&a=actualize&id=-1&ajax=1',
  874. data: {
  875. _csrf: context.csrf,
  876. noCommit: 0,
  877. },
  878. }).always(function (data) {
  879. ajax_loading = false;
  880. });
  881. return;
  882. }
  883. //Progress bar
  884. var feeds_count = data.feeds.length;
  885. $('body').after('<div id="actualizeProgress" class="notification good">' + data.feedback_actualize +
  886. '<br /><span class="title">/</span><br /><span class="progress">0 / ' + feeds_count +
  887. '</span></div>');
  888. for (var i = 10; i > 0; i--) {
  889. updateFeed(data.feeds, feeds_count);
  890. }
  891. });
  892. return false;
  893. });
  894. if (context.auto_actualize_feeds) {
  895. auto = true;
  896. $("#actualize").click();
  897. }
  898. }
  899. // </actualize>
  900. // <notification>
  901. var notification = null,
  902. notification_interval = null,
  903. notification_working = false;
  904. function openNotification(msg, status) {
  905. if (notification_working === true) {
  906. return false;
  907. }
  908. notification_working = true;
  909. notification.removeClass();
  910. notification.addClass("notification");
  911. notification.addClass(status);
  912. notification.find(".msg").html(msg);
  913. notification.fadeIn(300);
  914. notification_interval = window.setTimeout(closeNotification, 4000);
  915. }
  916. function closeNotification() {
  917. notification.fadeOut(600, function() {
  918. notification.removeClass();
  919. notification.addClass('closed');
  920. window.clearInterval(notification_interval);
  921. notification_working = false;
  922. });
  923. }
  924. function init_notifications() {
  925. notification = $("#notification");
  926. notification.find("a.close").click(function () {
  927. closeNotification();
  928. return false;
  929. });
  930. if (notification.find(".msg").html().length > 0) {
  931. notification_working = true;
  932. notification_interval = window.setTimeout(closeNotification, 4000);
  933. }
  934. }
  935. // </notification>
  936. // <notifs html5>
  937. var notifs_html5_permission = 'denied';
  938. function notifs_html5_is_supported() {
  939. return window.Notification !== undefined;
  940. }
  941. function notifs_html5_ask_permission() {
  942. window.Notification.requestPermission(function () {
  943. notifs_html5_permission = window.Notification.permission;
  944. });
  945. }
  946. function notifs_html5_show(nb) {
  947. if (notifs_html5_permission !== "granted") {
  948. return;
  949. }
  950. var notification = new window.Notification(i18n.notif_title_articles, {
  951. icon: "../themes/icons/favicon-256.png",
  952. body: i18n.notif_body_articles.replace('%d', nb),
  953. tag: "freshRssNewArticles"
  954. });
  955. notification.onclick = function() {
  956. window.location.reload();
  957. window.focus();
  958. notification.close();
  959. };
  960. if (context.html5_notif_timeout !== 0) {
  961. setTimeout(function() {
  962. notification.close();
  963. }, context.html5_notif_timeout * 1000);
  964. }
  965. }
  966. function init_notifs_html5() {
  967. if (!notifs_html5_is_supported()) {
  968. return;
  969. }
  970. notifs_html5_permission = notifs_html5_ask_permission();
  971. }
  972. // </notifs html5>
  973. function refreshUnreads() {
  974. $.getJSON('./?c=javascript&a=nbUnreadsPerFeed').done(function (data) {
  975. var isAll = $('.category.all.active').length > 0,
  976. new_articles = false;
  977. $.each(data.feeds, function(feed_id, nbUnreads) {
  978. feed_id = 'f_' + feed_id;
  979. var elem = $('#' + feed_id).get(0),
  980. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  981. if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
  982. (nbUnreads - feed_unreads > 0)) {
  983. $('#new-article').attr('aria-hidden', 'false').show();
  984. new_articles = true;
  985. }
  986. });
  987. var nbUnreadTags = 0;
  988. $.each(data.tags, function(tag_id, nbUnreads) {
  989. nbUnreadTags += nbUnreads;
  990. $('#t_' + tag_id).attr('data-unread', nbUnreads)
  991. .children('.item-title').attr('data-unread', numberFormat(nbUnreads));
  992. });
  993. $('.category.tags').attr('data-unread', nbUnreadTags)
  994. .find('.title').attr('data-unread', numberFormat(nbUnreadTags));
  995. var nb_unreads = str2int($('.category.all .title').attr('data-unread'));
  996. if (nb_unreads > 0 && new_articles) {
  997. faviconNbUnread(nb_unreads);
  998. notifs_html5_show(nb_unreads);
  999. }
  1000. });
  1001. }
  1002. //<endless_mode>
  1003. var url_load_more = "",
  1004. load_more = false,
  1005. box_load_more = null;
  1006. function load_more_posts() {
  1007. if (load_more || url_load_more === '' || box_load_more === null) {
  1008. return;
  1009. }
  1010. load_more = true;
  1011. $('#load_more').addClass('loading');
  1012. $.get(url_load_more, function (data) {
  1013. box_load_more.children('.flux:last').after($('#stream', data).children('.flux, .day'));
  1014. $('.pagination').replaceWith($('.pagination', data));
  1015. if (context.display_order === 'ASC') {
  1016. $('#nav_menu_read_all .read_all').attr(
  1017. 'formaction', $('#bigMarkAsRead').attr('formaction')
  1018. );
  1019. } else {
  1020. $('#bigMarkAsRead').attr(
  1021. 'formaction', $('#nav_menu_read_all .read_all').attr('formaction')
  1022. );
  1023. }
  1024. $('[id^=day_]').each(function (i) {
  1025. var ids = $('[id="' + this.id + '"]');
  1026. if (ids.length > 1) {
  1027. $('[id="' + this.id + '"]:gt(0)').remove();
  1028. }
  1029. });
  1030. init_load_more(box_load_more);
  1031. $('#load_more').removeClass('loading');
  1032. $('#bigMarkAsRead').removeAttr('disabled');
  1033. load_more = false;
  1034. $(document.body).trigger('sticky_kit:recalc');
  1035. });
  1036. }
  1037. function focus_search() {
  1038. $('#search').focus();
  1039. }
  1040. var freshrssLoadMoreEvent = document.createEvent('Event');
  1041. freshrssLoadMoreEvent.initEvent('freshrss:load-more', true, true);
  1042. function init_load_more(box) {
  1043. box_load_more = box;
  1044. document.body.dispatchEvent(freshrssLoadMoreEvent);
  1045. var $next_link = $("#load_more");
  1046. if (!$next_link.length) {
  1047. // no more article to load
  1048. url_load_more = "";
  1049. return;
  1050. }
  1051. url_load_more = $next_link.attr("href");
  1052. $next_link.click(function () {
  1053. load_more_posts();
  1054. return false;
  1055. });
  1056. }
  1057. //</endless_mode>
  1058. //<crypto form (Web login)>
  1059. function poormanSalt() { //If crypto.getRandomValues is not available
  1060. var text = '$2a$04$',
  1061. base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/abcdefghijklmnopqrstuvwxyz';
  1062. for (var i = 22; i > 0; i--) {
  1063. text += base.charAt(Math.floor(Math.random() * 64));
  1064. }
  1065. return text;
  1066. }
  1067. function init_crypto_form() {
  1068. /* globals dcodeIO */
  1069. var $crypto_form = $('#crypto-form');
  1070. if ($crypto_form.length === 0) {
  1071. return;
  1072. }
  1073. if (!(window.dcodeIO)) {
  1074. if (window.console) {
  1075. console.log('FreshRSS waiting for bcrypt.js…');
  1076. }
  1077. window.setTimeout(init_crypto_form, 100);
  1078. return;
  1079. }
  1080. $crypto_form.on('submit', function() {
  1081. var $submit_button = $(this).find('button[type="submit"]');
  1082. $submit_button.attr('disabled', '');
  1083. var success = false;
  1084. $.ajax({
  1085. url: './?c=javascript&a=nonce&user=' + $('#username').val(),
  1086. dataType: 'json',
  1087. async: false
  1088. }).done(function (data) {
  1089. if (!data.salt1 || !data.nonce) {
  1090. openNotification('Invalid user!', 'bad');
  1091. } else {
  1092. try {
  1093. var strong = window.Uint32Array && window.crypto && (typeof window.crypto.getRandomValues === 'function'),
  1094. s = dcodeIO.bcrypt.hashSync($('#passwordPlain').val(), data.salt1),
  1095. c = dcodeIO.bcrypt.hashSync(data.nonce + s, strong ? dcodeIO.bcrypt.genSaltSync(4) : poormanSalt());
  1096. $('#challenge').val(c);
  1097. if (!s || !c) {
  1098. openNotification('Crypto error!', 'bad');
  1099. } else {
  1100. success = true;
  1101. }
  1102. } catch (e) {
  1103. openNotification('Crypto exception! ' + e, 'bad');
  1104. }
  1105. }
  1106. }).fail(function() {
  1107. openNotification('Communication error!', 'bad');
  1108. });
  1109. $submit_button.removeAttr('disabled');
  1110. return success;
  1111. });
  1112. }
  1113. //</crypto form (Web login)>
  1114. function init_confirm_action() {
  1115. $('body').on('click', '.confirm', function () {
  1116. var str_confirmation = $(this).attr('data-str-confirm');
  1117. if (!str_confirmation) {
  1118. str_confirmation = i18n.confirmation_default;
  1119. }
  1120. return confirm(str_confirmation);
  1121. });
  1122. $('button.confirm').removeAttr('disabled');
  1123. }
  1124. function init_print_action() {
  1125. $('.item.share > a[href="#"]').click(function () {
  1126. var content = "<html><head><style>" +
  1127. "body { font-family: Serif; text-align: justify; }" +
  1128. "a { color: #000; text-decoration: none; }" +
  1129. "a:after { content: ' [' attr(href) ']'}" +
  1130. "</style></head><body>" +
  1131. $(".flux.current .content").html() +
  1132. "</body></html>";
  1133. var tmp_window = window.open();
  1134. tmp_window.document.writeln(content);
  1135. tmp_window.document.close();
  1136. tmp_window.focus();
  1137. tmp_window.print();
  1138. tmp_window.close();
  1139. return false;
  1140. });
  1141. }
  1142. function init_post_action() {
  1143. $('.item.share > a[href="POST"]').click(function (event) {
  1144. event.preventDefault();
  1145. var form = $(this).next('form');
  1146. $.post(form.data('url'), form.serialize());
  1147. });
  1148. }
  1149. function init_share_observers() {
  1150. shares = $('.group-share').length;
  1151. $('.share.add').on('click', function(e) {
  1152. var opt = $(this).siblings('select').find(':selected');
  1153. var row = $(this).parents('form').data(opt.data('form'));
  1154. row = row.replace(/##label##/g, opt.html().trim());
  1155. row = row.replace(/##type##/g, opt.val());
  1156. row = row.replace(/##help##/g, opt.data('help'));
  1157. row = row.replace(/##key##/g, shares);
  1158. row = row.replace(/##method##/g, opt.data('method'));
  1159. row = row.replace(/##field##/g, opt.data('field'));
  1160. $(this).parents('.form-group').before(row);
  1161. shares++;
  1162. return false;
  1163. });
  1164. }
  1165. function init_stats_observers() {
  1166. $('.select-change').on('change', function(e) {
  1167. redirect($(this).find(':selected').data('url'));
  1168. });
  1169. }
  1170. function init_remove_observers() {
  1171. $('.post').on('click', 'a.remove', function(e) {
  1172. var remove_what = $(this).attr('data-remove');
  1173. if (remove_what !== undefined) {
  1174. var remove_obj = $('#' + remove_what);
  1175. remove_obj.remove();
  1176. }
  1177. return false;
  1178. });
  1179. }
  1180. function init_feed_observers() {
  1181. $('select[id="category"]').on('change', function() {
  1182. var detail = $('#new_category_name').parent();
  1183. if ($(this).val() === 'nc') {
  1184. detail.attr('aria-hidden', 'false').show();
  1185. detail.find('input').focus();
  1186. } else {
  1187. detail.attr('aria-hidden', 'true').hide();
  1188. }
  1189. });
  1190. }
  1191. function init_password_observers() {
  1192. $('.toggle-password').on('mousedown', function(e) {
  1193. var button = $(this);
  1194. var passwordField = $('#' + button.attr('data-toggle'));
  1195. passwordField.attr('type', 'text');
  1196. button.addClass('active');
  1197. return false;
  1198. }).on('mouseup', function(e) {
  1199. var button = $(this);
  1200. var passwordField = $('#' + button.attr('data-toggle'));
  1201. passwordField.attr('type', 'password');
  1202. button.removeClass('active');
  1203. return false;
  1204. });
  1205. }
  1206. function faviconNbUnread(n) {
  1207. if (typeof n === 'undefined') {
  1208. n = str2int($('.category.all .title').attr('data-unread'));
  1209. }
  1210. //http://remysharp.com/2010/08/24/dynamic-favicons/
  1211. var canvas = document.createElement('canvas'),
  1212. link = document.getElementById('favicon').cloneNode(true);
  1213. if (canvas.getContext && link) {
  1214. canvas.height = canvas.width = 16;
  1215. var img = document.createElement('img');
  1216. img.onload = function () {
  1217. var ctx = canvas.getContext('2d');
  1218. ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
  1219. if (n > 0) {
  1220. var text = '';
  1221. if (n < 1000) {
  1222. text = n;
  1223. } else if (n < 100000) {
  1224. text = Math.floor(n / 1000) + 'k';
  1225. } else {
  1226. text = 'E' + Math.floor(Math.log10(n));
  1227. }
  1228. ctx.font = 'bold 9px "Arial", sans-serif';
  1229. ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
  1230. ctx.fillRect(0, 7, ctx.measureText(text).width, 9);
  1231. ctx.fillStyle = '#F00';
  1232. ctx.fillText(text, 0, canvas.height - 1);
  1233. }
  1234. link.href = canvas.toDataURL('image/png');
  1235. $('link[rel~=icon]').remove();
  1236. document.head.appendChild(link);
  1237. };
  1238. img.src = '../favicon.ico';
  1239. }
  1240. }
  1241. function init_slider_observers() {
  1242. var slider = $('#slider'),
  1243. closer = $('#close-slider');
  1244. if (slider.length < 1) {
  1245. return;
  1246. }
  1247. $('.post').on('click', '.open-slider', function() {
  1248. if (ajax_loading) {
  1249. return false;
  1250. }
  1251. ajax_loading = true;
  1252. var url_slide = $(this).attr('href');
  1253. $.ajax({
  1254. type: 'GET',
  1255. url: url_slide,
  1256. data: { ajax: true }
  1257. }).done(function (data) {
  1258. slider.html(data);
  1259. closer.addClass('active');
  1260. slider.addClass('active');
  1261. ajax_loading = false;
  1262. });
  1263. return false;
  1264. });
  1265. closer.on('click', function() {
  1266. closer.removeClass('active');
  1267. slider.removeClass('active');
  1268. return false;
  1269. });
  1270. }
  1271. function init_configuration_alert() {
  1272. $(window).on('submit', function(e) {
  1273. window.hasSubmit = true;
  1274. });
  1275. $(window).on('beforeunload', function(e) {
  1276. if (window.hasSubmit) {
  1277. return;
  1278. }
  1279. var fields = $("[data-leave-validation]");
  1280. for (var i = 0; i < fields.length; i++) {
  1281. if ($(fields[i]).attr('type') === 'checkbox' || $(fields[i]).attr('type') === 'radio') {
  1282. // The use of != is done on purpose to check boolean against integer
  1283. if ($(fields[i]).is(':checked') != $(fields[i]).attr('data-leave-validation')) {
  1284. return false;
  1285. }
  1286. } else {
  1287. if ($(fields[i]).attr('data-leave-validation') !== $(fields[i]).val()) {
  1288. return false;
  1289. }
  1290. }
  1291. }
  1292. return;
  1293. });
  1294. }
  1295. function init_subscription() {
  1296. $('body').on('click', '.bookmarkClick', function (e) {
  1297. return false;
  1298. });
  1299. }
  1300. function parseJsonVars() {
  1301. var jsonVars = document.getElementById('jsonVars'),
  1302. json = JSON.parse(jsonVars.innerHTML);
  1303. jsonVars.outerHTML = '';
  1304. window.context = json.context;
  1305. window.shortcuts = json.shortcuts;
  1306. window.url = json.url;
  1307. window.i18n = json.i18n;
  1308. window.icons = json.icons;
  1309. }
  1310. function init_normal() {
  1311. $stream = $('#stream');
  1312. if ($stream.length < 1) {
  1313. if (window.console) {
  1314. console.log('FreshRSS waiting for content…');
  1315. }
  1316. window.setTimeout(init_normal, 100);
  1317. return;
  1318. }
  1319. init_column_categories();
  1320. init_stream($stream);
  1321. init_shortcuts();
  1322. init_actualize();
  1323. faviconNbUnread();
  1324. }
  1325. function init_beforeDOM() {
  1326. if (!window.$) {
  1327. if (window.console) {
  1328. console.log('FreshRSS waiting for jQuery…');
  1329. }
  1330. window.setTimeout(init_beforeDOM, 100);
  1331. return;
  1332. }
  1333. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1334. inject_script('jquery.sticky-kit.min.js');
  1335. init_normal();
  1336. }
  1337. }
  1338. function init_afterDOM() {
  1339. if (!window.$) {
  1340. if (window.console) {
  1341. console.log('FreshRSS waiting again for jQuery…');
  1342. }
  1343. window.setTimeout(init_afterDOM, 100);
  1344. return;
  1345. }
  1346. init_notifications();
  1347. init_confirm_action();
  1348. $stream = $('#stream');
  1349. if ($stream.length > 0) {
  1350. init_load_more($stream);
  1351. init_posts();
  1352. init_nav_entries();
  1353. init_dynamic_tags();
  1354. init_print_action();
  1355. init_post_action();
  1356. init_notifs_html5();
  1357. window.setInterval(refreshUnreads, 120000);
  1358. } else {
  1359. init_subscription();
  1360. init_crypto_form();
  1361. init_share_observers();
  1362. init_remove_observers();
  1363. init_feed_observers();
  1364. init_password_observers();
  1365. init_stats_observers();
  1366. init_slider_observers();
  1367. init_configuration_alert();
  1368. }
  1369. if (window.console) {
  1370. console.log('FreshRSS init done.');
  1371. }
  1372. }
  1373. parseJsonVars();
  1374. init_beforeDOM(); //Can be called before DOM is fully loaded
  1375. if (document.readyState && document.readyState !== 'loading') {
  1376. init_afterDOM();
  1377. } else if (document.addEventListener) {
  1378. document.addEventListener('DOMContentLoaded', function () {
  1379. if (window.console) {
  1380. console.log('FreshRSS waiting for DOMContentLoaded…');
  1381. }
  1382. init_afterDOM();
  1383. }, false);
  1384. }