import * as zrUtil from 'zrender/src/core/util'; import Painter from './painter'; import ProxyHandle from './proxyHandle'; import deviceState from './config/deviceState'; import deviceStyle from './config/deviceStyle'; import deviceType from './config/deviceType'; import { parser, deviceFactory } from './utils/parser'; class Jmap { constructor(opts) { // 挂载的dom节点 this.$dom = opts.dom; // 原始数据 this.data = {}; // 默认初始状态 this.defaultStateDict = this.loaddefaultState(); // 皮肤参数 this.skinStyle = ''; // 皮肤风格 this.styleDict = {}; // 设备数据 this.mapDevice = {}; // 设备代理数据 this.proxyData = {}; // 绘图模块 this.$painter = new Painter(this, opts); } /** * 加载数据 * @param {*} data */ loadData(skinStyle, data) { // 保存原始数据 this.data = data; // 加载皮肤 this.styleDict = this.loadStyle(skinStyle); console.log('开始解析'); // 解析地图数据 this.mapDevice = parser(data, this.defaultStateDict); console.log('解析数据完成'); // 生成代理对象 this.proxyData = new Proxy(this.mapDevice, new ProxyHandle(this)); console.log('数据加载完成: dataLoaded'); // 初次渲染视图 this.$painter.repaint(this.mapDevice, this.styleDict); console.log('状态加载完成: stateLoaded'); } loadStyle(skinStyle) { return deviceStyle; } loaddefaultState() { const defaultStateDict = {}; zrUtil.each(Object.keys(deviceState), (type) => { defaultStateDict[type] = {}; zrUtil.each(Object.keys(deviceState[type] || {}), (state) => { defaultStateDict[type][state] = deviceState[type][state].Default; }) }) return defaultStateDict; } syncData(model) { let code = model.code; let type = model.type; let dispose = model._dispose; let prop = null; switch (type) { case deviceType.Link: prop = 'linkList'; break; } let list = this.data[prop]; if (list) { let idex = list.findIndex(elem => { return elem.code == code }); if (idex >= 0) { dispose && list.splice(idex, 1); } else { list.push(model); } } } render(list) { (list || []).forEach(elem => { let type = elem.type; let code = elem.code; let device = this.proxyData[code] || deviceFactory(type, this.styleDict[type], elem); let model = Object.assign(device.model || {}, elem); delete this.proxyData[code]; if (!elem._dispose) { device = this.proxyData[code] = Object.assign(device, { model }); } // this.syncData(model); }) } update(list) { (list || []).forEach(elem => { let code = elem.code; if (elem._dispose) { delete this.proxyData[code]; } else { let device = this.proxyData[code] || {}; let state = Object.assign(device.state || {}, elem); this.proxyData[code] = Object.assign(device, { state }); } }) } getPainter() { return this.$painter; } getSkinStyle() { return this.skinStyle; } getStyleDict() { return this.styleDict; } getDefaultStateDict() { return this.defaultStateDict; } getMapDevice() { return this.mapDevice; } refresh() { this.$painter.refresh(); } clear() { this.$painter.clear(); } dispose() { this.skinStyle = ''; this.styleDict = {}; this.mapDevice = {}; this.proxyData = {}; this.$painter.dispose(); } } export default Jmap;