-
Notifications
You must be signed in to change notification settings - Fork 0
/
Container.ts
320 lines (307 loc) · 9.58 KB
/
Container.ts
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
308
309
310
311
312
313
314
315
316
317
318
319
320
import { Shape } from "./Shape";
import { Point } from "./Point";
import { CanvasContext } from "./CanvasContext";
export class Container {
type: string = "Container";
className: string = "Container";
children: Array<any> = new Array<any>();
parent: any = null;//添加到group或者层 child.parent = this;
index: number;
constructor() {
}
//绘制该组所有内容
draw(ctx: CanvasContext, hitCtx: CanvasContext) {
this.children.forEach(obj => {
obj.draw(ctx, hitCtx);
})
}
getIndex() {
return this.index;
}
add(obj: any) {
if (this == obj) {
console.error("can not add yourself !")
return false;
}
var child = this.getChildren((ch: any) => { return ch == obj });
if (this.validateAdd(obj) && child.length == 0) {
this.children.push(obj);
obj.index = this.children.length - 1;
obj.setParent(this);
return true;
}
return false;
}
/**
* validate 验证添加
* @param child
*/
validateAdd(child: any) {
return true;
}
setParent(parent: any) {
this.parent = parent;
}
getParent() {
return this.parent;
}
/**
* destroy all children
* @method
* @memberof Konva.Container.prototype
*/
destroyChildren() {
var child;
for (var i = 0; i < this.children.length; i++) {
child = this.children[i];
// reset parent to prevent many setChildrenIndex calls
delete child.parent;
child.index = 0;
child.removeSelf();
}
this.children = null;
this.children = new Array<any>();
return this;
}
destroy() {
// destroy children
if (this.hasChildren()) {
this.destroyChildren();
}
this.removeSelf();
return this;
}
removeSelf() {
var parent = this.getParent();
if (parent && parent.children) {
parent.children.splice(this.index, 1);
parent.setChildrenIndex();
delete this.parent;
}
}
hasChildren() {
return this.getChildren().length > 0;
}
getChildren(filterFunc: Function = null) {
if (!filterFunc) {
return this.children;
}
var results = new Array<any>();
this.children.forEach(function (child) {
if (filterFunc(child)) {
results.push(child);
}
});
return results;
}
getDeepChildren(filterFunc: Function = null) {
if (!filterFunc) {
return this.children;
}
var results = new Array<any>();
this.children.forEach(function (child) {
if (child.getDeepChildren)
results = results.concat(child.getDeepChildren(filterFunc))
else
if (filterFunc(child)) {
results.push(child);
}
});
return results;
}
moveBy(diffX: number, diffY: number) {
this.children.forEach(obj => {
obj.moveBy(diffX, diffY);
})
}
setChildrenIndex() {
this.children.forEach(function (child, n) {
child.index = n;
});
}
setIndex(index: number) {
if (!this.parent) {
console.error('Node has no parent. zIndex parameter is ignored.');
return false;
}
var index = this.index;
this.parent.children.splice(index, 1);
this.parent.children.splice(index, 0, this);
this.parent.setChildrenIndex();
return this;
}
moveToTop() {
if (!this.parent) {
console.error('Node has no parent. moveToTop function is ignored.');
return false;
}
var index = this.index;
this.parent.children.splice(index, 1);
this.parent.children.push(this);
this.parent.setChildrenIndex();
return true;
}
moveUp() {
if (!this.parent) {
console.error('Node has no parent. moveUp function is ignored.');
return null;
}
var index = this.index,
len = this.parent.getChildren().length;
if (index < len - 1) {
this.parent.children.splice(index, 1);
this.parent.children.splice(index + 1, 0, this);
this.parent.setChildrenIndex();
return this;
}
return null;
}
moveDown() {
if (!this.parent) {
console.error('Node has no parent. moveDown function is ignored.');
return null;
}
var index = this.index;
if (index > 0) {
this.parent.children.splice(index, 1);
this.parent.children.splice(index - 1, 0, this);
this.parent.setChildrenIndex();
return this;
}
return null;
}
moveToBottom() {
if (!this.parent) {
console.error(
'Node has no parent. moveToBottom function is ignored.'
);
return null;
}
var index = this.index;
if (index > 0) {
this.parent.children.splice(index, 1);
this.parent.children.unshift(this);
this.parent.setChildrenIndex();
return this;
}
return null;
}
moveToContainer(newContainer: any) {
// do nothing if new container is already parent
if (this.getParent() !== newContainer) {
// this.remove my be overrided by drag and drop
// buy we need original
var parent = this.getParent();
if (parent && parent.children) {
parent.children.splice(this.index, 1);
parent.setChildrenIndex();
delete this.parent;
}
newContainer.add(this);
}
return this;
}
getPointRange() {
let passX: Array<number> = [];
let passY: Array<number> = [];
this.children.forEach(obj => {
let range = obj.getPointRange();
passX.push(range.minX);
passX.push(range.maxX);
passY.push(range.minY);
passY.push(range.maxY);
})
return {
minX: Math.min(...passX),
minY: Math.min(...passY),
maxX: Math.max(...passX),
maxY: Math.max(...passY),
}
}
findAncestor(selector: string, isIncludeSelf: boolean) {
return this.findAncestors(selector, isIncludeSelf)[0];
}
findAncestors(selector: string, isIncludeSelf: boolean) {
var res = [];
if (isIncludeSelf && this.type == selector) {
res.push(this);
}
var ancestor = this.parent;
while (ancestor) {
if (ancestor.type == selector) {
res.push(ancestor);
}
ancestor = ancestor.parent;
}
return res;
}
clone(obj: any) {
// call super method
// var node = this._clone(obj);
// this.children.forEach(element => {
// node.add(element.clone());
// });
// return node;
}
private _clone(obj: any) {
// instantiate new node
// var attrs = this._cloneObject(this.attrs),
// key,
// allListeners,
// len,
// n,
// listener;
// filter black attrs
// for (var i in CLONE_BLACK_LIST) {
// var blockAttr = CLONE_BLACK_LIST[i];
// delete attrs[blockAttr];
// }
// // apply attr overrides
// for (key in obj) {
// attrs[key] = obj[key];
// }
// var node = new this.constructor(attrs);
// // copy over listeners
// for (key in this.eventListeners) {
// allListeners = this.eventListeners[key];
// len = allListeners.length;
// for (n = 0; n < len; n++) {
// listener = allListeners[n];
// /*
// * don't include konva namespaced listeners because
// * these are generated by the constructors
// */
// if (listener.name.indexOf(KONVA) < 0) {
// // if listeners array doesn't exist, then create it
// if (!node.eventListeners[key]) {
// node.eventListeners[key] = [];
// }
// node.eventListeners[key].push(listener);
// }
// }
// }
// return node;
};
private _cloneObject(obj: any) {
var retObj: any = {};
for (var key in obj) {
if (this._isObject(obj[key])) {
retObj[key] = this._cloneObject(obj[key]);
} else if (this._isArray(obj[key])) {
retObj[key] = this._cloneArray(obj[key]);
} else {
retObj[key] = obj[key];
}
}
return retObj;
}
private _cloneArray(arr: Array<any>) {
return arr.slice(0);
}
private _isObject(obj: any) {
return !!obj && obj.constructor === Object;
}
private _isArray(obj: any) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
}