]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/_base/manager.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dijit / _base / manager.js
1 if(!dojo._hasResource["dijit._base.manager"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit._base.manager"] = true;
3 dojo.provide("dijit._base.manager");
4
5 dojo.declare("dijit.WidgetSet", null, {
6         // summary:
7         //      A set of widgets indexed by id
8
9         constructor: function(){
10                 this._hash={};
11         },
12
13         add: function(/*Widget*/ widget){
14                 if(this._hash[widget.id]){
15                         throw new Error("Tried to register widget with id==" + widget.id + " but that id is already registered");
16                 }
17                 this._hash[widget.id]=widget;
18         },
19
20         remove: function(/*String*/ id){
21                 delete this._hash[id];
22         },
23
24         forEach: function(/*Function*/ func){
25                 for(var id in this._hash){
26                         func(this._hash[id]);
27                 }
28         },
29
30         filter: function(/*Function*/ filter){
31                 var res = new dijit.WidgetSet();
32                 this.forEach(function(widget){
33                         if(filter(widget)){ res.add(widget); }
34                 });
35                 return res;             // dijit.WidgetSet
36         },
37
38         byId: function(/*String*/ id){
39                 return this._hash[id];
40         },
41
42         byClass: function(/*String*/ cls){
43                 return this.filter(function(widget){ return widget.declaredClass==cls; });      // dijit.WidgetSet
44         }
45         });
46
47 /*=====
48 dijit.registry = {
49         // summary: A list of widgets on a page.
50         // description: Is an instance of dijit.WidgetSet
51 };
52 =====*/
53 dijit.registry = new dijit.WidgetSet();
54
55 dijit._widgetTypeCtr = {};
56
57 dijit.getUniqueId = function(/*String*/widgetType){
58         // summary
59         //      Generates a unique id for a given widgetType
60
61         var id;
62         do{
63                 id = widgetType + "_" +
64                         (widgetType in dijit._widgetTypeCtr ?
65                                 ++dijit._widgetTypeCtr[widgetType] : dijit._widgetTypeCtr[widgetType] = 0);
66         }while(dijit.byId(id));
67         return id; // String
68 };
69
70
71 if(dojo.isIE){
72         // Only run this for IE because we think it's only necessary in that case,
73         // and because it causes problems on FF.  See bug #3531 for details.
74         dojo.addOnUnload(function(){
75                 dijit.registry.forEach(function(widget){ widget.destroy(); });
76         });
77 }
78
79 dijit.byId = function(/*String|Widget*/id){
80         // summary:
81         //              Returns a widget by its id, or if passed a widget, no-op (like dojo.byId())
82         return (dojo.isString(id)) ? dijit.registry.byId(id) : id; // Widget
83 };
84
85 dijit.byNode = function(/* DOMNode */ node){
86         // summary:
87         //              Returns the widget as referenced by node
88         return dijit.registry.byId(node.getAttribute("widgetId")); // Widget
89 };
90
91 dijit.getEnclosingWidget = function(/* DOMNode */ node){
92         // summary:
93         //              Returns the widget whose dom tree contains node or null if
94         //              the node is not contained within the dom tree of any widget
95         while(node){
96                 if(node.getAttribute && node.getAttribute("widgetId")){
97                         return dijit.registry.byId(node.getAttribute("widgetId"));
98                 }
99                 node = node.parentNode;
100         }
101         return null;
102 };
103
104 // elements that are tab-navigable if they have no tabindex value set
105 // (except for "a", which must have an href attribute)
106 dijit._tabElements = {
107         area: true,
108         button: true,
109         input: true,
110         object: true,
111         select: true,
112         textarea: true
113 };
114
115 dijit._isElementShown = function(/*Element*/elem){
116         var style = dojo.style(elem);
117         return (style.visibility != "hidden")
118                 && (style.visibility != "collapsed")
119                 && (style.display != "none");
120 }
121
122 dijit.isTabNavigable = function(/*Element*/elem){
123         // summary:
124         //              Tests if an element is tab-navigable
125         if(dojo.hasAttr(elem, "disabled")){ return false; }
126         var hasTabindex = dojo.hasAttr(elem, "tabindex");
127         var tabindex = dojo.attr(elem, "tabindex");
128         if(hasTabindex && tabindex >= 0) {
129                 return true; // boolean
130         }
131         var name = elem.nodeName.toLowerCase();
132         if(((name == "a" && dojo.hasAttr(elem, "href"))
133                         || dijit._tabElements[name])
134                 && (!hasTabindex || tabindex >= 0)){
135                 return true; // boolean
136         }
137         return false; // boolean
138 };
139
140 dijit._getTabNavigable = function(/*DOMNode*/root){
141         // summary:
142         //              Finds the following descendants of the specified root node:
143         //              * the first tab-navigable element in document order
144         //                without a tabindex or with tabindex="0"
145         //              * the last tab-navigable element in document order
146         //                without a tabindex or with tabindex="0"
147         //              * the first element in document order with the lowest
148         //                positive tabindex value
149         //              * the last element in document order with the highest
150         //                positive tabindex value
151         var first, last, lowest, lowestTabindex, highest, highestTabindex;
152         var walkTree = function(/*DOMNode*/parent){
153                 dojo.query("> *", parent).forEach(function(child){
154                         var isShown = dijit._isElementShown(child);
155                         if(isShown && dijit.isTabNavigable(child)){
156                                 var tabindex = dojo.attr(child, "tabindex");
157                                 if(!dojo.hasAttr(child, "tabindex") || tabindex == 0){
158                                         if(!first){ first = child; }
159                                         last = child;
160                                 }else if(tabindex > 0){
161                                         if(!lowest || tabindex < lowestTabindex){
162                                                 lowestTabindex = tabindex;
163                                                 lowest = child;
164                                         }
165                                         if(!highest || tabindex >= highestTabindex){
166                                                 highestTabindex = tabindex;
167                                                 highest = child;
168                                         }
169                                 }
170                         }
171                         if(isShown){ walkTree(child) }
172                 });
173         };
174         if(dijit._isElementShown(root)){ walkTree(root) }
175         return { first: first, last: last, lowest: lowest, highest: highest };
176 }
177
178 dijit.getFirstInTabbingOrder = function(/*String|DOMNode*/root){
179         // summary:
180         //              Finds the descendant of the specified root node
181         //              that is first in the tabbing order
182         var elems = dijit._getTabNavigable(dojo.byId(root));
183         return elems.lowest ? elems.lowest : elems.first; // Element
184 };
185
186 dijit.getLastInTabbingOrder = function(/*String|DOMNode*/root){
187         // summary:
188         //              Finds the descendant of the specified root node
189         //              that is last in the tabbing order
190         var elems = dijit._getTabNavigable(dojo.byId(root));
191         return elems.last ? elems.last : elems.highest; // Element
192 };
193
194 }