ElementContainer.html
11.1 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
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-util-ElementContainer'>/**
</span> * This mixin enables classes to declare relationships to child elements and provides the
* mechanics for acquiring the {@link Ext.Element elements} and storing them on an object
* instance as properties.
*
* This class is used by {@link Ext.Component components} and {@link Ext.layout.container.Container container layouts} to
* manage their child elements.
*
* A typical component that uses these features might look something like this:
*
* Ext.define('Ext.ux.SomeComponent', {
* extend: 'Ext.Component',
*
* childEls: [
* 'bodyEl'
* ],
*
* renderTpl: [
* '<div id="{id}-bodyEl"></div>'
* ],
*
* // ...
* });
*
* The `childEls` array lists one or more relationships to child elements managed by the
* component. The items in this array can be either of the following types:
*
* - String: the id suffix and property name in one. For example, "bodyEl" in the above
* example means a "bodyEl" property will be added to the instance with the result of
* {@link Ext#get} given "componentId-bodyEl" where "componentId" is the component instance's
* id.
* - Object: with a `name` property that names the instance property for the element, and
* one of the following additional properties:
* - `id`: The full id of the child element.
* - `itemId`: The suffix part of the id to which "componentId-" is prepended.
* - `select`: A selector that will be passed to {@link Ext#select}.
* - `selectNode`: A selector that will be passed to {@link Ext.DomQuery#selectNode}.
*
* The example above could have used this instead to achieve the same result:
*
* childEls: [
* { name: 'bodyEl', itemId: 'bodyEl' }
* ]
*
* When using `select`, the property will be an instance of {@link Ext.CompositeElement}. In
* all other cases, the property will be an {@link Ext.Element} or `null` if not found.
*
* Care should be taken when using `select` or `selectNode` to find child elements. The
* following issues should be considered:
*
* - Performance: using selectors can be slower than id lookup by a factor 10x or more.
* - Over-selecting: selectors are applied after the DOM elements for all children have
* been rendered, so selectors can match elements from child components (including nested
* versions of the same component) accidentally.
*
* This above issues are most important when using `select` since it returns multiple
* elements.
*
* **IMPORTANT**
* Unlike a `renderTpl` where there is a single value for an instance, `childEls` are aggregated
* up the class hierarchy so that they are effectively inherited. In other words, if a
* class where to derive from `Ext.ux.SomeComponent` in the example above, it could also
* have a `childEls` property in the same way as `Ext.ux.SomeComponent`.
*
* Ext.define('Ext.ux.AnotherComponent', {
* extend: 'Ext.ux.SomeComponent',
*
* childEls: [
* // 'bodyEl' is inherited
* 'innerEl'
* ],
*
* renderTpl: [
* '<div id="{id}-bodyEl">'
* '<div id="{id}-innerEl"></div>'
* '</div>'
* ],
*
* // ...
* });
*
* The `renderTpl` contains both child elements and unites them in the desired markup, but
* the `childEls` only contains the new child element. The {@link #applyChildEls} method
* takes care of looking up all `childEls` for an instance and considers `childEls`
* properties on all the super classes and mixins.
*
* @private
*/
Ext.define('Ext.util.ElementContainer', {
childEls: [
// empty - this solves a couple problems:
// 1. It ensures that all classes have a childEls (avoid null ptr)
// 2. It prevents mixins from smashing on their own childEls (these are gathered
// specifically)
],
constructor: function () {
var me = this,
childEls;
// if we have configured childEls, we need to merge them with those from this
// class, its bases and the set of mixins...
if (me.hasOwnProperty('childEls')) {
childEls = me.childEls;
delete me.childEls;
me.addChildEls.apply(me, childEls);
}
},
destroy: function () {
var me = this,
childEls = me.getChildEls(),
child, childName, i, k;
for (i = childEls.length; i--; ) {
childName = childEls[i];
if (typeof childName != 'string') {
childName = childName.name;
}
child = me[childName];
if (child) {
me[childName] = null; // better than delete since that changes the "shape"
child.remove();
}
}
},
<span id='Ext-util-ElementContainer-method-addChildEls'> /**
</span> * Adds each argument passed to this method to the {@link Ext.AbstractComponent#cfg-childEls childEls} array.
*/
addChildEls: function () {
var me = this,
args = arguments;
if (me.hasOwnProperty('childEls')) {
me.childEls.push.apply(me.childEls, args);
} else {
me.childEls = me.getChildEls().concat(Array.prototype.slice.call(args));
}
me.prune(me.childEls, false);
},
<span id='Ext-util-ElementContainer-method-applyChildEls'> /**
</span> * Sets references to elements inside the component.
* @private
*/
applyChildEls: function(el, id) {
var me = this,
childEls = me.getChildEls(),
baseId, childName, i, selector, value;
baseId = (id || me.id) + '-';
for (i = childEls.length; i--; ) {
childName = childEls[i];
if (typeof childName == 'string') {
// We don't use Ext.get because that is 3x (or more) slower on IE6-8. Since
// we know the el's are children of our el we use getById instead:
value = el.getById(baseId + childName);
} else {
if ((selector = childName.select)) {
value = Ext.select(selector, true, el.dom); // a CompositeElement
} else if ((selector = childName.selectNode)) {
value = Ext.get(Ext.DomQuery.selectNode(selector, el.dom));
} else {
// see above re:getById...
value = el.getById(childName.id || (baseId + childName.itemId));
}
childName = childName.name;
}
me[childName] = value;
}
},
getChildEls: function () {
var me = this,
self;
// If an instance has its own childEls, that is the complete set:
if (me.hasOwnProperty('childEls')) {
return me.childEls;
}
// Typically, however, the childEls is a class-level concept, so check to see if
// we have cached the complete set on the class:
self = me.self;
return self.$childEls || me.getClassChildEls(self);
},
getClassChildEls: function (cls) {
var me = this,
result = cls.$childEls,
childEls, i, length, forked, mixin, mixins, name, parts, proto, supr, superMixins;
if (!result) {
// We put the various childEls arrays into parts in the order of superclass,
// new mixins and finally from cls. These parts can be null or undefined and
// we will skip them later.
supr = cls.superclass;
if (supr) {
supr = supr.self;
parts = [supr.$childEls || me.getClassChildEls(supr)]; // super+mixins
superMixins = supr.prototype.mixins || {};
} else {
parts = [];
superMixins = {};
}
proto = cls.prototype;
mixins = proto.mixins; // since we are a mixin, there will be at least us
for (name in mixins) {
if (mixins.hasOwnProperty(name) && !superMixins.hasOwnProperty(name)) {
mixin = mixins[name].self;
parts.push(mixin.$childEls || me.getClassChildEls(mixin));
}
}
parts.push(proto.hasOwnProperty('childEls') && proto.childEls);
for (i = 0, length = parts.length; i < length; ++i) {
childEls = parts[i];
if (childEls && childEls.length) {
if (!result) {
result = childEls;
} else {
if (!forked) {
forked = true;
result = result.slice(0);
}
result.push.apply(result, childEls);
}
}
}
cls.$childEls = result = (result ? me.prune(result, !forked) : []);
}
return result;
},
prune: function (childEls, shared) {
var index = childEls.length,
map = {},
name;
while (index--) {
name = childEls[index];
if (typeof name != 'string') {
name = name.name;
}
if (!map[name]) {
map[name] = 1;
} else {
if (shared) {
shared = false;
childEls = childEls.slice(0);
}
Ext.Array.erase(childEls, index, 1);
}
}
return childEls;
},
<span id='Ext-util-ElementContainer-method-removeChildEls'> /**
</span> * Removes items in the childEls array based on the return value of a supplied test
* function. The function is called with a entry in childEls and if the test function
* return true, that entry is removed. If false, that entry is kept.
*
* @param {Function} testFn The test function.
*/
removeChildEls: function (testFn) {
var me = this,
old = me.getChildEls(),
keepers = (me.childEls = []),
n, i, cel;
for (i = 0, n = old.length; i < n; ++i) {
cel = old[i];
if (!testFn(cel)) {
keepers.push(cel);
}
}
}
});
</pre>
</body>
</html>