shortcuts.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /**
  2. * @typedef {{ key: string, altKey?: boolean, ctrlKey?: boolean, metaKey?: boolean, shiftKey?: boolean }} KeyCombination
  3. * @typedef {{ code: string, altKey?: boolean, ctrlKey?: boolean, metaKey?: boolean, shiftKey?: boolean }} CodeCombination
  4. * @typedef {{ combination: KeyCombination, event: 'keydown' | 'keyup', callback: (evt: KeyboardEvent) => void, target: EventTarget }} RegisteredShortcut
  5. * @typedef {{ type?: 'keydown' | 'keyup', propagate?: boolean, disabledInInput?: boolean, target?: EventTarget }} ShortcutOptions
  6. */
  7. document.addEventListener('alpine:init', () => {
  8. Alpine.store('shortcuts', {
  9. /**
  10. * @type RegisteredShortcut[]
  11. */
  12. registeredShortcuts: [],
  13. /**
  14. * @param {KeyCombination | CodeCombination} combination
  15. * A combination using a `code` representing a physical key on the keyboard or a `key`
  16. * representing the character generated by pressing the key. Modifiers can be added using the
  17. * `altKey`, `ctrlKey`, `metaKey` or `shiftKey` parameters.
  18. * @param {(evt: KeyboardEvent) => void} callback
  19. * The callback function that will be called when the correct combination is pressed.
  20. * @param {ShortcutOptions?} options
  21. * An object of options, containing the event `type`, whether it will `propagate`, the `target`
  22. * element, and whether it's `disabledInInput`.
  23. * @returns {this} The `Shortcuts` object.
  24. */
  25. register(combination, callback, options) {
  26. /** @type ShortcutOptions */
  27. const defaultOptions = {
  28. type: 'keydown',
  29. propagate: false,
  30. disabledInInput: false,
  31. target: document,
  32. };
  33. options = { ...defaultOptions, ...options };
  34. /**
  35. * @param {KeyboardEvent} evt
  36. */
  37. const func = (evt) => {
  38. if (options.disabledInInput) {
  39. // Don't enable shortcut keys in input, textarea, select fields
  40. const element = evt.target.nodeType === 3 ? evt.target.parentNode : evt.target;
  41. if (['input', 'textarea', 'selectbox'].includes(element.tagName.toLowerCase())) {
  42. return;
  43. }
  44. }
  45. const validations = [
  46. combination.code
  47. ? combination.code == evt.code
  48. : combination.key.toLowerCase() == evt.key.toLowerCase(),
  49. (combination.altKey && evt.altKey) || (!combination.altKey && !evt.altKey),
  50. (combination.ctrlKey && evt.ctrlKey) || (!combination.ctrlKey && !evt.ctrlKey),
  51. (combination.metaKey && evt.metaKey) || (!combination.metaKey && !evt.metaKey),
  52. (combination.shiftKey && evt.shiftKey) || (!combination.shiftKey && !evt.shiftKey),
  53. ];
  54. const valid = validations.filter((validation) => validation);
  55. if (valid.length === validations.length) {
  56. callback(evt);
  57. if (!options.propagate) {
  58. evt.stopPropagation();
  59. evt.preventDefault();
  60. }
  61. }
  62. };
  63. this.registeredShortcuts.push({
  64. combination: combination,
  65. callback: func,
  66. target: options.target,
  67. event: options.type,
  68. });
  69. options.target.addEventListener(options.type, func);
  70. return this;
  71. },
  72. /**
  73. * @param {KeyCombination | CodeCombination} combination
  74. * A combination using a `code` representing a physical key on the keyboard or a `key`
  75. * representing the character generated by pressing the key. Modifiers can be added using the
  76. * `altKey`, `ctrlKey`, `metaKey` or `shiftKey` parameters.
  77. * @returns {this} The `Shortcuts` object.
  78. */
  79. unregister(combination) {
  80. const shortcut = this.registeredShortcuts.find(
  81. (shortcut) => JSON.stringify(shortcut.combination) == JSON.stringify(combination)
  82. );
  83. if (!shortcut) return;
  84. this.registeredShortcuts = this.registeredShortcuts.filter(
  85. (shortcut) => JSON.stringify(shortcut.combination) != JSON.stringify(combination)
  86. );
  87. shortcut.target.removeEventListener(shortcut.event, shortcut.callback, false);
  88. return this;
  89. },
  90. });
  91. Alpine.store('shortcuts')
  92. .register(
  93. { key: 'A' },
  94. (_evt) => {
  95. const createButton = document.querySelector('.button#btn-create');
  96. if (!createButton) {
  97. return;
  98. }
  99. location.href = createButton.href;
  100. },
  101. { disabledInInput: true }
  102. )
  103. .register(
  104. { key: 'A', ctrlKey: true, shiftKey: true },
  105. (_evt) => {
  106. const checked = document.querySelector('.l-unit .ch-toggle:eq(0)').checked;
  107. document
  108. .querySelectorAll('.l-unit')
  109. .forEach((el) => el.classList.toggle('selected'), !checked);
  110. document.querySelectorAll('.l-unit .ch-toggle').forEach((el) => (el.checked = !checked));
  111. },
  112. { disabledInInput: true }
  113. )
  114. .register({ code: 'Enter', ctrlKey: true }, (_evt) => {
  115. document.querySelector('form#vstobjects').submit();
  116. })
  117. .register({ code: 'Backspace', ctrlKey: true }, (_evt) => {
  118. const redirect = document.querySelector('a.button#btn-back').href;
  119. if (!redirect) {
  120. return;
  121. }
  122. if (Alpine.store('form').dirty && redirect) {
  123. VE.helpers.createConfirmationDialog({
  124. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  125. targetUrl: redirect,
  126. });
  127. } else if (document.querySelector('form#vstobjects .button.cancel')) {
  128. location.href = $('form#vstobjects input.cancel')
  129. .attr('onclick')
  130. .replace("location.href='", '')
  131. .replace("'", '');
  132. } else if (redirect) {
  133. location.href = redirect;
  134. }
  135. })
  136. .register(
  137. { key: 'F' },
  138. (_evt) => {
  139. const searchBox = document.querySelector('.js-search-input');
  140. searchBox.focus();
  141. },
  142. { disabledInInput: true }
  143. )
  144. .register(
  145. { code: 'Digit1' },
  146. (_evt) => {
  147. const target = document.querySelector('.main-menu .main-menu-item:nth-of-type(1) a');
  148. if (!target) {
  149. return;
  150. }
  151. if (Alpine.store('form').dirty) {
  152. VE.helpers.createConfirmationDialog({
  153. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  154. targetUrl: target.href,
  155. });
  156. } else {
  157. location.href = target.href;
  158. }
  159. },
  160. { disabledInInput: true }
  161. )
  162. .register(
  163. { code: 'Digit2' },
  164. (_evt) => {
  165. const target = document.querySelector('.main-menu .main-menu-item:nth-of-type(2) a');
  166. if (!target) {
  167. return;
  168. }
  169. if (Alpine.store('form').dirty) {
  170. VE.helpers.createConfirmationDialog({
  171. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  172. targetUrl: target.href,
  173. });
  174. } else {
  175. location.href = target.href;
  176. }
  177. },
  178. { disabledInInput: true }
  179. )
  180. .register(
  181. { code: 'Digit3' },
  182. (_evt) => {
  183. const target = document.querySelector('.main-menu .main-menu-item:nth-of-type(3) a');
  184. if (!target) {
  185. return;
  186. }
  187. if (Alpine.store('form').dirty) {
  188. VE.helpers.createConfirmationDialog({
  189. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  190. targetUrl: target.href,
  191. });
  192. } else {
  193. location.href = target.href;
  194. }
  195. },
  196. { disabledInInput: true }
  197. )
  198. .register(
  199. { code: 'Digit4' },
  200. (_evt) => {
  201. const target = document.querySelector('.main-menu .main-menu-item:nth-of-type(4) a');
  202. if (!target) {
  203. return;
  204. }
  205. if (Alpine.store('form').dirty) {
  206. VE.helpers.createConfirmationDialog({
  207. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  208. targetUrl: target.href,
  209. });
  210. } else {
  211. location.href = target.href;
  212. }
  213. },
  214. { disabledInInput: true }
  215. )
  216. .register(
  217. { code: 'Digit5' },
  218. (_evt) => {
  219. const target = document.querySelector('.main-menu .main-menu-item:nth-of-type(5) a');
  220. if (!target) {
  221. return;
  222. }
  223. if (Alpine.store('form').dirty) {
  224. VE.helpers.createConfirmationDialog({
  225. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  226. targetUrl: target.href,
  227. });
  228. } else {
  229. location.href = target.href;
  230. }
  231. },
  232. { disabledInInput: true }
  233. )
  234. .register(
  235. { code: 'Digit6' },
  236. (_evt) => {
  237. const target = document.querySelector('.main-menu .main-menu-item:nth-of-type(6) a');
  238. if (!target) {
  239. return;
  240. }
  241. if (Alpine.store('form').dirty) {
  242. VE.helpers.createConfirmationDialog({
  243. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  244. targetUrl: target.href,
  245. });
  246. } else {
  247. location.href = target.href;
  248. }
  249. },
  250. { disabledInInput: true }
  251. )
  252. .register(
  253. { code: 'Digit7' },
  254. (_evt) => {
  255. const target = document.querySelector('.main-menu .main-menu-item:nth-of-type(7) a');
  256. if (!target) {
  257. return;
  258. }
  259. if (Alpine.store('form').dirty) {
  260. VE.helpers.createConfirmationDialog({
  261. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  262. targetUrl: target.href,
  263. });
  264. } else {
  265. location.href = target.href;
  266. }
  267. },
  268. { disabledInInput: true }
  269. )
  270. .register(
  271. { key: 'H' },
  272. (_evt) => {
  273. const shortcutsDialog = document.querySelector('.shortcuts');
  274. if (shortcutsDialog.open) {
  275. shortcutsDialog.close();
  276. } else {
  277. shortcutsDialog.showModal();
  278. }
  279. },
  280. { disabledInInput: true }
  281. )
  282. .register({ code: 'Escape' }, (_evt) => {
  283. const shortcutsDialog = document.querySelector('.shortcuts');
  284. if (shortcutsDialog.open) {
  285. shortcutsDialog.close();
  286. }
  287. document.querySelectorAll('input, checkbox, textarea, select').forEach((el) => el.blur());
  288. })
  289. .register(
  290. { code: 'ArrowLeft' },
  291. (_evt) => {
  292. VE.navigation.move_focus_left();
  293. },
  294. { disabledInInput: true }
  295. )
  296. .register(
  297. { code: 'ArrowRight' },
  298. (_evt) => {
  299. VE.navigation.move_focus_right();
  300. },
  301. { disabledInInput: true }
  302. )
  303. .register(
  304. { code: 'ArrowDown' },
  305. (_evt) => {
  306. VE.navigation.move_focus_down();
  307. },
  308. { disabledInInput: true }
  309. )
  310. .register(
  311. { code: 'ArrowUp' },
  312. (_evt) => {
  313. VE.navigation.move_focus_up();
  314. },
  315. { disabledInInput: true }
  316. )
  317. .register(
  318. { key: 'L' },
  319. (_evt) => {
  320. const el = $('.units.active .l-unit.focus .shortcut-l');
  321. if (el.length) {
  322. VE.navigation.shortcut(el);
  323. }
  324. },
  325. { disabledInInput: true }
  326. )
  327. .register(
  328. { key: 'S' },
  329. (_evt) => {
  330. const el = $('.units.active .l-unit.focus .shortcut-s');
  331. if (el.length) {
  332. VE.navigation.shortcut(el);
  333. }
  334. },
  335. { disabledInInput: true }
  336. )
  337. .register(
  338. { key: 'W' },
  339. (_evt) => {
  340. const el = $('.units.active .l-unit.focus .shortcut-w');
  341. if (el.length) {
  342. VE.navigation.shortcut(el);
  343. }
  344. },
  345. { disabledInInput: true }
  346. )
  347. .register(
  348. { key: 'D' },
  349. (_evt) => {
  350. const el = $('.units.active .l-unit.focus .shortcut-d');
  351. if (el.length) {
  352. VE.navigation.shortcut(el);
  353. }
  354. },
  355. { disabledInInput: true }
  356. )
  357. .register(
  358. { key: 'R' },
  359. (_evt) => {
  360. const el = $('.units.active .l-unit.focus .shortcut-r');
  361. if (el.length) {
  362. VE.navigation.shortcut(el);
  363. }
  364. },
  365. { disabledInInput: true }
  366. )
  367. .register(
  368. { key: 'N' },
  369. (_evt) => {
  370. const el = $('.units.active .l-unit.focus .shortcut-n');
  371. if (el.length) {
  372. VE.navigation.shortcut(el);
  373. }
  374. },
  375. { disabledInInput: true }
  376. )
  377. .register(
  378. { key: 'U' },
  379. (_evt) => {
  380. const el = $('.units.active .l-unit.focus .shortcut-u');
  381. if (el.length) {
  382. VE.navigation.shortcut(el);
  383. }
  384. },
  385. { disabledInInput: true }
  386. )
  387. .register(
  388. { code: 'Delete' },
  389. (_evt) => {
  390. const el = $('.units.active .l-unit.focus .shortcut-delete');
  391. if (el.length) {
  392. VE.navigation.shortcut(el);
  393. }
  394. },
  395. { disabledInInput: true }
  396. )
  397. .register(
  398. { code: 'Enter' },
  399. (evt) => {
  400. if (evt.target.tagName == 'INPUT' && evt.target.form.id == 'vstobjects') {
  401. document.querySelector('form#vstobjects').submit();
  402. }
  403. if (Alpine.store('form').dirty) {
  404. if (document.querySelector('dialog[open]')) {
  405. const dialog = document.querySelector('dialog[open]');
  406. dialog.querySelector('button[type="submit"]').click();
  407. } else {
  408. VE.helpers.createConfirmationDialog({
  409. message: document.querySelector('body').getAttribute('data-confirm-leave-page'),
  410. targetUrl: document.querySelector(`${VE.navigation.state.menu_selector}.focus a`)
  411. .href,
  412. });
  413. }
  414. } else {
  415. if (document.querySelector('dialog[open]')) {
  416. const dialog = document.querySelector('dialog[open]');
  417. dialog.querySelector('button[type="submit"]').click();
  418. } else {
  419. const el = $('.units.active .l-unit.focus .shortcut-enter');
  420. if (el.length) {
  421. VE.navigation.shortcut(el);
  422. } else {
  423. VE.navigation.enter_focused();
  424. }
  425. }
  426. }
  427. },
  428. { propagate: true }
  429. );
  430. });