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 ZoomHandle from './zoomHandle'; class Painter { constructor(jmap) { // 父级实例 this.$jmap = jmap; this.$zr = jmap.getZr(); // 视图控制器 this.$zoomHandle = new ZoomHandle(this, this.$zr); // 图层数据 this.mapInstanceLevel = {}; // 父级图层 this.parentLevel = null; // 初始绘图实例 this.initPainterInstance(); } /** * 初始绘图实例 * @param {*} dom * @param {*} config */ initPainterInstance() { // 添加父级图层 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 => { device && this.add(device); }); } /** * 添加视图 * @param {*} device */ add(device) { // console.log(device, device._type, 111); const type = device._type; const instance = shapefactory(type, device, this.$jmap); if (instance) { this.$zoomHandle.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); } } /** * 更新图层zoom * @param {*} zoom */ updateZoomTransform(zoom) { this.$zoomHandle.updateTransform(zoom); } /** * 设置图层显隐 * @param {*} code */ setLevelInvisible(list) { zrUtil.each(Object.values(deviceType), type => { const level = this.mapInstanceLevel[type]; if (list.includes(type)) { level.hide(); } else { 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;