2019-07-03 14:29:09 +08:00
|
|
|
|
import zrender from 'zrender';
|
|
|
|
|
import Group from 'zrender/src/container/Group';
|
|
|
|
|
import deviceType from './config/deviceType';
|
|
|
|
|
import shapefactory from './shape/factory';
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
|
|
|
|
const renderer = 'svg';
|
|
|
|
|
const devicePixelRatio = 2;
|
2019-07-03 14:29:09 +08:00
|
|
|
|
|
|
|
|
|
class Painter {
|
2019-07-04 18:39:30 +08:00
|
|
|
|
constructor(jamp, opts) {
|
|
|
|
|
// 父级实例
|
|
|
|
|
this.$jamp = jamp;
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
2019-07-04 18:39:30 +08:00
|
|
|
|
// zrender实例
|
|
|
|
|
this.$zr = null;
|
2019-07-03 14:29:09 +08:00
|
|
|
|
|
2019-07-04 18:39:30 +08:00
|
|
|
|
// 图层数据
|
|
|
|
|
this.viewLevelMap = {};
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
2019-07-04 18:39:30 +08:00
|
|
|
|
// 视图数据
|
|
|
|
|
this.viewInstance = {};
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
2019-07-04 18:39:30 +08:00
|
|
|
|
// 父级图层
|
|
|
|
|
this.parentLevel = new Group({ name: '__parent__' });
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
2019-07-04 18:39:30 +08:00
|
|
|
|
// 挂载视图
|
|
|
|
|
this.mount(opts.dom, Object.assign({ renderer, devicePixelRatio, width: opts.dom.width, height: opts.dom.clientHeight }, opts.config));
|
|
|
|
|
}
|
2019-07-03 14:29:09 +08:00
|
|
|
|
|
2019-07-04 10:59:40 +08:00
|
|
|
|
/**
|
2019-07-03 18:41:00 +08:00
|
|
|
|
* 挂载视图
|
2019-07-04 10:59:40 +08:00
|
|
|
|
* @param {*} dom
|
|
|
|
|
* @param {*} config
|
2019-07-03 18:41:00 +08:00
|
|
|
|
*/
|
2019-07-04 18:39:30 +08:00
|
|
|
|
mount(dom, config) {
|
|
|
|
|
// 挂载页面视图
|
|
|
|
|
this.$zr = zrender.init(dom, config);
|
2019-07-04 10:59:40 +08:00
|
|
|
|
|
2019-07-04 18:39:30 +08:00
|
|
|
|
// 添加父级图层
|
|
|
|
|
this.$zr.add(this.parentLevel);
|
2019-07-04 10:59:40 +08:00
|
|
|
|
|
2019-07-04 18:39:30 +08:00
|
|
|
|
// 添加子级图层
|
|
|
|
|
Object.values(deviceType).forEach(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-04 18:39:30 +08:00
|
|
|
|
repaint(mapDevice, styleDict) {
|
|
|
|
|
// 清空视图
|
|
|
|
|
this.clear();
|
|
|
|
|
|
|
|
|
|
// 创建视图
|
|
|
|
|
Object.values(mapDevice).forEach(device => {
|
|
|
|
|
device && this.add(device);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 视图是否存在
|
|
|
|
|
* @param {*} code
|
|
|
|
|
*/
|
|
|
|
|
hasViewInstance(code) {
|
|
|
|
|
return this.viewInstance[code];
|
|
|
|
|
}
|
2019-07-03 14:29:09 +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-04 18:39:30 +08:00
|
|
|
|
add(device) {
|
|
|
|
|
const type = device._type;
|
|
|
|
|
const code = device._code;
|
|
|
|
|
const view = shapefactory(type, device, this.$jamp.getStyleDict());
|
|
|
|
|
if (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-04 18:39:30 +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-04 18:39:30 +08:00
|
|
|
|
update(device) {
|
|
|
|
|
const code = device._code;
|
|
|
|
|
const view = this.viewInstance[code];
|
|
|
|
|
if (view) {
|
|
|
|
|
view.setState(device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新画布,将在下一个渲染帧的时候被刷新。
|
|
|
|
|
*/
|
|
|
|
|
refresh() {
|
|
|
|
|
this.$zr && this.$zr.refresh();
|
|
|
|
|
}
|
2019-07-04 10:59:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-03 18:41:00 +08:00
|
|
|
|
* 清除所有对象和画布
|
|
|
|
|
*/
|
2019-07-04 18:39:30 +08:00
|
|
|
|
clear() {
|
|
|
|
|
// 清空视图
|
|
|
|
|
this.viewInstance = {};
|
|
|
|
|
|
|
|
|
|
// 清空图层
|
|
|
|
|
Object.values(this.viewLevelMap).forEach(level => {
|
|
|
|
|
level && level.removeAll();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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-04 18:39:30 +08:00
|
|
|
|
dispose() {
|
|
|
|
|
this.$zr && zrender.dispose(this.$zr);
|
|
|
|
|
this.$zr = null;
|
|
|
|
|
this.viewInstance = {};
|
|
|
|
|
this.viewLevelMap = {};
|
|
|
|
|
}
|
2019-07-03 14:29:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-04 10:59:40 +08:00
|
|
|
|
export default Painter;
|