Vben
2021-06-09 55e9d9fc2953643cec95c74b6ed34b0e68641fb6
提交 | 用户 | age
e12c58 1 import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
55e9d9 2 import type { App, Plugin } from 'vue';
V 3
a305e5 4 import { unref } from 'vue';
498278 5 import { isObject } from '/@/utils/is';
e12c58 6
35d2bf 7 export const noop = () => {};
8a9ca4 8
2f6253 9 /**
10  * @description:  Set ui mount node
11  */
12 export function getPopupContainer(node?: HTMLElement): HTMLElement {
116a1f 13   return (node?.parentNode as HTMLElement) ?? document.body;
2f6253 14 }
ba068b 15
2f6253 16 /**
17  * Add the object as a parameter to the URL
18  * @param baseUrl url
19  * @param obj
20  * @returns {string}
21  * eg:
22  *  let obj = {a: '3', b: '4'}
23  *  setObjToUrlParams('www.baidu.com', obj)
24  *  ==>www.baidu.com?a=3&b=4
25  */
26 export function setObjToUrlParams(baseUrl: string, obj: any): string {
27   let parameters = '';
28   for (const key in obj) {
29     parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
30   }
31   parameters = parameters.replace(/&$/, '');
da0491 32   return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
2f6253 33 }
34
4ff1c4 35 export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
2f6253 36   let key: string;
37   for (key in target) {
498278 38     src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
2f6253 39   }
40   return src;
41 }
42
ba068b 43 export function openWindow(
V 44   url: string,
45   opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }
46 ) {
47   const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
48   const feature: string[] = [];
49
50   noopener && feature.push('noopener=yes');
51   noreferrer && feature.push('noreferrer=yes');
52
53   window.open(url, target, feature.join(','));
54 }
a305e5 55
V 56 // dynamic use hook props
57 export function getDynamicProps<T, U>(props: T): Partial<U> {
58   const ret: Recordable = {};
59
60   Object.keys(props).map((key) => {
61     ret[key] = unref((props as Recordable)[key]);
62   });
63
64   return ret as Partial<U>;
144ab5 65 }
581007 66
e12c58 67 export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
V 68   if (!route) return route;
69   const { matched, ...opt } = route;
70   return {
71     ...opt,
72     matched: (matched
73       ? matched.map((item) => ({
74           meta: item.meta,
75           name: item.name,
76           path: item.path,
77         }))
78       : undefined) as RouteRecordNormalized[],
79   };
80 }
55e9d9 81
V 82 export const withInstall = <T>(component: T, alias?: string) => {
83   const comp = component as any;
84   comp.install = (app: App) => {
85     app.component(comp.name || comp.displayName, component);
86     if (alias) {
87       app.config.globalProperties[alias] = component;
88     }
89   };
90   return component as T & Plugin;
91 };