]>
Commit | Line | Data |
---|---|---|
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){ | |
7 | ||
8 | // module: | |
9 | // dijit/WidgetSet | |
10 | // summary: | |
11 | // Legacy registry code. New modules should just use registry. | |
12 | // Will be removed in 2.0. | |
13 | ||
14 | var WidgetSet = declare("dijit.WidgetSet", null, { | |
15 | // summary: | |
16 | // A set of widgets indexed by id. A default instance of this class is | |
17 | // available as `dijit.registry` | |
18 | // | |
19 | // example: | |
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")); | |
24 | // | // destroy both: | |
25 | // | ws.forEach(function(w){ w.destroy(); }); | |
26 | // | |
27 | // example: | |
28 | // Using dijit.registry: | |
29 | // | dijit.registry.forEach(function(w){ /* do something */ }); | |
30 | ||
31 | constructor: function(){ | |
32 | this._hash = {}; | |
33 | this.length = 0; | |
34 | }, | |
35 | ||
36 | add: function(/*dijit._Widget*/ widget){ | |
37 | // summary: | |
38 | // Add a widget to this list. If a duplicate ID is detected, a error is thrown. | |
39 | // | |
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"); | |
44 | } | |
45 | this._hash[widget.id] = widget; | |
46 | this.length++; | |
47 | }, | |
48 | ||
49 | remove: function(/*String*/ id){ | |
50 | // summary: | |
51 | // Remove a widget from this WidgetSet. Does not destroy the widget; simply | |
52 | // removes the reference. | |
53 | if(this._hash[id]){ | |
54 | delete this._hash[id]; | |
55 | this.length--; | |
56 | } | |
57 | }, | |
58 | ||
59 | forEach: function(/*Function*/ func, /* Object? */thisObj){ | |
60 | // summary: | |
61 | // Call specified function for each widget in this set. | |
62 | // | |
63 | // func: | |
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`. | |
66 | // | |
67 | // thisObj: | |
68 | // An optional scope parameter | |
69 | // | |
70 | // example: | |
71 | // Using the default `dijit.registry` instance: | |
72 | // | dijit.registry.forEach(function(widget){ | |
73 | // | console.log(widget.declaredClass); | |
74 | // | }); | |
75 | // | |
76 | // returns: | |
77 | // Returns self, in order to allow for further chaining. | |
78 | ||
79 | thisObj = thisObj || win.global; | |
80 | var i = 0, id; | |
81 | for(id in this._hash){ | |
82 | func.call(thisObj, this._hash[id], i++, this._hash); | |
83 | } | |
84 | return this; // dijit.WidgetSet | |
85 | }, | |
86 | ||
87 | filter: function(/*Function*/ filter, /* Object? */thisObj){ | |
88 | // summary: | |
89 | // Filter down this WidgetSet to a smaller new WidgetSet | |
90 | // Works the same as `array.filter` and `NodeList.filter` | |
91 | // | |
92 | // filter: | |
93 | // Callback function to test truthiness. Is passed the widget | |
94 | // reference and the pseudo-index in the object. | |
95 | // | |
96 | // thisObj: Object? | |
97 | // Option scope to use for the filter function. | |
98 | // | |
99 | // example: | |
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 */ }); | |
104 | ||
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)){ | |
110 | res.add(w); | |
111 | } | |
112 | } | |
113 | return res; // dijit.WidgetSet | |
114 | }, | |
115 | ||
116 | byId: function(/*String*/ id){ | |
117 | // summary: | |
118 | // Find a widget in this list by it's id. | |
119 | // example: | |
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 | |
125 | ||
126 | return this._hash[id]; // dijit._Widget | |
127 | }, | |
128 | ||
129 | byClass: function(/*String*/ cls){ | |
130 | // summary: | |
131 | // Reduce this widgetset to a new WidgetSet of a particular `declaredClass` | |
132 | // | |
133 | // cls: String | |
134 | // The Class to scan for. Full dot-notated string. | |
135 | // | |
136 | // example: | |
137 | // Find all `dijit.TitlePane`s in a page: | |
138 | // | dijit.registry.byClass("dijit.TitlePane").forEach(function(tp){ tp.close(); }); | |
139 | ||
140 | var res = new WidgetSet(), id, widget; | |
141 | for(id in this._hash){ | |
142 | widget = this._hash[id]; | |
143 | if(widget.declaredClass == cls){ | |
144 | res.add(widget); | |
145 | } | |
146 | } | |
147 | return res; // dijit.WidgetSet | |
148 | }, | |
149 | ||
150 | toArray: function(){ | |
151 | // summary: | |
152 | // Convert this WidgetSet into a true Array | |
153 | // | |
154 | // example: | |
155 | // Work with the widget .domNodes in a real Array | |
156 | // | array.map(dijit.registry.toArray(), function(w){ return w.domNode; }); | |
157 | ||
158 | var ar = []; | |
159 | for(var id in this._hash){ | |
160 | ar.push(this._hash[id]); | |
161 | } | |
162 | return ar; // dijit._Widget[] | |
163 | }, | |
164 | ||
165 | map: function(/* Function */func, /* Object? */thisObj){ | |
166 | // summary: | |
167 | // Create a new Array from this WidgetSet, following the same rules as `array.map` | |
168 | // example: | |
169 | // | var nodes = dijit.registry.map(function(w){ return w.domNode; }); | |
170 | // | |
171 | // returns: | |
172 | // A new array of the returned values. | |
173 | return array.map(this.toArray(), func, thisObj); // Array | |
174 | }, | |
175 | ||
176 | every: function(func, thisObj){ | |
177 | // summary: | |
178 | // A synthetic clone of `array.every` acting explicitly on this WidgetSet | |
179 | // | |
180 | // func: Function | |
181 | // A callback function run for every widget in this list. Exits loop | |
182 | // when the first false return is encountered. | |
183 | // | |
184 | // thisObj: Object? | |
185 | // Optional scope parameter to use for the callback | |
186 | ||
187 | thisObj = thisObj || win.global; | |
188 | var x = 0, i; | |
189 | for(i in this._hash){ | |
190 | if(!func.call(thisObj, this._hash[i], x++, this._hash)){ | |
191 | return false; // Boolean | |
192 | } | |
193 | } | |
194 | return true; // Boolean | |
195 | }, | |
196 | ||
197 | some: function(func, thisObj){ | |
198 | // summary: | |
199 | // A synthetic clone of `array.some` acting explicitly on this WidgetSet | |
200 | // | |
201 | // func: Function | |
202 | // A callback function run for every widget in this list. Exits loop | |
203 | // when the first true return is encountered. | |
204 | // | |
205 | // thisObj: Object? | |
206 | // Optional scope parameter to use for the callback | |
207 | ||
208 | thisObj = thisObj || win.global; | |
209 | var x = 0, i; | |
210 | for(i in this._hash){ | |
211 | if(func.call(thisObj, this._hash[i], x++, this._hash)){ | |
212 | return true; // Boolean | |
213 | } | |
214 | } | |
215 | return false; // Boolean | |
216 | } | |
217 | ||
218 | }); | |
219 | ||
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]; | |
225 | }); | |
226 | ||
227 | ||
228 | return WidgetSet; | |
229 | }); |