splide-utils.esm.js 7.1 KB

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