Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Details | Compare with Previous | 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.Widget");
14
dojo.require("dojo.lang.func");
15
dojo.require("dojo.lang.array");
16
dojo.require("dojo.lang.extras");
17
dojo.require("dojo.lang.declare");
18
dojo.require("dojo.ns");
19
dojo.require("dojo.widget.Manager");
20
dojo.require("dojo.event.*");
21
dojo.require("dojo.a11y");
22
dojo.declare("dojo.widget.Widget", null, function () {
23
	this.children = [];
24
	this.extraArgs = {};
25
}, {parent:null, isTopLevel:false, disabled:false, isContainer:false, widgetId:"", widgetType:"Widget", ns:"dojo", getNamespacedType:function () {
26
	return (this.ns ? this.ns + ":" + this.widgetType : this.widgetType).toLowerCase();
27
}, toString:function () {
28
	return "[Widget " + this.getNamespacedType() + ", " + (this.widgetId || "NO ID") + "]";
29
}, repr:function () {
30
	return this.toString();
31
}, enable:function () {
32
	this.disabled = false;
33
}, disable:function () {
34
	this.disabled = true;
35
}, onResized:function () {
36
	this.notifyChildrenOfResize();
37
}, notifyChildrenOfResize:function () {
38
	for (var i = 0; i < this.children.length; i++) {
39
		var child = this.children[i];
40
		if (child.onResized) {
41
			child.onResized();
42
		}
43
	}
44
}, create:function (args, fragment, parent, ns) {
45
	if (ns) {
46
		this.ns = ns;
47
	}
48
	this.satisfyPropertySets(args, fragment, parent);
49
	this.mixInProperties(args, fragment, parent);
50
	this.postMixInProperties(args, fragment, parent);
51
	dojo.widget.manager.add(this);
52
	this.buildRendering(args, fragment, parent);
53
	this.initialize(args, fragment, parent);
54
	this.postInitialize(args, fragment, parent);
55
	this.postCreate(args, fragment, parent);
56
	return this;
57
}, destroy:function (finalize) {
58
	if (this.parent) {
59
		this.parent.removeChild(this);
60
	}
61
	this.destroyChildren();
62
	this.uninitialize();
63
	this.destroyRendering(finalize);
64
	dojo.widget.manager.removeById(this.widgetId);
65
}, destroyChildren:function () {
66
	var widget;
67
	var i = 0;
68
	while (this.children.length > i) {
69
		widget = this.children[i];
70
		if (widget instanceof dojo.widget.Widget) {
71
			this.removeChild(widget);
72
			widget.destroy();
73
			continue;
74
		}
75
		i++;
76
	}
77
}, getChildrenOfType:function (type, recurse) {
78
	var ret = [];
79
	var isFunc = dojo.lang.isFunction(type);
80
	if (!isFunc) {
81
		type = type.toLowerCase();
82
	}
83
	for (var x = 0; x < this.children.length; x++) {
84
		if (isFunc) {
85
			if (this.children[x] instanceof type) {
86
				ret.push(this.children[x]);
87
			}
88
		} else {
89
			if (this.children[x].widgetType.toLowerCase() == type) {
90
				ret.push(this.children[x]);
91
			}
92
		}
93
		if (recurse) {
94
			ret = ret.concat(this.children[x].getChildrenOfType(type, recurse));
95
		}
96
	}
97
	return ret;
98
}, getDescendants:function () {
99
	var result = [];
100
	var stack = [this];
101
	var elem;
102
	while ((elem = stack.pop())) {
103
		result.push(elem);
104
		if (elem.children) {
105
			dojo.lang.forEach(elem.children, function (elem) {
106
				stack.push(elem);
107
			});
108
		}
109
	}
110
	return result;
111
}, isFirstChild:function () {
112
	return this === this.parent.children[0];
113
}, isLastChild:function () {
114
	return this === this.parent.children[this.parent.children.length - 1];
115
}, satisfyPropertySets:function (args) {
116
	return args;
117
}, mixInProperties:function (args, frag) {
118
	if ((args["fastMixIn"]) || (frag["fastMixIn"])) {
119
		for (var x in args) {
120
			this[x] = args[x];
121
		}
122
		return;
123
	}
124
	var undef;
125
	var lcArgs = dojo.widget.lcArgsCache[this.widgetType];
126
	if (lcArgs == null) {
127
		lcArgs = {};
128
		for (var y in this) {
129
			lcArgs[((new String(y)).toLowerCase())] = y;
130
		}
131
		dojo.widget.lcArgsCache[this.widgetType] = lcArgs;
132
	}
133
	var visited = {};
134
	for (var x in args) {
135
		if (!this[x]) {
136
			var y = lcArgs[(new String(x)).toLowerCase()];
137
			if (y) {
138
				args[y] = args[x];
139
				x = y;
140
			}
141
		}
142
		if (visited[x]) {
143
			continue;
144
		}
145
		visited[x] = true;
146
		if ((typeof this[x]) != (typeof undef)) {
147
			if (typeof args[x] != "string") {
148
				this[x] = args[x];
149
			} else {
150
				if (dojo.lang.isString(this[x])) {
151
					this[x] = args[x];
152
				} else {
153
					if (dojo.lang.isNumber(this[x])) {
154
						this[x] = new Number(args[x]);
155
					} else {
156
						if (dojo.lang.isBoolean(this[x])) {
157
							this[x] = (args[x].toLowerCase() == "false") ? false : true;
158
						} else {
159
							if (dojo.lang.isFunction(this[x])) {
160
								if (args[x].search(/[^\w\.]+/i) == -1) {
161
									this[x] = dojo.evalObjPath(args[x], false);
162
								} else {
163
									var tn = dojo.lang.nameAnonFunc(new Function(args[x]), this);
164
									dojo.event.kwConnect({srcObj:this, srcFunc:x, adviceObj:this, adviceFunc:tn});
165
								}
166
							} else {
167
								if (dojo.lang.isArray(this[x])) {
168
									this[x] = args[x].split(";");
169
								} else {
170
									if (this[x] instanceof Date) {
171
										this[x] = new Date(Number(args[x]));
172
									} else {
173
										if (typeof this[x] == "object") {
174
											if (this[x] instanceof dojo.uri.Uri) {
175
												this[x] = dojo.uri.dojoUri(args[x]);
176
											} else {
177
												var pairs = args[x].split(";");
178
												for (var y = 0; y < pairs.length; y++) {
179
													var si = pairs[y].indexOf(":");
180
													if ((si != -1) && (pairs[y].length > si)) {
181
														this[x][pairs[y].substr(0, si).replace(/^\s+|\s+$/g, "")] = pairs[y].substr(si + 1);
182
													}
183
												}
184
											}
185
										} else {
186
											this[x] = args[x];
187
										}
188
									}
189
								}
190
							}
191
						}
192
					}
193
				}
194
			}
195
		} else {
196
			this.extraArgs[x.toLowerCase()] = args[x];
197
		}
198
	}
199
}, postMixInProperties:function (args, frag, parent) {
200
}, initialize:function (args, frag, parent) {
201
	return false;
202
}, postInitialize:function (args, frag, parent) {
203
	return false;
204
}, postCreate:function (args, frag, parent) {
205
	return false;
206
}, uninitialize:function () {
207
	return false;
208
}, buildRendering:function (args, frag, parent) {
209
	dojo.unimplemented("dojo.widget.Widget.buildRendering, on " + this.toString() + ", ");
210
	return false;
211
}, destroyRendering:function () {
212
	dojo.unimplemented("dojo.widget.Widget.destroyRendering");
213
	return false;
214
}, addedTo:function (parent) {
215
}, addChild:function (child) {
216
	dojo.unimplemented("dojo.widget.Widget.addChild");
217
	return false;
218
}, removeChild:function (widget) {
219
	for (var x = 0; x < this.children.length; x++) {
220
		if (this.children[x] === widget) {
221
			this.children.splice(x, 1);
222
			widget.parent = null;
223
			break;
224
		}
225
	}
226
	return widget;
227
}, getPreviousSibling:function () {
228
	var idx = this.getParentIndex();
229
	if (idx <= 0) {
230
		return null;
231
	}
232
	return this.parent.children[idx - 1];
233
}, getSiblings:function () {
234
	return this.parent.children;
235
}, getParentIndex:function () {
236
	return dojo.lang.indexOf(this.parent.children, this, true);
237
}, getNextSibling:function () {
238
	var idx = this.getParentIndex();
239
	if (idx == this.parent.children.length - 1) {
240
		return null;
241
	}
242
	if (idx < 0) {
243
		return null;
244
	}
245
	return this.parent.children[idx + 1];
246
}});
247
dojo.widget.lcArgsCache = {};
248
dojo.widget.tags = {};
249
dojo.widget.tags.addParseTreeHandler = function (type) {
250
	dojo.deprecated("addParseTreeHandler", ". ParseTreeHandlers are now reserved for components. Any unfiltered DojoML tag without a ParseTreeHandler is assumed to be a widget", "0.5");
251
};
252
dojo.widget.tags["dojo:propertyset"] = function (fragment, widgetParser, parentComp) {
253
	var properties = widgetParser.parseProperties(fragment["dojo:propertyset"]);
254
};
255
dojo.widget.tags["dojo:connect"] = function (fragment, widgetParser, parentComp) {
256
	var properties = widgetParser.parseProperties(fragment["dojo:connect"]);
257
};
258
dojo.widget.buildWidgetFromParseTree = function (type, frag, parser, parentComp, insertionIndex, localProps) {
259
	dojo.a11y.setAccessibleMode();
260
	var stype = type.split(":");
261
	stype = (stype.length == 2) ? stype[1] : type;
262
	var localProperties = localProps || parser.parseProperties(frag[frag["ns"] + ":" + stype]);
263
	var twidget = dojo.widget.manager.getImplementation(stype, null, null, frag["ns"]);
264
	if (!twidget) {
265
		throw new Error("cannot find \"" + type + "\" widget");
266
	} else {
267
		if (!twidget.create) {
268
			throw new Error("\"" + type + "\" widget object has no \"create\" method and does not appear to implement *Widget");
269
		}
270
	}
271
	localProperties["dojoinsertionindex"] = insertionIndex;
272
	var ret = twidget.create(localProperties, frag, parentComp, frag["ns"]);
273
	return ret;
274
};
275
dojo.widget.defineWidget = function (widgetClass, renderer, superclasses, init, props) {
276
	if (dojo.lang.isString(arguments[3])) {
277
		dojo.widget._defineWidget(arguments[0], arguments[3], arguments[1], arguments[4], arguments[2]);
278
	} else {
279
		var args = [arguments[0]], p = 3;
280
		if (dojo.lang.isString(arguments[1])) {
281
			args.push(arguments[1], arguments[2]);
282
		} else {
283
			args.push("", arguments[1]);
284
			p = 2;
285
		}
286
		if (dojo.lang.isFunction(arguments[p])) {
287
			args.push(arguments[p], arguments[p + 1]);
288
		} else {
289
			args.push(null, arguments[p]);
290
		}
291
		dojo.widget._defineWidget.apply(this, args);
292
	}
293
};
294
dojo.widget.defineWidget.renderers = "html|svg|vml";
295
dojo.widget._defineWidget = function (widgetClass, renderer, superclasses, init, props) {
296
	var module = widgetClass.split(".");
297
	var type = module.pop();
298
	var regx = "\\.(" + (renderer ? renderer + "|" : "") + dojo.widget.defineWidget.renderers + ")\\.";
299
	var r = widgetClass.search(new RegExp(regx));
300
	module = (r < 0 ? module.join(".") : widgetClass.substr(0, r));
301
	dojo.widget.manager.registerWidgetPackage(module);
302
	var pos = module.indexOf(".");
303
	var nsName = (pos > -1) ? module.substring(0, pos) : module;
304
	props = (props) || {};
305
	props.widgetType = type;
306
	if ((!init) && (props["classConstructor"])) {
307
		init = props.classConstructor;
308
		delete props.classConstructor;
309
	}
310
	dojo.declare(widgetClass, superclasses, init, props);
311
};
312