Vben
authored
|
1
|
import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
|
Vben
authored
|
2
3
|
import type { App, Plugin } from 'vue';
|
vben
authored
|
4
|
import { unref } from 'vue';
|
vben
authored
|
5
|
import { isObject } from '/@/utils/is';
|
Vben
authored
|
6
|
|
vben
authored
|
7
|
export const noop = () => {};
|
Vben
authored
|
8
|
|
|
9
10
11
12
|
/**
* @description: Set ui mount node
*/
export function getPopupContainer(node?: HTMLElement): HTMLElement {
|
vben
authored
|
13
|
return (node?.parentNode as HTMLElement) ?? document.body;
|
|
14
|
}
|
vben
authored
|
15
|
|
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* 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(/&$/, '');
|
vben
authored
|
32
|
return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
|
|
33
34
|
}
|
vben
authored
|
35
|
export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
|
|
36
37
|
let key: string;
for (key in target) {
|
vben
authored
|
38
|
src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
|
|
39
40
41
42
|
}
return src;
}
|
vben
authored
|
43
44
|
export function openWindow(
url: string,
|
vben
authored
|
45
|
opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
|
vben
authored
|
46
47
48
49
50
51
52
53
54
|
) {
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(','));
}
|
vben
authored
|
55
56
57
58
59
60
61
62
63
64
65
|
// dynamic use hook props
export function getDynamicProps<T, U>(props: T): Partial<U> {
const ret: Recordable = {};
Object.keys(props).map((key) => {
ret[key] = unref((props as Recordable)[key]);
});
return ret as Partial<U>;
}
|
vben
authored
|
66
|
|
Vben
authored
|
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
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[],
};
}
|
Vben
authored
|
81
82
83
84
85
86
87
88
89
90
91
|
export const withInstall = <T>(component: T, alias?: string) => {
const comp = component as any;
comp.install = (app: App) => {
app.component(comp.name || comp.displayName, component);
if (alias) {
app.config.globalProperties[alias] = component;
}
};
return component as T & Plugin;
};
|