]>
git.wh0rd.org - tt-rss.git/blob - lib/dijit/registry.js.uncompressed.js
1 define("dijit/registry", [
2 "dojo/_base/array", // array.forEach array.map
3 "dojo/_base/sniff", // has("ie")
4 "dojo/_base/unload", // unload.addOnWindowUnload
5 "dojo/_base/window", // win.body
6 "." // dijit._scopeName
7 ], function(array
, has
, unload
, win
, dijit
){
12 // Registry of existing widget on page, plus some utility methods.
13 // Must be accessed through AMD api, ex:
14 // require(["dijit/registry"], function(registry){ registry.byId("foo"); })
16 var _widgetTypeCtr
= {}, hash
= {};
20 // A set of widgets indexed by id
24 add: function(/*dijit._Widget*/ widget
){
26 // Add a widget to the registry. If a duplicate ID is detected, a error is thrown.
28 // widget: dijit._Widget
29 // Any dijit._Widget subclass.
31 throw new Error("Tried to register widget with id==" + widget
.id
+ " but that id is already registered");
33 hash
[widget
.id
] = widget
;
37 remove: function(/*String*/ id
){
39 // Remove a widget from the registry. Does not destroy the widget; simply
40 // removes the reference.
47 byId: function(/*String|Widget*/ id
){
49 // Find a widget by it's id.
50 // If passed a widget then just returns the widget.
51 return typeof id
== "string" ? hash
[id
] : id
; // dijit._Widget
54 byNode: function(/*DOMNode*/ node
){
56 // Returns the widget corresponding to the given DOMNode
57 return hash
[node
.getAttribute("widgetId")]; // dijit._Widget
62 // Convert registry into a true Array
65 // Work with the widget .domNodes in a real Array
66 // | array.map(dijit.registry.toArray(), function(w){ return w.domNode; });
72 return ar
; // dijit._Widget[]
75 getUniqueId: function(/*String*/widgetType
){
77 // Generates a unique id for a given widgetType
81 id
= widgetType
+ "_" +
82 (widgetType
in _widgetTypeCtr
?
83 ++_widgetTypeCtr
[widgetType
] : _widgetTypeCtr
[widgetType
] = 0);
85 return dijit
._scopeName
== "dijit" ? id
: dijit
._scopeName
+ "_" + id
; // String
88 findWidgets: function(/*DomNode*/ root
){
90 // Search subtree under root returning widgets found.
91 // Doesn't search for nested widgets (ie, widgets inside other widgets).
95 function getChildrenHelper(root
){
96 for(var node
= root
.firstChild
; node
; node
= node
.nextSibling
){
97 if(node
.nodeType
== 1){
98 var widgetId
= node
.getAttribute("widgetId");
100 var widget
= hash
[widgetId
];
101 if(widget
){ // may be null on page w/multiple dojo's loaded
105 getChildrenHelper(node
);
111 getChildrenHelper(root
);
115 _destroyAll: function(){
117 // Code to destroy all widgets and do other cleanup on page unload
119 // Clean up focus manager lingering references to widgets and nodes
120 dijit
._curFocus
= null;
121 dijit
._prevFocus
= null;
122 dijit
._activeStack
= [];
124 // Destroy all the widgets, top down
125 array
.forEach(registry
.findWidgets(win
.body()), function(widget
){
126 // Avoid double destroy of widgets like Menu that are attached to <body>
127 // even though they are logically children of other widgets.
128 if(!widget
._destroyed
){
129 if(widget
.destroyRecursive
){
130 widget
.destroyRecursive();
131 }else if(widget
.destroy
){
138 getEnclosingWidget: function(/*DOMNode*/ node
){
140 // Returns the widget whose DOM tree contains the specified DOMNode, or null if
141 // the node is not contained within the DOM tree of any widget
143 var id
= node
.getAttribute
&& node
.getAttribute("widgetId");
147 node
= node
.parentNode
;
152 // In case someone needs to access hash.
153 // Actually, this is accessed from WidgetSet back-compatibility code
158 // Only run _destroyAll() for IE because we think it's only necessary in that case,
159 // and because it causes problems on FF. See bug #3531 for details.
160 unload
.addOnWindowUnload(function(){
161 registry
._destroyAll();
168 // A list of widgets on a page.
171 dijit
.registry
= registry
;