Debian Codegraph @

dijit/registry.js

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