Manager_wev8.js
9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.widget.Manager");
dojo.require("dojo.lang");
dojo.require("dojo.event.*");
// Manager class
dojo.widget.manager = new function(){
this.widgets = [];
this.widgetIds = [];
// map of widgetId-->widget for widgets without parents (top level widgets)
this.topWidgets = {};
var widgetTypeCtr = {};
var renderPrefixCache = [];
this.getUniqueId = function (widgetType) {
return widgetType + "_" + (widgetTypeCtr[widgetType] != undefined ?
++widgetTypeCtr[widgetType] : widgetTypeCtr[widgetType] = 0);
}
this.add = function(widget){
dojo.profile.start("dojo.widget.manager.add");
this.widgets.push(widget);
// FIXME: the rest of this method is very slow!
if(widget.widgetId == ""){
if(widget["id"]){
widget.widgetId = widget["id"];
}else if(widget.extraArgs["id"]){
widget.widgetId = widget.extraArgs["id"];
}else{
widget.widgetId = this.getUniqueId(widget.widgetType);
}
}
if(this.widgetIds[widget.widgetId]){
dojo.debug("widget ID collision on ID: "+widget.widgetId);
}
this.widgetIds[widget.widgetId] = widget;
// Widget.destroy already calls removeById(), so we don't need to
// connect() it here
dojo.profile.end("dojo.widget.manager.add");
}
this.destroyAll = function(){
for(var x=this.widgets.length-1; x>=0; x--){
try{
// this.widgets[x].destroyChildren();
this.widgets[x].destroy(true);
delete this.widgets[x];
}catch(e){ }
}
}
// FIXME: we should never allow removal of the root widget until all others
// are removed!
this.remove = function(widgetIndex){
var tw = this.widgets[widgetIndex].widgetId;
delete this.widgetIds[tw];
this.widgets.splice(widgetIndex, 1);
}
// FIXME: suboptimal performance
this.removeById = function(id) {
for (var i=0; i<this.widgets.length; i++){
if(this.widgets[i].widgetId == id){
this.remove(i);
break;
}
}
}
this.getWidgetById = function(id){
return this.widgetIds[id];
}
this.getWidgetsByType = function(type){
var lt = type.toLowerCase();
var ret = [];
dojo.lang.forEach(this.widgets, function(x){
if(x.widgetType.toLowerCase() == lt){
ret.push(x);
}
});
return ret;
}
this.getWidgetsOfType = function (id) {
dj_deprecated("getWidgetsOfType is depecrecated, use getWidgetsByType");
return dojo.widget.manager.getWidgetsByType(id);
}
this.getWidgetsByFilter = function(unaryFunc){
var ret = [];
dojo.lang.forEach(this.widgets, function(x){
if(unaryFunc(x)){
ret.push(x);
}
});
return ret;
}
this.getAllWidgets = function() {
return this.widgets.concat();
}
// shortcuts, baby
this.byId = this.getWidgetById;
this.byType = this.getWidgetsByType;
this.byFilter = this.getWidgetsByFilter;
// map of previousally discovered implementation names to constructors
var knownWidgetImplementations = {};
// support manually registered widget packages
var widgetPackages = ["dojo.widget", "dojo.webui.widgets"];
for (var i=0; i<widgetPackages.length; i++) {
// convenience for checking if a package exists (reverse lookup)
widgetPackages[widgetPackages[i]] = true;
}
this.registerWidgetPackage = function(pname) {
if(!widgetPackages[pname]){
widgetPackages[pname] = true;
widgetPackages.push(pname);
}
}
this.getWidgetPackageList = function() {
return dojo.lang.map(widgetPackages, function(elt) { return(elt!==true ? elt : undefined); });
}
this.getImplementation = function(widgetName, ctorObject, mixins){
// try and find a name for the widget
var impl = this.getImplementationName(widgetName);
if(impl){
// var tic = new Date();
var ret = new impl(ctorObject);
// dojo.debug(new Date() - tic);
return ret;
}
}
this.getImplementationName = function(widgetName){
/*
* This is the overly-simplistic implemention of getImplementation (har
* har). In the future, we are going to want something that allows more
* freedom of expression WRT to specifying different specializations of
* a widget.
*
* Additionally, this implementation treats widget names as case
* insensitive, which does not necessarialy mesh with the markup which
* can construct a widget.
*/
var lowerCaseWidgetName = widgetName.toLowerCase();
var impl = knownWidgetImplementations[lowerCaseWidgetName];
if(impl){
return impl;
}
// first store a list of the render prefixes we are capable of rendering
if(!renderPrefixCache.length){
for(var renderer in dojo.render){
if(dojo.render[renderer]["capable"] === true){
var prefixes = dojo.render[renderer].prefixes;
for(var i = 0; i < prefixes.length; i++){
renderPrefixCache.push(prefixes[i].toLowerCase());
}
}
}
// make sure we don't HAVE to prefix widget implementation names
// with anything to get them to render
renderPrefixCache.push("");
}
// look for a rendering-context specific version of our widget name
for(var i = 0; i < widgetPackages.length; i++){
var widgetPackage = dojo.evalObjPath(widgetPackages[i]);
if(!widgetPackage) { continue; }
for (var j = 0; j < renderPrefixCache.length; j++) {
if (!widgetPackage[renderPrefixCache[j]]) { continue; }
for (var widgetClass in widgetPackage[renderPrefixCache[j]]) {
if (widgetClass.toLowerCase() != lowerCaseWidgetName) { continue; }
knownWidgetImplementations[lowerCaseWidgetName] =
widgetPackage[renderPrefixCache[j]][widgetClass];
return knownWidgetImplementations[lowerCaseWidgetName];
}
}
for (var j = 0; j < renderPrefixCache.length; j++) {
for (var widgetClass in widgetPackage) {
if (widgetClass.toLowerCase() !=
(renderPrefixCache[j] + lowerCaseWidgetName)) { continue; }
knownWidgetImplementations[lowerCaseWidgetName] =
widgetPackage[widgetClass];
return knownWidgetImplementations[lowerCaseWidgetName];
}
}
}
throw new Error('Could not locate "' + widgetName + '" class');
}
// FIXME: does it even belong in this name space?
// NOTE: this method is implemented by DomWidget_wev8.js since not all
// hostenv's would have an implementation.
/*this.getWidgetFromPrimitive = function(baseRenderType){
dj_unimplemented("dojo.widget.manager.getWidgetFromPrimitive");
}
this.getWidgetFromEvent = function(nativeEvt){
dj_unimplemented("dojo.widget.manager.getWidgetFromEvent");
}*/
// Catch window resize events and notify top level widgets
this.onResized = function() {
for(var id in this.topWidgets) {
var child = this.topWidgets[id];
//dojo.debug("root resizing child " + child.widgetId);
if ( child.onResized ) {
child.onResized();
}
}
}
if(typeof window != "undefined") {
dojo.addOnLoad(this, 'onResized'); // initial sizing
dojo.event.connect(window, 'onresize', this, 'onResized'); // window resize
}
// FIXME: what else?
}
// copy the methods from the default manager (this) to the widget namespace
dojo.widget.getUniqueId = function () { return dojo.widget.manager.getUniqueId.apply(dojo.widget.manager, arguments); }
dojo.widget.addWidget = function () { return dojo.widget.manager.add.apply(dojo.widget.manager, arguments); }
dojo.widget.destroyAllWidgets = function () { return dojo.widget.manager.destroyAll.apply(dojo.widget.manager, arguments); }
dojo.widget.removeWidget = function () { return dojo.widget.manager.remove.apply(dojo.widget.manager, arguments); }
dojo.widget.removeWidgetById = function () { return dojo.widget.manager.removeById.apply(dojo.widget.manager, arguments); }
dojo.widget.getWidgetById = function () { return dojo.widget.manager.getWidgetById.apply(dojo.widget.manager, arguments); }
dojo.widget.getWidgetsByType = function () { return dojo.widget.manager.getWidgetsByType.apply(dojo.widget.manager, arguments); }
dojo.widget.getWidgetsByFilter = function () { return dojo.widget.manager.getWidgetsByFilter.apply(dojo.widget.manager, arguments); }
dojo.widget.byId = function () { return dojo.widget.manager.getWidgetById.apply(dojo.widget.manager, arguments); }
dojo.widget.byType = function () { return dojo.widget.manager.getWidgetsByType.apply(dojo.widget.manager, arguments); }
dojo.widget.byFilter = function () { return dojo.widget.manager.getWidgetsByFilter.apply(dojo.widget.manager, arguments); }
dojo.widget.all = function () { return dojo.widget.manager.getAllWidgets.apply(dojo.widget.manager, arguments); }
dojo.widget.registerWidgetPackage = function () { return dojo.widget.manager.registerWidgetPackage.apply(dojo.widget.manager, arguments); }
dojo.widget.getWidgetImplementation = function () { return dojo.widget.manager.getImplementation.apply(dojo.widget.manager, arguments); }
dojo.widget.getWidgetImplementationName = function () { return dojo.widget.manager.getImplementationName.apply(dojo.widget.manager, arguments); }
dojo.widget.widgets = dojo.widget.manager.widgets;
dojo.widget.widgetIds = dojo.widget.manager.widgetIds;
dojo.widget.root = dojo.widget.manager.root;