1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.Container = void 0;
|
---|
4 | const Factory_1 = require("./Factory");
|
---|
5 | const Node_1 = require("./Node");
|
---|
6 | const Validators_1 = require("./Validators");
|
---|
7 | class Container extends Node_1.Node {
|
---|
8 | constructor() {
|
---|
9 | super(...arguments);
|
---|
10 | this.children = [];
|
---|
11 | }
|
---|
12 | getChildren(filterFunc) {
|
---|
13 | if (!filterFunc) {
|
---|
14 | return this.children || [];
|
---|
15 | }
|
---|
16 | const children = this.children || [];
|
---|
17 | var results = [];
|
---|
18 | children.forEach(function (child) {
|
---|
19 | if (filterFunc(child)) {
|
---|
20 | results.push(child);
|
---|
21 | }
|
---|
22 | });
|
---|
23 | return results;
|
---|
24 | }
|
---|
25 | hasChildren() {
|
---|
26 | return this.getChildren().length > 0;
|
---|
27 | }
|
---|
28 | removeChildren() {
|
---|
29 | this.getChildren().forEach((child) => {
|
---|
30 | child.parent = null;
|
---|
31 | child.index = 0;
|
---|
32 | child.remove();
|
---|
33 | });
|
---|
34 | this.children = [];
|
---|
35 | this._requestDraw();
|
---|
36 | return this;
|
---|
37 | }
|
---|
38 | destroyChildren() {
|
---|
39 | this.getChildren().forEach((child) => {
|
---|
40 | child.parent = null;
|
---|
41 | child.index = 0;
|
---|
42 | child.destroy();
|
---|
43 | });
|
---|
44 | this.children = [];
|
---|
45 | this._requestDraw();
|
---|
46 | return this;
|
---|
47 | }
|
---|
48 | add(...children) {
|
---|
49 | if (children.length === 0) {
|
---|
50 | return this;
|
---|
51 | }
|
---|
52 | if (children.length > 1) {
|
---|
53 | for (var i = 0; i < children.length; i++) {
|
---|
54 | this.add(children[i]);
|
---|
55 | }
|
---|
56 | return this;
|
---|
57 | }
|
---|
58 | const child = children[0];
|
---|
59 | if (child.getParent()) {
|
---|
60 | child.moveTo(this);
|
---|
61 | return this;
|
---|
62 | }
|
---|
63 | this._validateAdd(child);
|
---|
64 | child.index = this.getChildren().length;
|
---|
65 | child.parent = this;
|
---|
66 | child._clearCaches();
|
---|
67 | this.getChildren().push(child);
|
---|
68 | this._fire('add', {
|
---|
69 | child: child,
|
---|
70 | });
|
---|
71 | this._requestDraw();
|
---|
72 | return this;
|
---|
73 | }
|
---|
74 | destroy() {
|
---|
75 | if (this.hasChildren()) {
|
---|
76 | this.destroyChildren();
|
---|
77 | }
|
---|
78 | super.destroy();
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 | find(selector) {
|
---|
82 | return this._generalFind(selector, false);
|
---|
83 | }
|
---|
84 | findOne(selector) {
|
---|
85 | var result = this._generalFind(selector, true);
|
---|
86 | return result.length > 0 ? result[0] : undefined;
|
---|
87 | }
|
---|
88 | _generalFind(selector, findOne) {
|
---|
89 | var retArr = [];
|
---|
90 | this._descendants((node) => {
|
---|
91 | const valid = node._isMatch(selector);
|
---|
92 | if (valid) {
|
---|
93 | retArr.push(node);
|
---|
94 | }
|
---|
95 | if (valid && findOne) {
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 | return false;
|
---|
99 | });
|
---|
100 | return retArr;
|
---|
101 | }
|
---|
102 | _descendants(fn) {
|
---|
103 | let shouldStop = false;
|
---|
104 | const children = this.getChildren();
|
---|
105 | for (const child of children) {
|
---|
106 | shouldStop = fn(child);
|
---|
107 | if (shouldStop) {
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 | if (!child.hasChildren()) {
|
---|
111 | continue;
|
---|
112 | }
|
---|
113 | shouldStop = child._descendants(fn);
|
---|
114 | if (shouldStop) {
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 | toObject() {
|
---|
121 | var obj = Node_1.Node.prototype.toObject.call(this);
|
---|
122 | obj.children = [];
|
---|
123 | this.getChildren().forEach((child) => {
|
---|
124 | obj.children.push(child.toObject());
|
---|
125 | });
|
---|
126 | return obj;
|
---|
127 | }
|
---|
128 | isAncestorOf(node) {
|
---|
129 | var parent = node.getParent();
|
---|
130 | while (parent) {
|
---|
131 | if (parent._id === this._id) {
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 | parent = parent.getParent();
|
---|
135 | }
|
---|
136 | return false;
|
---|
137 | }
|
---|
138 | clone(obj) {
|
---|
139 | var node = Node_1.Node.prototype.clone.call(this, obj);
|
---|
140 | this.getChildren().forEach(function (no) {
|
---|
141 | node.add(no.clone());
|
---|
142 | });
|
---|
143 | return node;
|
---|
144 | }
|
---|
145 | getAllIntersections(pos) {
|
---|
146 | var arr = [];
|
---|
147 | this.find('Shape').forEach((shape) => {
|
---|
148 | if (shape.isVisible() && shape.intersects(pos)) {
|
---|
149 | arr.push(shape);
|
---|
150 | }
|
---|
151 | });
|
---|
152 | return arr;
|
---|
153 | }
|
---|
154 | _clearSelfAndDescendantCache(attr) {
|
---|
155 | var _a;
|
---|
156 | super._clearSelfAndDescendantCache(attr);
|
---|
157 | if (this.isCached()) {
|
---|
158 | return;
|
---|
159 | }
|
---|
160 | (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) {
|
---|
161 | node._clearSelfAndDescendantCache(attr);
|
---|
162 | });
|
---|
163 | }
|
---|
164 | _setChildrenIndices() {
|
---|
165 | var _a;
|
---|
166 | (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child, n) {
|
---|
167 | child.index = n;
|
---|
168 | });
|
---|
169 | this._requestDraw();
|
---|
170 | }
|
---|
171 | drawScene(can, top, bufferCanvas) {
|
---|
172 | var layer = this.getLayer(), canvas = can || (layer && layer.getCanvas()), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedSceneCanvas = cachedCanvas && cachedCanvas.scene;
|
---|
173 | var caching = canvas && canvas.isCache;
|
---|
174 | if (!this.isVisible() && !caching) {
|
---|
175 | return this;
|
---|
176 | }
|
---|
177 | if (cachedSceneCanvas) {
|
---|
178 | context.save();
|
---|
179 | var m = this.getAbsoluteTransform(top).getMatrix();
|
---|
180 | context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
---|
181 | this._drawCachedSceneCanvas(context);
|
---|
182 | context.restore();
|
---|
183 | }
|
---|
184 | else {
|
---|
185 | this._drawChildren('drawScene', canvas, top, bufferCanvas);
|
---|
186 | }
|
---|
187 | return this;
|
---|
188 | }
|
---|
189 | drawHit(can, top) {
|
---|
190 | if (!this.shouldDrawHit(top)) {
|
---|
191 | return this;
|
---|
192 | }
|
---|
193 | var layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedHitCanvas = cachedCanvas && cachedCanvas.hit;
|
---|
194 | if (cachedHitCanvas) {
|
---|
195 | context.save();
|
---|
196 | var m = this.getAbsoluteTransform(top).getMatrix();
|
---|
197 | context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
---|
198 | this._drawCachedHitCanvas(context);
|
---|
199 | context.restore();
|
---|
200 | }
|
---|
201 | else {
|
---|
202 | this._drawChildren('drawHit', canvas, top);
|
---|
203 | }
|
---|
204 | return this;
|
---|
205 | }
|
---|
206 | _drawChildren(drawMethod, canvas, top, bufferCanvas) {
|
---|
207 | var _a;
|
---|
208 | var context = canvas && canvas.getContext(), clipWidth = this.clipWidth(), clipHeight = this.clipHeight(), clipFunc = this.clipFunc(), hasClip = (typeof clipWidth === 'number' && typeof clipHeight === 'number') ||
|
---|
209 | clipFunc;
|
---|
210 | const selfCache = top === this;
|
---|
211 | if (hasClip) {
|
---|
212 | context.save();
|
---|
213 | var transform = this.getAbsoluteTransform(top);
|
---|
214 | var m = transform.getMatrix();
|
---|
215 | context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
---|
216 | context.beginPath();
|
---|
217 | let clipArgs;
|
---|
218 | if (clipFunc) {
|
---|
219 | clipArgs = clipFunc.call(this, context, this);
|
---|
220 | }
|
---|
221 | else {
|
---|
222 | var clipX = this.clipX();
|
---|
223 | var clipY = this.clipY();
|
---|
224 | context.rect(clipX || 0, clipY || 0, clipWidth, clipHeight);
|
---|
225 | }
|
---|
226 | context.clip.apply(context, clipArgs);
|
---|
227 | m = transform.copy().invert().getMatrix();
|
---|
228 | context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
---|
229 | }
|
---|
230 | var hasComposition = !selfCache &&
|
---|
231 | this.globalCompositeOperation() !== 'source-over' &&
|
---|
232 | drawMethod === 'drawScene';
|
---|
233 | if (hasComposition) {
|
---|
234 | context.save();
|
---|
235 | context._applyGlobalCompositeOperation(this);
|
---|
236 | }
|
---|
237 | (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
|
---|
238 | child[drawMethod](canvas, top, bufferCanvas);
|
---|
239 | });
|
---|
240 | if (hasComposition) {
|
---|
241 | context.restore();
|
---|
242 | }
|
---|
243 | if (hasClip) {
|
---|
244 | context.restore();
|
---|
245 | }
|
---|
246 | }
|
---|
247 | getClientRect(config = {}) {
|
---|
248 | var _a;
|
---|
249 | var skipTransform = config.skipTransform;
|
---|
250 | var relativeTo = config.relativeTo;
|
---|
251 | var minX, minY, maxX, maxY;
|
---|
252 | var selfRect = {
|
---|
253 | x: Infinity,
|
---|
254 | y: Infinity,
|
---|
255 | width: 0,
|
---|
256 | height: 0,
|
---|
257 | };
|
---|
258 | var that = this;
|
---|
259 | (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
|
---|
260 | if (!child.visible()) {
|
---|
261 | return;
|
---|
262 | }
|
---|
263 | var rect = child.getClientRect({
|
---|
264 | relativeTo: that,
|
---|
265 | skipShadow: config.skipShadow,
|
---|
266 | skipStroke: config.skipStroke,
|
---|
267 | });
|
---|
268 | if (rect.width === 0 && rect.height === 0) {
|
---|
269 | return;
|
---|
270 | }
|
---|
271 | if (minX === undefined) {
|
---|
272 | minX = rect.x;
|
---|
273 | minY = rect.y;
|
---|
274 | maxX = rect.x + rect.width;
|
---|
275 | maxY = rect.y + rect.height;
|
---|
276 | }
|
---|
277 | else {
|
---|
278 | minX = Math.min(minX, rect.x);
|
---|
279 | minY = Math.min(minY, rect.y);
|
---|
280 | maxX = Math.max(maxX, rect.x + rect.width);
|
---|
281 | maxY = Math.max(maxY, rect.y + rect.height);
|
---|
282 | }
|
---|
283 | });
|
---|
284 | var shapes = this.find('Shape');
|
---|
285 | var hasVisible = false;
|
---|
286 | for (var i = 0; i < shapes.length; i++) {
|
---|
287 | var shape = shapes[i];
|
---|
288 | if (shape._isVisible(this)) {
|
---|
289 | hasVisible = true;
|
---|
290 | break;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | if (hasVisible && minX !== undefined) {
|
---|
294 | selfRect = {
|
---|
295 | x: minX,
|
---|
296 | y: minY,
|
---|
297 | width: maxX - minX,
|
---|
298 | height: maxY - minY,
|
---|
299 | };
|
---|
300 | }
|
---|
301 | else {
|
---|
302 | selfRect = {
|
---|
303 | x: 0,
|
---|
304 | y: 0,
|
---|
305 | width: 0,
|
---|
306 | height: 0,
|
---|
307 | };
|
---|
308 | }
|
---|
309 | if (!skipTransform) {
|
---|
310 | return this._transformedRect(selfRect, relativeTo);
|
---|
311 | }
|
---|
312 | return selfRect;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | exports.Container = Container;
|
---|
316 | Factory_1.Factory.addComponentsGetterSetter(Container, 'clip', [
|
---|
317 | 'x',
|
---|
318 | 'y',
|
---|
319 | 'width',
|
---|
320 | 'height',
|
---|
321 | ]);
|
---|
322 | Factory_1.Factory.addGetterSetter(Container, 'clipX', undefined, (0, Validators_1.getNumberValidator)());
|
---|
323 | Factory_1.Factory.addGetterSetter(Container, 'clipY', undefined, (0, Validators_1.getNumberValidator)());
|
---|
324 | Factory_1.Factory.addGetterSetter(Container, 'clipWidth', undefined, (0, Validators_1.getNumberValidator)());
|
---|
325 | Factory_1.Factory.addGetterSetter(Container, 'clipHeight', undefined, (0, Validators_1.getNumberValidator)());
|
---|
326 | Factory_1.Factory.addGetterSetter(Container, 'clipFunc');
|
---|