-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathbaseNode.js
307 lines (286 loc) · 7.6 KB
/
baseNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const $ = require('../utils/tiny-jquery');
const _ = require('../utils/tiny-lodash');
import Node from '../interface/node';
import Endpoint from '../endpoint/baseEndpoint';
class BaseNode extends Node {
constructor(opts) {
super(opts);
this.id = opts.id;
this.scope = opts.scope;
this.group = opts.group;
this.top = opts.top || 0;
this.left = opts.left || 0;
this.dom = opts.dom || null;
this.draggable = opts.draggable;
this.options = opts;
// 鸭子辨识手动判断类型
this.__type = 'node';
this._on = opts._on;
this._emit = opts._emit;
this._global = opts._global;
// endpoint 这部分需要考虑
this.endpoints = [];
this._endpointsData = opts.endpoints;
this._endpointLimitNum = opts._endpointLimitNum;
// 标识是否在移动做,兼容冒泡
this._isMoving = false;
// 长宽
this.width = undefined;
this.height = undefined;
this._isForceUpdateSize = false;
}
draw(obj) {
let _dom = obj.dom;
if (!_dom) {
_dom = $('<div></div>')
.attr('class', 'node')
.attr('id', obj.id);
}
const node = $(_dom);
if (obj.top !== undefined) {
node.css('top', `${obj.top}px`);
}
if (obj.left !== undefined) {
node.css('left', `${obj.left}px`);
}
this.updated && this.updated();
return node[0];
}
focus() {}
unFocus() {}
addEndpoint(obj, isInited) {
if (isInited) {
this.emit('InnerEvents', {
type: 'node:addEndpoint',
data: obj,
isInited
});
return obj;
}
// 这部分可能还需要想一下
const EndpointClass = obj.Class || Endpoint;
const endpoint = new EndpointClass(_.assign({
limitNum: obj.limitNum || this._endpointLimitNum,
_on: this._on,
_emit: this._emit,
_node: this,
_global: this._global
}, obj));
this.emit('InnerEvents', {
type: 'node:addEndpoint',
data: endpoint
});
let nodeZindex = $(this.dom).style('z-index');
if (nodeZindex !== 'auto') {
$(endpoint.dom).css('z-index', Number(nodeZindex) + 1);
}
this.endpoints.push(endpoint);
return endpoint;
}
removeEndpoint(pointId) {
const rmEndpointIndex = _.findIndex(this.endpoints, point => point.id === pointId);
if (rmEndpointIndex !== -1) {
const rmEndpoint = this.endpoints.splice(rmEndpointIndex, 1)[0];
this.emit('InnerEvents', {
type: 'node:removeEndpoint',
data: rmEndpoint
});
rmEndpoint.destroy();
return rmEndpoint;
}
}
getEndpoint(pointId, type) {
return _.find(this.endpoints, (point) => {
if (!point.type || point.type === 'onlyConnect') {
return pointId === point.id;
} else {
return pointId === point.id && ((type && type === point.type) || !type);
}
});
}
_init(obj = {}) {
if (this._isInited) {
return;
}
// 这里可以抽象下,和constructor对比
if (obj.left) {
this.left = obj.left;
}
if (obj.top) {
this.top = obj.top;
}
if (obj._isDeleteGroup) {
this.group = undefined;
this._group = undefined;
} else {
obj.group && (this.group = obj.group);
}
delete obj._isDeleteGroup;
this._isInited = true;
if (obj.dom) {
this.dom = obj.dom;
obj.left && ($(this.dom).css('left', `${obj.left}px`));
obj.top && ($(this.dom).css('top', `${obj.top}px`));
} else {
this.dom = this.draw(_.assign({
id: this.id,
top: this.top,
left: this.left,
dom: this.dom,
options: this.options
}, obj));
}
if (!this._hasEventListener) {
this._addEventListener();
this._hasEventListener = true;
}
}
// drag的时候移动的api
_moveTo(x, y) {
// 自身移动
$(this.dom).css({top: y, left: x});
// 所在的点移动
this.endpoints.forEach((item) => {
item.moveTo(x - this.left + item._left, y - this.top + item._top);
});
this.top = y;
this.left = x;
}
moveTo(x, y, isNotEventEmit) {
this.emit('InnerEvents', {
type: 'node:move',
node: this,
x,
y,
isNotEventEmit
});
}
getWidth(useCache) {
if (!useCache || !this.width || this._isForceUpdateSize) {
this.width = $(this.dom).outerWidth();
this._isForceUpdateSize = false;
}
return this.width;
}
getHeight(useCache) {
if (!useCache || !this.height || this._isForceUpdateSize) {
this.height = $(this.dom).outerHeight();
this._isForceUpdateSize = false;
}
return this.height;
}
_createEndpoint(isInited) {
if (isInited) {
this.endpoints.forEach(item => this.addEndpoint(item, isInited));
} else if (this._endpointsData) {
this._endpointsData.map(item => this.addEndpoint(item));
}
}
_addEventListener() {
let _beforeStatus = {
timer: 0,
x: 0,
y: 0
};
// todo 做事件代理的形式
$(this.dom).on('mousedown', (e) => {
const LEFT_KEY = 0;
if (e.button !== LEFT_KEY) {
return;
}
if(_.isFunction(this.canMouseDown) && !this.canMouseDown(e)) {
return;
}
if (!['SELECT', 'INPUT', 'RADIO', 'CHECKBOX', 'TEXTAREA'].includes(e.target.nodeName)) {
e.preventDefault();
}
_beforeStatus = {
timer: new Date().getTime(),
x: this.left,
y: this.top
}
if (this.draggable) {
this._isMoving = true;
this.emit('InnerEvents', {
type: 'node:dragBegin',
data: this
});
} else {
// 单纯为了抛错事件给canvas,为了让canvas的dragtype不为空,不会触发canvas:click事件
this.emit('InnerEvents', {
type: 'node:mouseDown',
data: this
});
}
});
$(this.dom).on('mouseup', (e) => {
if(_.isFunction(this.canMouseUp) && !this.canMouseUp(e)) {
return;
}
let _currentStatus = {
timer: new Date().getTime(),
x: this.left,
y: this.top
};
if (_currentStatus.timer - _beforeStatus.timer < 300 || (Math.abs(_beforeStatus.x - _currentStatus.x) < 20 && Math.abs(_beforeStatus.y - _currentStatus.y) < 20)) {
this.emit('system.node.click', {
node: this
});
this.emit('events', {
type: 'node:click',
node: this
});
}
});
// todo: 尝试使用上面mouseup的机制来代替click的机制
// $(this.dom).on('click', (e) => {
// if(_.isFunction(this.canClick) && !this.canClick(e)) {
// return;
// }
// e.preventDefault();
// e.stopPropagation();
// this.emit('system.node.click', {
// node: this
// });
// this.emit('events', {
// type: 'node:click',
// node: this
// });
// });
this.setDraggable(this.draggable);
}
setDraggable(draggable) {
this.draggable = draggable;
}
remove() {
this.emit('InnerEvents', {
type: 'node:delete',
data: this
});
}
emit(type, data) {
super.emit(type, data);
this._emit(type, data);
}
on(type, callback) {
super.on(type, callback);
this._on(type, callback);
}
destroy(isNotEvent) {
if (!isNotEvent) {
this.endpoints.forEach((item) => {
!item._isInitedDom && item.destroy();
});
$(this.dom).remove();
this.removeAllListeners();
this._hasEventListener = false;
} else {
this.endpoints.forEach((item) => {
!item._isInitedDom && item.destroy(isNotEvent);
});
$(this.dom).detach();
}
this._isInited = false;
}
}
export default BaseNode;