UNPKG

@oruga-ui/oruga-next

Version:

UI components for Vue.js and CSS framework agnostic

88 lines (87 loc) 3.16 kB
"use strict"; /*! Oruga v0.11.0 | MIT License | github.com/oruga-ui/oruga */ const vue = require("vue"); const unrefElement = require("./unrefElement-BozOaxdL.cjs"); const useDebounce = require("./useDebounce-BAemuDpF.cjs"); const useSequentialId = require("./useSequentialId-BnH6otip.cjs"); function useProviderParent(options) { var _a; const vm = vue.getCurrentInstance(); if (!vm) throw new Error( "useProviderChild must be called within a component setup function." ); const configField = (_a = vm.proxy) == null ? void 0 : _a.$options.configField; const key = (options == null ? void 0 : options.key) || configField; const childItems = vue.ref([]); if (options == null ? void 0 : options.rootRef) { const sortHandler = useDebounce.useDebounce((items) => { const parent = unrefElement.unrefElement(options.rootRef); if (!parent) return; const ids = items.map((item) => `[data-id="${key}-${item.identifier}"]`).join(","); const children = parent.querySelectorAll(ids); const sortedIds = Array.from(children).map( (el) => { var _a2; return (_a2 = el.getAttribute("data-id")) == null ? void 0 : _a2.replace(`${key}-`, ""); } ); items.forEach( (item) => item.index = sortedIds.indexOf(`${item.identifier}`) ); items.sort((a, b) => a.index - b.index); }, 500); vue.watch(childItems, sortHandler); } const { nextSequence } = useSequentialId.useSequentialId(1); function registerItem(data) { const index = childItems.value.length; const identifier = nextSequence(); const item = { index, data, identifier }; childItems.value = [ ...childItems.value, item ]; return item; } function unregisterItem(item) { childItems.value = childItems.value.filter((i) => i !== item); } vue.provide("$o-" + key, { registerItem, unregisterItem, data: options == null ? void 0 : options.data }); return { childItems }; } function useProviderChild(options) { var _a, _b; options = Object.assign({ needParent: true, register: true }, options); const vm = vue.getCurrentInstance(); if (!vm) throw new Error( "useProviderChild must be called within a component setup function." ); const configField = (_a = vm.proxy) == null ? void 0 : _a.$options.configField; const key = (options == null ? void 0 : options.key) || configField; const parent = vue.inject("$o-" + key, void 0); if (options.needParent && !parent) throw new Error( `You should wrap ${(_b = vm.proxy) == null ? void 0 : _b.$options.name} in a ${key} component` ); const item = vue.ref(); if (parent && options.register) item.value = parent.registerItem( options == null ? void 0 : options.data ); vue.onUnmounted(() => { if (parent && item.value) parent.unregisterItem(item.value); }); const data = (parent == null ? void 0 : parent.data) || vue.ref(); return { parent: data, item }; } exports.useProviderChild = useProviderChild; exports.useProviderParent = useProviderParent; //# sourceMappingURL=useParentProvider-CPNahRzD.cjs.map