组合可以嵌套处理组合

This commit is contained in:
ival 2021-04-01 12:54:03 +08:00
parent abbc3b96d2
commit a56598888d
5 changed files with 50 additions and 41 deletions

View File

@ -8,18 +8,23 @@ import KeyBoardHandle from './keyboardHandle';
import events from './utils/events'; import events from './utils/events';
class MouseEvent { class MouseEvent {
constructor(e) { constructor(controller, e) {
const shapeFactory = controller.$map.getShapeFactory();
const view = e.target; const view = e.target;
this.clientX = e.event.clientX; this.clientX = e.event.clientX;
this.clientY = e.event.clientY; this.clientY = e.event.clientY;
if (view && !['__selecting__', '__drag__'].includes(view.subType)) { if (view && !['__selecting__', '__drag__'].includes(view.subType)) {
if (view.code) { if (view.code) {
this.code = view.code; let composeCode = view.composeCode;
if (view.composeCode) { let compose = null;
this.code = view.composeCode;
while(composeCode) {
compose = shapeFactory.getShapeByCode(composeCode);
composeCode = compose.model.composeCode;
} }
this.code = compose? compose.model.code: view.code;
} }
if (view.subType) { if (view.subType) {
@ -167,7 +172,7 @@ export default class Controller extends Eventful {
} }
mousedown(e) { mousedown(e) {
const event = new MouseEvent(e); const event = new MouseEvent(this, e);
const zr = this.$map.getZr(); const zr = this.$map.getZr();
this._x = e.offsetX; this._x = e.offsetX;
@ -273,7 +278,7 @@ export default class Controller extends Eventful {
} }
click(e) { click(e) {
const event = new MouseEvent(e); const event = new MouseEvent(this, e);
this.trigger(events.Selected, event); this.trigger(events.Selected, event);
if (event.code) { if (event.code) {
@ -288,7 +293,7 @@ export default class Controller extends Eventful {
contextmenu(e) { contextmenu(e) {
eventTool.stop(e.event); eventTool.stop(e.event);
const event = new MouseEvent(e); const event = new MouseEvent(this, e);
if (!this._pan) { if (!this._pan) {
this.trigger(events.ContextMenu, event); this.trigger(events.ContextMenu, event);
} }

View File

@ -10,9 +10,9 @@ class Compose extends AbstractShape {
// 拖动 // 拖动
draft({dx, dy}) { draft({dx, dy}) {
this.model.elementCodes.forEach(code => { this.model.elementCodes.forEach(code => {
const element = this.shapeFactory.getShapeByCode(code); const el = this.shapeFactory.getShapeByCode(code);
if (element) { if (el) {
element.draft({dx, dy}); el.draft({dx, dy});
} }
}) })
} }
@ -41,11 +41,11 @@ class Compose extends AbstractShape {
this.inactive(); this.inactive();
this.shapeFactory.hideHightLight(this); this.shapeFactory.hideHightLight(this);
this.model.elementCodes.forEach(code => { this.model.elementCodes.forEach(code => {
const element = this.shapeFactory.getShapeByCode(code); const el = this.shapeFactory.getShapeByCode(code);
if (element && if (el &&
element.model) { el.model) {
element.model.composeCode = this.model.code; el.model.composeCode = this.model.code;
element.instance.attr(element.model) el.attr(el.model)
} }
}) })
} }
@ -55,11 +55,12 @@ class Compose extends AbstractShape {
this.inactive(); this.inactive();
this.shapeFactory.hideHightLight(this); this.shapeFactory.hideHightLight(this);
this.model.elementCodes.forEach(code => { this.model.elementCodes.forEach(code => {
const element = this.shapeFactory.getShapeByCode(code); const el = this.shapeFactory.getShapeByCode(code);
if (element && if (el &&
element.model) { el.model &&
element.model.composeCode = ''; el.model.composeCode) {
element.instance.attr(element.model); el.model.composeCode = '';
el.attr(el.model);
} }
}) })
this.model.elementCodes = []; this.model.elementCodes = [];
@ -68,28 +69,30 @@ class Compose extends AbstractShape {
// 修改属性 // 修改属性
attr(attrs) { attr(attrs) {
this.model.elementCodes.forEach(code => { this.model.elementCodes.forEach(code => {
const element = this.shapeFactory.getShapeByCode(code); const el = this.shapeFactory.getShapeByCode(code);
if (element && if (el &&
element.instance) { el) {
element.instance.attr(attrs); el.attr(attrs);
} }
}) })
} }
// 获取依赖图形 // 获取依赖图形
getDepShapes() { getDepShapes() {
return this.model.elementCodes.map(code => this.shapeFactory.getShapeByCode(code)).filter(el => el) return this.model.elementCodes.reduce((shapes, code) => shapes.concat(this.shapeFactory.getShapeByCode(code).getDepShapes()), [this]);
} }
// 获取包围框 // 获取包围框
getBoundingRect() { getBoundingRect() {
let unionRect = null; let unionRect = null;
this.model.elementCodes.forEach(code => { this.model.elementCodes.forEach(code => {
const element = this.shapeFactory.getShapeByCode(code) const el = this.shapeFactory.getShapeByCode(code)
if (element && element.instance && !element.instance.invisible) { const rect = el.getBoundingRect().clone();
const rect = element.instance.getBoundingRect().clone(); rect
unionRect ? unionRect.union(rect): (unionRect = rect); ? unionRect
} ? unionRect.union(rect)
: (unionRect = rect)
: null;
}) })
return unionRect; return unionRect;
} }

View File

@ -134,7 +134,7 @@ class Element extends AbstractShape {
// 获取依赖图形 // 获取依赖图形
getDepShapes() { getDepShapes() {
return [] return [this];
} }

View File

@ -167,7 +167,6 @@ class JMap {
break; break;
case orders.DELETE: case orders.DELETE:
oldShape = this.$shapeFactory.removeShape(shape); oldShape = this.$shapeFactory.removeShape(shape);
this.$painter.remove(oldShape);
deps.forEach(shape => { deps.forEach(shape => {
this.$shapeFactory.updateSource(shape.model, {...action, shapeType: shapeType.Element}); this.$shapeFactory.updateSource(shape.model, {...action, shapeType: shapeType.Element});
this.$painter.remove(shape); this.$painter.remove(shape);
@ -214,6 +213,10 @@ class JMap {
return payload||{}; return payload||{};
} }
getEvents() {
return this.events;
}
getZr() { getZr() {
return this.$zr; return this.$zr;
} }
@ -230,12 +233,8 @@ class JMap {
return this.$controller; return this.$controller;
} }
getEvents() { getShapeFactory() {
return this.events; return this.$shapeFactory;
}
getDefaultState() {
return this.defaultState;
} }
getShapeByCode(code) { getShapeByCode(code) {

View File

@ -226,12 +226,14 @@ export default {
{ {
code: '100', code: '100',
type: 'Device', type: 'Device',
elementCodes: ['1', '2'] elementCodes: ['1', '2'],
composeCode: '101',
}, },
{ {
code: '101', code: '101',
type: 'Device', type: 'Device',
elementCodes: ['3', '4'] elementCodes: ['3', '4', '100'],
composeCode: ''
} }
] ]
}, { }, {