CompositeElement_wev8.js
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Ext JS Library 2.0.2
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* @class Ext.CompositeElement
* Standard composite class. Creates a Ext.Element for every element in the collection.
* <br><br>
* <b>NOTE: Although they are not listed, this class supports all of the set/update methods of Ext.Element. All Ext.Element
* actions will be performed on all the elements in this collection.</b>
* <br><br>
* All methods return <i>this</i> and can be chained.
<pre><code>
var els = Ext.select("#some-el div.some-class", true);
// or select directly from an existing element
var el = Ext.get('some-el');
el.select('div.some-class', true);
els.setWidth(100); // all elements become 100 width
els.hide(true); // all elements fade out and hide
// or
els.setWidth(100).hide(true);
</code></pre>
*/
Ext.CompositeElement = function(els){
this.elements = [];
this.addElements(els);
};
Ext.CompositeElement.prototype = {
isComposite: true,
addElements : function(els){
if(!els) return this;
if(typeof els == "string"){
els = Ext.Element.selectorFunction(els);
}
var yels = this.elements;
var index = yels.length-1;
for(var i = 0, len = els.length; i < len; i++) {
yels[++index] = Ext.get(els[i]);
}
return this;
},
/**
* Clears this composite and adds the elements returned by the passed selector.
* @param {String/Array} els A string CSS selector, an array of elements or an element
* @return {CompositeElement} this
*/
fill : function(els){
this.elements = [];
this.add(els);
return this;
},
/**
* Filters this composite to only elements that match the passed selector.
* @param {String} selector A string CSS selector
* @return {CompositeElement} this
*/
filter : function(selector){
var els = [];
this.each(function(el){
if(el.is(selector)){
els[els.length] = el.dom;
}
});
this.fill(els);
return this;
},
invoke : function(fn, args){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++) {
Ext.Element.prototype[fn].apply(els[i], args);
}
return this;
},
/**
* Adds elements to this composite.
* @param {String/Array} els A string CSS selector, an array of elements or an element
* @return {CompositeElement} this
*/
add : function(els){
if(typeof els == "string"){
this.addElements(Ext.Element.selectorFunction(els));
}else if(els.length !== undefined){
this.addElements(els);
}else{
this.addElements([els]);
}
return this;
},
/**
* Calls the passed function passing (el, this, index) for each element in this composite.
* @param {Function} fn The function to call
* @param {Object} scope (optional) The <i>this</i> object (defaults to the element)
* @return {CompositeElement} this
*/
each : function(fn, scope){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++){
if(fn.call(scope || els[i], els[i], this, i) === false) {
break;
}
}
return this;
},
/**
* Returns the Element object at the specified index
* @param {Number} index
* @return {Ext.Element}
*/
item : function(index){
return this.elements[index] || null;
},
/**
* Returns the first Element
* @return {Ext.Element}
*/
first : function(){
return this.item(0);
},
/**
* Returns the last Element
* @return {Ext.Element}
*/
last : function(){
return this.item(this.elements.length-1);
},
/**
* Returns the number of elements in this composite
* @return Number
*/
getCount : function(){
return this.elements.length;
},
/**
* Returns true if this composite contains the passed element
* @return Boolean
*/
contains : function(el){
return this.indexOf(el) !== -1;
},
/**
* Returns true if this composite contains the passed element
* @return Boolean
*/
indexOf : function(el){
return this.elements.indexOf(Ext.get(el));
},
/**
* Removes the specified element(s).
* @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
* or an array of any of those.
* @param {Boolean} removeDom (optional) True to also remove the element from the document
* @return {CompositeElement} this
*/
removeElement : function(el, removeDom){
if(Ext.isArray(el)){
for(var i = 0, len = el.length; i < len; i++){
this.removeElement(el[i]);
}
return this;
}
var index = typeof el == 'number' ? el : this.indexOf(el);
if(index !== -1 && this.elements[index]){
if(removeDom){
var d = this.elements[index];
if(d.dom){
d.remove();
}else{
Ext.removeNode(d);
}
}
this.elements.splice(index, 1);
}
return this;
},
/**
* Replaces the specified element with the passed element.
* @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
* to replace.
* @param {Mixed} replacement The id of an element or the Element itself.
* @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
* @return {CompositeElement} this
*/
replaceElement : function(el, replacement, domReplace){
var index = typeof el == 'number' ? el : this.indexOf(el);
if(index !== -1){
if(domReplace){
this.elements[index].replaceWith(replacement);
}else{
this.elements.splice(index, 1, Ext.get(replacement))
}
}
return this;
},
/**
* Removes all elements.
*/
clear : function(){
this.elements = [];
}
};
(function(){
Ext.CompositeElement.createCall = function(proto, fnName){
if(!proto[fnName]){
proto[fnName] = function(){
return this.invoke(fnName, arguments);
};
}
};
for(var fnName in Ext.Element.prototype){
if(typeof Ext.Element.prototype[fnName] == "function"){
Ext.CompositeElement.createCall(Ext.CompositeElement.prototype, fnName);
}
};
})();
/**
* @class Ext.CompositeElementLite
* @extends Ext.CompositeElement
* Flyweight composite class. Reuses the same Ext.Element for element operations.
<pre><code>
var els = Ext.select("#some-el div.some-class");
// or select directly from an existing element
var el = Ext.get('some-el');
el.select('div.some-class');
els.setWidth(100); // all elements become 100 width
els.hide(true); // all elements fade out and hide
// or
els.setWidth(100).hide(true);
</code></pre><br><br>
* <b>NOTE: Although they are not listed, this class supports all of the set/update methods of Ext.Element. All Ext.Element
* actions will be performed on all the elements in this collection.</b>
*/
Ext.CompositeElementLite = function(els){
Ext.CompositeElementLite.superclass.constructor.call(this, els);
this.el = new Ext.Element.Flyweight();
};
Ext.extend(Ext.CompositeElementLite, Ext.CompositeElement, {
addElements : function(els){
if(els){
if(Ext.isArray(els)){
this.elements = this.elements.concat(els);
}else{
var yels = this.elements;
var index = yels.length-1;
for(var i = 0, len = els.length; i < len; i++) {
yels[++index] = els[i];
}
}
}
return this;
},
invoke : function(fn, args){
var els = this.elements;
var el = this.el;
for(var i = 0, len = els.length; i < len; i++) {
el.dom = els[i];
Ext.Element.prototype[fn].apply(el, args);
}
return this;
},
/**
* Returns a flyweight Element of the dom element object at the specified index
* @param {Number} index
* @return {Ext.Element}
*/
item : function(index){
if(!this.elements[index]){
return null;
}
this.el.dom = this.elements[index];
return this.el;
},
// fixes scope with flyweight
addListener : function(eventName, handler, scope, opt){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++) {
Ext.EventManager.on(els[i], eventName, handler, scope || els[i], opt);
}
return this;
},
/**
* Calls the passed function passing (el, this, index) for each element in this composite. <b>The element
* passed is the flyweight (shared) Ext.Element instance, so if you require a
* a reference to the dom node, use el.dom.</b>
* @param {Function} fn The function to call
* @param {Object} scope (optional) The <i>this</i> object (defaults to the element)
* @return {CompositeElement} this
*/
each : function(fn, scope){
var els = this.elements;
var el = this.el;
for(var i = 0, len = els.length; i < len; i++){
el.dom = els[i];
if(fn.call(scope || el, el, this, i) === false){
break;
}
}
return this;
},
indexOf : function(el){
return this.elements.indexOf(Ext.getDom(el));
},
replaceElement : function(el, replacement, domReplace){
var index = typeof el == 'number' ? el : this.indexOf(el);
if(index !== -1){
replacement = Ext.getDom(replacement);
if(domReplace){
var d = this.elements[index];
d.parentNode.insertBefore(replacement, d);
Ext.removeNode(d);
}
this.elements.splice(index, 1, replacement);
}
return this;
}
});
Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addListener;
if(Ext.DomQuery){
Ext.Element.selectorFunction = Ext.DomQuery.select;
}
Ext.Element.select = function(selector, unique, root){
var els;
if(typeof selector == "string"){
els = Ext.Element.selectorFunction(selector, root);
}else if(selector.length !== undefined){
els = selector;
}else{
throw "Invalid selector";
}
if(unique === true){
return new Ext.CompositeElement(els);
}else{
return new Ext.CompositeElementLite(els);
}
};
/**
* Selects elements based on the passed CSS selector to enable working on them as 1.
* @param {String/Array} selector The CSS selector or an array of elements
* @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object)
* @param {HTMLElement/String} root (optional) The root element of the query or id of the root
* @return {CompositeElementLite/CompositeElement}
* @member Ext
* @method select
*/
Ext.select = Ext.Element.select;