forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.js
More file actions
265 lines (224 loc) · 8.02 KB
/
Group.js
File metadata and controls
265 lines (224 loc) · 8.02 KB
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
/* Copyright© 2000 - 2019 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {SuperMap} from '../../SuperMap';
import {Util as CommonUtil} from '../../commontypes/Util';
import {Eventful} from './Eventful';
import {Transformable} from './Transformable';
/**
* @class SuperMap.LevelRenderer.Group
* @category Visualization Theme
* @private
* @classdesc Group 是一个容器,可以插入子节点,Group 的变换也会被应用到子节点上。
* @extends {SuperMap.LevelRenderer.Transformable}
* (code)
* var g = new SuperMap.LevelRenderer.Group();
* var Circle = new SuperMap.LevelRenderer.Shape.Circle();
* g.position[0] = 100;
* g.position[1] = 100;
* g.addChild(new Circle({
* style: {
* x: 100,
* y: 100,
* r: 20,
* brushType: 'fill'
* }
* }));
* LR.addGroup(g);
* (end)
*/
export class Group extends SuperMap.mixin(Eventful, Transformable) {
/**
* @function SuperMap.LevelRenderer.Group.prototype.constructor
* @description 构造函数。
* @param {Array} options - Group 的配置(options)项,可以是 Group 的自有属性,也可以是自定义的属性。
*/
constructor(options) {
super(options)
options = options || {};
/**
* @member {string} SuperMap.LevelRenderer.Group.prototype.id
* @description Group 的唯一标识。
*/
this.id = null;
/**
* @readonly
* @member {string} [SuperMap.LevelRenderer.Group.prototype.type='group']
* @description 类型。
*/
this.type = 'group';
//http://www.w3.org/TR/2dcontext/#clipping-region
/**
* @member {string} SuperMap.LevelRenderer.Group.prototype.clipShape
* @description 用于裁剪的图形(shape),所有 Group 内的图形在绘制时都会被这个图形裁剪,该图形会继承 Group 的变换。
*/
this.clipShape = null;
/**
* @member {Array} SuperMap.LevelRenderer.Group.prototype._children
* @description _children。
*/
this._children = [];
/**
* @member {Array} SuperMap.LevelRenderer.Group.prototype._storage
* @description _storage。
*/
this._storage = null;
/**
* @member {boolean} [SuperMap.LevelRenderer.Group.prototype.__dirty=true]
* @description __dirty。
*/
this.__dirty = true;
/**
* @member {boolean} [SuperMap.LevelRenderer.Group.prototype.ignore=false]
* @description 是否忽略该 Group 及其所有子节点。
*/
this.ignore = false;
CommonUtil.extend(this, options);
this.id = this.id || CommonUtil.createUniqueID("smShapeGroup_");
this.CLASS_NAME = "SuperMap.LevelRenderer.Group";
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.destroy
* @description 销毁对象,释放资源。调用此函数后所有属性将被置为 null。
*/
destroy() {
this.id = null;
this.type = null;
this.clipShape = null;
this._children = null;
this._storage = null;
this.__dirty = null;
this.ignore = null;
super.destroy();
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.children
* @description 复制并返回一份新的包含所有儿子节点的数组。
* @returns {Array.<SuperMap.LevelRenderer.Shape>} 图形数组。
*/
children() {
return this._children.slice();
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.childAt
* @description 获取指定 index 的儿子节点
* @param {number} idx - 节点索引。
* @returns {SuperMap.LevelRenderer.Shape} 图形。
*/
childAt(idx) {
return this._children[idx];
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.addChild
* @description 添加子节点,可以是 Shape 或者 Group。
* @param {(SuperMap.LevelRenderer.Shape|SuperMap.LevelRenderer.Group)} child - 节点图形。
*/
// TODO Type Check
addChild(child) {
if (child == this) {
return;
}
if (child.parent == this) {
return;
}
if (child.parent) {
child.parent.removeChild(child);
}
this._children.push(child);
child.parent = this;
if (this._storage && this._storage !== child._storage) {
this._storage.addToMap(child);
if (child instanceof Group) {
child.addChildrenToStorage(this._storage);
}
}
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.removeChild
* @description 移除子节点。
* @param {SuperMap.LevelRenderer.Shape} child - 需要移除的子节点图形。
*/
removeChild(child) {
var idx = CommonUtil.indexOf(this._children, child);
this._children.splice(idx, 1);
child.parent = null;
if (this._storage) {
this._storage.delFromMap(child.id);
if (child instanceof Group) {
child.delChildrenFromStorage(this._storage);
}
}
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.eachChild
* @description 遍历所有子节点。
* @param {function} cb - 回调函数。
* @param {Object} context - 上下文。
*/
eachChild(cb, context) {
var haveContext = !!context;
for (var i = 0; i < this._children.length; i++) {
var child = this._children[i];
if (haveContext) {
cb.call(context, child);
} else {
cb(child);
}
}
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.traverse
* @description 深度优先遍历所有子孙节点。
* @param {function} cb - 回调函数。
* @param {Object} context - 上下文。
*/
traverse(cb, context) {
var haveContext = !!context;
for (var i = 0; i < this._children.length; i++) {
var child = this._children[i];
if (haveContext) {
cb.call(context, child);
} else {
cb(child);
}
if (child.type === 'group') {
child.traverse(cb, context);
}
}
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.addChildrenToStorage
* @description 把子图形添加到仓库。
* @param {SuperMap.LevelRenderer.Storage} storage - 图形仓库。
*/
addChildrenToStorage(storage) {
for (var i = 0; i < this._children.length; i++) {
var child = this._children[i];
storage.addToMap(child);
if (child.type === 'group') {
child.addChildrenToStorage(storage);
}
}
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.delChildrenFromStorage
* @description 从仓库把子图形删除。
* @param {SuperMap.LevelRenderer.Storage} storage - 图形仓库。
*/
delChildrenFromStorage(storage) {
for (var i = 0; i < this._children.length; i++) {
var child = this._children[i];
storage.delFromMap(child.id);
if (child.type === 'group') {
child.delChildrenFromStorage(storage);
}
}
}
/**
* @function SuperMap.LevelRenderer.Group.prototype.modSelf
* @description 是否修改。
*/
modSelf() {
this.__dirty = true;
}
}