Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dijit._Widget"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dijit._Widget"] = true;
3
dojo.provide("dijit._Widget");
4
 
5
dojo.require("dijit._base");
6
 
7
dojo.declare("dijit._Widget", null, {
8
	// summary:
9
	//		The foundation of dijit widgets.
10
	//
11
	// id: String
12
	//		a unique, opaque ID string that can be assigned by users or by the
13
	//		system. If the developer passes an ID which is known not to be
14
	//		unique, the specified ID is ignored and the system-generated ID is
15
	//		used instead.
16
	id: "",
17
 
18
	// lang: String
19
	//	Language to display this widget in (like en-us).
20
	//	Defaults to brower's specified preferred language (typically the language of the OS)
21
	lang: "",
22
 
23
	// dir: String
24
	//  Bi-directional support, as defined by the HTML DIR attribute. Either left-to-right "ltr" or right-to-left "rtl".
25
	dir: "",
26
 
27
	// class: String
28
	// HTML class attribute
29
	"class": "",
30
 
31
	// style: String
32
	// HTML style attribute
33
	style: "",
34
 
35
	// title: String
36
	// HTML title attribute
37
	title: "",
38
 
39
	// srcNodeRef: DomNode
40
	//		pointer to original dom node
41
	srcNodeRef: null,
42
 
43
	// domNode: DomNode
44
	//		this is our visible representation of the widget! Other DOM
45
	//		Nodes may by assigned to other properties, usually through the
46
	//		template system's dojoAttachPonit syntax, but the domNode
47
	//		property is the canonical "top level" node in widget UI.
48
	domNode: null,
49
 
50
	// attributeMap: Object
51
	//		A map of attributes and attachpoints -- typically standard HTML attributes -- to set
52
	//		on the widget's dom, at the "domNode" attach point, by default.
53
	//		Other node references can be specified as properties of 'this'
54
	attributeMap: {id:"", dir:"", lang:"", "class":"", style:"", title:""},  // TODO: add on* handlers?
55
 
56
	//////////// INITIALIZATION METHODS ///////////////////////////////////////
57
 
58
	postscript: function(params, srcNodeRef){
59
		this.create(params, srcNodeRef);
60
	},
61
 
62
	create: function(params, srcNodeRef){
63
		// summary:
64
		//		To understand the process by which widgets are instantiated, it
65
		//		is critical to understand what other methods create calls and
66
		//		which of them you'll want to override. Of course, adventurous
67
		//		developers could override create entirely, but this should
68
		//		only be done as a last resort.
69
		//
70
		//		Below is a list of the methods that are called, in the order
71
		//		they are fired, along with notes about what they do and if/when
72
		//		you should over-ride them in your widget:
73
		//
74
		//			postMixInProperties:
75
		//				a stub function that you can over-ride to modify
76
		//				variables that may have been naively assigned by
77
		//				mixInProperties
78
		//			# widget is added to manager object here
79
		//			buildRendering
80
		//				Subclasses use this method to handle all UI initialization
81
		//				Sets this.domNode.  Templated widgets do this automatically
82
		//				and otherwise it just uses the source dom node.
83
		//			postCreate
84
		//				a stub function that you can over-ride to modify take
85
		//				actions once the widget has been placed in the UI
86
 
87
		// store pointer to original dom tree
88
		this.srcNodeRef = dojo.byId(srcNodeRef);
89
 
90
		// For garbage collection.  An array of handles returned by Widget.connect()
91
		// Each handle returned from Widget.connect() is an array of handles from dojo.connect()
92
		this._connects=[];
93
 
94
		// _attaches: String[]
95
		// 		names of all our dojoAttachPoint variables
96
		this._attaches=[];
97
 
98
		//mixin our passed parameters
99
		if(this.srcNodeRef && (typeof this.srcNodeRef.id == "string")){ this.id = this.srcNodeRef.id; }
100
		if(params){
101
			dojo.mixin(this,params);
102
		}
103
		this.postMixInProperties();
104
 
105
		// generate an id for the widget if one wasn't specified
106
		// (be sure to do this before buildRendering() because that function might
107
		// expect the id to be there.
108
		if(!this.id){
109
			this.id=dijit.getUniqueId(this.declaredClass.replace(/\./g,"_"));
110
		}
111
		dijit.registry.add(this);
112
 
113
		this.buildRendering();
114
 
115
		// Copy attributes listed in attributeMap into the [newly created] DOM for the widget.
116
		// The placement of these attributes is according to the property mapping in attributeMap.
117
		// Note special handling for 'style' and 'class' attributes which are lists and can
118
		// have elements from both old and new structures, and some attributes like "type"
119
		// cannot be processed this way as they are not mutable.
120
		if(this.domNode){
121
			for(var attr in this.attributeMap){
122
				var mapNode = this[this.attributeMap[attr] || "domNode"];
123
				var value = this[attr];
124
				if(typeof value != "object" && (value !== "" || (params && params[attr]))){
125
					switch(attr){
126
					case "class":
127
						dojo.addClass(mapNode, value);
128
						break;
129
					case "style":
130
						if(mapNode.style.cssText){
131
							mapNode.style.cssText += "; " + value;// FIXME: Opera
132
						}else{
133
							mapNode.style.cssText = value;
134
						}
135
						break;
136
					default:
137
						mapNode.setAttribute(attr, value);
138
					}
139
				}
140
			}
141
		}
142
 
143
		if(this.domNode){
144
			this.domNode.setAttribute("widgetId", this.id);
145
		}
146
		this.postCreate();
147
 
148
		// If srcNodeRef has been processed and removed from the DOM (e.g. TemplatedWidget) then delete it to allow GC.
149
		if(this.srcNodeRef && !this.srcNodeRef.parentNode){
150
			delete this.srcNodeRef;
151
		}
152
	},
153
 
154
	postMixInProperties: function(){
155
		// summary
156
		//	Called after the parameters to the widget have been read-in,
157
		//	but before the widget template is instantiated.
158
		//	Especially useful to set properties that are referenced in the widget template.
159
	},
160
 
161
	buildRendering: function(){
162
		// summary:
163
		//		Construct the UI for this widget, setting this.domNode.
164
		//		Most widgets will mixin TemplatedWidget, which overrides this method.
165
		this.domNode = this.srcNodeRef || dojo.doc.createElement('div');
166
	},
167
 
168
	postCreate: function(){
169
		// summary:
170
		//		Called after a widget's dom has been setup
171
	},
172
 
173
	startup: function(){
174
		// summary:
175
		//		Called after a widget's children, and other widgets on the page, have been created.
176
		//		Provides an opportunity to manipulate any children before they are displayed
177
		//		This is useful for composite widgets that need to control or layout sub-widgets
178
		//		Many layout widgets can use this as a wiring phase
179
	},
180
 
181
	//////////// DESTROY FUNCTIONS ////////////////////////////////
182
 
183
	destroyRecursive: function(/*Boolean*/ finalize){
184
		// summary:
185
		// 		Destroy this widget and it's descendants. This is the generic
186
		// 		"destructor" function that all widget users should call to
187
		// 		cleanly discard with a widget. Once a widget is destroyed, it's
188
		// 		removed from the manager object.
189
		// finalize: Boolean
190
		//		is this function being called part of global environment
191
		//		tear-down?
192
 
193
		this.destroyDescendants();
194
		this.destroy();
195
	},
196
 
197
	destroy: function(/*Boolean*/ finalize){
198
		// summary:
199
		// 		Destroy this widget, but not its descendants
200
		// finalize: Boolean
201
		//		is this function being called part of global environment
202
		//		tear-down?
203
		this.uninitialize();
204
		dojo.forEach(this._connects, function(array){
205
			dojo.forEach(array, dojo.disconnect);
206
		});
207
		this.destroyRendering(finalize);
208
		dijit.registry.remove(this.id);
209
	},
210
 
211
	destroyRendering: function(/*Boolean*/ finalize){
212
		// summary:
213
		//		Destroys the DOM nodes associated with this widget
214
		// finalize: Boolean
215
		//		is this function being called part of global environment
216
		//		tear-down?
217
 
218
		if(this.bgIframe){
219
			this.bgIframe.destroy();
220
			delete this.bgIframe;
221
		}
222
 
223
		if(this.domNode){
224
			dojo._destroyElement(this.domNode);
225
			delete this.domNode;
226
		}
227
 
228
		if(this.srcNodeRef){
229
			dojo._destroyElement(this.srcNodeRef);
230
			delete this.srcNodeRef;
231
		}
232
	},
233
 
234
	destroyDescendants: function(){
235
		// summary:
236
		//		Recursively destroy the children of this widget and their
237
		//		descendants.
238
 
239
		// TODO: should I destroy in the reverse order, to go bottom up?
240
		dojo.forEach(this.getDescendants(), function(widget){ widget.destroy(); });
241
	},
242
 
243
	uninitialize: function(){
244
		// summary:
245
		//		stub function. Over-ride to implement custom widget tear-down
246
		//		behavior.
247
		return false;
248
	},
249
 
250
	////////////////// MISCELLANEOUS METHODS ///////////////////
251
 
252
	toString: function(){
253
		// summary:
254
		//		returns a string that represents the widget. When a widget is
255
		//		cast to a string, this method will be used to generate the
256
		//		output. Currently, it does not implement any sort of reversable
257
		//		serialization.
258
		return '[Widget ' + this.declaredClass + ', ' + (this.id || 'NO ID') + ']'; // String
259
	},
260
 
261
	getDescendants: function(){
262
		// summary:
263
		//	return all the descendant widgets
264
		var list = dojo.query('[widgetId]', this.domNode);
265
		return list.map(dijit.byNode);		// Array
266
	},
267
 
268
	nodesWithKeyClick : ["input", "button"],
269
 
270
	connect: function(
271
			/*Object|null*/ obj,
272
			/*String*/ event,
273
			/*String|Function*/ method){
274
 
275
		// summary:
276
		//		Connects specified obj/event to specified method of this object
277
		//		and registers for disconnect() on widget destroy.
278
		//		Special event: "ondijitclick" triggers on a click or enter-down or space-up
279
		//		Similar to dojo.connect() but takes three arguments rather than four.
280
		var handles =[];
281
		if(event == "ondijitclick"){
282
			var w = this;
283
			// add key based click activation for unsupported nodes.
284
			if(!this.nodesWithKeyClick[obj.nodeName]){
285
				handles.push(dojo.connect(obj, "onkeydown", this,
286
					function(e){
287
						if(e.keyCode == dojo.keys.ENTER){
288
							return (dojo.isString(method))?
289
								w[method](e) : method.call(w, e);
290
						}else if(e.keyCode == dojo.keys.SPACE){
291
							// stop space down as it causes IE to scroll
292
							// the browser window
293
							dojo.stopEvent(e);
294
						}
295
			 		}));
296
				handles.push(dojo.connect(obj, "onkeyup", this,
297
					function(e){
298
						if(e.keyCode == dojo.keys.SPACE){
299
							return dojo.isString(method) ?
300
								w[method](e) : method.call(w, e);
301
						}
302
			 		}));
303
			}
304
			event = "onclick";
305
		}
306
		handles.push(dojo.connect(obj, event, this, method));
307
 
308
		// return handles for FormElement and ComboBox
309
		this._connects.push(handles);
310
		return handles;
311
	},
312
 
313
	disconnect: function(/*Object*/ handles){
314
		// summary:
315
		//		Disconnects handle created by this.connect.
316
		//		Also removes handle from this widget's list of connects
317
		for(var i=0; i<this._connects.length; i++){
318
			if(this._connects[i]==handles){
319
				dojo.forEach(handles, dojo.disconnect);
320
				this._connects.splice(i, 1);
321
				return;
322
			}
323
		}
324
	},
325
 
326
	isLeftToRight: function(){
327
		// summary:
328
		//		Checks the DOM to for the text direction for bi-directional support
329
		// description:
330
		//		This method cannot be used during widget construction because the widget
331
		//		must first be connected to the DOM tree.  Parent nodes are searched for the
332
		//		'dir' attribute until one is found, otherwise left to right mode is assumed.
333
		//		See HTML spec, DIR attribute for more information.
334
 
335
		if(typeof this._ltr == "undefined"){
336
			this._ltr = dojo.getComputedStyle(this.domNode).direction != "rtl";
337
		}
338
		return this._ltr; //Boolean
339
	},
340
 
341
	isFocusable: function(){
342
		// summary:
343
		//		Return true if this widget can currently be focused
344
		//		and false if not
345
		return this.focus && (dojo.style(this.domNode, "display") != "none");
346
	}
347
});
348
 
349
}