interceptor.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { UtilTools } from '../../tools'
  2. import XEUtils from 'xe-utils'
  3. function toType (type) {
  4. return XEUtils.toValueString(type).replace('_', '').toLowerCase()
  5. }
  6. const eventTypes = 'created,mounted,activated,beforeDestroy,destroyed,event.clearActived,event.clearFilter,event.clearAreas,event.showMenu,event.keydown,event.export,event.import'.split(',').map(toType)
  7. const storeMap = {}
  8. export const interceptor = {
  9. mixin (map) {
  10. XEUtils.each(map, (callback, type) => interceptor.add(type, callback))
  11. return interceptor
  12. },
  13. get (type) {
  14. return storeMap[toType(type)] || []
  15. },
  16. add (type, callback) {
  17. type = toType(type)
  18. // 检测类型
  19. if (process.env.VUE_APP_VXE_TABLE_ENV === 'development') {
  20. if (eventTypes.indexOf(type) === -1) {
  21. UtilTools.warn('vxe.error.errProp', [`Interceptor.${type}`, eventTypes.join('|')])
  22. }
  23. }
  24. if (callback && eventTypes.indexOf(type) > -1) {
  25. let eList = storeMap[type]
  26. if (!eList) {
  27. eList = storeMap[type] = []
  28. }
  29. // 检测重复
  30. if (process.env.VUE_APP_VXE_TABLE_ENV === 'development') {
  31. if (eList.indexOf(callback) > -1) {
  32. UtilTools.warn('vxe.error.coverProp', ['Interceptor', type])
  33. }
  34. }
  35. eList.push(callback)
  36. }
  37. return interceptor
  38. },
  39. delete (type, callback) {
  40. const eList = storeMap[toType(type)]
  41. if (eList) {
  42. XEUtils.remove(eList, fn => fn === callback)
  43. }
  44. return interceptor
  45. }
  46. }