keyboard.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { g as getDocument, a as getWindow } from '../shared/ssr-window.esm.mjs';
  2. import { a as elementParents, b as elementOffset } from '../shared/utils.mjs';
  3. /* eslint-disable consistent-return */
  4. function Keyboard(_ref) {
  5. let {
  6. swiper,
  7. extendParams,
  8. on,
  9. emit
  10. } = _ref;
  11. const document = getDocument();
  12. const window = getWindow();
  13. swiper.keyboard = {
  14. enabled: false
  15. };
  16. extendParams({
  17. keyboard: {
  18. enabled: false,
  19. onlyInViewport: true,
  20. pageUpDown: true
  21. }
  22. });
  23. function handle(event) {
  24. if (!swiper.enabled) return;
  25. const {
  26. rtlTranslate: rtl
  27. } = swiper;
  28. let e = event;
  29. if (e.originalEvent) e = e.originalEvent; // jquery fix
  30. const kc = e.keyCode || e.charCode;
  31. const pageUpDown = swiper.params.keyboard.pageUpDown;
  32. const isPageUp = pageUpDown && kc === 33;
  33. const isPageDown = pageUpDown && kc === 34;
  34. const isArrowLeft = kc === 37;
  35. const isArrowRight = kc === 39;
  36. const isArrowUp = kc === 38;
  37. const isArrowDown = kc === 40;
  38. // Directions locks
  39. if (!swiper.allowSlideNext && (swiper.isHorizontal() && isArrowRight || swiper.isVertical() && isArrowDown || isPageDown)) {
  40. return false;
  41. }
  42. if (!swiper.allowSlidePrev && (swiper.isHorizontal() && isArrowLeft || swiper.isVertical() && isArrowUp || isPageUp)) {
  43. return false;
  44. }
  45. if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
  46. return undefined;
  47. }
  48. if (document.activeElement && document.activeElement.nodeName && (document.activeElement.nodeName.toLowerCase() === 'input' || document.activeElement.nodeName.toLowerCase() === 'textarea')) {
  49. return undefined;
  50. }
  51. if (swiper.params.keyboard.onlyInViewport && (isPageUp || isPageDown || isArrowLeft || isArrowRight || isArrowUp || isArrowDown)) {
  52. let inView = false;
  53. // Check that swiper should be inside of visible area of window
  54. if (elementParents(swiper.el, `.${swiper.params.slideClass}, swiper-slide`).length > 0 && elementParents(swiper.el, `.${swiper.params.slideActiveClass}`).length === 0) {
  55. return undefined;
  56. }
  57. const el = swiper.el;
  58. const swiperWidth = el.clientWidth;
  59. const swiperHeight = el.clientHeight;
  60. const windowWidth = window.innerWidth;
  61. const windowHeight = window.innerHeight;
  62. const swiperOffset = elementOffset(el);
  63. if (rtl) swiperOffset.left -= el.scrollLeft;
  64. const swiperCoord = [[swiperOffset.left, swiperOffset.top], [swiperOffset.left + swiperWidth, swiperOffset.top], [swiperOffset.left, swiperOffset.top + swiperHeight], [swiperOffset.left + swiperWidth, swiperOffset.top + swiperHeight]];
  65. for (let i = 0; i < swiperCoord.length; i += 1) {
  66. const point = swiperCoord[i];
  67. if (point[0] >= 0 && point[0] <= windowWidth && point[1] >= 0 && point[1] <= windowHeight) {
  68. if (point[0] === 0 && point[1] === 0) continue; // eslint-disable-line
  69. inView = true;
  70. }
  71. }
  72. if (!inView) return undefined;
  73. }
  74. if (swiper.isHorizontal()) {
  75. if (isPageUp || isPageDown || isArrowLeft || isArrowRight) {
  76. if (e.preventDefault) e.preventDefault();else e.returnValue = false;
  77. }
  78. if ((isPageDown || isArrowRight) && !rtl || (isPageUp || isArrowLeft) && rtl) swiper.slideNext();
  79. if ((isPageUp || isArrowLeft) && !rtl || (isPageDown || isArrowRight) && rtl) swiper.slidePrev();
  80. } else {
  81. if (isPageUp || isPageDown || isArrowUp || isArrowDown) {
  82. if (e.preventDefault) e.preventDefault();else e.returnValue = false;
  83. }
  84. if (isPageDown || isArrowDown) swiper.slideNext();
  85. if (isPageUp || isArrowUp) swiper.slidePrev();
  86. }
  87. emit('keyPress', kc);
  88. return undefined;
  89. }
  90. function enable() {
  91. if (swiper.keyboard.enabled) return;
  92. document.addEventListener('keydown', handle);
  93. swiper.keyboard.enabled = true;
  94. }
  95. function disable() {
  96. if (!swiper.keyboard.enabled) return;
  97. document.removeEventListener('keydown', handle);
  98. swiper.keyboard.enabled = false;
  99. }
  100. on('init', () => {
  101. if (swiper.params.keyboard.enabled) {
  102. enable();
  103. }
  104. });
  105. on('destroy', () => {
  106. if (swiper.keyboard.enabled) {
  107. disable();
  108. }
  109. });
  110. Object.assign(swiper.keyboard, {
  111. enable,
  112. disable
  113. });
  114. }
  115. export { Keyboard as default };