2023-10-02 10:46:09 +00:00
|
|
|
import BaseStyle from 'primevue/base/style';
|
2024-02-19 22:53:42 +00:00
|
|
|
import { toVariables } from 'primevue/themes';
|
2023-07-03 22:20:35 +00:00
|
|
|
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])],
|
2023-10-10 15:37:42 +00:00
|
|
|
_getConfig: (binding, vnode) => (binding?.instance?.$primevue || vnode?.ctx?.appContext?.config?.globalProperties?.$primevue)?.config,
|
2023-07-03 22:20:35 +00:00
|
|
|
_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-11 22:42:43 +00:00
|
|
|
const getValue = (...args) => {
|
|
|
|
const value = BaseDirective._getOptionValue(...args);
|
|
|
|
|
2023-07-17 12:31:09 +00:00
|
|
|
return ObjectUtils.isString(value) || ObjectUtils.isArray(value) ? { class: value } : value;
|
2023-07-11 22:42:43 +00:00
|
|
|
};
|
|
|
|
|
2023-09-20 11:25:33 +00:00
|
|
|
const { mergeSections = true, mergeProps: useMergeProps = false } = instance.binding?.value?.ptOptions || instance.$config?.ptOptions || {};
|
2023-10-10 15:37:42 +00:00
|
|
|
const global = searchInDefaultPT ? BaseDirective._useDefaultPT(instance, instance.defaultPT(), getValue, key, params) : undefined;
|
2023-09-05 10:18:36 +00:00
|
|
|
const self = BaseDirective._usePT(instance, BaseDirective._getPT(obj, instance.$name), getValue, key, { ...params, global: global || {} });
|
2024-01-30 14:35:32 +00:00
|
|
|
const datasets = BaseDirective._getPTDatasets(instance, key);
|
|
|
|
|
|
|
|
return mergeSections || (!mergeSections && self) ? (useMergeProps ? BaseDirective._mergeProps(instance, useMergeProps, global, self, datasets) : { ...global, ...self, ...datasets }) : { ...self, ...datasets };
|
|
|
|
},
|
|
|
|
_getPTDatasets(instance = {}, key = '') {
|
|
|
|
const datasetPrefix = 'data-pc-';
|
|
|
|
|
|
|
|
return {
|
2023-07-10 09:34:48 +00:00
|
|
|
...(key === 'root' && { [`${datasetPrefix}name`]: ObjectUtils.toFlatCase(instance.$name) }),
|
|
|
|
[`${datasetPrefix}section`]: ObjectUtils.toFlatCase(key)
|
2023-09-04 21:36:12 +00:00
|
|
|
};
|
2023-07-03 22:20:35 +00:00
|
|
|
},
|
2023-09-04 21:36:12 +00:00
|
|
|
_getPT: (pt, key = '', callback) => {
|
2023-08-17 23:51:01 +00:00
|
|
|
const getValue = (value) => {
|
|
|
|
const computedValue = callback ? callback(value) : value;
|
2023-09-05 10:18:36 +00:00
|
|
|
const _key = ObjectUtils.toFlatCase(key);
|
2023-08-17 23:51:01 +00:00
|
|
|
|
2023-09-05 10:18:36 +00:00
|
|
|
return computedValue?.[_key] ?? computedValue;
|
2023-08-17 23:51:01 +00:00
|
|
|
};
|
|
|
|
|
2023-09-20 11:48:21 +00:00
|
|
|
return pt?.hasOwnProperty('_usept')
|
2023-08-17 23:51:01 +00:00
|
|
|
? {
|
2023-09-20 11:48:21 +00:00
|
|
|
_usept: pt['_usept'],
|
2023-08-17 23:51:01 +00:00
|
|
|
originalValue: getValue(pt.originalValue),
|
|
|
|
value: getValue(pt.value)
|
|
|
|
}
|
|
|
|
: getValue(pt);
|
|
|
|
},
|
2023-09-04 23:50:32 +00:00
|
|
|
_usePT: (instance = {}, pt, callback, key, params) => {
|
2023-08-17 23:51:01 +00:00
|
|
|
const fn = (value) => callback(value, key, params);
|
|
|
|
|
|
|
|
if (pt?.hasOwnProperty('_usept')) {
|
2023-09-20 11:25:33 +00:00
|
|
|
const { mergeSections = true, mergeProps: useMergeProps = false } = pt['_usept'] || instance.$config?.ptOptions || {};
|
2023-08-17 23:51:01 +00:00
|
|
|
const originalValue = fn(pt.originalValue);
|
|
|
|
const value = fn(pt.value);
|
|
|
|
|
|
|
|
if (originalValue === undefined && value === undefined) return undefined;
|
|
|
|
else if (ObjectUtils.isString(value)) return value;
|
|
|
|
else if (ObjectUtils.isString(originalValue)) return originalValue;
|
|
|
|
|
2024-01-29 12:35:09 +00:00
|
|
|
return mergeSections || (!mergeSections && value) ? (useMergeProps ? BaseDirective._mergeProps(instance, useMergeProps, originalValue, value) : { ...originalValue, ...value }) : value;
|
2023-08-17 23:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fn(pt);
|
|
|
|
},
|
2023-09-04 23:50:32 +00:00
|
|
|
_useDefaultPT: (instance = {}, defaultPT = {}, callback, key, params) => {
|
|
|
|
return BaseDirective._usePT(instance, defaultPT, callback, key, params);
|
2023-08-17 23:51:01 +00:00
|
|
|
},
|
2024-02-13 09:36:36 +00:00
|
|
|
_loadThemeStyle: (instance = {}, useStyleOptions) => {
|
2024-02-19 22:53:42 +00:00
|
|
|
const preset = instance.globalPresetTheme();
|
2024-02-13 09:36:36 +00:00
|
|
|
|
2024-02-19 22:53:42 +00:00
|
|
|
if (preset) {
|
|
|
|
const { variables } = preset;
|
2024-02-13 09:36:36 +00:00
|
|
|
const { colorScheme, ...vRest } = variables || {};
|
|
|
|
const { dark, ...csRest } = colorScheme || {};
|
|
|
|
const vRest_css = ObjectUtils.isNotEmpty(vRest) ? toVariables({ [instance.$name]: vRest }).css : '';
|
|
|
|
const csRest_css = ObjectUtils.isNotEmpty(csRest) ? toVariables({ [instance.$name]: csRest }).css : '';
|
|
|
|
const dark_css = ObjectUtils.isNotEmpty(dark) ? toVariables({ [instance.$name]: dark }, { selector: '.p-dark' }).css : '';
|
|
|
|
const variables_css = `${vRest_css}${csRest_css}${dark_css}`;
|
|
|
|
|
2024-02-19 22:53:42 +00:00
|
|
|
instance.$style?.loadTheme(`${variables_css}`, { name: `${instance.$name}-directive-variable`, useStyleOptions });
|
|
|
|
}
|
|
|
|
|
|
|
|
const base = instance.globalBaseTheme();
|
|
|
|
|
|
|
|
if (base) {
|
|
|
|
const { css } = base;
|
|
|
|
|
|
|
|
instance.$style?.loadTheme(`${css}`, { name: `${instance.$name}-directive-style`, useStyleOptions });
|
2024-02-13 09:36:36 +00:00
|
|
|
}
|
|
|
|
},
|
2023-07-03 22:20:35 +00:00
|
|
|
_hook: (directiveName, hookName, el, binding, vnode, prevVnode) => {
|
2023-07-10 09:34:48 +00:00
|
|
|
const name = `on${ObjectUtils.toCapitalCase(hookName)}`;
|
2023-10-10 15:37:42 +00:00
|
|
|
const config = BaseDirective._getConfig(binding, vnode);
|
2023-09-04 23:50:32 +00:00
|
|
|
const instance = el?.$instance;
|
|
|
|
const selfHook = BaseDirective._usePT(instance, BaseDirective._getPT(binding?.value?.pt, directiveName), BaseDirective._getOptionValue, `hooks.${name}`);
|
|
|
|
const defaultHook = BaseDirective._useDefaultPT(instance, config?.pt?.directives?.[directiveName], BaseDirective._getOptionValue, `hooks.${name}`);
|
2023-07-10 09:34:48 +00:00
|
|
|
const options = { el, binding, vnode, prevVnode };
|
2023-07-03 22:20:35 +00:00
|
|
|
|
2023-09-04 23:50:32 +00:00
|
|
|
selfHook?.(instance, options);
|
|
|
|
defaultHook?.(instance, options);
|
2023-07-03 22:20:35 +00:00
|
|
|
},
|
2024-01-29 12:35:09 +00:00
|
|
|
_mergeProps(instance = {}, fn, ...args) {
|
|
|
|
return ObjectUtils.isFunction(fn) ? fn(...args) : mergeProps(...args);
|
|
|
|
},
|
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
|
|
|
|
2023-10-10 15:37:42 +00:00
|
|
|
const config = BaseDirective._getConfig(binding, vnode);
|
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-10-26 00:54:11 +00:00
|
|
|
$modifiers: binding?.modifiers,
|
|
|
|
$value: binding?.value,
|
|
|
|
$el: $prevInstance['$el'] || el || undefined,
|
2024-02-13 09:36:36 +00:00
|
|
|
$style: { classes: undefined, inlineStyles: undefined, loadStyle: () => {}, loadTheme: () => {}, ...options?.style },
|
2023-08-11 01:34:02 +00:00
|
|
|
$config: config,
|
2023-07-03 22:20:35 +00:00
|
|
|
/* computed instance variables */
|
2023-10-10 15:37:42 +00:00
|
|
|
defaultPT: () => BaseDirective._getPT(config?.pt, undefined, (value) => value?.directives?.[name]),
|
|
|
|
isUnstyled: () => (el.$instance?.$binding?.value?.unstyled !== undefined ? el.$instance?.$binding?.value?.unstyled : config?.unstyled),
|
2024-02-19 22:53:42 +00:00
|
|
|
baseTheme: () => ObjectUtils.getItemValue(config?.theme?.base),
|
2024-02-13 09:36:36 +00:00
|
|
|
presetTheme: () => ObjectUtils.getItemValue(config?.theme?.preset, config?.theme?.options),
|
2024-02-19 22:53:42 +00:00
|
|
|
globalBaseTheme: () => ObjectUtils.getItemValue(el.$instance?.baseTheme?.()?.directives?.[name], undefined),
|
|
|
|
globalPresetTheme: () => ObjectUtils.getItemValue(el.$instance?.presetTheme?.()?.directives?.[name], undefined),
|
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),
|
2023-10-10 15:37:42 +00:00
|
|
|
cx: (key = '', params = {}) => (!el.$instance?.isUnstyled() ? BaseDirective._getOptionValue(el.$instance?.$style?.classes, key, { ...params }) : undefined),
|
2023-10-02 10:46:09 +00:00
|
|
|
sx: (key = '', when = true, params = {}) => (when ? BaseDirective._getOptionValue(el.$instance?.$style?.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
|
2024-01-21 11:56:17 +00:00
|
|
|
el[`$${name}`] = el.$instance; // expose all options with $<directive_name>
|
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-10-10 15:37:42 +00:00
|
|
|
const config = BaseDirective._getConfig(binding, vnode);
|
2023-08-11 01:34:02 +00:00
|
|
|
|
2024-01-15 02:07:41 +00:00
|
|
|
BaseStyle.loadStyle({ nonce: config?.csp?.nonce });
|
|
|
|
!el.$instance?.isUnstyled() && el.$instance?.$style?.loadStyle({ nonce: config?.csp?.nonce });
|
2024-02-13 09:36:36 +00:00
|
|
|
BaseDirective._loadThemeStyle(el.$instance, { nonce: config?.csp?.nonce });
|
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) => {
|
2024-01-24 15:05:29 +00:00
|
|
|
const config = BaseDirective._getConfig(binding, vnode);
|
|
|
|
|
|
|
|
BaseStyle.loadStyle({ nonce: config?.csp?.nonce });
|
|
|
|
!el.$instance?.isUnstyled() && el.$instance?.$style?.loadStyle({ nonce: config?.csp?.nonce });
|
2024-02-13 09:36:36 +00:00
|
|
|
BaseDirective._loadThemeStyle(el.$instance, { nonce: config?.csp?.nonce });
|
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;
|