|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 4 | + |
| 5 | +function empty(array) { |
| 6 | + array.length = 0; |
| 7 | +} |
| 8 | + |
| 9 | +function slice(arrayLike, start, end) { |
| 10 | + return Array.prototype.slice.call(arrayLike, start, end); |
| 11 | +} |
| 12 | + |
| 13 | +function find(arrayLike, predicate) { |
| 14 | + return slice(arrayLike).filter(predicate)[0]; |
| 15 | +} |
| 16 | + |
| 17 | +function apply(func) { |
| 18 | + return func.bind(null, ...slice(arguments, 1)); |
| 19 | +} |
| 20 | + |
| 21 | +const nextTick = setTimeout; |
| 22 | + |
| 23 | +const noop = () => { |
| 24 | +}; |
| 25 | + |
| 26 | +function raf(func) { |
| 27 | + return requestAnimationFrame(func); |
| 28 | +} |
| 29 | + |
| 30 | +function typeOf(type, subject) { |
| 31 | + return typeof subject === type; |
| 32 | +} |
| 33 | +function isObject(subject) { |
| 34 | + return !isNull(subject) && typeOf("object", subject); |
| 35 | +} |
| 36 | +const isArray = Array.isArray; |
| 37 | +const isFunction = apply(typeOf, "function"); |
| 38 | +const isString = apply(typeOf, "string"); |
| 39 | +const isUndefined = apply(typeOf, "undefined"); |
| 40 | +function isNull(subject) { |
| 41 | + return subject === null; |
| 42 | +} |
| 43 | +function isHTMLElement(subject) { |
| 44 | + return subject instanceof HTMLElement; |
| 45 | +} |
| 46 | +function isHTMLButtonElement(subject) { |
| 47 | + return subject instanceof HTMLButtonElement; |
| 48 | +} |
| 49 | + |
| 50 | +function toArray(value) { |
| 51 | + return isArray(value) ? value : [value]; |
| 52 | +} |
| 53 | + |
| 54 | +function forEach(values, iteratee) { |
| 55 | + toArray(values).forEach(iteratee); |
| 56 | +} |
| 57 | + |
| 58 | +function includes(array, value) { |
| 59 | + return array.indexOf(value) > -1; |
| 60 | +} |
| 61 | + |
| 62 | +function push(array, items) { |
| 63 | + array.push(...toArray(items)); |
| 64 | + return array; |
| 65 | +} |
| 66 | + |
| 67 | +function toggleClass(elm, classes, add) { |
| 68 | + if (elm) { |
| 69 | + forEach(classes, (name) => { |
| 70 | + if (name) { |
| 71 | + elm.classList[add ? "add" : "remove"](name); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function addClass(elm, classes) { |
| 78 | + toggleClass(elm, isString(classes) ? classes.split(" ") : classes, true); |
| 79 | +} |
| 80 | + |
| 81 | +function append(parent, children) { |
| 82 | + forEach(children, parent.appendChild.bind(parent)); |
| 83 | +} |
| 84 | + |
| 85 | +function before(nodes, ref) { |
| 86 | + forEach(nodes, (node) => { |
| 87 | + const parent = (ref || node).parentNode; |
| 88 | + if (parent) { |
| 89 | + parent.insertBefore(node, ref); |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +function matches(elm, selector) { |
| 95 | + return isHTMLElement(elm) && (elm["msMatchesSelector"] || elm.matches).call(elm, selector); |
| 96 | +} |
| 97 | + |
| 98 | +function children(parent, selector) { |
| 99 | + const children2 = parent ? slice(parent.children) : []; |
| 100 | + return selector ? children2.filter((child) => matches(child, selector)) : children2; |
| 101 | +} |
| 102 | + |
| 103 | +function child(parent, selector) { |
| 104 | + return selector ? children(parent, selector)[0] : parent.firstElementChild; |
| 105 | +} |
| 106 | + |
| 107 | +const ownKeys = Object.keys; |
| 108 | + |
| 109 | +function forOwn(object, iteratee, right) { |
| 110 | + if (object) { |
| 111 | + let keys = ownKeys(object); |
| 112 | + keys = right ? keys.reverse() : keys; |
| 113 | + for (let i = 0; i < keys.length; i++) { |
| 114 | + const key = keys[i]; |
| 115 | + if (key !== "__proto__") { |
| 116 | + if (iteratee(object[key], key) === false) { |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return object; |
| 123 | +} |
| 124 | + |
| 125 | +function assign(object) { |
| 126 | + slice(arguments, 1).forEach((source) => { |
| 127 | + forOwn(source, (value, key) => { |
| 128 | + object[key] = source[key]; |
| 129 | + }); |
| 130 | + }); |
| 131 | + return object; |
| 132 | +} |
| 133 | + |
| 134 | +function merge(object) { |
| 135 | + slice(arguments, 1).forEach((source) => { |
| 136 | + forOwn(source, (value, key) => { |
| 137 | + if (isArray(value)) { |
| 138 | + object[key] = value.slice(); |
| 139 | + } else if (isObject(value)) { |
| 140 | + object[key] = merge(isObject(object[key]) ? object[key] : {}, value); |
| 141 | + } else { |
| 142 | + object[key] = value; |
| 143 | + } |
| 144 | + }); |
| 145 | + }); |
| 146 | + return object; |
| 147 | +} |
| 148 | + |
| 149 | +function omit(object, keys) { |
| 150 | + toArray(keys || ownKeys(object)).forEach((key) => { |
| 151 | + delete object[key]; |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +function removeAttribute(elms, attrs) { |
| 156 | + forEach(elms, (elm) => { |
| 157 | + forEach(attrs, (attr) => { |
| 158 | + elm && elm.removeAttribute(attr); |
| 159 | + }); |
| 160 | + }); |
| 161 | +} |
| 162 | + |
| 163 | +function setAttribute(elms, attrs, value) { |
| 164 | + if (isObject(attrs)) { |
| 165 | + forOwn(attrs, (value2, name) => { |
| 166 | + setAttribute(elms, name, value2); |
| 167 | + }); |
| 168 | + } else { |
| 169 | + forEach(elms, (elm) => { |
| 170 | + isNull(value) || value === "" ? removeAttribute(elm, attrs) : elm.setAttribute(attrs, String(value)); |
| 171 | + }); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +function create(tag, attrs, parent) { |
| 176 | + const elm = document.createElement(tag); |
| 177 | + if (attrs) { |
| 178 | + isString(attrs) ? addClass(elm, attrs) : setAttribute(elm, attrs); |
| 179 | + } |
| 180 | + parent && append(parent, elm); |
| 181 | + return elm; |
| 182 | +} |
| 183 | + |
| 184 | +function style(elm, prop, value) { |
| 185 | + if (isUndefined(value)) { |
| 186 | + return getComputedStyle(elm)[prop]; |
| 187 | + } |
| 188 | + if (!isNull(value)) { |
| 189 | + elm.style[prop] = `${value}`; |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +function display(elm, display2) { |
| 194 | + style(elm, "display", display2); |
| 195 | +} |
| 196 | + |
| 197 | +function focus(elm) { |
| 198 | + elm["setActive"] && elm["setActive"]() || elm.focus({ preventScroll: true }); |
| 199 | +} |
| 200 | + |
| 201 | +function getAttribute(elm, attr) { |
| 202 | + return elm.getAttribute(attr); |
| 203 | +} |
| 204 | + |
| 205 | +function hasClass(elm, className) { |
| 206 | + return elm && elm.classList.contains(className); |
| 207 | +} |
| 208 | + |
| 209 | +function rect(target) { |
| 210 | + return target.getBoundingClientRect(); |
| 211 | +} |
| 212 | + |
| 213 | +function remove(nodes) { |
| 214 | + forEach(nodes, (node) => { |
| 215 | + if (node && node.parentNode) { |
| 216 | + node.parentNode.removeChild(node); |
| 217 | + } |
| 218 | + }); |
| 219 | +} |
| 220 | + |
| 221 | +function measure(parent, value) { |
| 222 | + if (isString(value)) { |
| 223 | + const div = create("div", { style: `width: ${value}; position: absolute;` }, parent); |
| 224 | + value = rect(div).width; |
| 225 | + remove(div); |
| 226 | + } |
| 227 | + return value; |
| 228 | +} |
| 229 | + |
| 230 | +function parseHtml(html) { |
| 231 | + return child(new DOMParser().parseFromString(html, "text/html").body); |
| 232 | +} |
| 233 | + |
| 234 | +function prevent(e, stopPropagation) { |
| 235 | + e.preventDefault(); |
| 236 | + if (stopPropagation) { |
| 237 | + e.stopPropagation(); |
| 238 | + e.stopImmediatePropagation(); |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +function query(parent, selector) { |
| 243 | + return parent && parent.querySelector(selector); |
| 244 | +} |
| 245 | + |
| 246 | +function queryAll(parent, selector) { |
| 247 | + return selector ? slice(parent.querySelectorAll(selector)) : []; |
| 248 | +} |
| 249 | + |
| 250 | +function removeClass(elm, classes) { |
| 251 | + toggleClass(elm, classes, false); |
| 252 | +} |
| 253 | + |
| 254 | +function timeOf(e) { |
| 255 | + return e.timeStamp; |
| 256 | +} |
| 257 | + |
| 258 | +function unit(value) { |
| 259 | + return isString(value) ? value : value ? `${value}px` : ""; |
| 260 | +} |
| 261 | + |
| 262 | +const PROJECT_CODE = "splide"; |
| 263 | + |
| 264 | +function assert(condition, message) { |
| 265 | + if (!condition) { |
| 266 | + throw new Error(`[${PROJECT_CODE}] ${message || ""}`); |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +function error(message) { |
| 271 | + console.error(`[${PROJECT_CODE}] ${message}`); |
| 272 | +} |
| 273 | + |
| 274 | +const { min, max, floor, ceil, abs } = Math; |
| 275 | + |
| 276 | +function approximatelyEqual(x, y, epsilon) { |
| 277 | + return abs(x - y) < epsilon; |
| 278 | +} |
| 279 | + |
| 280 | +function between(number, minOrMax, maxOrMin, exclusive) { |
| 281 | + const minimum = min(minOrMax, maxOrMin); |
| 282 | + const maximum = max(minOrMax, maxOrMin); |
| 283 | + return exclusive ? minimum < number && number < maximum : minimum <= number && number <= maximum; |
| 284 | +} |
| 285 | + |
| 286 | +function clamp(number, x, y) { |
| 287 | + const minimum = min(x, y); |
| 288 | + const maximum = max(x, y); |
| 289 | + return min(max(minimum, number), maximum); |
| 290 | +} |
| 291 | + |
| 292 | +function sign(x) { |
| 293 | + return +(x > 0) - +(x < 0); |
| 294 | +} |
| 295 | + |
| 296 | +function camelToKebab(string) { |
| 297 | + return string.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase(); |
| 298 | +} |
| 299 | + |
| 300 | +function format(string, replacements) { |
| 301 | + forEach(replacements, (replacement) => { |
| 302 | + string = string.replace("%s", `${replacement}`); |
| 303 | + }); |
| 304 | + return string; |
| 305 | +} |
| 306 | + |
| 307 | +function pad(number) { |
| 308 | + return number < 10 ? `0${number}` : `${number}`; |
| 309 | +} |
| 310 | + |
| 311 | +const ids = {}; |
| 312 | +function uniqueId(prefix) { |
| 313 | + return `${prefix}${pad(ids[prefix] = (ids[prefix] || 0) + 1)}`; |
| 314 | +} |
| 315 | + |
| 316 | +exports.abs = abs; |
| 317 | +exports.addClass = addClass; |
| 318 | +exports.append = append; |
| 319 | +exports.apply = apply; |
| 320 | +exports.approximatelyEqual = approximatelyEqual; |
| 321 | +exports.assert = assert; |
| 322 | +exports.assign = assign; |
| 323 | +exports.before = before; |
| 324 | +exports.between = between; |
| 325 | +exports.camelToKebab = camelToKebab; |
| 326 | +exports.ceil = ceil; |
| 327 | +exports.child = child; |
| 328 | +exports.children = children; |
| 329 | +exports.clamp = clamp; |
| 330 | +exports.create = create; |
| 331 | +exports.display = display; |
| 332 | +exports.empty = empty; |
| 333 | +exports.error = error; |
| 334 | +exports.find = find; |
| 335 | +exports.floor = floor; |
| 336 | +exports.focus = focus; |
| 337 | +exports.forEach = forEach; |
| 338 | +exports.forOwn = forOwn; |
| 339 | +exports.format = format; |
| 340 | +exports.getAttribute = getAttribute; |
| 341 | +exports.hasClass = hasClass; |
| 342 | +exports.includes = includes; |
| 343 | +exports.isArray = isArray; |
| 344 | +exports.isFunction = isFunction; |
| 345 | +exports.isHTMLButtonElement = isHTMLButtonElement; |
| 346 | +exports.isHTMLElement = isHTMLElement; |
| 347 | +exports.isNull = isNull; |
| 348 | +exports.isObject = isObject; |
| 349 | +exports.isString = isString; |
| 350 | +exports.isUndefined = isUndefined; |
| 351 | +exports.matches = matches; |
| 352 | +exports.max = max; |
| 353 | +exports.measure = measure; |
| 354 | +exports.merge = merge; |
| 355 | +exports.min = min; |
| 356 | +exports.nextTick = nextTick; |
| 357 | +exports.noop = noop; |
| 358 | +exports.omit = omit; |
| 359 | +exports.ownKeys = ownKeys; |
| 360 | +exports.pad = pad; |
| 361 | +exports.parseHtml = parseHtml; |
| 362 | +exports.prevent = prevent; |
| 363 | +exports.push = push; |
| 364 | +exports.query = query; |
| 365 | +exports.queryAll = queryAll; |
| 366 | +exports.raf = raf; |
| 367 | +exports.rect = rect; |
| 368 | +exports.remove = remove; |
| 369 | +exports.removeAttribute = removeAttribute; |
| 370 | +exports.removeClass = removeClass; |
| 371 | +exports.setAttribute = setAttribute; |
| 372 | +exports.sign = sign; |
| 373 | +exports.slice = slice; |
| 374 | +exports.style = style; |
| 375 | +exports.timeOf = timeOf; |
| 376 | +exports.toArray = toArray; |
| 377 | +exports.toggleClass = toggleClass; |
| 378 | +exports.uniqueId = uniqueId; |
| 379 | +exports.unit = unit; |
0 commit comments