utils.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { a as getWindow, g as getDocument } from './ssr-window.esm.mjs';
  2. function classesToTokens(classes) {
  3. if (classes === void 0) {
  4. classes = '';
  5. }
  6. return classes.trim().split(' ').filter(c => !!c.trim());
  7. }
  8. function deleteProps(obj) {
  9. const object = obj;
  10. Object.keys(object).forEach(key => {
  11. try {
  12. object[key] = null;
  13. } catch (e) {
  14. // no getter for object
  15. }
  16. try {
  17. delete object[key];
  18. } catch (e) {
  19. // something got wrong
  20. }
  21. });
  22. }
  23. function nextTick(callback, delay) {
  24. if (delay === void 0) {
  25. delay = 0;
  26. }
  27. return setTimeout(callback, delay);
  28. }
  29. function now() {
  30. return Date.now();
  31. }
  32. function getComputedStyle(el) {
  33. const window = getWindow();
  34. let style;
  35. if (window.getComputedStyle) {
  36. style = window.getComputedStyle(el, null);
  37. }
  38. if (!style && el.currentStyle) {
  39. style = el.currentStyle;
  40. }
  41. if (!style) {
  42. style = el.style;
  43. }
  44. return style;
  45. }
  46. function getTranslate(el, axis) {
  47. if (axis === void 0) {
  48. axis = 'x';
  49. }
  50. const window = getWindow();
  51. let matrix;
  52. let curTransform;
  53. let transformMatrix;
  54. const curStyle = getComputedStyle(el);
  55. if (window.WebKitCSSMatrix) {
  56. curTransform = curStyle.transform || curStyle.webkitTransform;
  57. if (curTransform.split(',').length > 6) {
  58. curTransform = curTransform.split(', ').map(a => a.replace(',', '.')).join(', ');
  59. }
  60. // Some old versions of Webkit choke when 'none' is passed; pass
  61. // empty string instead in this case
  62. transformMatrix = new window.WebKitCSSMatrix(curTransform === 'none' ? '' : curTransform);
  63. } else {
  64. transformMatrix = curStyle.MozTransform || curStyle.OTransform || curStyle.MsTransform || curStyle.msTransform || curStyle.transform || curStyle.getPropertyValue('transform').replace('translate(', 'matrix(1, 0, 0, 1,');
  65. matrix = transformMatrix.toString().split(',');
  66. }
  67. if (axis === 'x') {
  68. // Latest Chrome and webkits Fix
  69. if (window.WebKitCSSMatrix) curTransform = transformMatrix.m41;
  70. // Crazy IE10 Matrix
  71. else if (matrix.length === 16) curTransform = parseFloat(matrix[12]);
  72. // Normal Browsers
  73. else curTransform = parseFloat(matrix[4]);
  74. }
  75. if (axis === 'y') {
  76. // Latest Chrome and webkits Fix
  77. if (window.WebKitCSSMatrix) curTransform = transformMatrix.m42;
  78. // Crazy IE10 Matrix
  79. else if (matrix.length === 16) curTransform = parseFloat(matrix[13]);
  80. // Normal Browsers
  81. else curTransform = parseFloat(matrix[5]);
  82. }
  83. return curTransform || 0;
  84. }
  85. function isObject(o) {
  86. return typeof o === 'object' && o !== null && o.constructor && Object.prototype.toString.call(o).slice(8, -1) === 'Object';
  87. }
  88. function isNode(node) {
  89. // eslint-disable-next-line
  90. if (typeof window !== 'undefined' && typeof window.HTMLElement !== 'undefined') {
  91. return node instanceof HTMLElement;
  92. }
  93. return node && (node.nodeType === 1 || node.nodeType === 11);
  94. }
  95. function extend() {
  96. const to = Object(arguments.length <= 0 ? undefined : arguments[0]);
  97. const noExtend = ['__proto__', 'constructor', 'prototype'];
  98. for (let i = 1; i < arguments.length; i += 1) {
  99. const nextSource = i < 0 || arguments.length <= i ? undefined : arguments[i];
  100. if (nextSource !== undefined && nextSource !== null && !isNode(nextSource)) {
  101. const keysArray = Object.keys(Object(nextSource)).filter(key => noExtend.indexOf(key) < 0);
  102. for (let nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex += 1) {
  103. const nextKey = keysArray[nextIndex];
  104. const desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
  105. if (desc !== undefined && desc.enumerable) {
  106. if (isObject(to[nextKey]) && isObject(nextSource[nextKey])) {
  107. if (nextSource[nextKey].__swiper__) {
  108. to[nextKey] = nextSource[nextKey];
  109. } else {
  110. extend(to[nextKey], nextSource[nextKey]);
  111. }
  112. } else if (!isObject(to[nextKey]) && isObject(nextSource[nextKey])) {
  113. to[nextKey] = {};
  114. if (nextSource[nextKey].__swiper__) {
  115. to[nextKey] = nextSource[nextKey];
  116. } else {
  117. extend(to[nextKey], nextSource[nextKey]);
  118. }
  119. } else {
  120. to[nextKey] = nextSource[nextKey];
  121. }
  122. }
  123. }
  124. }
  125. }
  126. return to;
  127. }
  128. function setCSSProperty(el, varName, varValue) {
  129. el.style.setProperty(varName, varValue);
  130. }
  131. function animateCSSModeScroll(_ref) {
  132. let {
  133. swiper,
  134. targetPosition,
  135. side
  136. } = _ref;
  137. const window = getWindow();
  138. const startPosition = -swiper.translate;
  139. let startTime = null;
  140. let time;
  141. const duration = swiper.params.speed;
  142. swiper.wrapperEl.style.scrollSnapType = 'none';
  143. window.cancelAnimationFrame(swiper.cssModeFrameID);
  144. const dir = targetPosition > startPosition ? 'next' : 'prev';
  145. const isOutOfBound = (current, target) => {
  146. return dir === 'next' && current >= target || dir === 'prev' && current <= target;
  147. };
  148. const animate = () => {
  149. time = new Date().getTime();
  150. if (startTime === null) {
  151. startTime = time;
  152. }
  153. const progress = Math.max(Math.min((time - startTime) / duration, 1), 0);
  154. const easeProgress = 0.5 - Math.cos(progress * Math.PI) / 2;
  155. let currentPosition = startPosition + easeProgress * (targetPosition - startPosition);
  156. if (isOutOfBound(currentPosition, targetPosition)) {
  157. currentPosition = targetPosition;
  158. }
  159. swiper.wrapperEl.scrollTo({
  160. [side]: currentPosition
  161. });
  162. if (isOutOfBound(currentPosition, targetPosition)) {
  163. swiper.wrapperEl.style.overflow = 'hidden';
  164. swiper.wrapperEl.style.scrollSnapType = '';
  165. setTimeout(() => {
  166. swiper.wrapperEl.style.overflow = '';
  167. swiper.wrapperEl.scrollTo({
  168. [side]: currentPosition
  169. });
  170. });
  171. window.cancelAnimationFrame(swiper.cssModeFrameID);
  172. return;
  173. }
  174. swiper.cssModeFrameID = window.requestAnimationFrame(animate);
  175. };
  176. animate();
  177. }
  178. function getSlideTransformEl(slideEl) {
  179. return slideEl.querySelector('.swiper-slide-transform') || slideEl.shadowRoot && slideEl.shadowRoot.querySelector('.swiper-slide-transform') || slideEl;
  180. }
  181. function elementChildren(element, selector) {
  182. if (selector === void 0) {
  183. selector = '';
  184. }
  185. const children = [...element.children];
  186. if (element instanceof HTMLSlotElement) {
  187. children.push(...element.assignedElements());
  188. }
  189. if (!selector) {
  190. return children;
  191. }
  192. return children.filter(el => el.matches(selector));
  193. }
  194. function elementIsChildOf(el, parent) {
  195. const isChild = parent.contains(el);
  196. if (!isChild && parent instanceof HTMLSlotElement) {
  197. const children = [...parent.assignedElements()];
  198. return children.includes(el);
  199. }
  200. return isChild;
  201. }
  202. function showWarning(text) {
  203. try {
  204. console.warn(text);
  205. return;
  206. } catch (err) {
  207. // err
  208. }
  209. }
  210. function createElement(tag, classes) {
  211. if (classes === void 0) {
  212. classes = [];
  213. }
  214. const el = document.createElement(tag);
  215. el.classList.add(...(Array.isArray(classes) ? classes : classesToTokens(classes)));
  216. return el;
  217. }
  218. function elementOffset(el) {
  219. const window = getWindow();
  220. const document = getDocument();
  221. const box = el.getBoundingClientRect();
  222. const body = document.body;
  223. const clientTop = el.clientTop || body.clientTop || 0;
  224. const clientLeft = el.clientLeft || body.clientLeft || 0;
  225. const scrollTop = el === window ? window.scrollY : el.scrollTop;
  226. const scrollLeft = el === window ? window.scrollX : el.scrollLeft;
  227. return {
  228. top: box.top + scrollTop - clientTop,
  229. left: box.left + scrollLeft - clientLeft
  230. };
  231. }
  232. function elementPrevAll(el, selector) {
  233. const prevEls = [];
  234. while (el.previousElementSibling) {
  235. const prev = el.previousElementSibling; // eslint-disable-line
  236. if (selector) {
  237. if (prev.matches(selector)) prevEls.push(prev);
  238. } else prevEls.push(prev);
  239. el = prev;
  240. }
  241. return prevEls;
  242. }
  243. function elementNextAll(el, selector) {
  244. const nextEls = [];
  245. while (el.nextElementSibling) {
  246. const next = el.nextElementSibling; // eslint-disable-line
  247. if (selector) {
  248. if (next.matches(selector)) nextEls.push(next);
  249. } else nextEls.push(next);
  250. el = next;
  251. }
  252. return nextEls;
  253. }
  254. function elementStyle(el, prop) {
  255. const window = getWindow();
  256. return window.getComputedStyle(el, null).getPropertyValue(prop);
  257. }
  258. function elementIndex(el) {
  259. let child = el;
  260. let i;
  261. if (child) {
  262. i = 0;
  263. // eslint-disable-next-line
  264. while ((child = child.previousSibling) !== null) {
  265. if (child.nodeType === 1) i += 1;
  266. }
  267. return i;
  268. }
  269. return undefined;
  270. }
  271. function elementParents(el, selector) {
  272. const parents = []; // eslint-disable-line
  273. let parent = el.parentElement; // eslint-disable-line
  274. while (parent) {
  275. if (selector) {
  276. if (parent.matches(selector)) parents.push(parent);
  277. } else {
  278. parents.push(parent);
  279. }
  280. parent = parent.parentElement;
  281. }
  282. return parents;
  283. }
  284. function elementTransitionEnd(el, callback) {
  285. function fireCallBack(e) {
  286. if (e.target !== el) return;
  287. callback.call(el, e);
  288. el.removeEventListener('transitionend', fireCallBack);
  289. }
  290. if (callback) {
  291. el.addEventListener('transitionend', fireCallBack);
  292. }
  293. }
  294. function elementOuterSize(el, size, includeMargins) {
  295. const window = getWindow();
  296. if (includeMargins) {
  297. return el[size === 'width' ? 'offsetWidth' : 'offsetHeight'] + parseFloat(window.getComputedStyle(el, null).getPropertyValue(size === 'width' ? 'margin-right' : 'margin-top')) + parseFloat(window.getComputedStyle(el, null).getPropertyValue(size === 'width' ? 'margin-left' : 'margin-bottom'));
  298. }
  299. return el.offsetWidth;
  300. }
  301. function makeElementsArray(el) {
  302. return (Array.isArray(el) ? el : [el]).filter(e => !!e);
  303. }
  304. function getRotateFix(swiper) {
  305. return v => {
  306. if (Math.abs(v) > 0 && swiper.browser && swiper.browser.need3dFix && Math.abs(v) % 90 === 0) {
  307. return v + 0.001;
  308. }
  309. return v;
  310. };
  311. }
  312. export { elementParents as a, elementOffset as b, createElement as c, now as d, elementChildren as e, elementOuterSize as f, getSlideTransformEl as g, elementIndex as h, classesToTokens as i, getTranslate as j, elementTransitionEnd as k, isObject as l, makeElementsArray as m, nextTick as n, getRotateFix as o, elementStyle as p, elementNextAll as q, elementPrevAll as r, setCSSProperty as s, animateCSSModeScroll as t, showWarning as u, elementIsChildOf as v, extend as w, deleteProps as x };