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