|
| 1 | +/* |
| 2 | + * @class: PlotRegister |
| 3 | + * @Description: 自定义标绘注册类 |
| 4 | + * @Author: zk |
| 5 | + * @Date: 2022-06-22 16:53:01 |
| 6 | + * @LastEditors: zk |
| 7 | + * @LastEditTime: 2022-06-23 11:54:41 |
| 8 | + */ |
| 9 | +import { ElementFactory } from '../PlotBase/SvgLoader/element'; |
| 10 | +import { PlotObjectFactory } from '../2DPlot/Shapes/PlotObjectFactory'; |
| 11 | +import { PrimitiveFactory } from '../3DPlot/Primitive/PrimitiveFactory'; |
| 12 | +import { DrawPlotObjectFactory2D } from '../2DPlot/Draw/DrawPlotObjectFactory2D'; |
| 13 | +import { DrawPlotObjectFactory3D } from '../3DPlot/Draw/DrawPlotObjectFactory3D'; |
| 14 | +import LogTool from '../PlotUtilBase/Log/LogTool'; |
| 15 | + |
| 16 | +class PlotRegister { |
| 17 | + constructor() { |
| 18 | + this._cacheRegisterTypes = []; |
| 19 | + } |
| 20 | + /** |
| 21 | + * @function: Module:PlotRegister.prototype.register |
| 22 | + * @description: 注册方法 |
| 23 | + * @param {*} type 新类名 |
| 24 | + * @param {*} func 处理新类名原型 |
| 25 | + * @return {*} |
| 26 | + */ |
| 27 | + register(type, func) { |
| 28 | + this.rewrite('irregular', type, func); |
| 29 | + } |
| 30 | + /** |
| 31 | + * @function: Module:PlotRegister.prototype.rewrite |
| 32 | + * @description: 重新方法 |
| 33 | + * @param {*} baseType 继承类类名 |
| 34 | + * @param {*} type 新类名 |
| 35 | + * @param {*} func 处理新类名原型 |
| 36 | + * @return {*} |
| 37 | + */ |
| 38 | + rewrite(baseType, type, func) { |
| 39 | + const baseProto = ElementFactory.getProto(baseType); |
| 40 | + const factories = [PlotObjectFactory, PrimitiveFactory, DrawPlotObjectFactory2D, DrawPlotObjectFactory3D]; |
| 41 | + const protos = factories.map((f) => f.getProto(baseType)); |
| 42 | + |
| 43 | + const isFactoryAllow = protos.every((t) => !!t); |
| 44 | + |
| 45 | + if (!baseProto || !isFactoryAllow) { |
| 46 | + LogTool.warn(`类型${baseType}对应的原型不存在!`); |
| 47 | + } |
| 48 | + |
| 49 | + const newProto = class t extends baseProto {}; |
| 50 | + |
| 51 | + // 修改原型 |
| 52 | + newProto.prototype.type = type; |
| 53 | + Object.assign(newProto.prototype, func(newProto)); |
| 54 | + |
| 55 | + // 注册 |
| 56 | + ElementFactory.setProto(type, newProto); |
| 57 | + |
| 58 | + factories.forEach((f, i) => { |
| 59 | + f.setProto(type, protos[i]); |
| 60 | + }); |
| 61 | + |
| 62 | + this._cacheRegisterTypes.push(type); |
| 63 | + } |
| 64 | + /** |
| 65 | + * @function: Module:PlotRegister.prototype.rewrite |
| 66 | + * @description: 移除注册类 |
| 67 | + * @param {*} type |
| 68 | + * @return {*} |
| 69 | + */ |
| 70 | + remove(type) { |
| 71 | + if (!(type in this._cacheRegisterTypes)) return; |
| 72 | + const factories = [ElementFactory, PlotObjectFactory, PrimitiveFactory, DrawPlotObjectFactory2D, DrawPlotObjectFactory3D]; |
| 73 | + factories.forEach((t) => { |
| 74 | + t.removeProto(type); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @function: Module:PlotRegister.prototype.removeAll |
| 80 | + * @description: 移除所有注册类 |
| 81 | + * @return {*} |
| 82 | + */ |
| 83 | + removeAll() { |
| 84 | + this._cacheRegisterTypes.forEach((type) => { |
| 85 | + this.remove(type); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export default PlotRegister; |
0 commit comments