splide-utils.cjs.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function empty(array) {
  4. array.length = 0;
  5. }
  6. function slice(arrayLike, start, end) {
  7. return Array.prototype.slice.call(arrayLike, start, end);
  8. }
  9. function find(arrayLike, predicate) {
  10. return slice(arrayLike).filter(predicate)[0];
  11. }
  12. function apply(func) {
  13. return func.bind(null, ...slice(arguments, 1));
  14. }
  15. const nextTick = setTimeout;
  16. const noop = () => {
  17. };
  18. function raf(func) {
  19. return requestAnimationFrame(func);
  20. }
  21. function typeOf(type, subject) {
  22. return typeof subject === type;
  23. }
  24. function isObject(subject) {
  25. return !isNull(subject) && typeOf("object", subject);
  26. }
  27. const isArray = Array.isArray;
  28. const isFunction = apply(typeOf, "function");
  29. const isString = apply(typeOf, "string");
  30. const isUndefined = apply(typeOf, "undefined");
  31. function isNull(subject) {
  32. return subject === null;
  33. }
  34. function isHTMLElement(subject) {
  35. return subject instanceof HTMLElement;
  36. }
  37. function isHTMLButtonElement(subject) {
  38. return subject instanceof HTMLButtonElement;
  39. }
  40. function toArray(value) {
  41. return isArray(value) ? value : [value];
  42. }
  43. function forEach(values, iteratee) {
  44. toArray(values).forEach(iteratee);
  45. }
  46. function includes(array, value) {
  47. return array.indexOf(value) > -1;
  48. }
  49. function push(array, items) {
  50. array.push(...toArray(items));
  51. return array;
  52. }
  53. function toggleClass(elm, classes, add) {
  54. if (elm) {
  55. forEach(classes, (name) => {
  56. if (name) {
  57. elm.classList[add ? "add" : "remove"](name);
  58. }
  59. });
  60. }
  61. }
  62. function addClass(elm, classes) {
  63. toggleClass(elm, isString(classes) ? classes.split(" ") : classes, true);
  64. }
  65. function append(parent, children) {
  66. forEach(children, parent.appendChild.bind(parent));
  67. }
  68. function before(nodes, ref) {
  69. forEach(nodes, (node) => {
  70. const parent = (ref || node).parentNode;
  71. if (parent) {
  72. parent.insertBefore(node, ref);
  73. }
  74. });
  75. }
  76. function matches(elm, selector) {
  77. return isHTMLElement(elm) && (elm["msMatchesSelector"] || elm.matches).call(elm, selector);
  78. }
  79. function children(parent, selector) {
  80. const children2 = parent ? slice(parent.children) : [];
  81. return selector ? children2.filter((child) => matches(child, selector)) : children2;
  82. }
  83. function child(parent, selector) {
  84. return selector ? children(parent, selector)[0] : parent.firstElementChild;
  85. }
  86. const ownKeys = Object.keys;
  87. function forOwn(object, iteratee, right) {
  88. if (object) {
  89. (right ? ownKeys(object).reverse() : ownKeys(object)).forEach((key) => {
  90. key !== "__proto__" && iteratee(object[key], key);
  91. });
  92. }
  93. return object;
  94. }
  95. function assign(object) {
  96. slice(arguments, 1).forEach((source) => {
  97. forOwn(source, (value, key) => {
  98. object[key] = source[key];
  99. });
  100. });
  101. return object;
  102. }
  103. function merge(object) {
  104. slice(arguments, 1).forEach((source) => {
  105. forOwn(source, (value, key) => {
  106. if (isArray(value)) {
  107. object[key] = value.slice();
  108. } else if (isObject(value)) {
  109. object[key] = merge({}, isObject(object[key]) ? object[key] : {}, value);
  110. } else {
  111. object[key] = value;
  112. }
  113. });
  114. });
  115. return object;
  116. }
  117. function omit(object, keys) {
  118. forEach(keys || ownKeys(object), (key) => {
  119. delete object[key];
  120. });
  121. }
  122. function removeAttribute(elms, attrs) {
  123. forEach(elms, (elm) => {
  124. forEach(attrs, (attr) => {
  125. elm && elm.removeAttribute(attr);
  126. });
  127. });
  128. }
  129. function setAttribute(elms, attrs, value) {
  130. if (isObject(attrs)) {
  131. forOwn(attrs, (value2, name) => {
  132. setAttribute(elms, name, value2);
  133. });
  134. } else {
  135. forEach(elms, (elm) => {
  136. isNull(value) || value === "" ? removeAttribute(elm, attrs) : elm.setAttribute(attrs, String(value));
  137. });
  138. }
  139. }
  140. function create(tag, attrs, parent) {
  141. const elm = document.createElement(tag);
  142. if (attrs) {
  143. isString(attrs) ? addClass(elm, attrs) : setAttribute(elm, attrs);
  144. }
  145. parent && append(parent, elm);
  146. return elm;
  147. }
  148. function style(elm, prop, value) {
  149. if (isUndefined(value)) {
  150. return getComputedStyle(elm)[prop];
  151. }
  152. if (!isNull(value)) {
  153. elm.style[prop] = `${value}`;
  154. }
  155. }
  156. function display(elm, display2) {
  157. style(elm, "display", display2);
  158. }
  159. function focus(elm) {
  160. elm["setActive"] && elm["setActive"]() || elm.focus({ preventScroll: true });
  161. }
  162. function getAttribute(elm, attr) {
  163. return elm.getAttribute(attr);
  164. }
  165. function hasClass(elm, className) {
  166. return elm && elm.classList.contains(className);
  167. }
  168. function rect(target) {
  169. return target.getBoundingClientRect();
  170. }
  171. function remove(nodes) {
  172. forEach(nodes, (node) => {
  173. if (node && node.parentNode) {
  174. node.parentNode.removeChild(node);
  175. }
  176. });
  177. }
  178. function measure(parent, value) {
  179. if (isString(value)) {
  180. const div = create("div", { style: `width: ${value}; position: absolute;` }, parent);
  181. value = rect(div).width;
  182. remove(div);
  183. }
  184. return value;
  185. }
  186. function parseHtml(html) {
  187. return child(new DOMParser().parseFromString(html, "text/html").body);
  188. }
  189. function prevent(e, stopPropagation) {
  190. e.preventDefault();
  191. if (stopPropagation) {
  192. e.stopPropagation();
  193. e.stopImmediatePropagation();
  194. }
  195. }
  196. function query(parent, selector) {
  197. return parent && parent.querySelector(selector);
  198. }
  199. function queryAll(parent, selector) {
  200. return selector ? slice(parent.querySelectorAll(selector)) : [];
  201. }
  202. function removeClass(elm, classes) {
  203. toggleClass(elm, classes, false);
  204. }
  205. function timeOf(e) {
  206. return e.timeStamp;
  207. }
  208. function unit(value) {
  209. return isString(value) ? value : value ? `${value}px` : "";
  210. }
  211. const PROJECT_CODE = "splide";
  212. function assert(condition, message) {
  213. if (!condition) {
  214. throw new Error(`[${PROJECT_CODE}] ${message || ""}`);
  215. }
  216. }
  217. function error(message) {
  218. console.error(`[${PROJECT_CODE}] ${message}`);
  219. }
  220. const { min, max, floor, ceil, abs } = Math;
  221. function approximatelyEqual(x, y, epsilon) {
  222. return abs(x - y) < epsilon;
  223. }
  224. function between(number, x, y, exclusive) {
  225. const minimum = min(x, y);
  226. const maximum = max(x, y);
  227. return exclusive ? minimum < number && number < maximum : minimum <= number && number <= maximum;
  228. }
  229. function clamp(number, x, y) {
  230. const minimum = min(x, y);
  231. const maximum = max(x, y);
  232. return min(max(minimum, number), maximum);
  233. }
  234. function sign(x) {
  235. return +(x > 0) - +(x < 0);
  236. }
  237. function camelToKebab(string) {
  238. return string.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
  239. }
  240. function format(string, replacements) {
  241. forEach(replacements, (replacement) => {
  242. string = string.replace("%s", `${replacement}`);
  243. });
  244. return string;
  245. }
  246. function pad(number) {
  247. return number < 10 ? `0${number}` : `${number}`;
  248. }
  249. const ids = {};
  250. function uniqueId(prefix) {
  251. return `${prefix}${pad(ids[prefix] = (ids[prefix] || 0) + 1)}`;
  252. }
  253. exports.abs = abs;
  254. exports.addClass = addClass;
  255. exports.append = append;
  256. exports.apply = apply;
  257. exports.approximatelyEqual = approximatelyEqual;
  258. exports.assert = assert;
  259. exports.assign = assign;
  260. exports.before = before;
  261. exports.between = between;
  262. exports.camelToKebab = camelToKebab;
  263. exports.ceil = ceil;
  264. exports.child = child;
  265. exports.children = children;
  266. exports.clamp = clamp;
  267. exports.create = create;
  268. exports.display = display;
  269. exports.empty = empty;
  270. exports.error = error;
  271. exports.find = find;
  272. exports.floor = floor;
  273. exports.focus = focus;
  274. exports.forEach = forEach;
  275. exports.forOwn = forOwn;
  276. exports.format = format;
  277. exports.getAttribute = getAttribute;
  278. exports.hasClass = hasClass;
  279. exports.includes = includes;
  280. exports.isArray = isArray;
  281. exports.isFunction = isFunction;
  282. exports.isHTMLButtonElement = isHTMLButtonElement;
  283. exports.isHTMLElement = isHTMLElement;
  284. exports.isNull = isNull;
  285. exports.isObject = isObject;
  286. exports.isString = isString;
  287. exports.isUndefined = isUndefined;
  288. exports.matches = matches;
  289. exports.max = max;
  290. exports.measure = measure;
  291. exports.merge = merge;
  292. exports.min = min;
  293. exports.nextTick = nextTick;
  294. exports.noop = noop;
  295. exports.omit = omit;
  296. exports.ownKeys = ownKeys;
  297. exports.pad = pad;
  298. exports.parseHtml = parseHtml;
  299. exports.prevent = prevent;
  300. exports.push = push;
  301. exports.query = query;
  302. exports.queryAll = queryAll;
  303. exports.raf = raf;
  304. exports.rect = rect;
  305. exports.remove = remove;
  306. exports.removeAttribute = removeAttribute;
  307. exports.removeClass = removeClass;
  308. exports.setAttribute = setAttribute;
  309. exports.sign = sign;
  310. exports.slice = slice;
  311. exports.style = style;
  312. exports.timeOf = timeOf;
  313. exports.toArray = toArray;
  314. exports.toggleClass = toggleClass;
  315. exports.uniqueId = uniqueId;
  316. exports.unit = unit;