splide-utils.cjs.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. let keys = ownKeys(object);
  90. keys = right ? keys.reverse() : keys;
  91. for (let i = 0; i < keys.length; i++) {
  92. const key = keys[i];
  93. if (key !== "__proto__") {
  94. if (iteratee(object[key], key) === false) {
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. return object;
  101. }
  102. function assign(object) {
  103. slice(arguments, 1).forEach((source) => {
  104. forOwn(source, (value, key) => {
  105. object[key] = source[key];
  106. });
  107. });
  108. return object;
  109. }
  110. function merge(object) {
  111. slice(arguments, 1).forEach((source) => {
  112. forOwn(source, (value, key) => {
  113. if (isArray(value)) {
  114. object[key] = value.slice();
  115. } else if (isObject(value)) {
  116. object[key] = merge(isObject(object[key]) ? object[key] : {}, value);
  117. } else {
  118. object[key] = value;
  119. }
  120. });
  121. });
  122. return object;
  123. }
  124. function omit(object, keys) {
  125. toArray(keys || ownKeys(object)).forEach((key) => {
  126. delete object[key];
  127. });
  128. }
  129. function removeAttribute(elms, attrs) {
  130. forEach(elms, (elm) => {
  131. forEach(attrs, (attr) => {
  132. elm && elm.removeAttribute(attr);
  133. });
  134. });
  135. }
  136. function setAttribute(elms, attrs, value) {
  137. if (isObject(attrs)) {
  138. forOwn(attrs, (value2, name) => {
  139. setAttribute(elms, name, value2);
  140. });
  141. } else {
  142. forEach(elms, (elm) => {
  143. isNull(value) || value === "" ? removeAttribute(elm, attrs) : elm.setAttribute(attrs, String(value));
  144. });
  145. }
  146. }
  147. function create(tag, attrs, parent) {
  148. const elm = document.createElement(tag);
  149. if (attrs) {
  150. isString(attrs) ? addClass(elm, attrs) : setAttribute(elm, attrs);
  151. }
  152. parent && append(parent, elm);
  153. return elm;
  154. }
  155. function style(elm, prop, value) {
  156. if (isUndefined(value)) {
  157. return getComputedStyle(elm)[prop];
  158. }
  159. if (!isNull(value)) {
  160. elm.style[prop] = `${value}`;
  161. }
  162. }
  163. function display(elm, display2) {
  164. style(elm, "display", display2);
  165. }
  166. function focus(elm) {
  167. elm["setActive"] && elm["setActive"]() || elm.focus({ preventScroll: true });
  168. }
  169. function getAttribute(elm, attr) {
  170. return elm.getAttribute(attr);
  171. }
  172. function hasClass(elm, className) {
  173. return elm && elm.classList.contains(className);
  174. }
  175. function rect(target) {
  176. return target.getBoundingClientRect();
  177. }
  178. function remove(nodes) {
  179. forEach(nodes, (node) => {
  180. if (node && node.parentNode) {
  181. node.parentNode.removeChild(node);
  182. }
  183. });
  184. }
  185. function measure(parent, value) {
  186. if (isString(value)) {
  187. const div = create("div", { style: `width: ${value}; position: absolute;` }, parent);
  188. value = rect(div).width;
  189. remove(div);
  190. }
  191. return value;
  192. }
  193. function parseHtml(html) {
  194. return child(new DOMParser().parseFromString(html, "text/html").body);
  195. }
  196. function prevent(e, stopPropagation) {
  197. e.preventDefault();
  198. if (stopPropagation) {
  199. e.stopPropagation();
  200. e.stopImmediatePropagation();
  201. }
  202. }
  203. function query(parent, selector) {
  204. return parent && parent.querySelector(selector);
  205. }
  206. function queryAll(parent, selector) {
  207. return selector ? slice(parent.querySelectorAll(selector)) : [];
  208. }
  209. function removeClass(elm, classes) {
  210. toggleClass(elm, classes, false);
  211. }
  212. function timeOf(e) {
  213. return e.timeStamp;
  214. }
  215. function unit(value) {
  216. return isString(value) ? value : value ? `${value}px` : "";
  217. }
  218. const PROJECT_CODE = "splide";
  219. function assert(condition, message) {
  220. if (!condition) {
  221. throw new Error(`[${PROJECT_CODE}] ${message || ""}`);
  222. }
  223. }
  224. function error(message) {
  225. console.error(`[${PROJECT_CODE}] ${message}`);
  226. }
  227. const { min, max, floor, ceil, abs } = Math;
  228. function approximatelyEqual(x, y, epsilon) {
  229. return abs(x - y) < epsilon;
  230. }
  231. function between(number, minOrMax, maxOrMin, exclusive) {
  232. const minimum = min(minOrMax, maxOrMin);
  233. const maximum = max(minOrMax, maxOrMin);
  234. return exclusive ? minimum < number && number < maximum : minimum <= number && number <= maximum;
  235. }
  236. function clamp(number, x, y) {
  237. const minimum = min(x, y);
  238. const maximum = max(x, y);
  239. return min(max(minimum, number), maximum);
  240. }
  241. function sign(x) {
  242. return +(x > 0) - +(x < 0);
  243. }
  244. function camelToKebab(string) {
  245. return string.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
  246. }
  247. function format(string, replacements) {
  248. forEach(replacements, (replacement) => {
  249. string = string.replace("%s", `${replacement}`);
  250. });
  251. return string;
  252. }
  253. function pad(number) {
  254. return number < 10 ? `0${number}` : `${number}`;
  255. }
  256. const ids = {};
  257. function uniqueId(prefix) {
  258. return `${prefix}${pad(ids[prefix] = (ids[prefix] || 0) + 1)}`;
  259. }
  260. exports.abs = abs;
  261. exports.addClass = addClass;
  262. exports.append = append;
  263. exports.apply = apply;
  264. exports.approximatelyEqual = approximatelyEqual;
  265. exports.assert = assert;
  266. exports.assign = assign;
  267. exports.before = before;
  268. exports.between = between;
  269. exports.camelToKebab = camelToKebab;
  270. exports.ceil = ceil;
  271. exports.child = child;
  272. exports.children = children;
  273. exports.clamp = clamp;
  274. exports.create = create;
  275. exports.display = display;
  276. exports.empty = empty;
  277. exports.error = error;
  278. exports.find = find;
  279. exports.floor = floor;
  280. exports.focus = focus;
  281. exports.forEach = forEach;
  282. exports.forOwn = forOwn;
  283. exports.format = format;
  284. exports.getAttribute = getAttribute;
  285. exports.hasClass = hasClass;
  286. exports.includes = includes;
  287. exports.isArray = isArray;
  288. exports.isFunction = isFunction;
  289. exports.isHTMLButtonElement = isHTMLButtonElement;
  290. exports.isHTMLElement = isHTMLElement;
  291. exports.isNull = isNull;
  292. exports.isObject = isObject;
  293. exports.isString = isString;
  294. exports.isUndefined = isUndefined;
  295. exports.matches = matches;
  296. exports.max = max;
  297. exports.measure = measure;
  298. exports.merge = merge;
  299. exports.min = min;
  300. exports.nextTick = nextTick;
  301. exports.noop = noop;
  302. exports.omit = omit;
  303. exports.ownKeys = ownKeys;
  304. exports.pad = pad;
  305. exports.parseHtml = parseHtml;
  306. exports.prevent = prevent;
  307. exports.push = push;
  308. exports.query = query;
  309. exports.queryAll = queryAll;
  310. exports.raf = raf;
  311. exports.rect = rect;
  312. exports.remove = remove;
  313. exports.removeAttribute = removeAttribute;
  314. exports.removeClass = removeClass;
  315. exports.setAttribute = setAttribute;
  316. exports.sign = sign;
  317. exports.slice = slice;
  318. exports.style = style;
  319. exports.timeOf = timeOf;
  320. exports.toArray = toArray;
  321. exports.toggleClass = toggleClass;
  322. exports.uniqueId = uniqueId;
  323. exports.unit = unit;