main.js 40 KB

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