exporter.esm.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*!
  2. * vue-awesome-swiper v4.1.1
  3. * Copyright (c) Surmon. All rights reserved.
  4. * Released under the MIT License.
  5. * Surmon <https://github.com/surmon-china>
  6. */
  7. import Vue from 'vue';
  8. /**
  9. * @file vue-awesome-swiper
  10. * @module constants
  11. * @author Surmon <https://github.com/surmon-china>
  12. */
  13. var CoreNames;
  14. (function (CoreNames) {
  15. CoreNames["SwiperComponent"] = "Swiper";
  16. CoreNames["SwiperSlideComponent"] = "SwiperSlide";
  17. CoreNames["SwiperDirective"] = "swiper";
  18. CoreNames["SwiperInstance"] = "$swiper";
  19. })(CoreNames || (CoreNames = {}));
  20. var DEFAULT_CLASSES = Object.freeze({
  21. containerClass: 'swiper-container',
  22. wrapperClass: 'swiper-wrapper',
  23. slideClass: 'swiper-slide'
  24. });
  25. var ComponentEvents;
  26. (function (ComponentEvents) {
  27. ComponentEvents["Ready"] = "ready";
  28. ComponentEvents["ClickSlide"] = "clickSlide";
  29. })(ComponentEvents || (ComponentEvents = {}));
  30. var ComponentPropNames;
  31. (function (ComponentPropNames) {
  32. ComponentPropNames["AutoUpdate"] = "autoUpdate";
  33. ComponentPropNames["AutoDestroy"] = "autoDestroy";
  34. ComponentPropNames["DeleteInstanceOnDestroy"] = "deleteInstanceOnDestroy";
  35. ComponentPropNames["CleanupStylesOnDestroy"] = "cleanupStylesOnDestroy";
  36. })(ComponentPropNames || (ComponentPropNames = {}));
  37. // https://swiperjs.com/api/#events
  38. var SWIPER_EVENTS = [
  39. 'init',
  40. 'beforeDestroy',
  41. 'slideChange',
  42. 'slideChangeTransitionStart',
  43. 'slideChangeTransitionEnd',
  44. 'slideNextTransitionStart',
  45. 'slideNextTransitionEnd',
  46. 'slidePrevTransitionStart',
  47. 'slidePrevTransitionEnd',
  48. 'transitionStart',
  49. 'transitionEnd',
  50. 'touchStart',
  51. 'touchMove',
  52. 'touchMoveOpposite',
  53. 'sliderMove',
  54. 'touchEnd',
  55. 'click',
  56. 'tap',
  57. 'doubleTap',
  58. 'imagesReady',
  59. 'progress',
  60. 'reachBeginning',
  61. 'reachEnd',
  62. 'fromEdge',
  63. 'setTranslate',
  64. 'setTransition',
  65. 'resize',
  66. 'observerUpdate',
  67. 'beforeLoopFix',
  68. 'loopFix'
  69. ];
  70. /*! *****************************************************************************
  71. Copyright (c) Microsoft Corporation. All rights reserved.
  72. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  73. this file except in compliance with the License. You may obtain a copy of the
  74. License at http://www.apache.org/licenses/LICENSE-2.0
  75. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  76. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  77. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  78. MERCHANTABLITY OR NON-INFRINGEMENT.
  79. See the Apache Version 2.0 License for specific language governing permissions
  80. and limitations under the License.
  81. ***************************************************************************** */
  82. function __spreadArrays() {
  83. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  84. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  85. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  86. r[k] = a[j];
  87. return r;
  88. }
  89. /**
  90. * @file vue-awesome-swiper
  91. * @module utils
  92. * @author Surmon <https://github.com/surmon-china>
  93. */
  94. var kebabcase = function (string) {
  95. return string
  96. .replace(/([a-z])([A-Z])/g, '$1-$2')
  97. .replace(/\s+/g, '-')
  98. .toLowerCase();
  99. };
  100. /**
  101. * @file vue-awesome-swiper
  102. * @module event
  103. * @author Surmon <https://github.com/surmon-china>
  104. */
  105. var handleClickSlideEvent = function (swiper, event, emit) {
  106. var _a, _b, _c;
  107. if (swiper && !(swiper.destroyed)) {
  108. var eventPath = ((_a = event.composedPath) === null || _a === void 0 ? void 0 : _a.call(event)) || event.path;
  109. if ((event === null || event === void 0 ? void 0 : event.target) && eventPath) {
  110. var slides_1 = Array.from(swiper.slides);
  111. var paths = Array.from(eventPath);
  112. // Click slide || slide[children]
  113. if (slides_1.includes(event.target) || paths.some(function (item) { return slides_1.includes(item); })) {
  114. var clickedIndex = swiper.clickedIndex;
  115. var reallyIndex = Number((_c = (_b = swiper.clickedSlide) === null || _b === void 0 ? void 0 : _b.dataset) === null || _c === void 0 ? void 0 : _c.swiperSlideIndex);
  116. var reallyIndexValue = Number.isInteger(reallyIndex) ? reallyIndex : null;
  117. emit(ComponentEvents.ClickSlide, clickedIndex, reallyIndexValue);
  118. emit(kebabcase(ComponentEvents.ClickSlide), clickedIndex, reallyIndexValue);
  119. }
  120. }
  121. }
  122. };
  123. var bindSwiperEvents = function (swiper, emit) {
  124. SWIPER_EVENTS.forEach(function (eventName) {
  125. swiper.on(eventName, function () {
  126. var arguments$1 = arguments;
  127. var args = [];
  128. for (var _i = 0; _i < arguments.length; _i++) {
  129. args[_i] = arguments$1[_i];
  130. }
  131. emit.apply(void 0, __spreadArrays([eventName], args));
  132. var kebabcaseName = kebabcase(eventName);
  133. if (kebabcaseName !== eventName) {
  134. emit.apply(void 0, __spreadArrays([kebabcaseName], args));
  135. }
  136. });
  137. });
  138. };
  139. /**
  140. * @file vue-awesome-swiper
  141. * @module directive
  142. * @author Surmon <https://github.com/surmon-china>
  143. */
  144. var INSTANCE_NAME_KEY = 'instanceName';
  145. function getDirective(SwiperClass, globalOptions) {
  146. var getStandardisedOptionByAttrs = function (vnode, key) {
  147. var _a, _b, _c, _d;
  148. var value = (_b = (_a = vnode.data) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b[key];
  149. return value !== undefined
  150. ? value
  151. : (_d = (_c = vnode.data) === null || _c === void 0 ? void 0 : _c.attrs) === null || _d === void 0 ? void 0 : _d[kebabcase(key)];
  152. };
  153. // Get swiper instace name in directive
  154. var getSwiperInstanceName = function (element, binding, vnode) {
  155. return (binding.arg ||
  156. getStandardisedOptionByAttrs(vnode, INSTANCE_NAME_KEY) ||
  157. element.id ||
  158. CoreNames.SwiperInstance);
  159. };
  160. var getSwiperInstance = function (element, binding, vnode) {
  161. var instanceName = getSwiperInstanceName(element, binding, vnode);
  162. return vnode.context[instanceName] || null;
  163. };
  164. var getSwipeOptions = function (binding) {
  165. return binding.value || globalOptions;
  166. };
  167. var getBooleanValueByInput = function (input) {
  168. return [true, undefined, null, ''].includes(input);
  169. };
  170. // Emit event in Vue directive
  171. var getEventEmiter = function (vnode) {
  172. var _a, _b;
  173. var handlers = ((_a = vnode.data) === null || _a === void 0 ? void 0 : _a.on) || ((_b = vnode.componentOptions) === null || _b === void 0 ? void 0 : _b.listeners);
  174. return function (name) {
  175. var arguments$1 = arguments;
  176. var args = [];
  177. for (var _i = 1; _i < arguments.length; _i++) {
  178. args[_i - 1] = arguments$1[_i];
  179. }
  180. var _a;
  181. var handle = (_a = handlers) === null || _a === void 0 ? void 0 : _a[name];
  182. if (handle) {
  183. handle.fns.apply(handle, args);
  184. }
  185. };
  186. };
  187. return {
  188. // Init
  189. bind: function (element, binding, vnode) {
  190. // auto class name
  191. if (element.className.indexOf(DEFAULT_CLASSES.containerClass) === -1) {
  192. element.className += ((element.className ? ' ' : '') + DEFAULT_CLASSES.containerClass);
  193. }
  194. // bind click event
  195. element.addEventListener('click', function (event) {
  196. var emitEvent = getEventEmiter(vnode);
  197. var swiper = getSwiperInstance(element, binding, vnode);
  198. handleClickSlideEvent(swiper, event, emitEvent);
  199. });
  200. },
  201. // DOM inserted
  202. inserted: function (element, binding, vnode) {
  203. var context = vnode.context;
  204. var swiperOptions = getSwipeOptions(binding);
  205. var instanceName = getSwiperInstanceName(element, binding, vnode);
  206. var emitEvent = getEventEmiter(vnode);
  207. var vueContext = context;
  208. var swiper = vueContext === null || vueContext === void 0 ? void 0 : vueContext[instanceName];
  209. // Swiper will destroy but not delete instance, when used <keep-alive>
  210. if (!swiper || swiper.destroyed) {
  211. swiper = new SwiperClass(element, swiperOptions);
  212. vueContext[instanceName] = swiper;
  213. bindSwiperEvents(swiper, emitEvent);
  214. emitEvent(ComponentEvents.Ready, swiper);
  215. // MARK: Reinstance when the nexttick with <keep-alive>
  216. // Vue.nextTick(instancing) | setTimeout(instancing)
  217. }
  218. },
  219. // On options changed or DOM updated
  220. componentUpdated: function (element, binding, vnode) {
  221. var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
  222. var autoUpdate = getStandardisedOptionByAttrs(vnode, ComponentPropNames.AutoUpdate);
  223. if (getBooleanValueByInput(autoUpdate)) {
  224. var swiper = getSwiperInstance(element, binding, vnode);
  225. if (swiper) {
  226. var swiperOptions = getSwipeOptions(binding);
  227. var isLoop = swiperOptions.loop;
  228. if (isLoop) {
  229. (_b = (_a = swiper) === null || _a === void 0 ? void 0 : _a.loopDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
  230. }
  231. (_c = swiper === null || swiper === void 0 ? void 0 : swiper.update) === null || _c === void 0 ? void 0 : _c.call(swiper);
  232. (_e = (_d = swiper.navigation) === null || _d === void 0 ? void 0 : _d.update) === null || _e === void 0 ? void 0 : _e.call(_d);
  233. (_g = (_f = swiper.pagination) === null || _f === void 0 ? void 0 : _f.render) === null || _g === void 0 ? void 0 : _g.call(_f);
  234. (_j = (_h = swiper.pagination) === null || _h === void 0 ? void 0 : _h.update) === null || _j === void 0 ? void 0 : _j.call(_h);
  235. if (isLoop) {
  236. (_l = (_k = swiper) === null || _k === void 0 ? void 0 : _k.loopCreate) === null || _l === void 0 ? void 0 : _l.call(_k);
  237. (_m = swiper === null || swiper === void 0 ? void 0 : swiper.update) === null || _m === void 0 ? void 0 : _m.call(swiper);
  238. }
  239. }
  240. }
  241. },
  242. // Destroy this directive
  243. unbind: function (element, binding, vnode) {
  244. var _a;
  245. var autoDestroy = getStandardisedOptionByAttrs(vnode, ComponentPropNames.AutoDestroy);
  246. if (getBooleanValueByInput(autoDestroy)) {
  247. var swiper = getSwiperInstance(element, binding, vnode);
  248. if (swiper && swiper.initialized) {
  249. (_a = swiper === null || swiper === void 0 ? void 0 : swiper.destroy) === null || _a === void 0 ? void 0 : _a.call(swiper, getBooleanValueByInput(getStandardisedOptionByAttrs(vnode, ComponentPropNames.DeleteInstanceOnDestroy)), getBooleanValueByInput(getStandardisedOptionByAttrs(vnode, ComponentPropNames.CleanupStylesOnDestroy)));
  250. }
  251. }
  252. }
  253. };
  254. }
  255. /**
  256. * @file vue-awesome-swiper
  257. * @module SwiperComponent
  258. * @author Surmon <https://github.com/surmon-china>
  259. */
  260. var SlotNames;
  261. (function (SlotNames) {
  262. SlotNames["ParallaxBg"] = "parallax-bg";
  263. SlotNames["Pagination"] = "pagination";
  264. SlotNames["Scrollbar"] = "scrollbar";
  265. SlotNames["PrevButton"] = "button-prev";
  266. SlotNames["NextButton"] = "button-next";
  267. })(SlotNames || (SlotNames = {}));
  268. function getSwiperComponent(SwiperClass) {
  269. var _a;
  270. return Vue.extend({
  271. name: CoreNames.SwiperComponent,
  272. props: (_a = {
  273. defaultOptions: {
  274. type: Object,
  275. required: false,
  276. default: function () { return ({}); }
  277. },
  278. // eslint-disable-next-line vue/require-default-prop
  279. options: {
  280. type: Object,
  281. required: false
  282. }
  283. },
  284. _a[ComponentPropNames.AutoUpdate] = {
  285. type: Boolean,
  286. default: true
  287. },
  288. // https://github.com/surmon-china/vue-awesome-swiper/pull/550/files
  289. _a[ComponentPropNames.AutoDestroy] = {
  290. type: Boolean,
  291. default: true
  292. },
  293. // https://github.com/surmon-china/vue-awesome-swiper/pull/388
  294. _a[ComponentPropNames.DeleteInstanceOnDestroy] = {
  295. type: Boolean,
  296. required: false,
  297. default: true
  298. },
  299. _a[ComponentPropNames.CleanupStylesOnDestroy] = {
  300. type: Boolean,
  301. required: false,
  302. default: true
  303. },
  304. _a),
  305. data: function () {
  306. var _a;
  307. return _a = {},
  308. _a[CoreNames.SwiperInstance] = null,
  309. _a;
  310. },
  311. computed: {
  312. swiperInstance: {
  313. cache: false,
  314. set: function (swiper) {
  315. this[CoreNames.SwiperInstance] = swiper;
  316. },
  317. get: function () {
  318. return this[CoreNames.SwiperInstance];
  319. }
  320. },
  321. swiperOptions: function () {
  322. return this.options || this.defaultOptions;
  323. },
  324. wrapperClass: function () {
  325. return this.swiperOptions.wrapperClass || DEFAULT_CLASSES.wrapperClass;
  326. }
  327. },
  328. methods: {
  329. // Feature: click event
  330. handleSwiperClick: function (event) {
  331. handleClickSlideEvent(this.swiperInstance, event, this.$emit.bind(this));
  332. },
  333. autoReLoopSwiper: function () {
  334. var _a, _b;
  335. if (this.swiperInstance && this.swiperOptions.loop) {
  336. // https://github.com/surmon-china/vue-awesome-swiper/issues/593
  337. // https://github.com/surmon-china/vue-awesome-swiper/issues/544
  338. // https://github.com/surmon-china/vue-awesome-swiper/pull/545/files
  339. var swiper = this.swiperInstance;
  340. (_a = swiper === null || swiper === void 0 ? void 0 : swiper.loopDestroy) === null || _a === void 0 ? void 0 : _a.call(swiper);
  341. (_b = swiper === null || swiper === void 0 ? void 0 : swiper.loopCreate) === null || _b === void 0 ? void 0 : _b.call(swiper);
  342. }
  343. },
  344. updateSwiper: function () {
  345. var _a, _b, _c, _d, _e, _f, _g, _h;
  346. if (this[ComponentPropNames.AutoUpdate] && this.swiperInstance) {
  347. this.autoReLoopSwiper();
  348. (_b = (_a = this.swiperInstance) === null || _a === void 0 ? void 0 : _a.update) === null || _b === void 0 ? void 0 : _b.call(_a);
  349. (_d = (_c = this.swiperInstance.navigation) === null || _c === void 0 ? void 0 : _c.update) === null || _d === void 0 ? void 0 : _d.call(_c);
  350. (_f = (_e = this.swiperInstance.pagination) === null || _e === void 0 ? void 0 : _e.render) === null || _f === void 0 ? void 0 : _f.call(_e);
  351. (_h = (_g = this.swiperInstance.pagination) === null || _g === void 0 ? void 0 : _g.update) === null || _h === void 0 ? void 0 : _h.call(_g);
  352. }
  353. },
  354. destroySwiper: function () {
  355. var _a, _b;
  356. if (this[ComponentPropNames.AutoDestroy] && this.swiperInstance) {
  357. // https://github.com/surmon-china/vue-awesome-swiper/pull/341
  358. // https://github.com/surmon-china/vue-awesome-swiper/issues/340
  359. if (this.swiperInstance.initialized) {
  360. (_b = (_a = this.swiperInstance) === null || _a === void 0 ? void 0 : _a.destroy) === null || _b === void 0 ? void 0 : _b.call(_a, this[ComponentPropNames.DeleteInstanceOnDestroy], this[ComponentPropNames.CleanupStylesOnDestroy]);
  361. }
  362. }
  363. },
  364. initSwiper: function () {
  365. this.swiperInstance = new SwiperClass(this.$el, this.swiperOptions);
  366. bindSwiperEvents(this.swiperInstance, this.$emit.bind(this));
  367. this.$emit(ComponentEvents.Ready, this.swiperInstance);
  368. }
  369. },
  370. mounted: function () {
  371. if (!this.swiperInstance) {
  372. this.initSwiper();
  373. }
  374. },
  375. // Update swiper when the parent component activated with `keep-alive`.
  376. activated: function () {
  377. this.updateSwiper();
  378. },
  379. updated: function () {
  380. this.updateSwiper();
  381. },
  382. beforeDestroy: function () {
  383. // https://github.com/surmon-china/vue-awesome-swiper/commit/2924a9d4d3d1cf51c0d46076410b1f804b2b8a43#diff-7f4e0261ac562c0f354cb91a1ca8864f
  384. this.$nextTick(this.destroySwiper);
  385. },
  386. render: function (createElement) {
  387. return createElement('div', {
  388. staticClass: DEFAULT_CLASSES.containerClass,
  389. on: {
  390. click: this.handleSwiperClick
  391. }
  392. }, [
  393. this.$slots[SlotNames.ParallaxBg],
  394. createElement('div', {
  395. class: this.wrapperClass
  396. }, this.$slots.default),
  397. this.$slots[SlotNames.Pagination],
  398. this.$slots[SlotNames.PrevButton],
  399. this.$slots[SlotNames.NextButton],
  400. this.$slots[SlotNames.Scrollbar]
  401. ]);
  402. }
  403. });
  404. }
  405. /**
  406. * @file vue-awesome-swiper
  407. * @module SwiperSlideComponent
  408. * @author Surmon <https://github.com/surmon-china>
  409. */
  410. var SwiperSlideComponent = Vue.extend({
  411. name: CoreNames.SwiperSlideComponent,
  412. computed: {
  413. slideClass: function () {
  414. var _a, _b;
  415. return ((_b = (_a = this.$parent) === null || _a === void 0 ? void 0 : _a.swiperOptions) === null || _b === void 0 ? void 0 : _b.slideClass) || DEFAULT_CLASSES.slideClass;
  416. }
  417. },
  418. methods: {
  419. update: function () {
  420. var _a;
  421. var parent = this.$parent;
  422. // https://github.com/surmon-china/vue-awesome-swiper/issues/632
  423. if (parent[ComponentPropNames.AutoUpdate]) {
  424. (_a = parent === null || parent === void 0 ? void 0 : parent.swiperInstance) === null || _a === void 0 ? void 0 : _a.update();
  425. }
  426. }
  427. },
  428. mounted: function () {
  429. this.update();
  430. },
  431. updated: function () {
  432. this.update();
  433. },
  434. render: function (createElement) {
  435. return createElement('div', {
  436. class: this.slideClass
  437. }, this.$slots.default);
  438. }
  439. });
  440. /**
  441. * @file vue-awesome-swiper
  442. * @module exporter
  443. * @author Surmon <https://github.com/surmon-china>
  444. */
  445. var getInstaller = function (SwiperClass) {
  446. var install = function (Vue, globalOptions) {
  447. if (install.installed)
  448. { return; }
  449. var SwiperComponent = getSwiperComponent(SwiperClass);
  450. if (globalOptions) {
  451. SwiperComponent.options.props.defaultOptions.default = function () { return globalOptions; };
  452. }
  453. Vue.component(CoreNames.SwiperComponent, SwiperComponent);
  454. Vue.component(CoreNames.SwiperSlideComponent, SwiperSlideComponent);
  455. Vue.directive(CoreNames.SwiperDirective, getDirective(SwiperClass, globalOptions));
  456. install.installed = true;
  457. };
  458. return install;
  459. };
  460. function exporter(SwiperClass) {
  461. var _a;
  462. return _a = {
  463. version: '4.1.1',
  464. install: getInstaller(SwiperClass),
  465. directive: getDirective(SwiperClass)
  466. },
  467. _a[CoreNames.SwiperComponent] = getSwiperComponent(SwiperClass),
  468. _a[CoreNames.SwiperSlideComponent] = SwiperSlideComponent,
  469. _a;
  470. }
  471. export default exporter;