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

95 lines
2.5 KiB
TypeScript
Raw Normal View History

import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
import type { App, Plugin } from 'vue';
2020-12-28 22:04:05 +08:00
import { unref } from 'vue';
2020-11-28 14:27:26 +08:00
import { isObject } from '/@/utils/is';
import { cloneDeep } from 'lodash-es';
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
}
// 深度合并
2020-12-25 01:09:44 +08:00
export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
2020-09-28 20:19:10 +08:00
let key: string;
修复 updateSchema 多个field 属性时,第二个无效问题。 (#2493) * Table BasicColumn 添加 editDynamicDisabled Co-authored-by: Cyrus Zhou <6802207@qq.com> 使用方式同 Form FormSchema dynamicDisabled ``` export const Columns: BasicColumn[] = [ { title: 'Title', dataIndex: 'Title', editRow: true, editComponent: 'Select', editDynamicDisabled: ({ record }) => record.isDisabled, }, * editComponentProps onChange 功能恢复 Co-authored-by: Cyrus Zhou <6802207@qq.com> 说明: ...omit(compProps, 'onChange') 这会忽略 onChange ,导致 editComponentProps onChange 被取消 如下功能将不支持: ``` editComponentProps: ({ record }) => { return { options: effectTypeData, onChange: () => { }, }; }, ``` * tableData == null 报错 * ApiSelect 第一次选择触发required错误提示问题 * 恢复 虽然可以解决第一次选择提示报错问题,但是会导致 onChange: (e: any, options: any) => 无法获得 options 的值 * 修复标签页切换灰屏不显示内容问题 Co-authored-by: Cyrus Zhou <6802207@qq.com> 问题描述页面没有用 div 包括 会提示 Component inside <Transition> renders non-element root node that cannot be animated , 导致页灰屏必须刷新页面才可以显示内容 * 添加 Form ApiTransfer ## 使用方式 api 方式: ``` ...... component: 'ApiTransfer', componentProps: { api: sysUserSelector, labelField: 'name', valueField: 'id', }, ..... ``` 数据方式: ``` .... componentProps: { dataSource: [ { title: 'Test01', key: '0', disabled: false, description: 'description 01' }, { title: 'Test02', key: '1', disabled: false, description: 'description 02' }, { title: 'Test03', key: '2', disabled: false, description: 'description 03' }, { title: 'Test04', key: '3', disabled: false, description: 'description 04' }, { title: 'Test05', key: '4', disabled: false, description: 'description 05' }, ], }, .... ``` * style: eslint 书写规范 * fix: 频繁切换页面导致灰屏 * fix: 修复 updateSchema 多个field 属性时,第二个无效问题。 如: ``` updateSchema([ { field: 'password', ifShow: !unref(isUpdate), }, { field: 'confirm', ifShow: !unref(isUpdate), }, ]); ``` Co-authored-by: CyrusZhou <6802207@qq.com>
2023-01-18 11:17:44 +08:00
const res: any = cloneDeep(src);
2020-09-28 20:19:10 +08:00
for (key in target) {
res[key] = isObject(res[key]) ? deepMerge(res[key], target[key]) : (res[key] = target[key]);
2020-09-28 20:19:10 +08:00
}
return res;
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
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>;
}
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[],
};
}
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;
};