main.js 41 KB

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