vxe-table-demonic
Version:
一个基于 vue 的 PC 端表单/表格组件,支持增删改查、虚拟列表、虚拟树、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、弹窗、自定义模板、渲染器、JSON 配置式...
53 lines (46 loc) • 1.75 kB
text/typescript
import XEUtils from 'xe-utils'
import { warnLog } from '../../tools/log'
import { VxeGlobalInterceptor, VxeGlobalInterceptorHandles } from '../../../types/v-x-e-table'
const storeMap: { [type: string]: VxeGlobalInterceptorHandles.InterceptorCallback[] } = {}
export const interceptor: VxeGlobalInterceptor = {
mixin (options) {
XEUtils.each(options, (callback: VxeGlobalInterceptorHandles.InterceptorCallback, type) => interceptor.add(type as VxeGlobalInterceptorHandles.Type, callback))
return interceptor
},
get (type) {
return storeMap[type] || []
},
add (type, callback) {
// 检测类型
if (process.env.VUE_APP_VXE_TABLE_ENV === 'development') {
const eventTypes: VxeGlobalInterceptorHandles.Type[] = ['created', 'mounted', 'activated', 'beforeUnmount', 'unmounted', 'event.clearActived', 'event.clearFilter', 'event.clearAreas', 'event.showMenu', 'event.keydown', 'event.export', 'event.import']
if (eventTypes.indexOf(type) === -1) {
warnLog('vxe.error.errProp', [`Interceptor.${type}`, eventTypes.join('|')])
}
}
if (callback) {
let eList = storeMap[type]
if (!eList) {
eList = storeMap[type] = []
}
// 检测重复
if (process.env.VUE_APP_VXE_TABLE_ENV === 'development') {
if (eList.indexOf(callback) > -1) {
warnLog('vxe.error.coverProp', ['Interceptor', type])
}
}
eList.push(callback)
}
return interceptor
},
delete (type, callback) {
const eList = storeMap[type]
if (eList) {
if (callback) {
XEUtils.remove(eList, fn => fn === callback)
} else {
delete storeMap[type]
}
}
}
}