2023-07-03 22:20:35 +00:00
|
|
|
import { loadBaseStyle } from 'primevue/base';
|
|
|
|
import { ObjectUtils } from 'primevue/utils';
|
|
|
|
import { mergeProps } from 'vue';
|
|
|
|
|
|
|
|
const BaseDirective = {
|
|
|
|
_getMeta: (...args) => [ObjectUtils.isObject(args[0]) ? undefined : args[0], ObjectUtils.getItemValue(ObjectUtils.isObject(args[0]) ? args[0] : args[1])],
|
|
|
|
_getOptionValue: (options, key = '', params = {}) => {
|
2023-07-10 09:34:48 +00:00
|
|
|
const fKeys = ObjectUtils.toFlatCase(key).split('.');
|
2023-07-03 22:20:35 +00:00
|
|
|
const fKey = fKeys.shift();
|
|
|
|
|
|
|
|
return fKey
|
|
|
|
? ObjectUtils.isObject(options)
|
2023-07-10 09:34:48 +00:00
|
|
|
? BaseDirective._getOptionValue(ObjectUtils.getItemValue(options[Object.keys(options).find((k) => ObjectUtils.toFlatCase(k) === fKey) || ''], params), fKeys.join('.'), params)
|
2023-07-03 22:20:35 +00:00
|
|
|
: undefined
|
|
|
|
: ObjectUtils.getItemValue(options, params);
|
|
|
|
},
|
|
|
|
_getPTValue: (instance = {}, obj = {}, key = '', params = {}, searchInDefaultPT = true) => {
|
2023-07-07 12:00:11 +00:00
|
|
|
const datasetPrefix = 'data-pc-';
|
2023-07-03 22:20:35 +00:00
|
|
|
const self = BaseDirective._getOptionValue(obj, key, params);
|
|
|
|
const globalPT = searchInDefaultPT ? BaseDirective._getOptionValue(instance.defaultPT, key, params) : undefined;
|
|
|
|
const merged = mergeProps(self, globalPT, {
|
2023-07-10 09:34:48 +00:00
|
|
|
...(key === 'root' && { [`${datasetPrefix}name`]: ObjectUtils.toFlatCase(instance.$name) }),
|
|
|
|
[`${datasetPrefix}section`]: ObjectUtils.toFlatCase(key)
|
2023-07-03 22:20:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return merged;
|
|
|
|
},
|
|
|
|
_hook: (directiveName, hookName, el, binding, vnode, prevVnode) => {
|
2023-07-10 09:34:48 +00:00
|
|
|
const name = `on${ObjectUtils.toCapitalCase(hookName)}`;
|
2023-07-03 22:20:35 +00:00
|
|
|
const config = binding?.instance?.$primevue?.config;
|
2023-07-10 09:34:48 +00:00
|
|
|
const selfHook = binding?.value?.pt?.hooks?.[name];
|
|
|
|
const globalHook = config?.pt?.directives?.[directiveName]?.hooks?.[name];
|
|
|
|
const options = { el, binding, vnode, prevVnode };
|
2023-07-03 22:20:35 +00:00
|
|
|
|
2023-07-10 09:34:48 +00:00
|
|
|
selfHook?.(el?.$instance, options);
|
|
|
|
globalHook?.(el?.$instance, options);
|
2023-07-03 22:20:35 +00:00
|
|
|
},
|
|
|
|
_extend: (name, options = {}) => {
|
|
|
|
const handleHook = (hook, el, binding, vnode, prevVnode) => {
|
2023-07-03 23:12:09 +00:00
|
|
|
el._$instances = el._$instances || {};
|
2023-07-03 22:20:35 +00:00
|
|
|
|
|
|
|
const config = binding?.instance?.$primevue?.config;
|
2023-07-06 12:43:23 +00:00
|
|
|
const $prevInstance = el._$instances[name] || {};
|
|
|
|
const $options = ObjectUtils.isEmpty($prevInstance) ? { ...options, ...options?.methods } : {};
|
2023-07-03 22:20:35 +00:00
|
|
|
|
2023-07-03 23:12:09 +00:00
|
|
|
el._$instances[name] = {
|
2023-07-06 12:43:23 +00:00
|
|
|
...$prevInstance,
|
2023-07-03 22:20:35 +00:00
|
|
|
/* new instance variables to pass in directive methods */
|
|
|
|
$name: name,
|
|
|
|
$host: el,
|
|
|
|
$binding: binding,
|
2023-07-06 12:43:23 +00:00
|
|
|
$el: $prevInstance['$el'] || undefined,
|
2023-07-03 22:20:35 +00:00
|
|
|
$css: { classes: undefined, inlineStyles: undefined, loadStyle: () => {}, ...options?.css },
|
|
|
|
/* computed instance variables */
|
|
|
|
defaultPT: config?.pt?.directives?.[name],
|
2023-07-07 11:23:03 +00:00
|
|
|
isUnstyled: el.unstyled !== undefined ? el.unstyled : config?.unstyled,
|
2023-07-03 22:20:35 +00:00
|
|
|
/* instance's methods */
|
2023-07-06 12:43:23 +00:00
|
|
|
ptm: (key = '', params = {}) => BaseDirective._getPTValue(el.$instance, el.$instance?.$binding?.value?.pt, key, { ...params }),
|
|
|
|
ptmo: (obj = {}, key = '', params = {}) => BaseDirective._getPTValue(el.$instance, obj, key, params, false),
|
|
|
|
cx: (key = '', params = {}) => (!el.$instance?.isUnstyled ? BaseDirective._getOptionValue(el.$instance?.$css?.classes, key, { ...params }) : undefined),
|
|
|
|
sx: (key = '', when = true, params = {}) => (when ? BaseDirective._getOptionValue(el.$instance?.$css?.inlineStyles, key, { ...params }) : undefined),
|
2023-07-03 22:20:35 +00:00
|
|
|
...$options
|
|
|
|
};
|
|
|
|
|
2023-07-06 12:43:23 +00:00
|
|
|
el.$instance = el._$instances[name]; // pass instance data to hooks
|
|
|
|
el.$instance[hook]?.(el, binding, vnode, prevVnode); // handle hook in directive implementation
|
2023-07-03 22:20:35 +00:00
|
|
|
BaseDirective._hook(name, hook, el, binding, vnode, prevVnode); // handle hooks during directive uses (global and self-definition)
|
|
|
|
};
|
2023-06-21 13:48:00 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
created: (el, binding, vnode, prevVnode) => {
|
2023-07-03 22:20:35 +00:00
|
|
|
handleHook('created', el, binding, vnode, prevVnode);
|
2023-06-21 13:48:00 +00:00
|
|
|
},
|
|
|
|
beforeMount: (el, binding, vnode, prevVnode) => {
|
2023-07-06 12:43:23 +00:00
|
|
|
loadBaseStyle();
|
|
|
|
!el.$instance?.isUnstyled && el.$instance?.$css?.loadStyle();
|
2023-07-03 22:20:35 +00:00
|
|
|
handleHook('beforeMount', el, binding, vnode, prevVnode);
|
2023-06-21 13:48:00 +00:00
|
|
|
},
|
|
|
|
mounted: (el, binding, vnode, prevVnode) => {
|
2023-07-03 22:20:35 +00:00
|
|
|
handleHook('mounted', el, binding, vnode, prevVnode);
|
2023-06-21 13:48:00 +00:00
|
|
|
},
|
|
|
|
beforeUpdate: (el, binding, vnode, prevVnode) => {
|
2023-07-03 22:20:35 +00:00
|
|
|
handleHook('beforeUpdate', el, binding, vnode, prevVnode);
|
2023-06-21 13:48:00 +00:00
|
|
|
},
|
|
|
|
updated: (el, binding, vnode, prevVnode) => {
|
2023-07-03 22:20:35 +00:00
|
|
|
handleHook('updated', el, binding, vnode, prevVnode);
|
2023-06-21 13:48:00 +00:00
|
|
|
},
|
|
|
|
beforeUnmount: (el, binding, vnode, prevVnode) => {
|
2023-07-03 22:20:35 +00:00
|
|
|
handleHook('beforeUnmount', el, binding, vnode, prevVnode);
|
2023-06-21 13:48:00 +00:00
|
|
|
},
|
|
|
|
unmounted: (el, binding, vnode, prevVnode) => {
|
2023-07-03 22:20:35 +00:00
|
|
|
handleHook('unmounted', el, binding, vnode, prevVnode);
|
2023-06-21 13:48:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2023-07-03 22:20:35 +00:00
|
|
|
extend: (...args) => {
|
|
|
|
const [name, options] = BaseDirective._getMeta(...args);
|
2023-06-21 13:48:00 +00:00
|
|
|
|
2023-07-03 22:20:35 +00:00
|
|
|
return {
|
|
|
|
extend: (..._args) => {
|
|
|
|
const [_name, _options] = BaseDirective._getMeta(..._args);
|
2023-06-21 13:48:00 +00:00
|
|
|
|
2023-07-03 22:20:35 +00:00
|
|
|
return BaseDirective.extend(_name, { ...options, ...options?.methods, ..._options });
|
|
|
|
},
|
|
|
|
...BaseDirective._extend(name, options)
|
|
|
|
};
|
2023-06-21 13:48:00 +00:00
|
|
|
}
|
|
|
|
};
|
2023-07-03 22:20:35 +00:00
|
|
|
|
|
|
|
export default BaseDirective;
|