|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>Extending the base widget class</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<!--begin custom header content for this example--> |
|
|
25 |
<style type="text/css"> |
|
|
26 |
|
|
|
27 |
.yui-spinner-hidden { |
|
|
28 |
display:none; |
|
|
29 |
} |
|
|
30 |
|
|
|
31 |
.yui-spinner { |
|
|
32 |
display:-moz-inline-stack; |
|
|
33 |
display:inline-block; |
|
|
34 |
zoom:1; |
|
|
35 |
*display:inline; |
|
|
36 |
vertical-align:middle; |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
.yui-spinner-content { |
|
|
40 |
padding:1px; |
|
|
41 |
position:relative; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
.yui-spinner-value { |
|
|
45 |
width:2em; |
|
|
46 |
height:1.5em; |
|
|
47 |
text-align:right; |
|
|
48 |
margin-right:22px; |
|
|
49 |
vertical-align:top; |
|
|
50 |
border:1px solid #000; |
|
|
51 |
padding:2px; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
.yui-spinner-increment, .yui-spinner-decrement { |
|
|
55 |
position:absolute; |
|
|
56 |
height:1em; |
|
|
57 |
width:22px; |
|
|
58 |
overflow:hidden; |
|
|
59 |
text-indent:-10em; |
|
|
60 |
border:1px solid #999; |
|
|
61 |
margin:0; |
|
|
62 |
padding:0px; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
.yui-spinner-increment { |
|
|
66 |
top:1px; |
|
|
67 |
*top:2px; |
|
|
68 |
right:1px; |
|
|
69 |
background:#ddd url(assets/arrows.png) no-repeat 50% 0px; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
.yui-spinner-decrement { |
|
|
73 |
bottom:1px; |
|
|
74 |
*bottom:2px; |
|
|
75 |
right:1px; |
|
|
76 |
background:#ddd url(assets/arrows.png) no-repeat 50% -20px; |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
#widget-extend-example { |
|
|
80 |
padding:5px; |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
#widget-extend-example .hint { |
|
|
84 |
margin-top:10px; |
|
|
85 |
font-size:85%; |
|
|
86 |
color:#00a; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
</style> |
|
|
90 |
<!--end custom header content for this example--> |
|
|
91 |
|
|
|
92 |
</head> |
|
|
93 |
|
|
|
94 |
<body class=" yui-skin-sam"> |
|
|
95 |
|
|
|
96 |
<h1>Extending the base widget class</h1> |
|
|
97 |
|
|
|
98 |
<div class="exampleIntro"> |
|
|
99 |
This example shows how to extend the base <code>Widget</code> class to create a simple, re-usable spinner control. The <code>Spinner</code> class created in the example is not intended to be a fully featured spinner. It is used here as a concrete example, to convey some of the key concepts to keep in mind when extending the <code>Widget</code> class. |
|
|
100 |
|
|
|
101 |
</div> |
|
|
102 |
|
|
|
103 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
104 |
|
|
|
105 |
<div id="widget-extend-example"> |
|
|
106 |
A basic spinner widget: <div id="numberField"><input type="text" class="yui-spinner-value" value="20" /></div> |
|
|
107 |
<p class="hint">Click the buttons, or the arrow up/down and page up/down keys on your keyboard to change the spinner's value</p> |
|
|
108 |
</div> |
|
|
109 |
|
|
|
110 |
<script type="text/javascript"> |
|
|
111 |
YUI({base:"../../build/", timeout: 10000}).use("event-key", "widget", function(Y) { |
|
|
112 |
|
|
|
113 |
var Lang = Y.Lang, |
|
|
114 |
Widget = Y.Widget, |
|
|
115 |
Node = Y.Node; |
|
|
116 |
|
|
|
117 |
/* Spinner class constructor */ |
|
|
118 |
function Spinner(config) { |
|
|
119 |
Spinner.superclass.constructor.apply(this, arguments); |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
/* |
|
|
123 |
* Required NAME static field, to identify the Widget class and |
|
|
124 |
* used as an event prefix, to generate class names etc. (set to the |
|
|
125 |
* class name in camel case). |
|
|
126 |
*/ |
|
|
127 |
Spinner.NAME = "spinner"; |
|
|
128 |
|
|
|
129 |
/* |
|
|
130 |
* The attribute configuration for the Spinner widget. Attributes can be |
|
|
131 |
* defined with default values, get/set functions and validator functions |
|
|
132 |
* as with any other class extending Base. |
|
|
133 |
*/ |
|
|
134 |
Spinner.ATTRS = { |
|
|
135 |
// The minimum value for the spinner. |
|
|
136 |
min : { |
|
|
137 |
value:0 |
|
|
138 |
}, |
|
|
139 |
|
|
|
140 |
// The maximum value for the spinner. |
|
|
141 |
max : { |
|
|
142 |
value:100 |
|
|
143 |
}, |
|
|
144 |
|
|
|
145 |
// The current value of the spinner. |
|
|
146 |
value : { |
|
|
147 |
value:0, |
|
|
148 |
validator: function(val) { |
|
|
149 |
return this._validateValue(val); |
|
|
150 |
} |
|
|
151 |
}, |
|
|
152 |
|
|
|
153 |
// Amount to increment/decrement the spinner when the buttons or arrow up/down keys are pressed. |
|
|
154 |
minorStep : { |
|
|
155 |
value:1 |
|
|
156 |
}, |
|
|
157 |
|
|
|
158 |
// Amount to increment/decrement the spinner when the page up/down keys are pressed. |
|
|
159 |
majorStep : { |
|
|
160 |
value:10 |
|
|
161 |
}, |
|
|
162 |
|
|
|
163 |
// The localizable strings for the spinner. This attribute is |
|
|
164 |
// defined by the base Widget class but has an empty value. The |
|
|
165 |
// spinner is simply providing a default value for the attribute. |
|
|
166 |
strings: { |
|
|
167 |
value: { |
|
|
168 |
tooltip: "Press the arrow up/down keys for minor increments, page up/down for major increments.", |
|
|
169 |
increment: "Increment", |
|
|
170 |
decrement: "Decrement" |
|
|
171 |
} |
|
|
172 |
} |
|
|
173 |
}; |
|
|
174 |
|
|
|
175 |
/* Static constant used to identify the classname applied to the spinners value field */ |
|
|
176 |
Spinner.INPUT_CLASS = Y.ClassNameManager.getClassName(Spinner.NAME, "value"); |
|
|
177 |
|
|
|
178 |
/* Static constants used to define the markup templates used to create Spinner DOM elements */ |
|
|
179 |
Spinner.INPUT_TEMPLATE = '<input type="text" class="' + Spinner.INPUT_CLASS + '">'; |
|
|
180 |
Spinner.BTN_TEMPLATE = '<button type="button"></button>'; |
|
|
181 |
|
|
|
182 |
/* |
|
|
183 |
* The HTML_PARSER static constant is used by the Widget base class to populate |
|
|
184 |
* the configuration for the spinner instance from markup already on the page. |
|
|
185 |
* |
|
|
186 |
* The Spinner class attempts to set the value of the spinner widget if it |
|
|
187 |
* finds the appropriate input element on the page. |
|
|
188 |
*/ |
|
|
189 |
Spinner.HTML_PARSER = { |
|
|
190 |
value: function (contentBox) { |
|
|
191 |
var node = contentBox.one("." + Spinner.INPUT_CLASS); |
|
|
192 |
return (node) ? parseInt(node.get("value")) : null; |
|
|
193 |
} |
|
|
194 |
}; |
|
|
195 |
|
|
|
196 |
/* Spinner extends the base Widget class */ |
|
|
197 |
Y.extend(Spinner, Widget, { |
|
|
198 |
|
|
|
199 |
/* |
|
|
200 |
* initializer is part of the lifecycle introduced by |
|
|
201 |
* the Widget class. It is invoked during construction, |
|
|
202 |
* and can be used to setup instance specific state. |
|
|
203 |
* |
|
|
204 |
* The Spinner class does not need to perform anything |
|
|
205 |
* specific in this method, but it is left in as an example. |
|
|
206 |
*/ |
|
|
207 |
initializer: function() { |
|
|
208 |
// Not doing anything special during initialization |
|
|
209 |
}, |
|
|
210 |
|
|
|
211 |
/* |
|
|
212 |
* destructor is part of the lifecycle introduced by |
|
|
213 |
* the Widget class. It is invoked during destruction, |
|
|
214 |
* and can be used to cleanup instance specific state. |
|
|
215 |
* |
|
|
216 |
* The spinner cleans up any node references it's holding |
|
|
217 |
* onto. The Widget classes destructor will purge the |
|
|
218 |
* widget's bounding box of event listeners, so spinner |
|
|
219 |
* only needs to clean up listeners it attaches outside of |
|
|
220 |
* the bounding box. |
|
|
221 |
*/ |
|
|
222 |
destructor : function() { |
|
|
223 |
this._documentMouseUpHandle.detach(); |
|
|
224 |
|
|
|
225 |
this.inputNode = null; |
|
|
226 |
this.incrementNode = null; |
|
|
227 |
this.decrementNode = null; |
|
|
228 |
}, |
|
|
229 |
|
|
|
230 |
/* |
|
|
231 |
* renderUI is part of the lifecycle introduced by the |
|
|
232 |
* Widget class. Widget's renderer method invokes: |
|
|
233 |
* |
|
|
234 |
* renderUI() |
|
|
235 |
* bindUI() |
|
|
236 |
* syncUI() |
|
|
237 |
* |
|
|
238 |
* renderUI is intended to be used by the Widget subclass |
|
|
239 |
* to create or insert new elements into the DOM. |
|
|
240 |
* |
|
|
241 |
* For spinner the method adds the input (if it's not already |
|
|
242 |
* present in the markup), and creates the inc/dec buttons |
|
|
243 |
*/ |
|
|
244 |
renderUI : function() { |
|
|
245 |
this._renderInput(); |
|
|
246 |
this._renderButtons(); |
|
|
247 |
}, |
|
|
248 |
|
|
|
249 |
/* |
|
|
250 |
* bindUI is intended to be used by the Widget subclass |
|
|
251 |
* to bind any event listeners which will drive the Widget UI. |
|
|
252 |
* |
|
|
253 |
* It will generally bind event listeners for attribute change |
|
|
254 |
* events, to update the state of the rendered UI in response |
|
|
255 |
* to attribute value changes, and also attach any DOM events, |
|
|
256 |
* to activate the UI. |
|
|
257 |
* |
|
|
258 |
* For spinner, the method: |
|
|
259 |
* |
|
|
260 |
* - Sets up the attribute change listener for the "value" attribute |
|
|
261 |
* |
|
|
262 |
* - Binds key listeners for the arrow/page keys |
|
|
263 |
* - Binds mouseup/down listeners on the boundingBox, document respectively. |
|
|
264 |
* - Binds a simple change listener on the input box. |
|
|
265 |
*/ |
|
|
266 |
bindUI : function() { |
|
|
267 |
this.after("valueChange", this._afterValueChange); |
|
|
268 |
|
|
|
269 |
var boundingBox = this.get("boundingBox"); |
|
|
270 |
|
|
|
271 |
// Looking for a key event which will fire continously across browsers while the key is held down. 38, 40 = arrow up/down, 33, 34 = page up/down |
|
|
272 |
var keyEventSpec = (!Y.UA.opera) ? "down:" : "press:"; |
|
|
273 |
keyEventSpec += "38, 40, 33, 34"; |
|
|
274 |
|
|
|
275 |
Y.on("key", Y.bind(this._onDirectionKey, this), boundingBox, keyEventSpec); |
|
|
276 |
Y.on("mousedown", Y.bind(this._onMouseDown, this), boundingBox); |
|
|
277 |
this._documentMouseUpHandle = Y.on("mouseup", Y.bind(this._onDocMouseUp, this), boundingBox.get("ownerDocument")); |
|
|
278 |
|
|
|
279 |
Y.on("change", Y.bind(this._onInputChange, this), this.inputNode); |
|
|
280 |
}, |
|
|
281 |
|
|
|
282 |
/* |
|
|
283 |
* syncUI is intended to be used by the Widget subclass to |
|
|
284 |
* update the UI to reflect the current state of the widget. |
|
|
285 |
* |
|
|
286 |
* For spinner, the method sets the value of the input field, |
|
|
287 |
* to match the current state of the value attribute. |
|
|
288 |
*/ |
|
|
289 |
syncUI : function() { |
|
|
290 |
this._uiSetValue(this.get("value")); |
|
|
291 |
}, |
|
|
292 |
|
|
|
293 |
/* |
|
|
294 |
* Creates the input control for the spinner and adds it to |
|
|
295 |
* the widget's content box, if not already in the markup. |
|
|
296 |
*/ |
|
|
297 |
_renderInput : function() { |
|
|
298 |
var contentBox = this.get("contentBox"), |
|
|
299 |
input = contentBox.one("." + Spinner.INPUT_CLASS), |
|
|
300 |
strings = this.get("strings"); |
|
|
301 |
|
|
|
302 |
if (!input) { |
|
|
303 |
input = Node.create(Spinner.INPUT_TEMPLATE); |
|
|
304 |
contentBox.appendChild(input); |
|
|
305 |
} |
|
|
306 |
|
|
|
307 |
input.set("title", strings.tooltip); |
|
|
308 |
this.inputNode = input; |
|
|
309 |
}, |
|
|
310 |
|
|
|
311 |
/* |
|
|
312 |
* Creates the button controls for the spinner and add them to |
|
|
313 |
* the widget's content box, if not already in the markup. |
|
|
314 |
*/ |
|
|
315 |
_renderButtons : function() { |
|
|
316 |
var contentBox = this.get("contentBox"), |
|
|
317 |
strings = this.get("strings"); |
|
|
318 |
|
|
|
319 |
var inc = this._createButton(strings.increment, this.getClassName("increment")); |
|
|
320 |
var dec = this._createButton(strings.decrement, this.getClassName("decrement")); |
|
|
321 |
|
|
|
322 |
this.incrementNode = contentBox.appendChild(inc); |
|
|
323 |
this.decrementNode = contentBox.appendChild(dec); |
|
|
324 |
}, |
|
|
325 |
|
|
|
326 |
/* |
|
|
327 |
* Utility method, to create a spinner button |
|
|
328 |
*/ |
|
|
329 |
_createButton : function(text, className) { |
|
|
330 |
|
|
|
331 |
var btn = Y.Node.create(Spinner.BTN_TEMPLATE); |
|
|
332 |
btn.set("innerHTML", text); |
|
|
333 |
btn.set("title", text); |
|
|
334 |
btn.addClass(className); |
|
|
335 |
|
|
|
336 |
return btn; |
|
|
337 |
}, |
|
|
338 |
|
|
|
339 |
/* |
|
|
340 |
* Bounding box mouse down handler. Will determine if the mouse down |
|
|
341 |
* is on one of the spinner buttons, and increment/decrement the value |
|
|
342 |
* accordingly. |
|
|
343 |
* |
|
|
344 |
* The method also sets up a timer, to support the user holding the mouse |
|
|
345 |
* down on the spinner buttons. The timer is cleared when a mouse up event |
|
|
346 |
* is detected. |
|
|
347 |
*/ |
|
|
348 |
_onMouseDown : function(e) { |
|
|
349 |
var node = e.target, |
|
|
350 |
dir, |
|
|
351 |
handled = false, |
|
|
352 |
currVal = this.get("value"), |
|
|
353 |
minorStep = this.get("minorStep"); |
|
|
354 |
|
|
|
355 |
if (node.hasClass(this.getClassName("increment"))) { |
|
|
356 |
this.set("value", currVal + minorStep); |
|
|
357 |
dir = 1; |
|
|
358 |
handled = true; |
|
|
359 |
} else if (node.hasClass(this.getClassName("decrement"))) { |
|
|
360 |
this.set("value", currVal - minorStep); |
|
|
361 |
dir = -1; |
|
|
362 |
handled = true; |
|
|
363 |
} |
|
|
364 |
|
|
|
365 |
if (handled) { |
|
|
366 |
this._setMouseDownTimers(dir, minorStep); |
|
|
367 |
} |
|
|
368 |
}, |
|
|
369 |
|
|
|
370 |
/* |
|
|
371 |
* Document mouse up handler. Clears the timers supporting |
|
|
372 |
* the "mouse held down" behavior. |
|
|
373 |
*/ |
|
|
374 |
_onDocMouseUp : function(e) { |
|
|
375 |
this._clearMouseDownTimers(); |
|
|
376 |
}, |
|
|
377 |
|
|
|
378 |
/* |
|
|
379 |
* Bounding box Arrow up/down, Page up/down key listener. |
|
|
380 |
* |
|
|
381 |
* Increments/Decrement the spinner value, based on the key pressed. |
|
|
382 |
*/ |
|
|
383 |
_onDirectionKey : function(e) { |
|
|
384 |
|
|
|
385 |
e.preventDefault(); |
|
|
386 |
|
|
|
387 |
var currVal = this.get("value"), |
|
|
388 |
newVal = currVal, |
|
|
389 |
minorStep = this.get("minorStep"), |
|
|
390 |
majorStep = this.get("majorStep"); |
|
|
391 |
|
|
|
392 |
switch (e.charCode) { |
|
|
393 |
case 38: |
|
|
394 |
newVal += minorStep; |
|
|
395 |
break; |
|
|
396 |
case 40: |
|
|
397 |
newVal -= minorStep; |
|
|
398 |
break; |
|
|
399 |
case 33: |
|
|
400 |
newVal += majorStep; |
|
|
401 |
newVal = Math.min(newVal, this.get("max")); |
|
|
402 |
break; |
|
|
403 |
case 34: |
|
|
404 |
newVal -= majorStep; |
|
|
405 |
newVal = Math.max(newVal, this.get("min")); |
|
|
406 |
break; |
|
|
407 |
} |
|
|
408 |
|
|
|
409 |
if (newVal !== currVal) { |
|
|
410 |
this.set("value", newVal); |
|
|
411 |
} |
|
|
412 |
}, |
|
|
413 |
|
|
|
414 |
/* |
|
|
415 |
* Simple change handler, to make sure user does not input an invalid value |
|
|
416 |
*/ |
|
|
417 |
_onInputChange : function(e) { |
|
|
418 |
if (!this._validateValue(this.inputNode.get("value"))) { |
|
|
419 |
this.syncUI(); |
|
|
420 |
} |
|
|
421 |
}, |
|
|
422 |
|
|
|
423 |
/* |
|
|
424 |
* Initiates mouse down timers, to increment slider, while mouse button |
|
|
425 |
* is held down |
|
|
426 |
*/ |
|
|
427 |
_setMouseDownTimers : function(dir, step) { |
|
|
428 |
this._mouseDownTimer = Y.later(500, this, function() { |
|
|
429 |
this._mousePressTimer = Y.later(100, this, function() { |
|
|
430 |
this.set("value", this.get("value") + (dir * step)); |
|
|
431 |
}, null, true) |
|
|
432 |
}); |
|
|
433 |
}, |
|
|
434 |
|
|
|
435 |
/* |
|
|
436 |
* Clears timers used to support the "mouse held down" behavior |
|
|
437 |
*/ |
|
|
438 |
_clearMouseDownTimers : function() { |
|
|
439 |
if (this._mouseDownTimer) { |
|
|
440 |
this._mouseDownTimer.cancel(); |
|
|
441 |
this._mouseDownTimer = null; |
|
|
442 |
} |
|
|
443 |
if (this._mousePressTimer) { |
|
|
444 |
this._mousePressTimer.cancel(); |
|
|
445 |
this._mousePressTimer = null; |
|
|
446 |
} |
|
|
447 |
}, |
|
|
448 |
|
|
|
449 |
/* |
|
|
450 |
* value attribute change listener. Updates the |
|
|
451 |
* value in the rendered input box, whenever the |
|
|
452 |
* attribute value changes. |
|
|
453 |
*/ |
|
|
454 |
_afterValueChange : function(e) { |
|
|
455 |
this._uiSetValue(e.newVal); |
|
|
456 |
}, |
|
|
457 |
|
|
|
458 |
/* |
|
|
459 |
* Updates the value of the input box to reflect |
|
|
460 |
* the value passed in |
|
|
461 |
*/ |
|
|
462 |
_uiSetValue : function(val) { |
|
|
463 |
this.inputNode.set("value", val); |
|
|
464 |
}, |
|
|
465 |
|
|
|
466 |
/* |
|
|
467 |
* value attribute default validator. Verifies that |
|
|
468 |
* the value being set lies between the min/max value |
|
|
469 |
*/ |
|
|
470 |
_validateValue: function(val) { |
|
|
471 |
var min = this.get("min"), |
|
|
472 |
max = this.get("max"); |
|
|
473 |
|
|
|
474 |
return (Lang.isNumber(val) && val >= min && val <= max); |
|
|
475 |
} |
|
|
476 |
}); |
|
|
477 |
|
|
|
478 |
// Create a new Spinner instance, drawing it's |
|
|
479 |
// starting value from an input field already on the |
|
|
480 |
// page (contained in the #numberField content box) |
|
|
481 |
var spinner = new Spinner({ |
|
|
482 |
contentBox: "#numberField", |
|
|
483 |
max:100, |
|
|
484 |
min:0 |
|
|
485 |
}); |
|
|
486 |
spinner.render(); |
|
|
487 |
spinner.focus(); |
|
|
488 |
}); |
|
|
489 |
</script> |
|
|
490 |
|
|
|
491 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
492 |
|
|
|
493 |
</body> |
|
|
494 |
</html> |