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-03 18:41:00 +08:00
|
|
|
constructor(jamp, opts) {
|
|
|
|
// 父级实例
|
2019-07-03 14:29:09 +08:00
|
|
|
this.$jamp = jamp;
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
|
|
// zrender实例
|
2019-07-03 14:29:09 +08:00
|
|
|
this.$zr = null;
|
|
|
|
|
2019-07-03 18:41:00 +08:00
|
|
|
// 图层数据
|
|
|
|
this.viewLevelMap = {};
|
|
|
|
|
|
|
|
// 视图数据
|
2019-07-03 14:29:09 +08:00
|
|
|
this.viewInstance = {};
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
|
|
// 父级图层
|
|
|
|
this.parentLevel = new Group({ name: '__parent__' });
|
|
|
|
|
|
|
|
// 挂载视图
|
|
|
|
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-03 18:41:00 +08:00
|
|
|
/**
|
|
|
|
* 挂载视图
|
|
|
|
* @param {*} dom
|
|
|
|
* @param {*} config
|
|
|
|
*/
|
2019-07-03 14:29:09 +08:00
|
|
|
mount(dom, config) {
|
2019-07-03 18:41:00 +08:00
|
|
|
// 挂载页面视图
|
|
|
|
this.$zr = zrender.init(dom, config);
|
|
|
|
|
|
|
|
// 添加父级图层
|
|
|
|
this.$zr.add(this.parentLevel);
|
|
|
|
|
|
|
|
// 添加子级图层
|
2019-07-03 14:29:09 +08:00
|
|
|
Object.values(deviceType).forEach(type => {
|
2019-07-03 18:41:00 +08:00
|
|
|
let level = new Group({ name: `__${type}__` })
|
|
|
|
this.viewLevelMap[type] = level;
|
|
|
|
this.parentLevel.add(level);
|
2019-07-03 14:29:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-03 18:41:00 +08:00
|
|
|
/**
|
|
|
|
* 初次渲染视图
|
|
|
|
* @param {*} mapDevice
|
|
|
|
*/
|
2019-07-04 09:20:51 +08:00
|
|
|
render(mapDevice, styleDict) {
|
2019-07-03 18:41:00 +08:00
|
|
|
// 清空视图
|
2019-07-03 14:29:09 +08:00
|
|
|
this.viewInstance = {};
|
|
|
|
|
|
|
|
// 清空图层
|
2019-07-03 18:41:00 +08:00
|
|
|
Object.values(this.viewLevelMap).forEach(level => {
|
2019-07-03 14:29:09 +08:00
|
|
|
level && level.removeAll();
|
|
|
|
})
|
|
|
|
|
2019-07-03 18:41:00 +08:00
|
|
|
// 创建视图
|
2019-07-03 14:29:09 +08:00
|
|
|
Object.values(mapDevice).forEach(device => {
|
2019-07-04 09:20:51 +08:00
|
|
|
device && this.add(device, styleDict);
|
2019-07-03 14:29:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加视图
|
|
|
|
* @param {*} device
|
|
|
|
*/
|
2019-07-04 09:20:51 +08:00
|
|
|
add(device, styleDict) {
|
2019-07-03 18:41:00 +08:00
|
|
|
let type = device._type;
|
|
|
|
let code = device._code;
|
2019-07-04 09:20:51 +08:00
|
|
|
let view = shapefactory(type, device, styleDict);
|
2019-07-03 14:29:09 +08:00
|
|
|
if (view) {
|
|
|
|
this.viewInstance[code] = view;
|
2019-07-03 18:41:00 +08:00
|
|
|
this.viewLevelMap[type].add(view);
|
2019-07-03 14:29:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除视图
|
|
|
|
* @param {*} device
|
|
|
|
*/
|
|
|
|
delete(device) {
|
2019-07-03 18:41:00 +08:00
|
|
|
let code = device._code;
|
|
|
|
let type = device._type;
|
2019-07-03 14:29:09 +08:00
|
|
|
let view = this.viewInstance[code];
|
|
|
|
if (view) {
|
2019-07-03 18:41:00 +08:00
|
|
|
this.viewLevelMap[type].remove(view);
|
2019-07-03 14:29:09 +08:00
|
|
|
delete this.viewInstance[code];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新视图
|
|
|
|
* @param {*} device
|
|
|
|
*/
|
|
|
|
update(device) {
|
2019-07-03 18:41:00 +08:00
|
|
|
let code = device._code;
|
2019-07-03 14:29:09 +08:00
|
|
|
let view = this.viewInstance[code];
|
|
|
|
if (view) {
|
2019-07-03 18:41:00 +08:00
|
|
|
view.setStatus(device);
|
2019-07-03 14:29:09 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-03 18:41:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 清除所有对象和画布
|
|
|
|
*/
|
|
|
|
clear() {
|
|
|
|
this.$zr && this.$zr.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 销毁 ZRender 实例
|
|
|
|
*/
|
|
|
|
dispose() {
|
|
|
|
this.$zr && zrender.dispose(this.$zr);
|
|
|
|
this.$zr = null;
|
|
|
|
this.viewInstance = {};
|
|
|
|
this.viewLevelMap = {};
|
|
|
|
}
|
2019-07-03 14:29:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Painter;
|