WidgetSet.js 6.4 KB

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