2019-07-08 09:38:39 +08:00
|
|
|
|
import * as zrUtil from 'zrender/src/core/util';
|
2019-07-03 14:29:09 +08:00
|
|
|
|
import Group from 'zrender/src/container/Group';
|
|
|
|
|
import deviceType from './config/deviceType';
|
|
|
|
|
import shapefactory from './shape/factory';
|
2019-07-09 19:04:45 +08:00
|
|
|
|
import ZoomHandle from './zoomHandle';
|
2019-07-03 14:29:09 +08:00
|
|
|
|
|
|
|
|
|
class Painter {
|
2019-07-11 17:58:58 +08:00
|
|
|
|
constructor(jmap) {
|
|
|
|
|
// 父级实例
|
|
|
|
|
this.$jmap = jmap;
|
2019-07-10 13:17:12 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
// 视图控制器
|
|
|
|
|
this.$zoomHandle = new ZoomHandle(this, jmap.getZr());
|
2019-07-03 14:29:09 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
// 图层数据
|
|
|
|
|
this.viewLevelMap = {};
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
// 视图数据
|
|
|
|
|
this.viewInstance = {};
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
// 父级图层
|
|
|
|
|
this.parentLevel = null;
|
2019-07-05 00:40:53 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
// 初始绘图实例
|
|
|
|
|
this.initPainterInstance();
|
|
|
|
|
}
|
2019-07-03 14:29:09 +08:00
|
|
|
|
|
2019-07-04 10:59:40 +08:00
|
|
|
|
/**
|
2019-07-09 19:04:45 +08:00
|
|
|
|
* 初始绘图实例
|
2019-07-04 10:59:40 +08:00
|
|
|
|
* @param {*} dom
|
|
|
|
|
* @param {*} config
|
2019-07-03 18:41:00 +08:00
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
initPainterInstance() {
|
|
|
|
|
// 添加父级图层
|
|
|
|
|
this.parentLevel = new Group({ name: '__parent__' });
|
|
|
|
|
this.$jmap.getZr().add(this.parentLevel);
|
|
|
|
|
|
|
|
|
|
// 添加子级图层
|
|
|
|
|
zrUtil.each(Object.values(deviceType), (type) => {
|
|
|
|
|
const level = new Group({ name: `__${type}__` });
|
|
|
|
|
this.viewLevelMap[type] = level;
|
|
|
|
|
this.parentLevel.add(level);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-07-04 10:59:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-04 18:39:30 +08:00
|
|
|
|
* 重绘视图
|
2019-07-04 10:59:40 +08:00
|
|
|
|
* @param {*} mapDevice
|
2019-07-03 18:41:00 +08:00
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
repaint(mapDevice) {
|
|
|
|
|
// 清空视图
|
|
|
|
|
this.clear();
|
2019-07-04 18:39:30 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
// 创建视图
|
|
|
|
|
Object.values(mapDevice).forEach(device => {
|
|
|
|
|
device && this.add(device);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-07-04 18:39:30 +08:00
|
|
|
|
|
2019-07-04 10:59:40 +08:00
|
|
|
|
/**
|
2019-07-03 14:29:09 +08:00
|
|
|
|
* 添加视图
|
2019-07-04 10:59:40 +08:00
|
|
|
|
* @param {*} device
|
2019-07-03 14:29:09 +08:00
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
add(device) {
|
|
|
|
|
const type = device._type;
|
|
|
|
|
const code = device._code;
|
|
|
|
|
const view = shapefactory(type, device, this.$jmap.getStyleDict());
|
|
|
|
|
if (view) {
|
|
|
|
|
this.$zoomHandle.transformView(view);
|
|
|
|
|
this.viewInstance[code] = view;
|
|
|
|
|
this.viewLevelMap[type].add(view);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-04 10:59:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-03 14:29:09 +08:00
|
|
|
|
* 删除视图
|
2019-07-04 10:59:40 +08:00
|
|
|
|
* @param {*} device
|
2019-07-03 14:29:09 +08:00
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
delete(device) {
|
|
|
|
|
const code = device._code;
|
|
|
|
|
const type = device._type;
|
|
|
|
|
const view = this.viewInstance[code];
|
|
|
|
|
if (view) {
|
|
|
|
|
this.viewLevelMap[type].remove(view);
|
|
|
|
|
delete this.viewInstance[code];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-04 10:59:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-03 14:29:09 +08:00
|
|
|
|
* 更新视图
|
2019-07-04 10:59:40 +08:00
|
|
|
|
* @param {*} device
|
2019-07-03 14:29:09 +08:00
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
update(device) {
|
|
|
|
|
const code = device._code;
|
|
|
|
|
const view = this.viewInstance[code];
|
|
|
|
|
if (view) {
|
|
|
|
|
view.setState(device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-05 00:40:53 +08:00
|
|
|
|
* 更新zoom
|
2019-07-11 17:58:58 +08:00
|
|
|
|
* @param {*} zoom
|
2019-07-05 00:40:53 +08:00
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
updateZoomTransform(zoom) {
|
|
|
|
|
this.$zoomHandle.updateTransform(zoom);
|
|
|
|
|
}
|
2019-07-08 09:38:39 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
/**
|
2019-07-05 00:40:53 +08:00
|
|
|
|
* 视图是否存在
|
2019-07-11 17:58:58 +08:00
|
|
|
|
* @param {*} code
|
2019-07-05 00:40:53 +08:00
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
hasViewInstance(code) {
|
|
|
|
|
return this.viewInstance[code];
|
|
|
|
|
}
|
2019-07-05 00:40:53 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
/**
|
2019-07-04 18:39:30 +08:00
|
|
|
|
* 刷新画布,将在下一个渲染帧的时候被刷新。
|
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
refresh() {
|
|
|
|
|
this.$jmap.getZr() && this.$jmap.getZr().refresh();
|
|
|
|
|
}
|
2019-07-04 10:59:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-03 18:41:00 +08:00
|
|
|
|
* 清除所有对象和画布
|
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
clear() {
|
|
|
|
|
// 清空视图
|
|
|
|
|
this.viewInstance = {};
|
2019-07-04 18:39:30 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
// 清空图层
|
|
|
|
|
zrUtil.each(Object.values(this.viewLevelMap), (level) => {
|
|
|
|
|
level && level.removeAll();
|
|
|
|
|
});
|
2019-07-04 18:39:30 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
this.refresh();
|
|
|
|
|
}
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
2019-07-04 10:59:40 +08:00
|
|
|
|
/**
|
2019-07-03 18:41:00 +08:00
|
|
|
|
* 销毁 ZRender 实例
|
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
dispose() {
|
|
|
|
|
this.viewInstance = {};
|
|
|
|
|
this.viewLevelMap = {};
|
|
|
|
|
this.parentLevel = null;
|
|
|
|
|
}
|
2019-07-10 12:02:02 +08:00
|
|
|
|
|
2019-07-11 17:58:58 +08:00
|
|
|
|
/**
|
2019-07-10 12:02:02 +08:00
|
|
|
|
* 获取父级图层
|
|
|
|
|
*/
|
2019-07-11 17:58:58 +08:00
|
|
|
|
getParentLevel() {
|
|
|
|
|
return this.parentLevel;
|
|
|
|
|
}
|
2019-07-03 14:29:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-04 10:59:40 +08:00
|
|
|
|
export default Painter;
|