swiper-element.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * Swiper Custom Element 9.4.1
  3. * Most modern mobile touch slider and framework with hardware accelerated transitions
  4. * https://swiperjs.com
  5. *
  6. * Copyright 2014-2023 Vladimir Kharlampidi
  7. *
  8. * Released under the MIT License
  9. *
  10. * Released on: June 13, 2023
  11. */
  12. /* eslint-disable spaced-comment */
  13. import Swiper from 'swiper';
  14. import { paramsList } from '../components-shared/params-list.js';
  15. import { getParams } from './get-params.js';
  16. import {
  17. needsScrollbar,
  18. needsNavigation,
  19. needsPagination,
  20. attrToProp,
  21. } from '../components-shared/utils.js';
  22. import { updateSwiper } from '../components-shared/update-swiper.js';
  23. const SwiperFontCSS = `@font-face{font-family:swiper-icons;src:url('data:application/font-woff;charset=utf-8;base64, d09GRgABAAAAAAZgABAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAAGRAAAABoAAAAci6qHkUdERUYAAAWgAAAAIwAAACQAYABXR1BPUwAABhQAAAAuAAAANuAY7+xHU1VCAAAFxAAAAFAAAABm2fPczU9TLzIAAAHcAAAASgAAAGBP9V5RY21hcAAAAkQAAACIAAABYt6F0cBjdnQgAAACzAAAAAQAAAAEABEBRGdhc3AAAAWYAAAACAAAAAj//wADZ2x5ZgAAAywAAADMAAAD2MHtryVoZWFkAAABbAAAADAAAAA2E2+eoWhoZWEAAAGcAAAAHwAAACQC9gDzaG10eAAAAigAAAAZAAAArgJkABFsb2NhAAAC0AAAAFoAAAB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4dj3EvXhxPqH/SKUY3rJ7srZ4FZnh1PMAtPhwP6fl2PMJMPDgeQ4rY8YT6Gzao0eAEA409DuggmTnFnOcSCiEiLMgxCiTI6Cq5DZUd3Qmp10vO0LaLTd2cjN4fOumlc7lUYbSQcZFkutRG7g6JKZKy0RmdLY680CDnEJ+UMkpFFe1RN7nxdVpXrC4aTtnaurOnYercZg2YVmLN/d/gczfEimrE/fs/bOuq29Zmn8tloORaXgZgGa78yO9/cnXm2BpaGvq25Dv9S4E9+5SIc9PqupJKhYFSSl47+Qcr1mYNAAAAeNptw0cKwkAAAMDZJA8Q7OUJvkLsPfZ6zFVERPy8qHh2YER+3i/BP83vIBLLySsoKimrqKqpa2hp6+jq6RsYGhmbmJqZSy0sraxtbO3sHRydnEMU4uR6yx7JJXveP7WrDycAAAAAAAH//wACeNpjYGRgYOABYhkgZgJCZgZNBkYGLQZtIJsFLMYAAAw3ALgAeNolizEKgDAQBCchRbC2sFER0YD6qVQiBCv/H9ezGI6Z5XBAw8CBK/m5iQQVauVbXLnOrMZv2oLdKFa8Pjuru2hJzGabmOSLzNMzvutpB3N42mNgZGBg4GKQYzBhYMxJLMlj4GBgAYow/P/PAJJhLM6sSoWKfWCAAwDAjgbRAAB42mNgYGBkAIIbCZo5IPrmUn0hGA0AO8EFTQAA');font-weight:400;font-style:normal}`; const SwiperCSS = `:root{--swiper-theme-color:#007aff}.swiper,swiper-container{margin-left:auto;margin-right:auto;position:relative;overflow:hidden;list-style:none;padding:0;z-index:1;display:block}:host(.swiper-vertical)>.swiper-wrapper{flex-direction:column}.swiper-wrapper{position:relative;width:100%;height:100%;z-index:1;display:flex;transition-property:transform;transition-timing-function:var(--swiper-wrapper-transition-timing-function,initial);box-sizing:content-box}.swiper-android swiper-slide,.swiper-wrapper{transform:translate3d(0px,0,0)}.swiper-horizontal{touch-action:pan-y}.swiper-vertical{touch-action:pan-x}swiper-slide{flex-shrink:0;width:100%;height:100%;position:relative;transition-property:transform;display:block}.swiper-slide-invisible-blank{visibility:hidden}.swiper-autoheight,.swiper-autoheight swiper-slide{height:auto}:host(.swiper-autoheight) .swiper-wrapper{align-items:flex-start;transition-property:transform,height}.swiper-backface-hidden swiper-slide{transform:translateZ(0);-webkit-backface-visibility:hidden;backface-visibility:hidden}:host(.swiper-3d.swiper-css-mode) .swiper-wrapper{perspective:1200px}:host(.swiper-3d) .swiper-wrapper{transform-style:preserve-3d}.swiper-3d{perspective:1200px}.swiper-3d .swiper-cube-shadow,.swiper-3d .swiper-slide-shadow,.swiper-3d .swiper-slide-shadow-bottom,.swiper-3d .swiper-slide-shadow-left,.swiper-3d .swiper-slide-shadow-right,.swiper-3d .swiper-slide-shadow-top,.swiper-3d swiper-slide{transform-style:preserve-3d}.swiper-3d .swiper-slide-shadow,.swiper-3d .swiper-slide-shadow-bottom,.swiper-3d .swiper-slide-shadow-left,.swiper-3d .swiper-slide-shadow-right,.swiper-3d .swiper-slide-shadow-top{position:absolute;left:0;top:0;width:100%;height:100%;pointer-events:none;z-index:10}.swiper-3d .swiper-slide-shadow{background:rgba(0,0,0,.15)}.swiper-3d .swiper-slide-shadow-left{background-image:linear-gradient(to left,rgba(0,0,0,.5),rgba(0,0,0,0))}.swiper-3d .swiper-slide-shadow-right{background-image:linear-gradient(to right,rgba(0,0,0,.5),rgba(0,0,0,0))}.swiper-3d .swiper-slide-shadow-top{background-image:linear-gradient(to top,rgba(0,0,0,.5),rgba(0,0,0,0))}.swiper-3d .swiper-slide-shadow-bottom{background-image:linear-gradient(to bottom,rgba(0,0,0,.5),rgba(0,0,0,0))}:host(.swiper-css-mode)>.swiper-wrapper{overflow:auto;scrollbar-width:none;-ms-overflow-style:none}:host(.swiper-css-mode)>.swiper-wrapper::-webkit-scrollbar{display:none}.swiper-css-mode>swiper-slide{scroll-snap-align:start start}:host(.swiper-horizontal.swiper-css-mode)>.swiper-wrapper{scroll-snap-type:x mandatory}:host(.swiper-vertical.swiper-css-mode)>.swiper-wrapper{scroll-snap-type:y mandatory}:host(.swiper-css-mode.swiper-free-mode)>.swiper-wrapper{scroll-snap-type:none}.swiper-css-mode.swiper-free-mode>swiper-slide{scroll-snap-align:none}:host(.swiper-centered)>.swiper-wrapper::before{content:'';flex-shrink:0;order:9999}.swiper-centered>swiper-slide{scroll-snap-align:center center;scroll-snap-stop:always}.swiper-centered.swiper-horizontal>swiper-slide:first-child{margin-inline-start:var(--swiper-centered-offset-before)}:host(.swiper-centered.swiper-horizontal)>.swiper-wrapper::before{height:100%;min-height:1px;width:var(--swiper-centered-offset-after)}.swiper-centered.swiper-vertical>swiper-slide:first-child{margin-block-start:var(--swiper-centered-offset-before)}:host(.swiper-centered.swiper-vertical)>.swiper-wrapper::before{width:100%;min-width:1px;height:var(--swiper-centered-offset-after)}.swiper-lazy-preloader{width:42px;height:42px;position:absolute;left:50%;top:50%;margin-left:-21px;margin-top:-21px;z-index:10;transform-origin:50%;box-sizing:border-box;border:4px solid var(--swiper-preloader-color,var(--swiper-theme-color));border-radius:50%;border-top-color:transparent}.swiper-watch-progress .swiper-slide-visible .swiper-lazy-preloader,.swiper:not(.swiper-watch-progress) .swiper-lazy-preloader,swiper-container:not(.swiper-watch-progress) .swiper-lazy-preloader{animation:swiper-preloader-spin 1s infinite linear}.swiper-lazy-preloader-white{--swiper-preloader-color:#fff}.swiper-lazy-preloader-black{--swiper-preloader-color:#000}@keyframes swiper-preloader-spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}`;
  24. let globalInjectStyles = true;
  25. const addGlobalStyles = (preInit, swiper) => {
  26. let globalStyles = document.querySelector('style#swiper-element-styles');
  27. const shouldOverwrite = globalStyles && globalStyles.preInit && !preInit;
  28. if (!preInit && swiper) {
  29. swiper.cssLinks().forEach((url) => {
  30. const linkEl = document.createElement('link');
  31. linkEl.rel = 'stylesheet';
  32. linkEl.href = url;
  33. document.head.prepend(linkEl);
  34. });
  35. }
  36. if (!globalStyles || shouldOverwrite) {
  37. globalStyles = globalStyles || document.createElement('style');
  38. globalStyles.textContent = [SwiperFontCSS, swiper ? swiper.cssStyles() : ''].join('\n'); // eslint-disable-line
  39. globalStyles.id = 'swiper-element-styles';
  40. globalStyles.preInit = preInit;
  41. document.head.prepend(globalStyles);
  42. }
  43. };
  44. class DummyHTMLElement {}
  45. const ClassToExtend =
  46. typeof window === 'undefined' || typeof HTMLElement === 'undefined'
  47. ? DummyHTMLElement
  48. : HTMLElement;
  49. class SwiperContainer extends ClassToExtend {
  50. constructor() {
  51. super();
  52. this.tempDiv = document.createElement('div');
  53. this.shadowEl = this.attachShadow({ mode: 'open' });
  54. }
  55. cssStyles() {
  56. return [
  57. globalInjectStyles ? SwiperCSS : '', // eslint-disable-line
  58. ...(this.injectStyles && Array.isArray(this.injectStyles) ? this.injectStyles : []),
  59. ].join('\n');
  60. }
  61. cssLinks() {
  62. return this.injectStylesUrls || [];
  63. }
  64. render() {
  65. if (this.rendered) return;
  66. if (globalInjectStyles) {
  67. // global styles
  68. addGlobalStyles(false, this);
  69. }
  70. // local styles
  71. const localStyles = this.cssStyles();
  72. if (localStyles.length) {
  73. this.stylesEl = document.createElement('style');
  74. this.stylesEl.textContent = localStyles;
  75. this.shadowEl.appendChild(this.stylesEl);
  76. }
  77. this.cssLinks().forEach((url) => {
  78. const linkExists = this.shadowEl.querySelector(`link[href="${url}"]`);
  79. if (linkExists) return;
  80. const linkEl = document.createElement('link');
  81. linkEl.rel = 'stylesheet';
  82. linkEl.href = url;
  83. this.shadowEl.appendChild(linkEl);
  84. });
  85. // prettier-ignore
  86. this.tempDiv.innerHTML = `
  87. <slot name="container-start"></slot>
  88. <div class="swiper-wrapper">
  89. <slot></slot>
  90. </div>
  91. <slot name="container-end"></slot>
  92. ${needsNavigation(this.passedParams) ? `
  93. <div part="button-prev" class="swiper-button-prev"></div>
  94. <div part="button-next" class="swiper-button-next"></div>
  95. ` : ''}
  96. ${needsPagination(this.passedParams) ? `
  97. <div part="pagination" class="swiper-pagination"></div>
  98. ` : '' }
  99. ${needsScrollbar(this.passedParams) ? `
  100. <div part="scrollbar" class="swiper-scrollbar"></div>
  101. ` : '' }
  102. `;
  103. [...this.tempDiv.children].forEach((el) => {
  104. this.shadowEl.appendChild(el);
  105. });
  106. this.rendered = true;
  107. }
  108. initialize() {
  109. if (this.initialized) return;
  110. this.initialized = true;
  111. const { params: swiperParams, passedParams } = getParams(this);
  112. this.swiperParams = swiperParams;
  113. this.passedParams = passedParams;
  114. delete this.swiperParams.init;
  115. this.render();
  116. // eslint-disable-next-line
  117. this.swiper = new Swiper(this, {
  118. ...swiperParams,
  119. touchEventsTarget: 'container',
  120. ...(swiperParams.virtual ? {} : { observer: true }),
  121. onAny: (name, ...args) => {
  122. const eventName = swiperParams.eventsPrefix
  123. ? `${swiperParams.eventsPrefix}${name.toLowerCase()}`
  124. : name.toLowerCase();
  125. const event = new CustomEvent(eventName, {
  126. detail: args,
  127. bubbles: true,
  128. cancelable: true,
  129. });
  130. this.dispatchEvent(event);
  131. },
  132. });
  133. }
  134. connectedCallback() {
  135. if (
  136. this.initialized &&
  137. this.nested &&
  138. this.closest('swiper-slide') &&
  139. this.closest('swiper-slide').swiperLoopMoveDOM
  140. ) {
  141. return;
  142. }
  143. if (this.init === false || this.getAttribute('init') === 'false') {
  144. addGlobalStyles(true, this);
  145. return;
  146. }
  147. this.initialize();
  148. }
  149. disconnectedCallback() {
  150. if (
  151. this.nested &&
  152. this.closest('swiper-slide') &&
  153. this.closest('swiper-slide').swiperLoopMoveDOM
  154. ) {
  155. return;
  156. }
  157. if (this.swiper && this.swiper.destroy) {
  158. this.swiper.destroy();
  159. }
  160. this.initialized = false;
  161. }
  162. updateSwiperOnPropChange(propName, propValue) {
  163. const { params: swiperParams, passedParams } = getParams(this, propName, propValue);
  164. this.passedParams = passedParams;
  165. this.swiperParams = swiperParams;
  166. updateSwiper({
  167. swiper: this.swiper,
  168. passedParams: this.passedParams,
  169. changedParams: [attrToProp(propName)],
  170. ...(propName === 'navigation' && passedParams[propName]
  171. ? {
  172. prevEl: '.swiper-button-prev',
  173. nextEl: '.swiper-button-next',
  174. }
  175. : {}),
  176. ...(propName === 'pagination' && passedParams[propName]
  177. ? {
  178. paginationEl: '.swiper-pagination',
  179. }
  180. : {}),
  181. ...(propName === 'scrollbar' && passedParams[propName]
  182. ? {
  183. scrollbarEl: '.swiper-scrollbar',
  184. }
  185. : {}),
  186. });
  187. }
  188. attributeChangedCallback(attr, prevValue, newValue) {
  189. if (!this.initialized) return;
  190. if (prevValue === 'true' && newValue === null) {
  191. newValue = false;
  192. }
  193. this.updateSwiperOnPropChange(attr, newValue);
  194. }
  195. static get observedAttributes() {
  196. const attrs = paramsList
  197. .filter((param) => param.includes('_'))
  198. .map((param) =>
  199. param
  200. .replace(/[A-Z]/g, (v) => `-${v}`)
  201. .replace('_', '')
  202. .toLowerCase(),
  203. );
  204. return attrs;
  205. }
  206. }
  207. paramsList.forEach((paramName) => {
  208. if (paramName === 'init') return;
  209. paramName = paramName.replace('_', '');
  210. Object.defineProperty(SwiperContainer.prototype, paramName, {
  211. configurable: true,
  212. get() {
  213. return (this.passedParams || {})[paramName];
  214. },
  215. set(value) {
  216. if (!this.passedParams) this.passedParams = {};
  217. this.passedParams[paramName] = value;
  218. if (!this.initialized) return;
  219. this.updateSwiperOnPropChange(paramName);
  220. },
  221. });
  222. });
  223. class SwiperSlide extends ClassToExtend {
  224. constructor() {
  225. super();
  226. this.tempDiv = document.createElement('div');
  227. this.shadowEl = this.attachShadow({ mode: 'open' });
  228. }
  229. render() {
  230. const lazy =
  231. this.lazy || this.getAttribute('lazy') === '' || this.getAttribute('lazy') === 'true';
  232. this.tempDiv.innerHTML = `<slot />`;
  233. [...this.tempDiv.children].forEach((el) => {
  234. this.shadowEl.appendChild(el);
  235. });
  236. if (lazy) {
  237. const lazyDiv = document.createElement('div');
  238. lazyDiv.classList.add('swiper-lazy-preloader');
  239. this.appendChild(lazyDiv);
  240. }
  241. }
  242. initialize() {
  243. this.render();
  244. }
  245. connectedCallback() {
  246. this.initialize();
  247. }
  248. }
  249. // eslint-disable-next-line
  250. const register = (injectStyles = true) => {
  251. if (typeof window === 'undefined') return;
  252. if (!injectStyles) {
  253. globalInjectStyles = false;
  254. }
  255. if (globalInjectStyles) {
  256. addGlobalStyles(true);
  257. }
  258. if (!window.customElements.get('swiper-container'))
  259. window.customElements.define('swiper-container', SwiperContainer);
  260. if (!window.customElements.get('swiper-slide'))
  261. window.customElements.define('swiper-slide', SwiperSlide);
  262. };
  263. if (typeof window !== 'undefined') {
  264. window.SwiperElementRegisterParams = (params) => {
  265. paramsList.push(...params);
  266. };
  267. }
  268. //BROWSER_REGISTER
  269. export { SwiperContainer, SwiperSlide, register };