]>
Commit | Line | Data |
---|---|---|
1354d172 AD |
1 | define("dijit/_Widget", [ |
2 | "dojo/aspect", // aspect.around | |
3 | "dojo/_base/config", // config.isDebug | |
4 | "dojo/_base/connect", // connect.connect | |
5 | "dojo/_base/declare", // declare | |
6 | "dojo/_base/kernel", // kernel.deprecated | |
7 | "dojo/_base/lang", // lang.hitch | |
8 | "dojo/query", | |
9 | "dojo/ready", | |
10 | "./registry", // registry.byNode | |
11 | "./_WidgetBase", | |
12 | "./_OnDijitClickMixin", | |
13 | "./_FocusMixin", | |
14 | "dojo/uacss", // browser sniffing (included for back-compat; subclasses may be using) | |
15 | "./hccss" // high contrast mode sniffing (included to set CSS classes on <body>, module ret value unused) | |
16 | ], function(aspect, config, connect, declare, kernel, lang, query, ready, | |
17 | registry, _WidgetBase, _OnDijitClickMixin, _FocusMixin){ | |
18 | ||
19 | /*===== | |
20 | var _WidgetBase = dijit._WidgetBase; | |
21 | var _OnDijitClickMixin = dijit._OnDijitClickMixin; | |
22 | var _FocusMixin = dijit._FocusMixin; | |
23 | =====*/ | |
24 | ||
25 | ||
26 | // module: | |
27 | // dijit/_Widget | |
28 | // summary: | |
29 | // Old base for widgets. New widgets should extend _WidgetBase instead | |
30 | ||
31 | ||
32 | function connectToDomNode(){ | |
33 | // summary: | |
34 | // If user connects to a widget method === this function, then they will | |
35 | // instead actually be connecting the equivalent event on this.domNode | |
36 | } | |
37 | ||
38 | // Trap dojo.connect() calls to connectToDomNode methods, and redirect to _Widget.on() | |
39 | function aroundAdvice(originalConnect){ | |
40 | return function(obj, event, scope, method){ | |
41 | if(obj && typeof event == "string" && obj[event] == connectToDomNode){ | |
42 | return obj.on(event.substring(2).toLowerCase(), lang.hitch(scope, method)); | |
43 | } | |
44 | return originalConnect.apply(connect, arguments); | |
45 | }; | |
46 | } | |
47 | aspect.around(connect, "connect", aroundAdvice); | |
48 | if(kernel.connect){ | |
49 | aspect.around(kernel, "connect", aroundAdvice); | |
50 | } | |
51 | ||
52 | var _Widget = declare("dijit._Widget", [_WidgetBase, _OnDijitClickMixin, _FocusMixin], { | |
53 | // summary: | |
54 | // Base class for all Dijit widgets. | |
55 | // | |
56 | // Extends _WidgetBase, adding support for: | |
57 | // - declaratively/programatically specifying widget initialization parameters like | |
58 | // onMouseMove="foo" that call foo when this.domNode gets a mousemove event | |
59 | // - ondijitclick | |
60 | // Support new data-dojo-attach-event="ondijitclick: ..." that is triggered by a mouse click or a SPACE/ENTER keypress | |
61 | // - focus related functions | |
62 | // In particular, the onFocus()/onBlur() callbacks. Driven internally by | |
63 | // dijit/_base/focus.js. | |
64 | // - deprecated methods | |
65 | // - onShow(), onHide(), onClose() | |
66 | // | |
67 | // Also, by loading code in dijit/_base, turns on: | |
68 | // - browser sniffing (putting browser id like .dj_ie on <html> node) | |
69 | // - high contrast mode sniffing (add .dijit_a11y class to <body> if machine is in high contrast mode) | |
70 | ||
71 | ||
72 | ////////////////// DEFERRED CONNECTS /////////////////// | |
73 | ||
74 | onClick: connectToDomNode, | |
75 | /*===== | |
76 | onClick: function(event){ | |
77 | // summary: | |
78 | // Connect to this function to receive notifications of mouse click events. | |
79 | // event: | |
80 | // mouse Event | |
81 | // tags: | |
82 | // callback | |
83 | }, | |
84 | =====*/ | |
85 | onDblClick: connectToDomNode, | |
86 | /*===== | |
87 | onDblClick: function(event){ | |
88 | // summary: | |
89 | // Connect to this function to receive notifications of mouse double click events. | |
90 | // event: | |
91 | // mouse Event | |
92 | // tags: | |
93 | // callback | |
94 | }, | |
95 | =====*/ | |
96 | onKeyDown: connectToDomNode, | |
97 | /*===== | |
98 | onKeyDown: function(event){ | |
99 | // summary: | |
100 | // Connect to this function to receive notifications of keys being pressed down. | |
101 | // event: | |
102 | // key Event | |
103 | // tags: | |
104 | // callback | |
105 | }, | |
106 | =====*/ | |
107 | onKeyPress: connectToDomNode, | |
108 | /*===== | |
109 | onKeyPress: function(event){ | |
110 | // summary: | |
111 | // Connect to this function to receive notifications of printable keys being typed. | |
112 | // event: | |
113 | // key Event | |
114 | // tags: | |
115 | // callback | |
116 | }, | |
117 | =====*/ | |
118 | onKeyUp: connectToDomNode, | |
119 | /*===== | |
120 | onKeyUp: function(event){ | |
121 | // summary: | |
122 | // Connect to this function to receive notifications of keys being released. | |
123 | // event: | |
124 | // key Event | |
125 | // tags: | |
126 | // callback | |
127 | }, | |
128 | =====*/ | |
129 | onMouseDown: connectToDomNode, | |
130 | /*===== | |
131 | onMouseDown: function(event){ | |
132 | // summary: | |
133 | // Connect to this function to receive notifications of when the mouse button is pressed down. | |
134 | // event: | |
135 | // mouse Event | |
136 | // tags: | |
137 | // callback | |
138 | }, | |
139 | =====*/ | |
140 | onMouseMove: connectToDomNode, | |
141 | /*===== | |
142 | onMouseMove: function(event){ | |
143 | // summary: | |
144 | // Connect to this function to receive notifications of when the mouse moves over nodes contained within this widget. | |
145 | // event: | |
146 | // mouse Event | |
147 | // tags: | |
148 | // callback | |
149 | }, | |
150 | =====*/ | |
151 | onMouseOut: connectToDomNode, | |
152 | /*===== | |
153 | onMouseOut: function(event){ | |
154 | // summary: | |
155 | // Connect to this function to receive notifications of when the mouse moves off of nodes contained within this widget. | |
156 | // event: | |
157 | // mouse Event | |
158 | // tags: | |
159 | // callback | |
160 | }, | |
161 | =====*/ | |
162 | onMouseOver: connectToDomNode, | |
163 | /*===== | |
164 | onMouseOver: function(event){ | |
165 | // summary: | |
166 | // Connect to this function to receive notifications of when the mouse moves onto nodes contained within this widget. | |
167 | // event: | |
168 | // mouse Event | |
169 | // tags: | |
170 | // callback | |
171 | }, | |
172 | =====*/ | |
173 | onMouseLeave: connectToDomNode, | |
174 | /*===== | |
175 | onMouseLeave: function(event){ | |
176 | // summary: | |
177 | // Connect to this function to receive notifications of when the mouse moves off of this widget. | |
178 | // event: | |
179 | // mouse Event | |
180 | // tags: | |
181 | // callback | |
182 | }, | |
183 | =====*/ | |
184 | onMouseEnter: connectToDomNode, | |
185 | /*===== | |
186 | onMouseEnter: function(event){ | |
187 | // summary: | |
188 | // Connect to this function to receive notifications of when the mouse moves onto this widget. | |
189 | // event: | |
190 | // mouse Event | |
191 | // tags: | |
192 | // callback | |
193 | }, | |
194 | =====*/ | |
195 | onMouseUp: connectToDomNode, | |
196 | /*===== | |
197 | onMouseUp: function(event){ | |
198 | // summary: | |
199 | // Connect to this function to receive notifications of when the mouse button is released. | |
200 | // event: | |
201 | // mouse Event | |
202 | // tags: | |
203 | // callback | |
204 | }, | |
205 | =====*/ | |
206 | ||
207 | constructor: function(params){ | |
208 | // extract parameters like onMouseMove that should connect directly to this.domNode | |
209 | this._toConnect = {}; | |
210 | for(var name in params){ | |
211 | if(this[name] === connectToDomNode){ | |
212 | this._toConnect[name.replace(/^on/, "").toLowerCase()] = params[name]; | |
213 | delete params[name]; | |
214 | } | |
215 | } | |
216 | }, | |
217 | ||
218 | postCreate: function(){ | |
219 | this.inherited(arguments); | |
220 | ||
221 | // perform connection from this.domNode to user specified handlers (ex: onMouseMove) | |
222 | for(var name in this._toConnect){ | |
223 | this.on(name, this._toConnect[name]); | |
224 | } | |
225 | delete this._toConnect; | |
226 | }, | |
227 | ||
228 | on: function(/*String*/ type, /*Function*/ func){ | |
229 | if(this[this._onMap(type)] === connectToDomNode){ | |
230 | // Use connect.connect() rather than on() to get handling for "onmouseenter" on non-IE, etc. | |
231 | // Also, need to specify context as "this" rather than the default context of the DOMNode | |
232 | return connect.connect(this.domNode, type.toLowerCase(), this, func); | |
233 | } | |
234 | return this.inherited(arguments); | |
235 | }, | |
236 | ||
237 | _setFocusedAttr: function(val){ | |
238 | // Remove this method in 2.0 (or sooner), just here to set _focused == focused, for back compat | |
239 | // (but since it's a private variable we aren't required to keep supporting it). | |
240 | this._focused = val; | |
241 | this._set("focused", val); | |
242 | }, | |
243 | ||
244 | ////////////////// DEPRECATED METHODS /////////////////// | |
245 | ||
246 | setAttribute: function(/*String*/ attr, /*anything*/ value){ | |
247 | // summary: | |
248 | // Deprecated. Use set() instead. | |
249 | // tags: | |
250 | // deprecated | |
251 | kernel.deprecated(this.declaredClass+"::setAttribute(attr, value) is deprecated. Use set() instead.", "", "2.0"); | |
252 | this.set(attr, value); | |
253 | }, | |
254 | ||
255 | attr: function(/*String|Object*/name, /*Object?*/value){ | |
256 | // summary: | |
257 | // Set or get properties on a widget instance. | |
258 | // name: | |
259 | // The property to get or set. If an object is passed here and not | |
260 | // a string, its keys are used as names of attributes to be set | |
261 | // and the value of the object as values to set in the widget. | |
262 | // value: | |
263 | // Optional. If provided, attr() operates as a setter. If omitted, | |
264 | // the current value of the named property is returned. | |
265 | // description: | |
266 | // This method is deprecated, use get() or set() directly. | |
267 | ||
268 | // Print deprecation warning but only once per calling function | |
269 | if(config.isDebug){ | |
270 | var alreadyCalledHash = arguments.callee._ach || (arguments.callee._ach = {}), | |
271 | caller = (arguments.callee.caller || "unknown caller").toString(); | |
272 | if(!alreadyCalledHash[caller]){ | |
273 | kernel.deprecated(this.declaredClass + "::attr() is deprecated. Use get() or set() instead, called from " + | |
274 | caller, "", "2.0"); | |
275 | alreadyCalledHash[caller] = true; | |
276 | } | |
277 | } | |
278 | ||
279 | var args = arguments.length; | |
280 | if(args >= 2 || typeof name === "object"){ // setter | |
281 | return this.set.apply(this, arguments); | |
282 | }else{ // getter | |
283 | return this.get(name); | |
284 | } | |
285 | }, | |
286 | ||
287 | getDescendants: function(){ | |
288 | // summary: | |
289 | // Returns all the widgets contained by this, i.e., all widgets underneath this.containerNode. | |
290 | // This method should generally be avoided as it returns widgets declared in templates, which are | |
291 | // supposed to be internal/hidden, but it's left here for back-compat reasons. | |
292 | ||
293 | kernel.deprecated(this.declaredClass+"::getDescendants() is deprecated. Use getChildren() instead.", "", "2.0"); | |
294 | return this.containerNode ? query('[widgetId]', this.containerNode).map(registry.byNode) : []; // dijit._Widget[] | |
295 | }, | |
296 | ||
297 | ////////////////// MISCELLANEOUS METHODS /////////////////// | |
298 | ||
299 | _onShow: function(){ | |
300 | // summary: | |
301 | // Internal method called when this widget is made visible. | |
302 | // See `onShow` for details. | |
303 | this.onShow(); | |
304 | }, | |
305 | ||
306 | onShow: function(){ | |
307 | // summary: | |
308 | // Called when this widget becomes the selected pane in a | |
309 | // `dijit.layout.TabContainer`, `dijit.layout.StackContainer`, | |
310 | // `dijit.layout.AccordionContainer`, etc. | |
311 | // | |
312 | // Also called to indicate display of a `dijit.Dialog`, `dijit.TooltipDialog`, or `dijit.TitlePane`. | |
313 | // tags: | |
314 | // callback | |
315 | }, | |
316 | ||
317 | onHide: function(){ | |
318 | // summary: | |
319 | // Called when another widget becomes the selected pane in a | |
320 | // `dijit.layout.TabContainer`, `dijit.layout.StackContainer`, | |
321 | // `dijit.layout.AccordionContainer`, etc. | |
322 | // | |
323 | // Also called to indicate hide of a `dijit.Dialog`, `dijit.TooltipDialog`, or `dijit.TitlePane`. | |
324 | // tags: | |
325 | // callback | |
326 | }, | |
327 | ||
328 | onClose: function(){ | |
329 | // summary: | |
330 | // Called when this widget is being displayed as a popup (ex: a Calendar popped | |
331 | // up from a DateTextBox), and it is hidden. | |
332 | // This is called from the dijit.popup code, and should not be called directly. | |
333 | // | |
334 | // Also used as a parameter for children of `dijit.layout.StackContainer` or subclasses. | |
335 | // Callback if a user tries to close the child. Child will be closed if this function returns true. | |
336 | // tags: | |
337 | // extension | |
338 | ||
339 | return true; // Boolean | |
340 | } | |
341 | }); | |
342 | ||
343 | // For back-compat, remove in 2.0. | |
344 | if(!kernel.isAsync){ | |
345 | ready(0, function(){ | |
346 | var requires = ["dijit/_base"]; | |
347 | require(requires); // use indirection so modules not rolled into a build | |
348 | }); | |
349 | } | |
350 | return _Widget; | |
351 | }); |