import zrender from 'zrender'; import Group from 'zrender/src/container/Group'; import deviceType from './config/deviceType'; import shapefactory from './shape/factory'; import { copyFile } from 'fs'; class Painter { constructor(jamp) { this.$jamp = jamp; this.$zr = null; this.mapLevel = {}; this.viewInstance = {}; } /** 挂载视图*/ mount(dom, config) { // 初始化图层 Object.values(deviceType).forEach(type => { this.mapLevel[type] = new Group({ name: `__${type}__` }); }) // 挂载视图 zrender.init(dom, config); } /** 渲染视图*/ render(mapDevice) { // 初始化视图实例 this.viewInstance = {}; // 清空图层 Object.values(this.mapLevel).forEach(level => { level && level.removeAll(); }) // 批量创建视图 Object.values(mapDevice).forEach(device => { if (device) { this.add(device); } }) } /** * 添加视图 * @param {*} device */ add(device) { let type = device.type; let code = device.code; let view = shapefactory(device); if (view) { this.viewInstance[code] = view; this.mapLevel[type].add(view); } } /** * 删除视图 * @param {*} device */ delete(device) { let code = device.code; let type = device.type; let view = this.viewInstance[code]; if (view) { this.mapLevel[type].remove(view); delete this.viewInstance[code]; } } /** * 更新视图 * @param {*} device */ update(device) { let code = device.code; let view = this.viewInstance[code]; if (view) { if (device.model) { view.setModel(device.model); } if (device.status) { view.setStatus(device.status); } } } } export default Painter;