import * as zrUtil from 'zrender/src/core/util'; import Group from 'zrender/src/container/Group'; import deviceType from './constant/deviceType'; import shapefactory from './shape/factory'; import TransformHandle from './transformHandle'; class Painter { constructor(jmap) { // 父级实例 this.$jmap = jmap; this.$zr = jmap.getZr(); // 图层数据 this.mapInstanceLevel = {}; // 初始图层 this.initPainterLevels(); // 视图控制器 this.$transformHandle = new TransformHandle(this, this.$zr); } /** * 初始绘图实例 * @param {*} dom * @param {*} config */ initPainterLevels() { // 添加父级图层 this.parentLevel = new Group({ name: '__parent__' }); this.$zr.add(this.parentLevel); // 添加子级图层 zrUtil.each(Object.values(deviceType), (type) => { const level = new Group({ name: `__${type}__` }); this.mapInstanceLevel[type] = level; this.parentLevel.add(level); }); } /** * 重绘视图 * @param {*} mapDevice */ repaint(mapDevice) { // 清空视图 this.clear(); // 创建视图 Object.values(mapDevice).forEach(device => { this.add(device); }); } /** * 添加视图 * @param {*} device */ add(device) { const type = device._type; const instance = shapefactory(type, device, this.$jmap); if (instance) { this.$transformHandle.transformView(instance); device.instance = instance; this.mapInstanceLevel[type].add(instance); } } /** * 删除视图 * @param {*} device */ delete(device) { const type = device._type; const instance = device.instance; if (instance) { this.mapInstanceLevel[type].remove(instance); } } /** * 更新视图 * @param {*} device */ update(device) { const instance = device.instance; if (instance) { instance.setState(device.state); } } /** * 更新transform变化 * @param {*} opt */ updateTransform(opt) { this.$transformHandle.updateTransform(opt); } /** * 更新zrender尺寸 * @param {*} opt */ updateZrSize(opt) { this.$transformHandle.updateZrSize(opt); } /** * 设置图层可见 * @param {*} code */ setLevelVisible(list, show) { zrUtil.each(Object.values(deviceType), type => { const level = this.mapInstanceLevel[type]; if (list.includes(type)) { show ? level.show() : level.hide(); } else { show ? level.hide() : level.show(); } }); } /** * 刷新图层 */ refresh() { this.$zr && this.$zr.refresh(); } /** * 清除图层 */ clear() { zrUtil.each(Object.values(this.mapInstanceLevel), (level) => { level && level.removeAll(); }); this.refresh(); } /** * 销毁图层 */ dispose() { this.mapInstanceLevel = {}; this.parentLevel = null; } /** * 父级图层 */ getParentLevel() { return this.parentLevel; } } export default Painter;