|
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>Attribute Getters, Setters and Validators</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 |
#boxParent { |
|
|
28 |
overflow:hidden; |
|
|
29 |
background-color:#004c6d; |
|
|
30 |
height:25em; |
|
|
31 |
padding:10px; |
|
|
32 |
margin:5px; |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
#boxParent .yui-box p, #attrs p { |
|
|
36 |
margin:2px; |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
.attrs { |
|
|
40 |
border:1px solid #000; |
|
|
41 |
background-color:#cdcdcd; |
|
|
42 |
margin:5px; |
|
|
43 |
} |
|
|
44 |
|
|
|
45 |
.attrs .header { |
|
|
46 |
font-weight:bold; |
|
|
47 |
background-color:#aaa; |
|
|
48 |
padding:5px; |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
.attrs .body { |
|
|
52 |
padding:10px; |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
.attrs .body .hints li { |
|
|
56 |
padding-bottom:10px; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
.attrs .footer { |
|
|
60 |
padding:0px 20px 10px 20px; |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
.attrs label { |
|
|
64 |
font-weight:bold; |
|
|
65 |
display:block; |
|
|
66 |
float:left; |
|
|
67 |
width:4em; |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
.attrs .hint { |
|
|
71 |
font-size:85%; |
|
|
72 |
color: #004c6d; |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
.attrs .fields { |
|
|
76 |
border-top:1px solid #aaa; |
|
|
77 |
padding:10px; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
.yui-box { |
|
|
81 |
padding:5px; |
|
|
82 |
border:1px solid #000; |
|
|
83 |
width:8em; |
|
|
84 |
height:8em; |
|
|
85 |
text-align:center; |
|
|
86 |
color:#000; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
.yui-box .color, .yui-box .coord { |
|
|
90 |
font-family:courier; |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
</style> |
|
|
94 |
|
|
|
95 |
<!--end custom header content for this example--> |
|
|
96 |
|
|
|
97 |
</head> |
|
|
98 |
|
|
|
99 |
<body class=" yui-skin-sam"> |
|
|
100 |
|
|
|
101 |
<h1>Attribute Getters, Setters and Validators</h1> |
|
|
102 |
|
|
|
103 |
<div class="exampleIntro"> |
|
|
104 |
The <a href="attribute-basic.html">"Basic Attribute Configuration" example</a> shows how you can add attributes to a host class, and set up default values for them using the attribute configuration object. This example explores how you can configure <code>setter</code>, <code>getter</code> and <code>validator</code> functions for individual attributes, which can be used to modify or normalize attribute values during get and set invocations, and prevent invalid values from being stored. |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
</div> |
|
|
109 |
|
|
|
110 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
111 |
|
|
|
112 |
<div id="attrs" class="attrs"> |
|
|
113 |
<div class="header">Enter new values and click the "Set" buttons</div> |
|
|
114 |
<div class="body"> |
|
|
115 |
<ul class="hints"> |
|
|
116 |
<li>Try entering valid and invalid values for x, y; or values which attempt to position the box outside it's parent (parent box co-ordinates are displayed next to the text box).</li> |
|
|
117 |
<li>Try entering rgb, hex or keyword color values [ <code>rgb(255,0,0)</code>, <code>#ff0000</code>, <code>red</code> ].</li> |
|
|
118 |
</ul> |
|
|
119 |
<div class="fields"> |
|
|
120 |
<p> |
|
|
121 |
<label for="x">x:</label> |
|
|
122 |
<input type="text" name="x" id="x" /> |
|
|
123 |
<button type="button" class="action" id="setX">Set</button> |
|
|
124 |
<span id="xhint" class="hint"></span> |
|
|
125 |
</p> |
|
|
126 |
<p> |
|
|
127 |
<label for="y">y:</label> |
|
|
128 |
<input type="text" name="y" id="y" /> |
|
|
129 |
<button type="button" class="action" id="setY">Set</button> |
|
|
130 |
<span id="yhint" class="hint"></span> |
|
|
131 |
</p> |
|
|
132 |
<p> |
|
|
133 |
<label for="color">color:</label> |
|
|
134 |
<input type="text" name="color" id="color" /> |
|
|
135 |
<button type="button" class="action" id="setColor">Set</button> |
|
|
136 |
</p> |
|
|
137 |
</div> |
|
|
138 |
</div> |
|
|
139 |
<div class="footer"> |
|
|
140 |
<button type="button" class="action" id="setXY">Set XY</button> |
|
|
141 |
<button type="button" class="action" id="setAll">Set All</button> |
|
|
142 |
<button type="button" class="action" id="getAll">Get All</button> |
|
|
143 |
</div> |
|
|
144 |
</div> |
|
|
145 |
|
|
|
146 |
<div id="boxParent"></div> |
|
|
147 |
|
|
|
148 |
<script type="text/javascript"> |
|
|
149 |
// Get a new YUI instance |
|
|
150 |
YUI({base:"../../build/", timeout: 10000}).use("node", "attribute", function(Y) { |
|
|
151 |
|
|
|
152 |
var boxParent = Y.one("#boxParent"); |
|
|
153 |
|
|
|
154 |
// Setup a custom class with attribute support |
|
|
155 |
function Box(cfg) { |
|
|
156 |
this._createNode(cfg); |
|
|
157 |
|
|
|
158 |
// Attribute configuration |
|
|
159 |
var attrs = { |
|
|
160 |
|
|
|
161 |
"parent" : { |
|
|
162 |
value: null |
|
|
163 |
}, |
|
|
164 |
|
|
|
165 |
"x" : { |
|
|
166 |
setter: function(val, name) { |
|
|
167 |
// Pass through x value to xy |
|
|
168 |
this.set("xy", [parseInt(val), this.get("y")]); |
|
|
169 |
}, |
|
|
170 |
|
|
|
171 |
getter: function(val, name) { |
|
|
172 |
// Get x value from xy |
|
|
173 |
return this.get("xy")[0]; |
|
|
174 |
} |
|
|
175 |
}, |
|
|
176 |
|
|
|
177 |
"y" : { |
|
|
178 |
setter: function(val, name) { |
|
|
179 |
// Pass through y value to xy |
|
|
180 |
this.set("xy", [this.get("x"), parseInt(val)]); |
|
|
181 |
}, |
|
|
182 |
|
|
|
183 |
getter: function() { |
|
|
184 |
// Get y value from xy |
|
|
185 |
return this.get("xy")[1]; |
|
|
186 |
} |
|
|
187 |
}, |
|
|
188 |
|
|
|
189 |
"xy" : { |
|
|
190 |
// Actual stored xy co-ordinates |
|
|
191 |
value: [0, 0], |
|
|
192 |
|
|
|
193 |
setter: function(val, name) { |
|
|
194 |
// Constrain XY value to the parent element. |
|
|
195 |
|
|
|
196 |
// Returns the constrained xy value, which will |
|
|
197 |
// be the final value stored. |
|
|
198 |
return this.constrain(val); |
|
|
199 |
}, |
|
|
200 |
|
|
|
201 |
validator: function(val, name) { |
|
|
202 |
// Ensure we only store a valid data value |
|
|
203 |
return (Y.Lang.isArray(val) && val.length == 2 && Y.Lang.isNumber(val[0]) && Y.Lang.isNumber(val[1])); |
|
|
204 |
} |
|
|
205 |
}, |
|
|
206 |
|
|
|
207 |
"color" : { |
|
|
208 |
value: "olive", |
|
|
209 |
|
|
|
210 |
getter: function(val, name) { |
|
|
211 |
// Always normalize the returned value to |
|
|
212 |
// a hex color value, even if the stored |
|
|
213 |
// value is a keyword, or an rgb value. |
|
|
214 |
if (val) { |
|
|
215 |
return Y.Color.toHex(val); |
|
|
216 |
} else { |
|
|
217 |
return null; |
|
|
218 |
} |
|
|
219 |
}, |
|
|
220 |
|
|
|
221 |
validator: function(val, name) { |
|
|
222 |
// Ensure we only store rgb, hex or keyword values. |
|
|
223 |
return (Y.Color.re_RGB.test(val) || Y.Color.re_hex.test(val) || Y.Color.KEYWORDS[val]); |
|
|
224 |
} |
|
|
225 |
} |
|
|
226 |
}; |
|
|
227 |
|
|
|
228 |
this.addAttrs(attrs, cfg); |
|
|
229 |
|
|
|
230 |
this._sync(); |
|
|
231 |
this._bind(); |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
Box.BUFFER = 5; |
|
|
235 |
|
|
|
236 |
// Create the box node |
|
|
237 |
Box.prototype._createNode = function() { |
|
|
238 |
this._node = Y.Node.create('<div class="yui-box"><p>Positioned Box</p><p class="coord"></p><p class="color">None</p></div>'); |
|
|
239 |
}; |
|
|
240 |
|
|
|
241 |
// Update rendered state to match the attribute state |
|
|
242 |
Box.prototype._sync = function() { |
|
|
243 |
this._syncParent(); |
|
|
244 |
this._syncXY(); |
|
|
245 |
this._syncColor(); |
|
|
246 |
}; |
|
|
247 |
|
|
|
248 |
Box.prototype._syncParent = function() { |
|
|
249 |
this.get("parent").appendChild(this._node); |
|
|
250 |
}; |
|
|
251 |
|
|
|
252 |
Box.prototype._syncXY = function() { |
|
|
253 |
this._node.setXY(this.get("xy")); |
|
|
254 |
this._node.one("p.coord").set("innerHTML", "[" + this.get("x") + "," + this.get("y") + "]"); |
|
|
255 |
}; |
|
|
256 |
|
|
|
257 |
Box.prototype._syncColor = function() { |
|
|
258 |
this._node.setStyle("backgroundColor", this.get("color")); |
|
|
259 |
this._node.one("p.color").set("innerHTML", this.get("color")); |
|
|
260 |
}; |
|
|
261 |
|
|
|
262 |
// Bind listeners for attribute change events |
|
|
263 |
Box.prototype._bind = function() { |
|
|
264 |
|
|
|
265 |
// Reflect any changes in xy, to the rendered Node |
|
|
266 |
this.after("xyChange", this._syncXY); |
|
|
267 |
|
|
|
268 |
// Reflect any changes in color, to the rendered Node |
|
|
269 |
// and output the color value received from get |
|
|
270 |
this.after("colorChange", this._syncColor); |
|
|
271 |
|
|
|
272 |
// Append the rendered node to the parent provided |
|
|
273 |
this.after("parentChange", this._syncParent); |
|
|
274 |
|
|
|
275 |
}; |
|
|
276 |
|
|
|
277 |
// Get min, max unconstrained values for X. Using Math.round to handle FF3's sub-pixel region values |
|
|
278 |
Box.prototype.getXConstraints = function() { |
|
|
279 |
var parentRegion = this.get("parent").get("region"); |
|
|
280 |
return [Math.round(parentRegion.left + Box.BUFFER), Math.round(parentRegion.right - this._node.get("offsetWidth") - Box.BUFFER)]; |
|
|
281 |
}; |
|
|
282 |
|
|
|
283 |
// Get min, max unconstrained values for Y. Using Math.round to handle FF3's sub-pixel region values |
|
|
284 |
Box.prototype.getYConstraints = function() { |
|
|
285 |
var parentRegion = this.get("parent").get("region"); |
|
|
286 |
return [Math.round(parentRegion.top + Box.BUFFER), Math.round(parentRegion.bottom - this._node.get("offsetHeight") - Box.BUFFER)]; |
|
|
287 |
}; |
|
|
288 |
|
|
|
289 |
// Constrain the x,y value provided |
|
|
290 |
Box.prototype.constrain = function(val) { |
|
|
291 |
|
|
|
292 |
// If the X value places the box outside it's parent, |
|
|
293 |
// modify it's value to place the box inside it's parent. |
|
|
294 |
|
|
|
295 |
var xConstraints = this.getXConstraints(); |
|
|
296 |
|
|
|
297 |
if (val[0] < xConstraints[0]) { |
|
|
298 |
val[0] = xConstraints[0]; |
|
|
299 |
} else { |
|
|
300 |
if (val[0] > xConstraints[1]) { |
|
|
301 |
val[0] = xConstraints[1]; |
|
|
302 |
} |
|
|
303 |
} |
|
|
304 |
|
|
|
305 |
// If the Y value places the box outside it's parent, |
|
|
306 |
// modify it's value to place the box inside it's parent. |
|
|
307 |
|
|
|
308 |
var yConstraints = this.getYConstraints(); |
|
|
309 |
|
|
|
310 |
if (val[1] < yConstraints[0]) { |
|
|
311 |
val[1] = yConstraints[0]; |
|
|
312 |
} else { |
|
|
313 |
if (val[1] > yConstraints[1]) { |
|
|
314 |
val[1] = yConstraints[1]; |
|
|
315 |
} |
|
|
316 |
} |
|
|
317 |
|
|
|
318 |
return val; |
|
|
319 |
}; |
|
|
320 |
|
|
|
321 |
|
|
|
322 |
Y.augment(Box, Y.Attribute); |
|
|
323 |
|
|
|
324 |
// ------ |
|
|
325 |
|
|
|
326 |
// Create a new instance of Box |
|
|
327 |
var box = new Box({ |
|
|
328 |
parent : boxParent |
|
|
329 |
}); |
|
|
330 |
|
|
|
331 |
// Set references to form controls |
|
|
332 |
var xTxt = Y.one("#x"); |
|
|
333 |
var yTxt = Y.one("#y"); |
|
|
334 |
var colorTxt = Y.one("#color"); |
|
|
335 |
|
|
|
336 |
var xHint = Y.one("#xhint"); |
|
|
337 |
var yHint = Y.one("#yhint"); |
|
|
338 |
|
|
|
339 |
function getAll() { |
|
|
340 |
xTxt.set("value", box.get("x")); |
|
|
341 |
yTxt.set("value", box.get("y")); |
|
|
342 |
colorTxt.set("value", box.get("color")); |
|
|
343 |
} |
|
|
344 |
|
|
|
345 |
// Bind DOM events for Form Controls |
|
|
346 |
|
|
|
347 |
// Use event delegation for the action button clicks |
|
|
348 |
Y.on("delegate", function(e) { |
|
|
349 |
|
|
|
350 |
// Get Node target from the event object |
|
|
351 |
|
|
|
352 |
// We already know it's a button which has an action because |
|
|
353 |
// of our selector (button.action), so all we need to do is |
|
|
354 |
// route it based on the id. |
|
|
355 |
var id = e.currentTarget.get("id"); |
|
|
356 |
|
|
|
357 |
switch (id) { |
|
|
358 |
case "setXY": |
|
|
359 |
box.set("xy", [parseInt(xTxt.get("value")), parseInt(yTxt.get("value"))]); |
|
|
360 |
break; |
|
|
361 |
case "setX": |
|
|
362 |
box.set("x", parseInt(xTxt.get("value"))); |
|
|
363 |
break; |
|
|
364 |
case "setY": |
|
|
365 |
box.set("y", parseInt(yTxt.get("value"))); |
|
|
366 |
break; |
|
|
367 |
case "setColor": |
|
|
368 |
box.set("color", Y.Lang.trim(colorTxt.get("value"))); |
|
|
369 |
break; |
|
|
370 |
case "setAll": |
|
|
371 |
box.set("xy", [parseInt(xTxt.get("value")), parseInt(yTxt.get("value"))]); |
|
|
372 |
box.set("color", Y.Lang.trim(colorTxt.get("value"))); |
|
|
373 |
break; |
|
|
374 |
case "getAll": |
|
|
375 |
getAll(); |
|
|
376 |
break; |
|
|
377 |
default: |
|
|
378 |
break; |
|
|
379 |
} |
|
|
380 |
|
|
|
381 |
}, "#attrs", "click", "button.action"); |
|
|
382 |
|
|
|
383 |
// Bind listeners to provide min, max unconstrained value hints for x, y |
|
|
384 |
// (focus/blur doesn't bubble, so bind individual listeners) |
|
|
385 |
Y.on("focus", function() { |
|
|
386 |
var minmax = box.getXConstraints(); |
|
|
387 |
xHint.set("innerHTML", "Valid values: " + minmax[0] + " to " + minmax[1]); |
|
|
388 |
}, xTxt); |
|
|
389 |
|
|
|
390 |
Y.on("focus", function() { |
|
|
391 |
var minmax = box.getYConstraints(); |
|
|
392 |
yHint.set("innerHTML", "Valid values: " + minmax[0] + " to " + minmax[1]); |
|
|
393 |
}, yTxt); |
|
|
394 |
|
|
|
395 |
Y.on("blur", function() { |
|
|
396 |
xHint.set("innerHTML", ""); |
|
|
397 |
}, xTxt); |
|
|
398 |
|
|
|
399 |
Y.on("blur", function() { |
|
|
400 |
yHint.set("innerHTML", ""); |
|
|
401 |
}, yTxt); |
|
|
402 |
|
|
|
403 |
getAll(); |
|
|
404 |
}); |
|
|
405 |
</script> |
|
|
406 |
|
|
|
407 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
408 |
|
|
|
409 |
</body> |
|
|
410 |
</html> |