]>
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/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
){
12 var _widgetTypeCtr
= {}, hash
= {};
16 // Registry of existing widget on page, plus some utility methods.
19 // Number of registered widgets
22 add: function(widget
){
24 // Add a widget to the registry. If a duplicate ID is detected, a error is thrown.
25 // widget: dijit/_WidgetBase
26 // Any dijit/_WidgetBase subclass.
28 throw new Error("Tried to register widget with id==" + widget
.id
+ " but that id is already registered");
30 hash
[widget
.id
] = widget
;
34 remove: function(/*String*/ id
){
36 // Remove a widget from the registry. Does not destroy the widget; simply
37 // removes the reference.
44 byId: function(/*String|Widget*/ id
){
46 // Find a widget by it's id.
47 // If passed a widget then just returns the widget.
48 return typeof id
== "string" ? hash
[id
] : id
; // dijit/_WidgetBase
51 byNode: function(/*DOMNode*/ node
){
53 // Returns the widget corresponding to the given DOMNode
54 return hash
[node
.getAttribute("widgetId")]; // dijit/_WidgetBase
59 // Convert registry into a true Array
62 // Work with the widget .domNodes in a real Array
63 // | array.map(registry.toArray(), function(w){ return w.domNode; });
69 return ar
; // dijit/_WidgetBase[]
72 getUniqueId: function(/*String*/widgetType
){
74 // Generates a unique id for a given widgetType
78 id
= widgetType
+ "_" +
79 (widgetType
in _widgetTypeCtr
?
80 ++_widgetTypeCtr
[widgetType
] : _widgetTypeCtr
[widgetType
] = 0);
82 return dijit
._scopeName
== "dijit" ? id
: dijit
._scopeName
+ "_" + id
; // String
85 findWidgets: function(root
, skipNode
){
87 // Search subtree under root returning widgets found.
88 // Doesn't search for nested widgets (ie, widgets inside other widgets).
90 // Node to search under.
92 // If specified, don't search beneath this node (usually containerNode).
96 function getChildrenHelper(root
){
97 for(var node
= root
.firstChild
; node
; node
= node
.nextSibling
){
98 if(node
.nodeType
== 1){
99 var widgetId
= node
.getAttribute("widgetId");
101 var widget
= hash
[widgetId
];
102 if(widget
){ // may be null on page w/multiple dojo's loaded
105 }else if(node
!== skipNode
){
106 getChildrenHelper(node
);
112 getChildrenHelper(root
);
116 _destroyAll: function(){
118 // Code to destroy all widgets and do other cleanup on page unload
120 // Clean up focus manager lingering references to widgets and nodes
121 dijit
._curFocus
= null;
122 dijit
._prevFocus
= null;
123 dijit
._activeStack
= [];
125 // Destroy all the widgets, top down
126 array
.forEach(registry
.findWidgets(win
.body()), function(widget
){
127 // Avoid double destroy of widgets like Menu that are attached to <body>
128 // even though they are logically children of other widgets.
129 if(!widget
._destroyed
){
130 if(widget
.destroyRecursive
){
131 widget
.destroyRecursive();
132 }else if(widget
.destroy
){
139 getEnclosingWidget: function(/*DOMNode*/ node
){
141 // Returns the widget whose DOM tree contains the specified DOMNode, or null if
142 // the node is not contained within the DOM tree of any widget
144 var id
= node
.nodeType
== 1 && node
.getAttribute("widgetId");
148 node
= node
.parentNode
;
153 // In case someone needs to access hash.
154 // Actually, this is accessed from WidgetSet back-compatibility code
158 dijit
.registry
= registry
;