'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var core = require('@vuecs/core'); var vue = require('vue'); var smob = require('smob'); function boolableToObject(input) { if (typeof input === 'boolean') { return {}; } return input; } var Component = /*#__PURE__*/ function(Component) { Component["List"] = "list"; Component["ListFooter"] = "listFooter"; Component["ListHeader"] = "listHeader"; Component["ListItem"] = "listItem"; Component["ListBody"] = "listBody"; Component["ListLoading"] = "listLoading"; Component["ListNoMore"] = "listNoMore"; return Component; }({}); var SlotName = /*#__PURE__*/ function(SlotName) { SlotName["DEFAULT"] = "default"; SlotName["FOOTER"] = "footer"; SlotName["HEADER"] = "header"; SlotName["BODY"] = "body"; SlotName["LOADING"] = "loading"; SlotName["NO_MORE"] = "noMore"; SlotName["ITEM"] = "item"; SlotName["ITEM_ACTIONS"] = "itemActions"; SlotName["ITEM_ACTIONS_EXTRA"] = "itemActionsExtra"; return SlotName; }({}); var CSSClassDefault = /*#__PURE__*/ function(CSSClassDefault) { CSSClassDefault["LIST"] = "vc-list"; CSSClassDefault["LIST_BODY"] = "vc-list-body"; CSSClassDefault["LIST_FOOTER"] = "vc-list-footer"; CSSClassDefault["LIST_HEADER"] = "vc-list-header"; CSSClassDefault["LIST_ITEM"] = "vc-list-item"; CSSClassDefault["LIST_LOADING"] = "vc-list-loading"; CSSClassDefault["LIST_NO_MORE"] = "vc-list-no-more"; return CSSClassDefault; }({}); function normalizeListBaseOptions(options, component, defaults = {}) { defaults = defaults || {}; const manager = core.createComponentOptionsManager({ name: component }); return { slotItems: options.slotItems || {}, slotProps: manager.buildOrFail({ key: 'slotProps', value: options.slotProps, alt: defaults.slotProps || {} }), slotPropsBuilt: options.slotPropsBuilt, tag: manager.buildOrFail({ key: 'tag', value: options.tag, alt: defaults.tag || 'div' }), class: core.mergeOption('class', manager.buildOrFail({ key: 'class', value: options.class, alt: [] }), defaults.class || []), props: manager.buildOrFail({ key: 'props', value: options.props, alt: defaults.props || {} }), total: options.total, load: options.load, meta: options.meta || {}, busy: options.busy, itemId: options.itemId, itemKey: options.itemKey, onCreated: options.onCreated, onUpdated: options.onUpdated, onDeleted: options.onDeleted }; } const buildFilterFn = (item, itemId, itemKey)=>{ let filterFn; if (itemId) { filterFn = (el)=>itemId(el) === itemId(item); } if (itemKey) { filterFn = (el)=>{ if (typeof itemKey === 'function') { return el[itemKey(el)] === item[itemKey(item)]; } return el[itemKey] === item[itemKey]; }; } if (!filterFn && core.isObject(item) && core.hasOwnProperty(item, 'id')) { filterFn = (el)=>el['id'] === item['id']; } return filterFn; }; function mergeUnknown(primary, secondary) { if (core.isObject(primary) && core.isObject(secondary)) { return smob.merge({}, primary, secondary); } return primary; } function buildListBaseSlotProps(ctx) { const props = { ...ctx.busy ? { busy: ctx.busy } : {}, ...ctx.meta ? { meta: ctx.meta } : {}, ...ctx.total ? { total: ctx.total } : {}, ...ctx.load ? { load: ctx.load } : {} }; props.created = (item)=>{ if (typeof ctx.data === 'undefined') { if (typeof ctx.onCreated === 'function') { ctx.onCreated(item); } return; } if (Array.isArray(ctx.data)) { const filterFn = buildFilterFn(item, ctx.itemId, ctx.itemKey); if (filterFn) { const index = ctx.data.findIndex(filterFn); if (index === -1) { if (typeof ctx.onCreated === 'function') { ctx.onCreated(item); } } return; } } if (typeof ctx.onCreated === 'function') { ctx.onCreated(item); } }; props.deleted = (item)=>{ if (typeof ctx.data === 'undefined') { if (typeof ctx.onDeleted === 'function' && item) { ctx.onDeleted(item); } return; } if (Array.isArray(ctx.data) && item) { const filterFn = buildFilterFn(item, ctx.itemId, ctx.itemKey); if (filterFn) { const index = ctx.data.findIndex(filterFn); if (index !== -1) { if (typeof ctx.onDeleted === 'function') { if (item) { ctx.onDeleted(item); } else { ctx.onDeleted(ctx.data[index]); } } } return; } } if (typeof ctx.onDeleted === 'function') { if (item) { ctx.onDeleted(item); return; } if (!Array.isArray(ctx.data)) { ctx.onDeleted(ctx.data); } } }; props.updated = (item)=>{ if (typeof ctx.data === 'undefined') { if (typeof ctx.onUpdated === 'function') { ctx.onUpdated(item); } return; } if (Array.isArray(ctx.data)) { const filterFn = buildFilterFn(item, ctx.itemId, ctx.itemKey); if (filterFn) { const index = ctx.data.findIndex(filterFn); if (index !== -1) { if (typeof ctx.onUpdated === 'function') { ctx.onUpdated(mergeUnknown(item, ctx.data[index])); } } return; } if (typeof ctx.onUpdated === 'function') { ctx.onUpdated(item); } return; } if (typeof ctx.onUpdated === 'function') { ctx.onUpdated(mergeUnknown(item, ctx.data)); } }; return props; } function normalizeListItemOptions(input) { const options = normalizeListBaseOptions(input, Component.ListItem, { class: CSSClassDefault.LIST_ITEM, tag: 'li' }); const manager = core.createComponentOptionsManager({ name: Component.ListItem }); return { ...options, data: input.data, content: input.content, index: input.index, icon: manager.buildOrFail({ key: 'icon', value: input.icon, alt: true }), iconTag: manager.buildOrFail({ key: 'iconTag', value: input.iconTag, alt: 'i' }), iconClass: manager.buildOrFail({ key: 'iconClass', value: input.iconClass, alt: [] }), iconProps: input.iconProps || {}, iconWrapper: manager.buildOrFail({ key: 'iconWrapper', value: input.iconWrapper, alt: true }), iconWrapperClass: manager.buildOrFail({ key: 'iconWrapperClass', value: input.iconWrapperClass, alt: [] }), iconWrapperTag: manager.buildOrFail({ key: 'iconWrapperTag', value: input.iconWrapperTag, alt: 'div' }), text: manager.buildOrFail({ key: 'text', value: input.text, alt: true }), textContent: input.textContent, textPropName: manager.buildOrFail({ key: 'textPropName', value: input.textPropName, alt: 'name' }), textWrapper: manager.buildOrFail({ key: 'textWrapper', value: input.textWrapper, alt: true }), textWrapperClass: manager.buildOrFail({ key: 'textWrapperClass', value: input.textWrapperClass, alt: [] }), textWrapperTag: manager.buildOrFail({ key: 'textWrapperTag', value: input.textWrapperTag, alt: 'div' }), actions: manager.buildOrFail({ key: 'actions', value: input.actions, alt: true }), actionsContent: manager.buildOrFail({ key: 'actionsContent', value: input.actionsContent, alt: [] }), actionsWrapper: manager.buildOrFail({ key: 'actionsWrapper', value: input.actionsWrapper, alt: true }), actionsWrapperClass: manager.buildOrFail({ key: 'actionsWrapperClass', value: input.actionsWrapperClass, alt: [] }), actionsWrapperTag: manager.buildOrFail({ key: 'actionsWrapperTag', value: input.actionsWrapperTag, alt: 'div' }) }; } function maybeWrapContent(input, ctx) { if (!ctx.wrap) { return input; } return vue.h(ctx.tag, { class: ctx.class }, [ input ]); } function buildListItem(input) { const options = normalizeListItemOptions(input); const overrideUpdatedFn = (fn, item)=>{ if (typeof item === 'undefined') { return fn(options.data); } if (core.isObject(item) && core.isObject(options.data)) { options.data = smob.merge(item, options.data); } else { options.data = item; } return fn(options.data); }; let slotProps; if (options.slotPropsBuilt) { const { updated, deleted, ...original } = options.slotProps; slotProps = { ...original, data: options.data, index: options.index }; if (updated) { slotProps.updated = (item)=>overrideUpdatedFn(updated, item); } if (deleted) { slotProps.deleted = (item)=>deleted(item || options.data); } } else { const { updated, deleted, ...original } = buildListBaseSlotProps({ ...options, data: options.data }); slotProps = { ...original, data: options.data, index: options.index }; if (updated) { slotProps.updated = (item)=>overrideUpdatedFn(updated, item); } if (deleted) { slotProps.deleted = (item)=>deleted(item || options.data); } } const renderContent = (content)=>vue.h(options.tag, vue.mergeProps({ key: options.index }, { class: options.class }, options.props), [ content || [] ]); const children = {}; if (core.hasNormalizedSlot(SlotName.ITEM, options.slotItems)) { children.slot = core.normalizeSlot(SlotName.ITEM, slotProps, options.slotItems); } else { if (options.icon) { children.icon = maybeWrapContent(vue.h(options.iconTag, vue.mergeProps({ class: options.iconClass }, options.iconProps)), { wrap: options.iconWrapper, tag: options.iconWrapperTag, class: options.iconWrapperClass }); } if (options.text) { let text; if (options.textContent) { text = core.evaluateFnOrValue(options.textContent, options.data, slotProps); } else if (options.textPropName && core.isObject(options.data) && core.hasOwnProperty(options.data, options.textPropName)) { text = options.data[options.textPropName]; } if (text) { children.text = maybeWrapContent(text, { wrap: options.textWrapper, tag: options.textWrapperTag, class: options.textWrapperClass }); } } let actions; if (options.actions) { if (core.hasNormalizedSlot(SlotName.ITEM_ACTIONS, options.slotItems)) { actions = [ core.normalizeSlot(SlotName.ITEM_ACTIONS, slotProps, options.slotItems) ]; } else if (options.actionsContent) { actions = core.evaluateFnOrValue(options.actionsContent, options.data, slotProps); } if (core.hasNormalizedSlot(SlotName.ITEM_ACTIONS_EXTRA, options.slotItems)) { actions = [ actions, core.normalizeSlot(SlotName.ITEM_ACTIONS_EXTRA, slotProps, options.slotItems) ]; } } if (actions) { children.actions = maybeWrapContent(actions, { wrap: options.actionsWrapper, tag: options.actionsWrapperTag, class: options.actionsWrapperClass }); } } if (options.content) { return renderContent(core.evaluateFnOrValue(options.content, options.data, slotProps, children)); } if (children.slot) { return renderContent(children.slot); } const content = [ ...children.icon ? [ children.icon ] : [], ...children.text ? [ children.text ] : [], ...children.actions ? [ children.actions ] : [] ]; return renderContent(content); } function normalizeListBodyOptions(input) { const options = normalizeListBaseOptions(input, Component.ListBody, { class: CSSClassDefault.LIST_BODY, tag: 'ul' }); return { ...options, data: input.data || [], item: input.item }; } function buildListBody(input) { input = input || {}; const options = normalizeListBodyOptions(input); let slotProps; if (options.slotPropsBuilt) { slotProps = { data: options.data, ...options.slotProps }; } else { slotProps = { ...buildListBaseSlotProps(options), data: options.data, ...options.slotProps }; } if (core.hasNormalizedSlot(SlotName.BODY, options.slotItems)) { return vue.h(options.tag, vue.mergeProps({ class: options.class }, options.props), core.normalizeSlot(SlotName.BODY, slotProps, options.slotItems)); } // ---------------------------------------------------------------------- if (options.data.length === 0) { return []; } return vue.h(options.tag, vue.mergeProps({ class: options.class }, options.props), options.data.map((item, index)=>buildListItem({ slotProps, slotPropsBuilt: true, slotItems: options.slotItems, ...options.item, data: item, index, busy: options.busy }))); } function normalizeListFooterOptions(input) { const options = normalizeListBaseOptions(input, Component.ListFooter, { class: CSSClassDefault.LIST_FOOTER }); const manager = core.createComponentOptionsManager({ name: Component.ListFooter }); return { ...options, content: manager.buildOrFail({ key: 'content', value: input.content, alt: [] }) }; } function buildListFooter(input) { input = input || {}; const options = normalizeListFooterOptions(input); let slotProps; if (options.slotPropsBuilt) { slotProps = options.slotProps; } else { slotProps = { ...buildListBaseSlotProps(options), ...options.slotProps }; } let children = []; if (core.hasNormalizedSlot(SlotName.FOOTER, options.slotItems)) { children = [ core.normalizeSlot(SlotName.FOOTER, slotProps, options.slotItems) ]; } else if (options.content) { if (typeof options.content === 'function') { children = [ options.content(slotProps) ]; } else { children = [ options.content ]; } } if (children.length > 0) { return vue.h(options.tag, vue.mergeProps({ class: options.class }, options.props), [ children ]); } return []; } function normalizeListHeaderOptions(input) { const options = normalizeListBaseOptions(input, Component.ListHeader, { class: CSSClassDefault.LIST_HEADER }); const manager = core.createComponentOptionsManager({ name: Component.ListHeader }); return { ...options, content: manager.buildOrFail({ key: 'content', value: input.content, alt: [] }) }; } function buildListHeader(input) { input = input || {}; const options = normalizeListHeaderOptions(input); let slotProps; if (options.slotPropsBuilt) { slotProps = options.slotProps; } else { slotProps = { ...buildListBaseSlotProps(options), ...options.slotProps }; } let content = []; if (core.hasNormalizedSlot(SlotName.HEADER, options.slotItems)) { content = [ core.normalizeSlot(SlotName.HEADER, slotProps, options.slotItems) ]; } else if (options.content) { if (typeof options.content === 'function') { content = [ options.content(slotProps) ]; } else { content = [ options.content ]; } } if (content.length > 0) { return vue.h(options.tag, vue.mergeProps({ class: options.class }, options.props), [ content ]); } return []; } function normalizeListLoadingOptions(input) { const options = normalizeListBaseOptions(input, Component.ListLoading, { class: CSSClassDefault.LIST_LOADING }); return { ...options }; } function buildListLoading(input) { input = input || {}; const options = normalizeListLoadingOptions(input); let slotProps; if (options.slotPropsBuilt) { slotProps = options.slotProps; } else { slotProps = { ...buildListBaseSlotProps(options), ...options.slotProps }; } if (!options.busy) { return []; } let content; if (core.hasNormalizedSlot(SlotName.LOADING, options.slotItems)) { content = core.normalizeSlot(SlotName.LOADING, slotProps, options.slotItems); } if (options.content) { if (typeof options.content === 'function') { content = [ options.content(slotProps) ]; } else { content = [ options.content ]; } } if (content) { return vue.h(options.tag, vue.mergeProps({ class: options.class }, options.props), [ content ]); } return []; } function normalizeListNoMoreOptions(input) { const options = normalizeListBaseOptions(input, Component.ListNoMore, { class: CSSClassDefault.LIST_NO_MORE }); const manager = core.createComponentOptionsManager({ name: Component.ListNoMore }); return { ...options, content: manager.buildOrFail({ key: 'content', value: input.content, alt: 'No more items available...' }) }; } function buildListNoMore(input = {}) { const options = normalizeListNoMoreOptions(input); if (options.busy) { return []; } if (typeof options.total === 'number') { if (options.total > 0) { return []; } } else if (core.isObject(options.meta) && core.hasOwnProperty(options.meta, 'total') && typeof options.meta.total === 'number' && options.meta.total > 0) { return []; } let slotProps; if (options.slotPropsBuilt) { slotProps = options.slotProps; } else { slotProps = { ...buildListBaseSlotProps(options), ...options.slotProps }; } const renderContent = (content)=>vue.h(options.tag, vue.mergeProps({ class: options.class }, options.props), [ content ]); if (core.hasNormalizedSlot(SlotName.NO_MORE, options.slotItems)) { return renderContent(core.normalizeSlot(SlotName.NO_MORE, slotProps, options.slotItems)); } if (typeof options.content === 'function') { return renderContent(options.content(slotProps)); } return renderContent(options.content); } function normalizeListOptions(input) { const options = normalizeListBaseOptions(input, Component.List, { class: CSSClassDefault.LIST }); return { ...options, data: input.data ?? [], header: input.header ?? true, footer: input.footer ?? true, body: input.body ?? true, loading: input.loading ?? true, noMore: input.noMore ?? true }; } function buildList(input) { const options = normalizeListOptions(input); if (typeof options.total === 'undefined') { options.total = options.data.length; } const buildSlotProps = (props)=>({ ...props, ...buildListBaseSlotProps(options) }); const extendChildOptions = (input)=>{ input.slotItems = options.slotItems; input.slotProps = buildSlotProps(options.slotProps); input.slotPropsBuilt = true; input.busy = options.busy; input.meta = options.meta; input.total = options.total; return input; }; if (core.hasNormalizedSlot(SlotName.DEFAULT, options.slotItems)) { const slotProps = { ...buildSlotProps(options.slotProps), data: vue.unref(options.data) }; return core.normalizeSlot(SlotName.DEFAULT, slotProps, options.slotItems); } const children = []; if (options.header) { children.push(buildListHeader(extendChildOptions(boolableToObject(options.header)))); } if (options.body) { let childOptions; if (typeof options.body === 'boolean') { childOptions = { data: options.data, busy: options.busy }; } else { childOptions = options.body; childOptions.data = options.data; childOptions.busy = options.busy; } children.push(buildListBody(extendChildOptions(childOptions))); } if (options.loading) { children.push(buildListLoading(extendChildOptions(boolableToObject(options.loading)))); } if (options.noMore) { children.push(buildListNoMore(extendChildOptions(boolableToObject(options.noMore)))); } if (options.footer) { children.push(buildListFooter(extendChildOptions(boolableToObject(options.footer)))); } return vue.h(options.tag, vue.mergeProps({ class: options.class }, options.props), children); } function install(instance, options = {}) { const storeManager = core.installStoreManager(instance); if (options.storeManager) { core.applyStoreManagerOptions(storeManager, options.storeManager); } } var index = { install }; exports.CSSClassDefault = CSSClassDefault; exports.Component = Component; exports.SlotName = SlotName; exports.buildList = buildList; exports.buildListBaseSlotProps = buildListBaseSlotProps; exports.buildListBody = buildListBody; exports.buildListFooter = buildListFooter; exports.buildListHeader = buildListHeader; exports.buildListItem = buildListItem; exports.buildListLoading = buildListLoading; exports.buildListNoMore = buildListNoMore; exports.default = index; exports.install = install; exports.normalizeListBaseOptions = normalizeListBaseOptions; exports.normalizeListBodyOptions = normalizeListBodyOptions; exports.normalizeListFooterOptions = normalizeListFooterOptions; exports.normalizeListHeaderOptions = normalizeListHeaderOptions; exports.normalizeListItemOptions = normalizeListItemOptions; exports.normalizeListLoadingOptions = normalizeListLoadingOptions; exports.normalizeListNoMoreOptions = normalizeListNoMoreOptions; exports.normalizeListOptions = normalizeListOptions; //# sourceMappingURL=index.cjs.map