rt-sim-training-client/src/iscs/iscs.js

303 lines
9.5 KiB
JavaScript
Raw Normal View History

2020-01-13 15:25:10 +08:00
import zrender from 'zrender';
import localStore from 'storejs';
import Options from './options';
import MouseController from './mouseController';
import Painter from './painter';
import deviceType from './constant/deviceType';
import {calculateDCenter, createBoundingRect, deviceFactory} from './utils/parser';
2020-01-14 09:06:03 +08:00
import { updateIscsData } from './utils/parser';
2020-01-19 13:49:55 +08:00
import store from '@/store';
2020-01-13 15:25:10 +08:00
const renderer = 'canvas';
const devicePixelRatio = 1;
2020-01-14 09:06:03 +08:00
class Iscs {
2020-01-13 15:25:10 +08:00
constructor(opts) {
this.methods = opts.methods;
// 鼠标事件
this.events = { __Pan: 'pan', Selected: 'selected', Contextmenu: 'contextmenu'};
// 设备数据
2020-01-14 09:06:03 +08:00
this.iscsDevice = {};
2020-01-13 15:25:10 +08:00
// 展示的画布大小
this.canvasSize = {};
2020-01-14 09:06:03 +08:00
this.initIscsPage(opts);
2020-01-13 15:25:10 +08:00
}
2020-01-14 09:06:03 +08:00
initIscsPage(opts) {
2020-01-13 15:25:10 +08:00
const width = opts.config.width;
const height = opts.config.height;
this.isAllowDragging = false;
2020-01-14 09:06:03 +08:00
this.$iscsZr = zrender.init(opts.dom, Object.assign({ renderer, devicePixelRatio, width, height }, opts.config));
2020-01-13 15:25:10 +08:00
this.$options = new Options(Object.assign({ scaleRate: 1, offsetX: 0, offsetY: 0 }, opts.options || {})); // 缩放
this.$mouseController = new MouseController(this);
this.$mouseController.enable();
this.$painter = new Painter(this);
2020-01-14 09:06:03 +08:00
this.$painter.updateZrSize({width: this.$iscsZr.getWidth(), height: this.$iscsZr.getHeight()});
2020-01-13 15:25:10 +08:00
this.$painter.updateTransform(this.$options, this.canvasSize);
this.optionsHandler = this.setOptions.bind(this);
this.$mouseController.on(this.events.__Pan, this.optionsHandler);
}
2020-01-14 09:06:03 +08:00
setIscs(config, iscsDevice) {
2020-01-13 15:25:10 +08:00
// 保存平移缩放数据
if (config.config) {
this.$options.scaleRate = config.scaling;
this.$options.offsetX = config.origin.x;
this.$options.offsetY = config.origin.y;
}
// 保存原始数据
this.data = config;
// 保存需展现的画布大小
this.canvasSize = {
x: 0,
y: 0,
2020-01-15 13:27:07 +08:00
width: config.width,
height: config.height
2020-01-13 15:25:10 +08:00
};
// 地图数据
2020-01-14 09:06:03 +08:00
this.iscsDevice = iscsDevice;
2020-01-13 15:25:10 +08:00
// 数据加载完成 回调
2020-01-14 09:06:03 +08:00
if (this.methods.dataLoaded instanceof Function) { this.methods.dataLoaded(this.iscsDevice); }
2020-01-13 15:25:10 +08:00
// 初次渲染视图
2020-01-14 09:06:03 +08:00
this.$painter.repaint(this.iscsDevice);
2020-01-13 15:25:10 +08:00
// 视图加载完成 回调
2020-01-14 09:06:03 +08:00
if (this.methods.viewLoaded instanceof Function) { this.methods.viewLoaded(this.iscsDevice); }
2020-01-13 15:25:10 +08:00
this.$painter.updateTransform(this.$options, this.canvasSize);
}
setDefaultState() {
const list = [];
Object.values(this.mapDevice).forEach(elem => {
const type = elem.model._type;
list.push(deviceFactory(type, Object.assign(elem.model, this.defaultStateDict[type]) ));
});
this.update(list);
if (this.methods.stateLoaded instanceof Function) { this.methods.stateLoaded(list); }
}
setOptions(opts) {
const options = this.pullBack(opts);
this.$options.update(options);
this.$painter.updateTransform(this.$options, this.canvasSize);
if (this.$options.disabled == true) {
this.$mouseController.disable();
} else {
this.$mouseController.enable(opts);
}
if (this.methods.optionsUpdate instanceof Function) { this.methods.optionsUpdate(this.$options); }
}
setCenter(deviceCode) {
2020-01-14 09:06:03 +08:00
const device = this.iscsDevice[deviceCode];
2020-01-13 15:25:10 +08:00
if (device && device.instance) {
var rect = createBoundingRect(device.instance);
2020-01-14 09:06:03 +08:00
var dcenter = calculateDCenter(rect, { width: this.$iscsZr.getWidth(), height: this.$iscsZr.getHeight() });
2020-01-13 15:25:10 +08:00
this.setOptions(dcenter);
}
}
setLevelVisible(list) {
this.$painter.setLevelVisible(list);
}
render(list) {
(list || []).forEach(elem => {
const code = elem.code;
const type = elem._type;
2020-01-19 14:09:15 +08:00
updateIscsData(store.state.iscs, elem);
2020-01-14 09:06:03 +08:00
const oDevice = this.iscsDevice[code] || deviceFactory(type, elem);
2020-01-13 15:25:10 +08:00
const nDevice = deviceFactory(type, Object.assign(oDevice.model || {}, elem));
2020-01-14 09:06:03 +08:00
delete this.iscsDevice[code];
2020-01-13 15:25:10 +08:00
this.$painter.delete(oDevice);
if (!elem._dispose) {
2020-01-14 09:06:03 +08:00
this.iscsDevice[code] = nDevice;
2020-01-13 15:25:10 +08:00
this.$painter.add(nDevice);
}
});
if (this.methods.viewUpdate instanceof Function) { this.methods.viewUpdate(list); }
}
// 中间处理
hookHandle(model, elem) {
const code = elem.code;
const type = elem._type;
// 如果是延时计时,需要保存计数值到全局
if (type === deviceType.StationCounter) {
let val = '' + elem.val;
if (val === '0' || !elem.val) {
val = elem.val = localStore.get(code) || '0';
}
localStore(code, val);
}
for (var prop in elem) {
if (elem[prop] != model[prop]) {
Object.assign(model, elem);
return true;
}
}
return false;
}
update(list) {
(list || []).forEach(elem => {
const code = elem.code;
2020-01-14 09:06:03 +08:00
const oDevice = this.iscsDevice[code];
2020-01-13 15:25:10 +08:00
if (elem.dispose) {
this.$painter.delete(oDevice);
} else {
if (this.hookHandle(oDevice.model, elem)) {
this.$painter.update(oDevice);
}
}
});
if (this.methods.stateUpdate instanceof Function) { this.methods.stateUpdate(list); }
}
setStatus(code, model) {
2020-01-14 09:06:03 +08:00
const oDevcie = this.iscsDevice[code].instance;
2020-01-13 15:25:10 +08:00
oDevcie.setStatus(model);
}
setDeviceStatus(list) {
2020-01-14 09:06:03 +08:00
const deviceList = Object.values(this.iscsDevice);
2020-01-13 15:25:10 +08:00
deviceList.forEach(elem =>{
(list || []).forEach(it =>{
if (elem.model.linkDevice === it.code) {
elem.instance.setStatus(it);
}
});
});
}
2020-01-14 09:06:03 +08:00
drawIscsInit() {
2020-01-13 15:25:10 +08:00
this.$mouseController.setAllowDragging(true);
}
pullBack(payload) {
if (payload.type === 'zoom') {
2020-01-14 09:06:03 +08:00
const zrWidth = this.$iscsZr.getWidth();
const zrHeight = this.$iscsZr.getHeight();
2020-01-13 15:25:10 +08:00
const originX = payload.originX || zrWidth / 2;
const originY = payload.originY || zrHeight / 2;
const x = (this.$options.offsetX + originX) / this.$options.scaleRate;
const y = (this.$options.offsetY + originY) / this.$options.scaleRate;
const newScaleRate = this.$options.getScaleRate(payload.scale);
const dx = originX - (x * newScaleRate - this.$options.offsetX);
const dy = originY - (y * newScaleRate - this.$options.offsetY);
payload.dx = dx;
payload.dy = dy;
}
return payload || {};
}
getZr() {
2020-01-14 09:06:03 +08:00
return this.$iscsZr;
2020-01-13 15:25:10 +08:00
}
getEvents() {
return this.events;
}
getDeviceByCode(code) {
2020-01-14 09:06:03 +08:00
return this.iscsDevice[code];
2020-01-13 15:25:10 +08:00
}
resize(opt) {
2020-01-14 09:06:03 +08:00
this.$iscsZr.resize(opt);
2020-01-13 15:25:10 +08:00
this.$painter.updateZrSize(opt);
}
refresh() {
this.$painter.refresh();
}
clear() {
this.skinCode = '';
this.style = {};
2020-01-14 09:06:03 +08:00
this.iscsDevice = {};
2020-01-13 15:25:10 +08:00
this.$painter.clear();
}
dispose() {
this.off(this.events.Pan, this.optionsHandler);
this.clear();
this.$mouseController.dispose();
2020-01-14 09:06:03 +08:00
this.$iscsZr && zrender.dispose(this.$iscsZr);
2020-01-13 15:25:10 +08:00
this.$painter.dispose();
}
on(eventname, cb, context) {
const idx = Object.values(this.events).indexOf(eventname);
if (idx >= 0) {
switch (eventname) {
2020-01-14 09:06:03 +08:00
case this.events.Selected:
this.$mouseController.on(this.events.Selected, cb, context);
break;
case this.events.Contextmenu:
this.$mouseController.on(this.events.Contextmenu, cb, context);
break;
case this.events.DataZoom:
this.$mouseController.on(this.events.DataZoom, cb, context);
break;
2020-01-13 15:25:10 +08:00
}
}
}
off(eventname, cb) {
const idx = Object.values(this.events).indexOf(eventname);
if (idx >= 0) {
switch (eventname) {
2020-01-14 09:06:03 +08:00
case this.events.Selected:
this.$mouseController.off(this.events.Selected, cb);
break;
case this.events.Contextmenu:
this.$mouseController.off(this.events.Contextmenu, cb);
break;
case this.events.DataZoom:
this.$mouseController.off(this.events.DataZoom, cb);
break;
2020-01-13 15:25:10 +08:00
}
}
}
renderCheckBox(model) {
const type = model._type;
const code = model.code;
2020-01-14 09:06:03 +08:00
const oDevice = this.iscsDevice[code] || deviceFactory(type, model);
2020-01-13 15:25:10 +08:00
const nDevice = deviceFactory(type, Object.assign(oDevice.model || {}, model));
2020-01-14 09:06:03 +08:00
delete this.iscsDevice[code];
2020-01-13 15:25:10 +08:00
this.$painter.delete(oDevice);
if (!model._dispose) {
2020-01-14 09:06:03 +08:00
this.iscsDevice[code] = nDevice;
2020-01-13 15:25:10 +08:00
this.$painter.add(nDevice);
}
}
deleteCheckBox(code) {
2020-01-14 09:06:03 +08:00
const oDevice = this.iscsDevice[code];
2020-01-13 15:25:10 +08:00
if (oDevice) {
2020-01-14 09:06:03 +08:00
delete this.iscsDevice[code];
2020-01-13 15:25:10 +08:00
this.$painter.delete(oDevice);
}
}
}
2020-01-14 09:06:03 +08:00
export default Iscs;