vue-vben-admin/src/utils/index.ts

148 lines
4.5 KiB
TypeScript
Raw Normal View History

import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
import type { App, Component } from 'vue';
import { intersectionWith, isEqual, mergeWith, unionWith } from 'lodash-es';
2020-12-28 22:04:05 +08:00
import { unref } from 'vue';
2023-03-28 21:34:11 +08:00
import { isArray, isObject } from '/@/utils/is';
2020-11-01 18:34:35 +08:00
export const noop = () => {};
2021-02-25 20:17:08 +08:00
2020-09-28 20:19:10 +08:00
/**
* @description: Set ui mount node
*/
export function getPopupContainer(node?: HTMLElement): HTMLElement {
2020-12-29 23:37:40 +08:00
return (node?.parentNode as HTMLElement) ?? document.body;
2020-09-28 20:19:10 +08:00
}
2020-11-23 23:24:13 +08:00
2020-09-28 20:19:10 +08:00
/**
* Add the object as a parameter to the URL
* @param baseUrl url
* @param obj
* @returns {string}
* eg:
* let obj = {a: '3', b: '4'}
* setObjToUrlParams('www.baidu.com', obj)
* ==>www.baidu.com?a=3&b=4
*/
export function setObjToUrlParams(baseUrl: string, obj: any): string {
let parameters = '';
for (const key in obj) {
parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
}
parameters = parameters.replace(/&$/, '');
return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
2020-09-28 20:19:10 +08:00
}
2023-03-28 21:34:11 +08:00
/**
* Recursively merge two objects.
*
*
* @param source The source object to merge from.
* @param target The target object to merge into.
* @param mergeArrays How to merge arrays. Default is "replace".
* replace
* - "union": Union the arrays.
* - "intersection": Intersect the arrays.
* - "concat": Concatenate the arrays.
* - "replace": Replace the source array with the target array.
* @returns The merged object.
2023-03-28 21:34:11 +08:00
*/
export function deepMerge<T extends object | null | undefined, U extends object | null | undefined>(
source: T,
target: U,
mergeArrays: 'union' | 'intersection' | 'concat' | 'replace' = 'replace',
2023-03-28 21:34:11 +08:00
): T & U {
if (!target) {
return source as T & U;
}
if (!source) {
return target as T & U;
}
return mergeWith({}, source, target, (sourceValue, targetValue) => {
if (isArray(targetValue) && isArray(sourceValue)) {
switch (mergeArrays) {
case 'union':
return unionWith(sourceValue, targetValue, isEqual);
case 'intersection':
return intersectionWith(sourceValue, targetValue, isEqual);
case 'concat':
return sourceValue.concat(targetValue);
case 'replace':
return targetValue;
default:
throw new Error(`Unknown merge array strategy: ${mergeArrays as string}`);
}
2023-03-28 21:34:11 +08:00
}
if (isObject(targetValue) && isObject(sourceValue)) {
return deepMerge(sourceValue, targetValue, mergeArrays);
}
return undefined;
});
2020-09-28 20:19:10 +08:00
}
2020-11-23 23:24:13 +08:00
export function openWindow(
url: string,
2021-08-24 22:41:48 +08:00
opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
2020-11-23 23:24:13 +08:00
) {
const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
const feature: string[] = [];
noopener && feature.push('noopener=yes');
noreferrer && feature.push('noreferrer=yes');
window.open(url, target, feature.join(','));
}
2020-12-28 22:04:05 +08:00
// dynamic use hook props
2023-02-05 16:32:44 +08:00
export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
2020-12-28 22:04:05 +08:00
const ret: Recordable = {};
Object.keys(props).map((key) => {
ret[key] = unref((props as Recordable)[key]);
});
return ret as Partial<U>;
}
2021-01-06 00:08:45 +08:00
export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
if (!route) return route;
const { matched, ...opt } = route;
return {
...opt,
matched: (matched
? matched.map((item) => ({
meta: item.meta,
name: item.name,
path: item.path,
}))
: undefined) as RouteRecordNormalized[],
};
}
2023-02-05 16:32:44 +08:00
// https://github.com/vant-ui/vant/issues/8302
type EventShim = {
new (...args: any[]): {
$props: {
onClick?: (...args: any[]) => void;
};
};
};
export type WithInstall<T> = T & {
install(app: App): void;
} & EventShim;
export type CustomComponent = Component & { displayName?: string };
export const withInstall = <T extends CustomComponent>(component: T, alias?: string) => {
(component as Record<string, unknown>).install = (app: App) => {
const compName = component.name || component.displayName;
if (!compName) return;
app.component(compName, component);
if (alias) {
app.config.globalProperties[alias] = component;
}
};
2023-02-05 16:32:44 +08:00
return component as WithInstall<T>;
};