Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.widget.Manager");
14
dojo.require("dojo.lang.array");
15
dojo.require("dojo.lang.func");
16
dojo.require("dojo.event.*");
17
dojo.widget.manager = new function () {
18
	this.widgets = [];
19
	this.widgetIds = [];
20
	this.topWidgets = {};
21
	var widgetTypeCtr = {};
22
	var renderPrefixCache = [];
23
	this.getUniqueId = function (widgetType) {
24
		var widgetId;
25
		do {
26
			widgetId = widgetType + "_" + (widgetTypeCtr[widgetType] != undefined ? ++widgetTypeCtr[widgetType] : widgetTypeCtr[widgetType] = 0);
27
		} while (this.getWidgetById(widgetId));
28
		return widgetId;
29
	};
30
	this.add = function (widget) {
31
		this.widgets.push(widget);
32
		if (!widget.extraArgs["id"]) {
33
			widget.extraArgs["id"] = widget.extraArgs["ID"];
34
		}
35
		if (widget.widgetId == "") {
36
			if (widget["id"]) {
37
				widget.widgetId = widget["id"];
38
			} else {
39
				if (widget.extraArgs["id"]) {
40
					widget.widgetId = widget.extraArgs["id"];
41
				} else {
42
					widget.widgetId = this.getUniqueId(widget.ns + "_" + widget.widgetType);
43
				}
44
			}
45
		}
46
		if (this.widgetIds[widget.widgetId]) {
47
			dojo.debug("widget ID collision on ID: " + widget.widgetId);
48
		}
49
		this.widgetIds[widget.widgetId] = widget;
50
	};
51
	this.destroyAll = function () {
52
		for (var x = this.widgets.length - 1; x >= 0; x--) {
53
			try {
54
				this.widgets[x].destroy(true);
55
				delete this.widgets[x];
56
			}
57
			catch (e) {
58
			}
59
		}
60
	};
61
	this.remove = function (widgetIndex) {
62
		if (dojo.lang.isNumber(widgetIndex)) {
63
			var tw = this.widgets[widgetIndex].widgetId;
64
			delete this.topWidgets[tw];
65
			delete this.widgetIds[tw];
66
			this.widgets.splice(widgetIndex, 1);
67
		} else {
68
			this.removeById(widgetIndex);
69
		}
70
	};
71
	this.removeById = function (id) {
72
		if (!dojo.lang.isString(id)) {
73
			id = id["widgetId"];
74
			if (!id) {
75
				dojo.debug("invalid widget or id passed to removeById");
76
				return;
77
			}
78
		}
79
		for (var i = 0; i < this.widgets.length; i++) {
80
			if (this.widgets[i].widgetId == id) {
81
				this.remove(i);
82
				break;
83
			}
84
		}
85
	};
86
	this.getWidgetById = function (id) {
87
		if (dojo.lang.isString(id)) {
88
			return this.widgetIds[id];
89
		}
90
		return id;
91
	};
92
	this.getWidgetsByType = function (type) {
93
		var lt = type.toLowerCase();
94
		var getType = (type.indexOf(":") < 0 ? function (x) {
95
			return x.widgetType.toLowerCase();
96
		} : function (x) {
97
			return x.getNamespacedType();
98
		});
99
		var ret = [];
100
		dojo.lang.forEach(this.widgets, function (x) {
101
			if (getType(x) == lt) {
102
				ret.push(x);
103
			}
104
		});
105
		return ret;
106
	};
107
	this.getWidgetsByFilter = function (unaryFunc, onlyOne) {
108
		var ret = [];
109
		dojo.lang.every(this.widgets, function (x) {
110
			if (unaryFunc(x)) {
111
				ret.push(x);
112
				if (onlyOne) {
113
					return false;
114
				}
115
			}
116
			return true;
117
		});
118
		return (onlyOne ? ret[0] : ret);
119
	};
120
	this.getAllWidgets = function () {
121
		return this.widgets.concat();
122
	};
123
	this.getWidgetByNode = function (node) {
124
		var w = this.getAllWidgets();
125
		node = dojo.byId(node);
126
		for (var i = 0; i < w.length; i++) {
127
			if (w[i].domNode == node) {
128
				return w[i];
129
			}
130
		}
131
		return null;
132
	};
133
	this.byId = this.getWidgetById;
134
	this.byType = this.getWidgetsByType;
135
	this.byFilter = this.getWidgetsByFilter;
136
	this.byNode = this.getWidgetByNode;
137
	var knownWidgetImplementations = {};
138
	var widgetPackages = ["dojo.widget"];
139
	for (var i = 0; i < widgetPackages.length; i++) {
140
		widgetPackages[widgetPackages[i]] = true;
141
	}
142
	this.registerWidgetPackage = function (pname) {
143
		if (!widgetPackages[pname]) {
144
			widgetPackages[pname] = true;
145
			widgetPackages.push(pname);
146
		}
147
	};
148
	this.getWidgetPackageList = function () {
149
		return dojo.lang.map(widgetPackages, function (elt) {
150
			return (elt !== true ? elt : undefined);
151
		});
152
	};
153
	this.getImplementation = function (widgetName, ctorObject, mixins, ns) {
154
		var impl = this.getImplementationName(widgetName, ns);
155
		if (impl) {
156
			var ret = ctorObject ? new impl(ctorObject) : new impl();
157
			return ret;
158
		}
159
	};
160
	function buildPrefixCache() {
161
		for (var renderer in dojo.render) {
162
			if (dojo.render[renderer]["capable"] === true) {
163
				var prefixes = dojo.render[renderer].prefixes;
164
				for (var i = 0; i < prefixes.length; i++) {
165
					renderPrefixCache.push(prefixes[i].toLowerCase());
166
				}
167
			}
168
		}
169
	}
170
	var findImplementationInModule = function (lowerCaseWidgetName, module) {
171
		if (!module) {
172
			return null;
173
		}
174
		for (var i = 0, l = renderPrefixCache.length, widgetModule; i <= l; i++) {
175
			widgetModule = (i < l ? module[renderPrefixCache[i]] : module);
176
			if (!widgetModule) {
177
				continue;
178
			}
179
			for (var name in widgetModule) {
180
				if (name.toLowerCase() == lowerCaseWidgetName) {
181
					return widgetModule[name];
182
				}
183
			}
184
		}
185
		return null;
186
	};
187
	var findImplementation = function (lowerCaseWidgetName, moduleName) {
188
		var module = dojo.evalObjPath(moduleName, false);
189
		return (module ? findImplementationInModule(lowerCaseWidgetName, module) : null);
190
	};
191
	this.getImplementationName = function (widgetName, ns) {
192
		var lowerCaseWidgetName = widgetName.toLowerCase();
193
		ns = ns || "dojo";
194
		var imps = knownWidgetImplementations[ns] || (knownWidgetImplementations[ns] = {});
195
		var impl = imps[lowerCaseWidgetName];
196
		if (impl) {
197
			return impl;
198
		}
199
		if (!renderPrefixCache.length) {
200
			buildPrefixCache();
201
		}
202
		var nsObj = dojo.ns.get(ns);
203
		if (!nsObj) {
204
			dojo.ns.register(ns, ns + ".widget");
205
			nsObj = dojo.ns.get(ns);
206
		}
207
		if (nsObj) {
208
			nsObj.resolve(widgetName);
209
		}
210
		impl = findImplementation(lowerCaseWidgetName, nsObj.module);
211
		if (impl) {
212
			return (imps[lowerCaseWidgetName] = impl);
213
		}
214
		nsObj = dojo.ns.require(ns);
215
		if ((nsObj) && (nsObj.resolver)) {
216
			nsObj.resolve(widgetName);
217
			impl = findImplementation(lowerCaseWidgetName, nsObj.module);
218
			if (impl) {
219
				return (imps[lowerCaseWidgetName] = impl);
220
			}
221
		}
222
		dojo.deprecated("dojo.widget.Manager.getImplementationName", "Could not locate widget implementation for \"" + widgetName + "\" in \"" + nsObj.module + "\" registered to namespace \"" + nsObj.name + "\". " + "Developers must specify correct namespaces for all non-Dojo widgets", "0.5");
223
		for (var i = 0; i < widgetPackages.length; i++) {
224
			impl = findImplementation(lowerCaseWidgetName, widgetPackages[i]);
225
			if (impl) {
226
				return (imps[lowerCaseWidgetName] = impl);
227
			}
228
		}
229
		throw new Error("Could not locate widget implementation for \"" + widgetName + "\" in \"" + nsObj.module + "\" registered to namespace \"" + nsObj.name + "\"");
230
	};
231
	this.resizing = false;
232
	this.onWindowResized = function () {
233
		if (this.resizing) {
234
			return;
235
		}
236
		try {
237
			this.resizing = true;
238
			for (var id in this.topWidgets) {
239
				var child = this.topWidgets[id];
240
				if (child.checkSize) {
241
					child.checkSize();
242
				}
243
			}
244
		}
245
		catch (e) {
246
		}
247
		finally {
248
			this.resizing = false;
249
		}
250
	};
251
	if (typeof window != "undefined") {
252
		dojo.addOnLoad(this, "onWindowResized");
253
		dojo.event.connect(window, "onresize", this, "onWindowResized");
254
	}
255
};
256
(function () {
257
	var dw = dojo.widget;
258
	var dwm = dw.manager;
259
	var h = dojo.lang.curry(dojo.lang, "hitch", dwm);
260
	var g = function (oldName, newName) {
261
		dw[(newName || oldName)] = h(oldName);
262
	};
263
	g("add", "addWidget");
264
	g("destroyAll", "destroyAllWidgets");
265
	g("remove", "removeWidget");
266
	g("removeById", "removeWidgetById");
267
	g("getWidgetById");
268
	g("getWidgetById", "byId");
269
	g("getWidgetsByType");
270
	g("getWidgetsByFilter");
271
	g("getWidgetsByType", "byType");
272
	g("getWidgetsByFilter", "byFilter");
273
	g("getWidgetByNode", "byNode");
274
	dw.all = function (n) {
275
		var widgets = dwm.getAllWidgets.apply(dwm, arguments);
276
		if (arguments.length > 0) {
277
			return widgets[n];
278
		}
279
		return widgets;
280
	};
281
	g("registerWidgetPackage");
282
	g("getImplementation", "getWidgetImplementation");
283
	g("getImplementationName", "getWidgetImplementationName");
284
	dw.widgets = dwm.widgets;
285
	dw.widgetIds = dwm.widgetIds;
286
	dw.root = dwm.root;
287
})();
288