]>
git.wh0rd.org - tt-rss.git/blob - lib/dijit/WidgetSet.js.uncompressed.js
1 define("dijit/WidgetSet", [
2 "dojo/_base/array", // array.forEach array.map
3 "dojo/_base/declare", // declare
4 "dojo/_base/window", // win.global
5 "./registry" // to add functions to dijit.registry
6 ], function(array
, declare
, win
, registry
){
11 // Legacy registry code. New modules should just use registry.
12 // Will be removed in 2.0.
14 var WidgetSet
= declare("dijit.WidgetSet", null, {
16 // A set of widgets indexed by id. A default instance of this class is
17 // available as `dijit.registry`
20 // Create a small list of widgets:
21 // | var ws = new dijit.WidgetSet();
22 // | ws.add(dijit.byId("one"));
23 // | ws.add(dijit.byId("two"));
25 // | ws.forEach(function(w){ w.destroy(); });
28 // Using dijit.registry:
29 // | dijit.registry.forEach(function(w){ /* do something */ });
31 constructor: function(){
36 add: function(/*dijit._Widget*/ widget
){
38 // Add a widget to this list. If a duplicate ID is detected, a error is thrown.
40 // widget: dijit._Widget
41 // Any dijit._Widget subclass.
42 if(this._hash
[widget
.id
]){
43 throw new Error("Tried to register widget with id==" + widget
.id
+ " but that id is already registered");
45 this._hash
[widget
.id
] = widget
;
49 remove: function(/*String*/ id
){
51 // Remove a widget from this WidgetSet. Does not destroy the widget; simply
52 // removes the reference.
54 delete this._hash
[id
];
59 forEach: function(/*Function*/ func
, /* Object? */thisObj
){
61 // Call specified function for each widget in this set.
64 // A callback function to run for each item. Is passed the widget, the index
65 // in the iteration, and the full hash, similar to `array.forEach`.
68 // An optional scope parameter
71 // Using the default `dijit.registry` instance:
72 // | dijit.registry.forEach(function(widget){
73 // | console.log(widget.declaredClass);
77 // Returns self, in order to allow for further chaining.
79 thisObj
= thisObj
|| win
.global
;
81 for(id
in this._hash
){
82 func
.call(thisObj
, this._hash
[id
], i
++, this._hash
);
84 return this; // dijit.WidgetSet
87 filter: function(/*Function*/ filter
, /* Object? */thisObj
){
89 // Filter down this WidgetSet to a smaller new WidgetSet
90 // Works the same as `array.filter` and `NodeList.filter`
93 // Callback function to test truthiness. Is passed the widget
94 // reference and the pseudo-index in the object.
97 // Option scope to use for the filter function.
100 // Arbitrary: select the odd widgets in this list
101 // | dijit.registry.filter(function(w, i){
102 // | return i % 2 == 0;
103 // | }).forEach(function(w){ /* odd ones */ });
105 thisObj
= thisObj
|| win
.global
;
106 var res
= new WidgetSet(), i
= 0, id
;
107 for(id
in this._hash
){
108 var w
= this._hash
[id
];
109 if(filter
.call(thisObj
, w
, i
++, this._hash
)){
113 return res
; // dijit.WidgetSet
116 byId: function(/*String*/ id
){
118 // Find a widget in this list by it's id.
120 // Test if an id is in a particular WidgetSet
121 // | var ws = new dijit.WidgetSet();
122 // | ws.add(dijit.byId("bar"));
123 // | var t = ws.byId("bar") // returns a widget
124 // | var x = ws.byId("foo"); // returns undefined
126 return this._hash
[id
]; // dijit._Widget
129 byClass: function(/*String*/ cls
){
131 // Reduce this widgetset to a new WidgetSet of a particular `declaredClass`
134 // The Class to scan for. Full dot-notated string.
137 // Find all `dijit.TitlePane`s in a page:
138 // | dijit.registry.byClass("dijit.TitlePane").forEach(function(tp){ tp.close(); });
140 var res
= new WidgetSet(), id
, widget
;
141 for(id
in this._hash
){
142 widget
= this._hash
[id
];
143 if(widget
.declaredClass
== cls
){
147 return res
; // dijit.WidgetSet
152 // Convert this WidgetSet into a true Array
155 // Work with the widget .domNodes in a real Array
156 // | array.map(dijit.registry.toArray(), function(w){ return w.domNode; });
159 for(var id
in this._hash
){
160 ar
.push(this._hash
[id
]);
162 return ar
; // dijit._Widget[]
165 map: function(/* Function */func
, /* Object? */thisObj
){
167 // Create a new Array from this WidgetSet, following the same rules as `array.map`
169 // | var nodes = dijit.registry.map(function(w){ return w.domNode; });
172 // A new array of the returned values.
173 return array
.map(this.toArray(), func
, thisObj
); // Array
176 every: function(func
, thisObj
){
178 // A synthetic clone of `array.every` acting explicitly on this WidgetSet
181 // A callback function run for every widget in this list. Exits loop
182 // when the first false return is encountered.
185 // Optional scope parameter to use for the callback
187 thisObj
= thisObj
|| win
.global
;
189 for(i
in this._hash
){
190 if(!func
.call(thisObj
, this._hash
[i
], x
++, this._hash
)){
191 return false; // Boolean
194 return true; // Boolean
197 some: function(func
, thisObj
){
199 // A synthetic clone of `array.some` acting explicitly on this WidgetSet
202 // A callback function run for every widget in this list. Exits loop
203 // when the first true return is encountered.
206 // Optional scope parameter to use for the callback
208 thisObj
= thisObj
|| win
.global
;
210 for(i
in this._hash
){
211 if(func
.call(thisObj
, this._hash
[i
], x
++, this._hash
)){
212 return true; // Boolean
215 return false; // Boolean
220 // Add in 1.x compatibility methods to dijit.registry.
221 // These functions won't show up in the API doc but since they are deprecated anyway,
222 // that's probably for the best.
223 array
.forEach(["forEach", "filter", "byClass", "map", "every", "some"], function(func
){
224 registry
[func
] = WidgetSet
.prototype[func
];