splide-utils.esm.js 7.3 KB

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