main.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. "use strict";
  2. /* globals $, jQuery, context, i18n, shortcut, shortcuts, url */
  3. /* jshint strict:global */
  4. var $stream = null,
  5. isCollapsed = true,
  6. shares = 0,
  7. ajax_loading = false;
  8. function redirect(url, new_tab) {
  9. if (url) {
  10. if (new_tab) {
  11. window.open(url);
  12. } else {
  13. location.href = url;
  14. }
  15. }
  16. }
  17. function needsScroll($elem) {
  18. var $win = $(window),
  19. winTop = $win.scrollTop(),
  20. winHeight = $win.height(),
  21. winBottom = winTop + winHeight,
  22. elemTop = $elem.offset().top,
  23. elemBottom = elemTop + $elem.outerHeight();
  24. return (elemTop < winTop || elemBottom > winBottom) ? elemTop - (winHeight / 2) : 0;
  25. }
  26. function str2int(str) {
  27. if (!str) {
  28. return 0;
  29. }
  30. return parseInt(str.replace(/\D/g, ''), 10) || 0;
  31. }
  32. function numberFormat(nStr) {
  33. if (nStr < 0) {
  34. return 0;
  35. }
  36. // http://www.mredkj.com/javascript/numberFormat.html
  37. nStr += '';
  38. var x = nStr.split('.'),
  39. x1 = x[0],
  40. x2 = x.length > 1 ? '.' + x[1] : '',
  41. rgx = /(\d+)(\d{3})/;
  42. while (rgx.test(x1)) {
  43. x1 = x1.replace(rgx, '$1' + ' ' + '$2');
  44. }
  45. return x1 + x2;
  46. }
  47. function incLabel(p, inc, spaceAfter) {
  48. var i = str2int(p) + inc;
  49. return i > 0 ? ((spaceAfter ? '' : ' ') + '(' + numberFormat(i) + ')' + (spaceAfter ? ' ' : '')) : '';
  50. }
  51. function incUnreadsFeed(article, feed_id, nb) {
  52. //Update unread: feed
  53. var elem = $('#' + feed_id).get(0),
  54. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0,
  55. feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0;
  56. if (elem) {
  57. elem.setAttribute('data-unread', feed_unreads + nb);
  58. elem = $(elem).children('.item-title').get(0);
  59. if (elem) {
  60. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  61. }
  62. }
  63. //Update unread: category
  64. elem = $('#' + feed_id).parents('.category').get(0);
  65. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  66. if (elem) {
  67. elem.setAttribute('data-unread', feed_unreads + nb);
  68. elem = $(elem).find('.title').get(0);
  69. if (elem) {
  70. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  71. }
  72. }
  73. //Update unread: all
  74. if (feed_priority > 0) {
  75. elem = $('#aside_feed .all .title').get(0);
  76. if (elem) {
  77. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  78. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  79. }
  80. }
  81. //Update unread: favourites
  82. if (article && article.closest('div').hasClass('favorite')) {
  83. elem = $('#aside_feed .favorites .title').get(0);
  84. if (elem) {
  85. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  86. elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
  87. }
  88. }
  89. var isCurrentView = false;
  90. // Update unread: title
  91. document.title = document.title.replace(/^((?:\([ 0-9]+\) )?)/, function (m, p1) {
  92. var $feed = $('#' + feed_id);
  93. if (article || ($feed.closest('.active').length > 0 && $feed.siblings('.active').length === 0)) {
  94. isCurrentView = true;
  95. return incLabel(p1, nb, true);
  96. } else if ($('.all.active').length > 0) {
  97. isCurrentView = feed_priority > 0;
  98. return incLabel(p1, feed_priority > 0 ? nb : 0, true);
  99. } else {
  100. return p1;
  101. }
  102. });
  103. return isCurrentView;
  104. }
  105. function incUnreadsTag(tag_id, nb) {
  106. var $t = $('#t_' + tag_id);
  107. var unreads = str2int($t.attr('data-unread'));
  108. $t.attr('data-unread', unreads + nb)
  109. .children('.item-title').attr('data-unread', numberFormat(unreads + nb));
  110. $t = $('.category.tags').find('.title');
  111. unreads = str2int($t.attr('data-unread'));
  112. $t.attr('data-unread', numberFormat(unreads + nb));
  113. }
  114. var pending_entries = {};
  115. function mark_read(active, only_not_read) {
  116. if ((active.length === 0) || (!active.attr('id')) ||
  117. context.anonymous ||
  118. (only_not_read && !active.hasClass("not_read"))) {
  119. return false;
  120. }
  121. if (pending_entries[active.attr('id')]) {
  122. return false;
  123. }
  124. pending_entries[active.attr('id')] = true;
  125. var url = '.?c=entry&a=read&id=' + active.attr('id').replace(/^flux_/, '') +
  126. (active.hasClass('not_read') ? '' : '&is_read=0');
  127. $.ajax({
  128. type: 'POST',
  129. url: url,
  130. data: {
  131. ajax: true,
  132. _csrf: context.csrf,
  133. },
  134. }).done(function (data) {
  135. var $r = active.find("a.read").attr("href", data.url),
  136. inc = 0;
  137. if (active.hasClass("not_read")) {
  138. active.removeClass("not_read");
  139. inc--;
  140. } else {
  141. active.addClass("not_read");
  142. active.addClass("keep_unread");
  143. inc++;
  144. }
  145. $r.find('.icon').replaceWith(data.icon);
  146. var feed_url = active.find(".website>a").attr("href");
  147. if (feed_url) {
  148. var feed_id = feed_url.substr(feed_url.lastIndexOf('f_'));
  149. incUnreadsFeed(active, feed_id, inc);
  150. }
  151. faviconNbUnread();
  152. if (data.tags) {
  153. for (var i = data.tags.length - 1; i >= 0; i--) {
  154. incUnreadsTag(data.tags[i], inc);
  155. }
  156. }
  157. delete pending_entries[active.attr('id')];
  158. }).fail(function (data) {
  159. openNotification(i18n.notif_request_failed, 'bad');
  160. delete pending_entries[active.attr('id')];
  161. });
  162. }
  163. function mark_favorite(active) {
  164. if (active.length === 0) {
  165. return false;
  166. }
  167. var url = active.find("a.bookmark").attr("href");
  168. if (url === undefined) {
  169. return false;
  170. }
  171. if (pending_entries[active.attr('id')]) {
  172. return false;
  173. }
  174. pending_entries[active.attr('id')] = true;
  175. $.ajax({
  176. type: 'POST',
  177. url: url,
  178. data: {
  179. ajax: true,
  180. _csrf: context.csrf,
  181. },
  182. }).done(function (data) {
  183. var $b = active.find("a.bookmark").attr("href", data.url),
  184. inc = 0;
  185. if (active.hasClass("favorite")) {
  186. active.removeClass("favorite");
  187. inc--;
  188. } else {
  189. active.addClass("favorite").find('.bookmark');
  190. inc++;
  191. }
  192. $b.find('.icon').replaceWith(data.icon);
  193. var favourites = $('#aside_feed .favorites .title').contents().last().get(0);
  194. if (favourites && favourites.textContent) {
  195. favourites.textContent = favourites.textContent.replace(/((?: \([ 0-9]+\))?\s*)$/, function (m, p1) {
  196. return incLabel(p1, inc, false);
  197. });
  198. }
  199. if (active.closest('div').hasClass('not_read')) {
  200. var elem = $('#aside_feed .favorites .title').get(0),
  201. feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
  202. if (elem) {
  203. elem.setAttribute('data-unread', numberFormat(feed_unreads + inc));
  204. }
  205. }
  206. delete pending_entries[active.attr('id')];
  207. }).fail(function (data) {
  208. openNotification(i18n.notif_request_failed, 'bad');
  209. delete pending_entries[active.attr('id')];
  210. });
  211. }
  212. var freshrssOpenArticleEvent = document.createEvent('Event');
  213. freshrssOpenArticleEvent.initEvent('freshrss:openArticle', true, true);
  214. function toggleContent(new_active, old_active, skipping) {
  215. // If skipping, move current without activating or marking as read
  216. if (new_active.length === 0) {
  217. return;
  218. }
  219. if (context.does_lazyload && !skipping) {
  220. new_active.find('img[data-original], iframe[data-original]').each(function () {
  221. this.setAttribute('src', this.getAttribute('data-original'));
  222. this.removeAttribute('data-original');
  223. });
  224. }
  225. if (old_active[0] !== new_active[0]) {
  226. if (isCollapsed && !skipping) { // BUG?: isCollapsed can only ever be true
  227. new_active.addClass("active");
  228. }
  229. old_active.removeClass("active current");
  230. new_active.addClass("current");
  231. if (context.auto_remove_article && !old_active.hasClass('not_read') && !skipping) {
  232. auto_remove(old_active);
  233. }
  234. } else { // collapse_entry calls toggleContent(flux_current, flux_current, false)
  235. new_active.toggleClass('active');
  236. }
  237. var relative_move = context.current_view === 'global',
  238. box_to_move = $(relative_move ? "#panel" : "html,body");
  239. if (context.sticky_post) {
  240. var prev_article = new_active.prevAll('.flux'),
  241. new_pos = new_active.offset().top,
  242. old_scroll = box_to_move.scrollTop();
  243. if (prev_article.length > 0 && new_pos - prev_article.offset().top <= 150) {
  244. new_pos = prev_article.offset().top;
  245. if (relative_move) {
  246. new_pos -= box_to_move.offset().top;
  247. }
  248. }
  249. if (skipping) {
  250. // when skipping, this feels more natural if it's not so near the top
  251. new_pos -= $(window).height() / 4;
  252. }
  253. if (context.hide_posts) {
  254. if (relative_move) {
  255. new_pos += old_scroll;
  256. }
  257. new_active.children(".flux_content").first().each(function () {
  258. box_to_move.scrollTop(new_pos).scrollTop();
  259. });
  260. } else {
  261. if (relative_move) {
  262. new_pos += old_scroll;
  263. }
  264. box_to_move.scrollTop(new_pos).scrollTop();
  265. }
  266. }
  267. if (new_active.hasClass('active') && !skipping) {
  268. if (context.auto_mark_article) {
  269. mark_read(new_active, true);
  270. }
  271. new_active[0].dispatchEvent(freshrssOpenArticleEvent);
  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 1514498 in Firefox 64
  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(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. }
  1325. function init_normal() {
  1326. $stream = $('#stream');
  1327. if ($stream.length < 1) {
  1328. if (window.console) {
  1329. console.log('FreshRSS waiting for content…');
  1330. }
  1331. window.setTimeout(init_normal, 100);
  1332. return;
  1333. }
  1334. init_column_categories();
  1335. init_stream($stream);
  1336. init_shortcuts();
  1337. init_actualize();
  1338. faviconNbUnread();
  1339. }
  1340. function init_beforeDOM() {
  1341. if (!window.$) {
  1342. if (window.console) {
  1343. console.log('FreshRSS waiting for jQuery…');
  1344. }
  1345. window.setTimeout(init_beforeDOM, 100);
  1346. return;
  1347. }
  1348. if (['normal', 'reader', 'global'].indexOf(context.current_view) >= 0) {
  1349. init_normal();
  1350. }
  1351. }
  1352. function init_afterDOM() {
  1353. if (!window.$) {
  1354. if (window.console) {
  1355. console.log('FreshRSS waiting again for jQuery…');
  1356. }
  1357. window.setTimeout(init_afterDOM, 100);
  1358. return;
  1359. }
  1360. init_notifications();
  1361. init_confirm_action();
  1362. $stream = $('#stream');
  1363. if ($stream.length > 0) {
  1364. init_load_more($stream);
  1365. init_posts();
  1366. init_nav_entries();
  1367. init_dynamic_tags();
  1368. init_print_action();
  1369. init_post_action();
  1370. init_notifs_html5();
  1371. window.setInterval(refreshUnreads, 120000);
  1372. } else {
  1373. init_subscription();
  1374. init_crypto_form();
  1375. init_share_observers();
  1376. init_remove_observers();
  1377. init_feed_observers();
  1378. init_password_observers();
  1379. init_stats_observers();
  1380. init_slider_observers();
  1381. init_configuration_alert();
  1382. }
  1383. if (window.console) {
  1384. console.log('FreshRSS init done.');
  1385. }
  1386. }
  1387. parseJsonVars();
  1388. init_beforeDOM(); //Can be called before DOM is fully loaded
  1389. if (document.readyState && document.readyState !== 'loading') {
  1390. init_afterDOM();
  1391. } else if (document.addEventListener) {
  1392. document.addEventListener('DOMContentLoaded', function () {
  1393. if (window.console) {
  1394. console.log('FreshRSS waiting for DOMContentLoaded…');
  1395. }
  1396. init_afterDOM();
  1397. }, false);
  1398. }