UNPKG

@splidejs/splide-extension-url-hash

Version:
236 lines (199 loc) 5.57 kB
/*! * @splidejs/splide-extension-url-hash * Version : 0.3.0 * License : MIT * Copyright: 2022 Naotoshi Fujita */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function empty(array) { array.length = 0; } function slice$1(arrayLike, start, end) { return Array.prototype.slice.call(arrayLike, start, end); } function apply$1(func) { return func.bind.apply(func, [null].concat(slice$1(arguments, 1))); } function typeOf$1(type, subject) { return typeof subject === type; } var isArray = Array.isArray; apply$1(typeOf$1, "function"); apply$1(typeOf$1, "string"); apply$1(typeOf$1, "undefined"); function toArray(value) { return isArray(value) ? value : [value]; } function forEach(values, iteratee) { toArray(values).forEach(iteratee); } var ownKeys = Object.keys; function forOwn(object, iteratee, right) { if (object) { var keys = ownKeys(object); keys = right ? keys.reverse() : keys; for (var i = 0; i < keys.length; i++) { var key = keys[i]; if (key !== "__proto__") { if (iteratee(object[key], key) === false) { break; } } } } return object; } function assign(object) { slice$1(arguments, 1).forEach(function (source) { forOwn(source, function (value, key) { object[key] = source[key]; }); }); return object; } function EventBinder() { var listeners = []; function bind(targets, events, callback, options) { forEachEvent(targets, events, function (target, event, namespace) { var isEventTarget = ("addEventListener" in target); var remover = isEventTarget ? target.removeEventListener.bind(target, event, callback, options) : target["removeListener"].bind(target, callback); isEventTarget ? target.addEventListener(event, callback, options) : target["addListener"](callback); listeners.push([target, event, namespace, callback, remover]); }); } function unbind(targets, events, callback) { forEachEvent(targets, events, function (target, event, namespace) { listeners = listeners.filter(function (listener) { if (listener[0] === target && listener[1] === event && listener[2] === namespace && (!callback || listener[3] === callback)) { listener[4](); return false; } return true; }); }); } function dispatch(target, type, detail) { var e; var bubbles = true; if (typeof CustomEvent === "function") { e = new CustomEvent(type, { bubbles: bubbles, detail: detail }); } else { e = document.createEvent("CustomEvent"); e.initCustomEvent(type, bubbles, false, detail); } target.dispatchEvent(e); return e; } function forEachEvent(targets, events, iteratee) { forEach(targets, function (target) { target && forEach(events, function (events2) { events2.split(" ").forEach(function (eventNS) { var fragment = eventNS.split("."); iteratee(target, fragment[0], fragment[1]); }); }); }); } function destroy() { listeners.forEach(function (data) { data[4](); }); empty(listeners); } return { bind: bind, unbind: unbind, dispatch: dispatch, destroy: destroy }; } var EVENT_ACTIVE = "active"; var EVENT_DESTROY = "destroy"; function EventInterface(Splide2) { var bus = Splide2 ? Splide2.event.bus : document.createDocumentFragment(); var binder = EventBinder(); function on(events, callback) { binder.bind(bus, toArray(events).join(" "), function (e) { callback.apply(callback, isArray(e.detail) ? e.detail : []); }); } function emit(event) { binder.dispatch(bus, event, slice$1(arguments, 1)); } if (Splide2) { Splide2.event.on(EVENT_DESTROY, binder.destroy); } return assign(binder, { bus: bus, on: on, off: apply$1(binder.unbind, bus), emit: emit }); } function slice(arrayLike, start, end) { return Array.prototype.slice.call(arrayLike, start, end); } function apply(func) { return func.bind(null, ...slice(arguments, 1)); } function typeOf(type, subject) { return typeof subject === type; } apply(typeOf, "function"); apply(typeOf, "string"); apply(typeOf, "undefined"); function getAttribute(elm, attr) { return elm.getAttribute(attr); } const HASH_ATTRIBUTE_NAME = "data-splide-hash"; function URLHash(Splide2, Components2, options) { const { on, bind } = EventInterface(Splide2); const { setIndex, go } = Components2.Controller; function setup() { const index = convertHashToIndex(location.hash); setIndex(index > -1 ? index : options.start || 0); } function mount() { on(EVENT_ACTIVE, onActive); bind(window, "hashchange", onHashChange); } function onActive(Slide) { const hash = getAttribute(Slide.slide, HASH_ATTRIBUTE_NAME); if (hash) { location.hash = hash; } else { if (history) { history.replaceState(null, null, " "); } else { location.hash = ""; } } } function onHashChange() { const index = convertHashToIndex(location.hash); if (index > -1) { go(index); } } function convertHashToIndex(hash) { hash = hash.replace("#", ""); if (hash) { const { slides } = Components2.Elements; for (let i = 0; i < slides.length; i++) { if (getAttribute(slides[i], HASH_ATTRIBUTE_NAME) === hash) { return i; } } } return -1; } return { setup, mount }; } exports.URLHash = URLHash;