main.js 42 KB

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