main.js 41 KB

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