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