]>
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/kernel", // kernel.global
5 "./registry" // to add functions to dijit.registry
6 ], function(array
, declare
, kernel
, registry
){
11 var WidgetSet
= declare("dijit.WidgetSet", null, {
13 // A set of widgets indexed by id.
14 // Deprecated, will be removed in 2.0.
17 // Create a small list of widgets:
18 // | require(["dijit/WidgetSet", "dijit/registry"],
19 // | function(WidgetSet, registry){
20 // | var ws = new WidgetSet();
21 // | ws.add(registry.byId("one"));
22 // | ws.add(registry.byId("two"));
24 // | ws.forEach(function(w){ w.destroy(); });
27 constructor: function(){
32 add: function(/*dijit/_WidgetBase*/ widget){
34 // Add a widget to this list. If a duplicate ID is detected, a error is thrown.
36 // widget: dijit/_WidgetBase
37 // Any dijit/_WidgetBase subclass.
38 if(this._hash[widget.id]){
39 throw new Error("Tried to register widget with id==" + widget.id + " but that id is already registered");
41 this._hash[widget.id] = widget;
45 remove: function(/*String*/ id
){
47 // Remove a widget from this WidgetSet. Does not destroy the widget; simply
48 // removes the reference.
50 delete this._hash
[id
];
55 forEach: function(/*Function*/ func
, /* Object? */thisObj
){
57 // Call specified function for each widget in this set.
60 // A callback function to run for each item. Is passed the widget, the index
61 // in the iteration, and the full hash, similar to `array.forEach`.
64 // An optional scope parameter
67 // Using the default `dijit.registry` instance:
68 // | require(["dijit/WidgetSet", "dijit/registry"],
69 // | function(WidgetSet, registry){
70 // | registry.forEach(function(widget){
71 // | console.log(widget.declaredClass);
76 // Returns self, in order to allow for further chaining.
78 thisObj
= thisObj
|| kernel
.global
;
80 for(id
in this._hash
){
81 func
.call(thisObj
, this._hash
[id
], i
++, this._hash
);
83 return this; // dijit/WidgetSet
86 filter: function(/*Function*/ filter
, /* Object? */thisObj
){
88 // Filter down this WidgetSet to a smaller new WidgetSet
89 // Works the same as `array.filter` and `NodeList.filter`
92 // Callback function to test truthiness. Is passed the widget
93 // reference and the pseudo-index in the object.
96 // Option scope to use for the filter function.
99 // Arbitrary: select the odd widgets in this list
103 // | require(["dijit/WidgetSet", "dijit/registry"],
104 // | function(WidgetSet, registry){
105 // | registry.filter(function(w, i){
106 // | return i % 2 == 0;
107 // | }).forEach(function(w){ /* odd ones */ });
110 thisObj
= thisObj
|| kernel
.global
;
111 var res
= new WidgetSet(), i
= 0, id
;
112 for(id
in this._hash
){
113 var w
= this._hash
[id
];
114 if(filter
.call(thisObj
, w
, i
++, this._hash
)){
118 return res
; // dijit/WidgetSet
121 byId: function(/*String*/ id
){
123 // Find a widget in this list by it's id.
125 // Test if an id is in a particular WidgetSet
126 // | require(["dijit/WidgetSet", "dijit/registry"],
127 // | function(WidgetSet, registry){
128 // | var ws = new WidgetSet();
129 // | ws.add(registry.byId("bar"));
130 // | var t = ws.byId("bar") // returns a widget
131 // | var x = ws.byId("foo"); // returns undefined
134 return this._hash
[id
]; // dijit/_WidgetBase
137 byClass: function(/*String*/ cls
){
139 // Reduce this widgetset to a new WidgetSet of a particular `declaredClass`
142 // The Class to scan for. Full dot-notated string.
145 // Find all `dijit.TitlePane`s in a page:
146 // | require(["dijit/WidgetSet", "dijit/registry"],
147 // | function(WidgetSet, registry){
148 // | registry.byClass("dijit.TitlePane").forEach(function(tp){ tp.close(); });
151 var res
= new WidgetSet(), id
, widget
;
152 for(id
in this._hash
){
153 widget
= this._hash
[id
];
154 if(widget
.declaredClass
== cls
){
158 return res
; // dijit/WidgetSet
163 // Convert this WidgetSet into a true Array
166 // Work with the widget .domNodes in a real Array
167 // | require(["dijit/WidgetSet", "dijit/registry"],
168 // | function(WidgetSet, registry){
169 // | array.map(registry.toArray(), function(w){ return w.domNode; });
174 for(var id
in this._hash
){
175 ar
.push(this._hash
[id
]);
177 return ar
; // dijit/_WidgetBase[]
180 map: function(/* Function */func
, /* Object? */thisObj
){
182 // Create a new Array from this WidgetSet, following the same rules as `array.map`
184 // | require(["dijit/WidgetSet", "dijit/registry"],
185 // | function(WidgetSet, registry){
186 // | var nodes = registry.map(function(w){ return w.domNode; });
190 // A new array of the returned values.
191 return array
.map(this.toArray(), func
, thisObj
); // Array
194 every: function(func
, thisObj
){
196 // A synthetic clone of `array.every` acting explicitly on this WidgetSet
199 // A callback function run for every widget in this list. Exits loop
200 // when the first false return is encountered.
203 // Optional scope parameter to use for the callback
205 thisObj
= thisObj
|| kernel
.global
;
207 for(i
in this._hash
){
208 if(!func
.call(thisObj
, this._hash
[i
], x
++, this._hash
)){
209 return false; // Boolean
212 return true; // Boolean
215 some: function(func
, thisObj
){
217 // A synthetic clone of `array.some` acting explicitly on this WidgetSet
220 // A callback function run for every widget in this list. Exits loop
221 // when the first true return is encountered.
224 // Optional scope parameter to use for the callback
226 thisObj
= thisObj
|| kernel
.global
;
228 for(i
in this._hash
){
229 if(func
.call(thisObj
, this._hash
[i
], x
++, this._hash
)){
230 return true; // Boolean
233 return false; // Boolean
238 // Add in 1.x compatibility methods to dijit/registry.
239 // These functions won't show up in the API doc but since they are deprecated anyway,
240 // that's probably for the best.
241 array
.forEach(["forEach", "filter", "byClass", "map", "every", "some"], function(func
){
242 registry
[func
] = WidgetSet
.prototype[func
];