bootstrap-vue-next
Version:
BootstrapVueNext is an early and lovely component library for Vue 3 & Nuxt 3 based on Bootstrap 5 and Typescript.
424 lines (423 loc) • 11.4 kB
JavaScript
import { computed, toValue, shallowRef, isRef, watch, shallowReadonly, effectScope, getCurrentScope, onScopeDispose, onMounted, nextTick, toRef as toRef$1, readonly, ref, customRef, getCurrentInstance, hasInjectionContext, inject } from "vue";
function tryOnScopeDispose(fn) {
if (getCurrentScope()) {
onScopeDispose(fn);
return true;
}
return false;
}
const localProvidedStateMap = /* @__PURE__ */ new WeakMap();
const injectLocal = (...args) => {
var _a;
const key = args[0];
const instance = (_a = getCurrentInstance()) == null ? void 0 : _a.proxy;
if (instance == null && !hasInjectionContext())
throw new Error("injectLocal must be called in setup");
if (instance && localProvidedStateMap.has(instance) && key in localProvidedStateMap.get(instance))
return localProvidedStateMap.get(instance)[key];
return inject(...args);
};
function createSharedComposable(composable) {
let subscribers = 0;
let state;
let scope;
const dispose = () => {
subscribers -= 1;
if (scope && subscribers <= 0) {
scope.stop();
state = void 0;
scope = void 0;
}
};
return (...args) => {
subscribers += 1;
if (!scope) {
scope = effectScope(true);
state = scope.run(() => composable(...args));
}
tryOnScopeDispose(dispose);
return state;
};
}
function makeDestructurable(obj, arr) {
if (typeof Symbol !== "undefined") {
const clone = { ...obj };
Object.defineProperty(clone, Symbol.iterator, {
enumerable: false,
value() {
let index = 0;
return {
next: () => ({
value: arr[index++],
done: index > arr.length
})
};
}
});
return clone;
} else {
return Object.assign([...arr], obj);
}
}
const isClient = typeof window !== "undefined" && typeof document !== "undefined";
typeof WorkerGlobalScope !== "undefined" && globalThis instanceof WorkerGlobalScope;
const notNullish = (val) => val != null;
const toString = Object.prototype.toString;
const isObject = (val) => toString.call(val) === "[object Object]";
const timestamp = () => +Date.now();
const noop = () => {
};
const isIOS = /* @__PURE__ */ getIsIOS();
function getIsIOS() {
var _a, _b;
return isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && (/iP(?:ad|hone|od)/.test(window.navigator.userAgent) || ((_b = window == null ? void 0 : window.navigator) == null ? void 0 : _b.maxTouchPoints) > 2 && /iPad|Macintosh/.test(window == null ? void 0 : window.navigator.userAgent));
}
function toRef(...args) {
if (args.length !== 1)
return toRef$1(...args);
const r = args[0];
return typeof r === "function" ? readonly(customRef(() => ({ get: r, set: noop }))) : ref(r);
}
function createFilterWrapper(filter, fn) {
function wrapper(...args) {
return new Promise((resolve, reject) => {
Promise.resolve(filter(() => fn.apply(this, args), { fn, thisArg: this, args })).then(resolve).catch(reject);
});
}
return wrapper;
}
const bypassFilter = (invoke) => {
return invoke();
};
function debounceFilter(ms, options = {}) {
let timer;
let maxTimer;
let lastRejector = noop;
const _clearTimeout = (timer2) => {
clearTimeout(timer2);
lastRejector();
lastRejector = noop;
};
let lastInvoker;
const filter = (invoke) => {
const duration = toValue(ms);
const maxDuration = toValue(options.maxWait);
if (timer)
_clearTimeout(timer);
if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) {
if (maxTimer) {
_clearTimeout(maxTimer);
maxTimer = null;
}
return Promise.resolve(invoke());
}
return new Promise((resolve, reject) => {
lastRejector = options.rejectOnCancel ? reject : resolve;
lastInvoker = invoke;
if (maxDuration && !maxTimer) {
maxTimer = setTimeout(() => {
if (timer)
_clearTimeout(timer);
maxTimer = null;
resolve(lastInvoker());
}, maxDuration);
}
timer = setTimeout(() => {
if (maxTimer)
_clearTimeout(maxTimer);
maxTimer = null;
resolve(invoke());
}, duration);
});
};
return filter;
}
function throttleFilter(...args) {
let lastExec = 0;
let timer;
let isLeading = true;
let lastRejector = noop;
let lastValue;
let ms;
let trailing;
let leading;
let rejectOnCancel;
if (!isRef(args[0]) && typeof args[0] === "object")
({ delay: ms, trailing = true, leading = true, rejectOnCancel = false } = args[0]);
else
[ms, trailing = true, leading = true, rejectOnCancel = false] = args;
const clear = () => {
if (timer) {
clearTimeout(timer);
timer = void 0;
lastRejector();
lastRejector = noop;
}
};
const filter = (_invoke) => {
const duration = toValue(ms);
const elapsed = Date.now() - lastExec;
const invoke = () => {
return lastValue = _invoke();
};
clear();
if (duration <= 0) {
lastExec = Date.now();
return invoke();
}
if (elapsed > duration && (leading || !isLeading)) {
lastExec = Date.now();
invoke();
} else if (trailing) {
lastValue = new Promise((resolve, reject) => {
lastRejector = rejectOnCancel ? reject : resolve;
timer = setTimeout(() => {
lastExec = Date.now();
isLeading = true;
resolve(invoke());
clear();
}, Math.max(0, duration - elapsed));
});
}
if (!leading && !timer)
timer = setTimeout(() => isLeading = true, duration);
isLeading = false;
return lastValue;
};
return filter;
}
function pausableFilter(extendFilter = bypassFilter, options = {}) {
const {
initialState = "active"
} = options;
const isActive = toRef(initialState === "active");
function pause() {
isActive.value = false;
}
function resume() {
isActive.value = true;
}
const eventFilter = (...args) => {
if (isActive.value)
extendFilter(...args);
};
return { isActive: readonly(isActive), pause, resume, eventFilter };
}
function increaseWithUnit(target, delta) {
var _a;
if (typeof target === "number")
return target + delta;
const value = ((_a = target.match(/^-?\d+\.?\d*/)) == null ? void 0 : _a[0]) || "";
const unit = target.slice(value.length);
const result = Number.parseFloat(value) + delta;
if (Number.isNaN(result))
return target;
return result + unit;
}
function pxValue(px) {
return px.endsWith("rem") ? Number.parseFloat(px) * 16 : Number.parseFloat(px);
}
function toArray(value) {
return Array.isArray(value) ? value : [value];
}
function cacheStringFunction(fn) {
const cache = /* @__PURE__ */ Object.create(null);
return (str) => {
const hit = cache[str];
return hit || (cache[str] = fn(str));
};
}
const camelizeRE = /-(\w)/g;
const camelize = cacheStringFunction((str) => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : "");
});
function getLifeCycleTarget(target) {
return getCurrentInstance();
}
function useDebounceFn(fn, ms = 200, options = {}) {
return createFilterWrapper(
debounceFilter(ms, options),
fn
);
}
function useThrottleFn(fn, ms = 200, trailing = false, leading = true, rejectOnCancel = false) {
return createFilterWrapper(
throttleFilter(ms, trailing, leading, rejectOnCancel),
fn
);
}
function watchWithFilter(source, cb, options = {}) {
const {
eventFilter = bypassFilter,
...watchOptions
} = options;
return watch(
source,
createFilterWrapper(
eventFilter,
cb
),
watchOptions
);
}
function watchPausable(source, cb, options = {}) {
const {
eventFilter: filter,
initialState = "active",
...watchOptions
} = options;
const { eventFilter, pause, resume, isActive } = pausableFilter(filter, { initialState });
const stop = watchWithFilter(
source,
cb,
{
...watchOptions,
eventFilter
}
);
return { stop, pause, resume, isActive };
}
function syncRef(left, right, ...[options]) {
const {
flush = "sync",
deep = false,
immediate = true,
direction = "both",
transform = {}
} = options || {};
const watchers = [];
const transformLTR = "ltr" in transform && transform.ltr || ((v) => v);
const transformRTL = "rtl" in transform && transform.rtl || ((v) => v);
if (direction === "both" || direction === "ltr") {
watchers.push(watchPausable(
left,
(newValue) => {
watchers.forEach((w) => w.pause());
right.value = transformLTR(newValue);
watchers.forEach((w) => w.resume());
},
{ flush, deep, immediate }
));
}
if (direction === "both" || direction === "rtl") {
watchers.push(watchPausable(
right,
(newValue) => {
watchers.forEach((w) => w.pause());
left.value = transformRTL(newValue);
watchers.forEach((w) => w.resume());
},
{ flush, deep, immediate }
));
}
const stop = () => {
watchers.forEach((w) => w.stop());
};
return stop;
}
function tryOnMounted(fn, sync = true, target) {
const instance = getLifeCycleTarget();
if (instance)
onMounted(fn, target);
else if (sync)
fn();
else
nextTick(fn);
}
function useIntervalFn(cb, interval = 1e3, options = {}) {
const {
immediate = true,
immediateCallback = false
} = options;
let timer = null;
const isActive = shallowRef(false);
function clean() {
if (timer) {
clearInterval(timer);
timer = null;
}
}
function pause() {
isActive.value = false;
clean();
}
function resume() {
const intervalValue = toValue(interval);
if (intervalValue <= 0)
return;
isActive.value = true;
if (immediateCallback)
cb();
clean();
if (isActive.value)
timer = setInterval(cb, intervalValue);
}
if (immediate && isClient)
resume();
if (isRef(interval) || typeof interval === "function") {
const stopWatch = watch(interval, () => {
if (isActive.value && isClient)
resume();
});
tryOnScopeDispose(stopWatch);
}
tryOnScopeDispose(pause);
return {
isActive: shallowReadonly(isActive),
pause,
resume
};
}
function useToNumber(value, options = {}) {
const {
method = "parseFloat",
radix,
nanToZero
} = options;
return computed(() => {
let resolved = toValue(value);
if (typeof method === "function")
resolved = method(resolved);
else if (typeof resolved === "string")
resolved = Number[method](resolved, radix);
if (nanToZero && Number.isNaN(resolved))
resolved = 0;
return resolved;
});
}
function watchImmediate(source, cb, options) {
return watch(
source,
cb,
{
...options,
immediate: true
}
);
}
export {
toRef as a,
toArray as b,
notNullish as c,
tryOnScopeDispose as d,
timestamp as e,
useIntervalFn as f,
isClient as g,
camelize as h,
isIOS as i,
injectLocal as j,
increaseWithUnit as k,
watchPausable as l,
makeDestructurable as m,
noop as n,
isObject as o,
pxValue as p,
useThrottleFn as q,
useDebounceFn as r,
syncRef as s,
tryOnMounted as t,
useToNumber as u,
createSharedComposable as v,
watchImmediate as w
};
//# sourceMappingURL=index-CaguhSuF.mjs.map