main.js 41 KB

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