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