splide-utils.esm.js 7.1 KB

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