registry.js.uncompressed.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. define("dijit/registry", [
  2. "dojo/_base/array", // array.forEach array.map
  3. "dojo/sniff", // has("ie")
  4. "dojo/_base/unload", // unload.addOnWindowUnload
  5. "dojo/_base/window", // win.body
  6. "./main" // dijit._scopeName
  7. ], function(array, has, unload, win, dijit){
  8. // module:
  9. // dijit/registry
  10. var _widgetTypeCtr = {}, hash = {};
  11. var registry = {
  12. // summary:
  13. // Registry of existing widget on page, plus some utility methods.
  14. // length: Number
  15. // Number of registered widgets
  16. length: 0,
  17. add: function(widget){
  18. // summary:
  19. // Add a widget to the registry. If a duplicate ID is detected, a error is thrown.
  20. // widget: dijit/_WidgetBase
  21. // Any dijit/_WidgetBase subclass.
  22. if(hash[widget.id]){
  23. throw new Error("Tried to register widget with id==" + widget.id + " but that id is already registered");
  24. }
  25. hash[widget.id] = widget;
  26. this.length++;
  27. },
  28. remove: function(/*String*/ id){
  29. // summary:
  30. // Remove a widget from the registry. Does not destroy the widget; simply
  31. // removes the reference.
  32. if(hash[id]){
  33. delete hash[id];
  34. this.length--;
  35. }
  36. },
  37. byId: function(/*String|Widget*/ id){
  38. // summary:
  39. // Find a widget by it's id.
  40. // If passed a widget then just returns the widget.
  41. return typeof id == "string" ? hash[id] : id; // dijit/_WidgetBase
  42. },
  43. byNode: function(/*DOMNode*/ node){
  44. // summary:
  45. // Returns the widget corresponding to the given DOMNode
  46. return hash[node.getAttribute("widgetId")]; // dijit/_WidgetBase
  47. },
  48. toArray: function(){
  49. // summary:
  50. // Convert registry into a true Array
  51. //
  52. // example:
  53. // Work with the widget .domNodes in a real Array
  54. // | array.map(registry.toArray(), function(w){ return w.domNode; });
  55. var ar = [];
  56. for(var id in hash){
  57. ar.push(hash[id]);
  58. }
  59. return ar; // dijit/_WidgetBase[]
  60. },
  61. getUniqueId: function(/*String*/widgetType){
  62. // summary:
  63. // Generates a unique id for a given widgetType
  64. var id;
  65. do{
  66. id = widgetType + "_" +
  67. (widgetType in _widgetTypeCtr ?
  68. ++_widgetTypeCtr[widgetType] : _widgetTypeCtr[widgetType] = 0);
  69. }while(hash[id]);
  70. return dijit._scopeName == "dijit" ? id : dijit._scopeName + "_" + id; // String
  71. },
  72. findWidgets: function(root, skipNode){
  73. // summary:
  74. // Search subtree under root returning widgets found.
  75. // Doesn't search for nested widgets (ie, widgets inside other widgets).
  76. // root: DOMNode
  77. // Node to search under.
  78. // skipNode: DOMNode
  79. // If specified, don't search beneath this node (usually containerNode).
  80. var outAry = [];
  81. function getChildrenHelper(root){
  82. for(var node = root.firstChild; node; node = node.nextSibling){
  83. if(node.nodeType == 1){
  84. var widgetId = node.getAttribute("widgetId");
  85. if(widgetId){
  86. var widget = hash[widgetId];
  87. if(widget){ // may be null on page w/multiple dojo's loaded
  88. outAry.push(widget);
  89. }
  90. }else if(node !== skipNode){
  91. getChildrenHelper(node);
  92. }
  93. }
  94. }
  95. }
  96. getChildrenHelper(root);
  97. return outAry;
  98. },
  99. _destroyAll: function(){
  100. // summary:
  101. // Code to destroy all widgets and do other cleanup on page unload
  102. // Clean up focus manager lingering references to widgets and nodes
  103. dijit._curFocus = null;
  104. dijit._prevFocus = null;
  105. dijit._activeStack = [];
  106. // Destroy all the widgets, top down
  107. array.forEach(registry.findWidgets(win.body()), function(widget){
  108. // Avoid double destroy of widgets like Menu that are attached to <body>
  109. // even though they are logically children of other widgets.
  110. if(!widget._destroyed){
  111. if(widget.destroyRecursive){
  112. widget.destroyRecursive();
  113. }else if(widget.destroy){
  114. widget.destroy();
  115. }
  116. }
  117. });
  118. },
  119. getEnclosingWidget: function(/*DOMNode*/ node){
  120. // summary:
  121. // Returns the widget whose DOM tree contains the specified DOMNode, or null if
  122. // the node is not contained within the DOM tree of any widget
  123. while(node){
  124. var id = node.nodeType == 1 && node.getAttribute("widgetId");
  125. if(id){
  126. return hash[id];
  127. }
  128. node = node.parentNode;
  129. }
  130. return null;
  131. },
  132. // In case someone needs to access hash.
  133. // Actually, this is accessed from WidgetSet back-compatibility code
  134. _hash: hash
  135. };
  136. dijit.registry = registry;
  137. return registry;
  138. });