Vben
authored
|
1
|
import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
|
|
2
|
import type { App, Component } from 'vue';
|
Vben
authored
|
3
|
|
vben
authored
|
4
|
import { unref } from 'vue';
|
|
5
6
|
import { isArray, isObject } from '/@/utils/is';
import { cloneDeep, mergeWith } from 'lodash-es';
|
Vben
authored
|
7
|
|
vben
authored
|
8
|
export const noop = () => {};
|
Vben
authored
|
9
|
|
|
10
11
12
13
|
/**
* @description: Set ui mount node
*/
export function getPopupContainer(node?: HTMLElement): HTMLElement {
|
vben
authored
|
14
|
return (node?.parentNode as HTMLElement) ?? document.body;
|
|
15
|
}
|
vben
authored
|
16
|
|
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/**
* 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
|
33
|
return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
|
|
34
35
|
}
|
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
/**
递归合并两个对象。
Recursively merge two objects.
@param target 目标对象,合并后结果存放于此。The target object to merge into.
@param source 要合并的源对象。The source object to merge from.
@returns 合并后的对象。The merged object.
*/
export function deepMerge<T extends object | null | undefined, U extends object | null | undefined>(
target: T,
source: U,
): T & U {
return mergeWith(cloneDeep(target), source, (objValue, srcValue) => {
if (isObject(objValue) && isObject(srcValue)) {
return mergeWith(cloneDeep(objValue), srcValue, (prevValue, nextValue) => {
return isArray(prevValue) ? prevValue.concat(nextValue) : undefined;
});
}
});
|
|
55
56
|
}
|
vben
authored
|
57
58
|
export function openWindow(
url: string,
|
vben
authored
|
59
|
opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
|
vben
authored
|
60
61
62
63
64
65
66
67
68
|
) {
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
|
69
70
|
// dynamic use hook props
|
|
71
|
export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> {
|
vben
authored
|
72
73
74
75
76
77
78
79
|
const ret: Recordable = {};
Object.keys(props).map((key) => {
ret[key] = unref((props as Recordable)[key]);
});
return ret as Partial<U>;
}
|
vben
authored
|
80
|
|
Vben
authored
|
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
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
|
95
|
|
|
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// 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);
|
Vben
authored
|
116
117
118
119
|
if (alias) {
app.config.globalProperties[alias] = component;
}
};
|
|
120
|
return component as WithInstall<T>;
|
Vben
authored
|
121
|
};
|