|
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 Based Speed Dating</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 #speeddate h1 { |
|
27 font-size: 108%; |
|
28 color:#000; |
|
29 margin-bottom:2em; |
|
30 } |
|
31 |
|
32 #john { |
|
33 margin-bottom:10px; |
|
34 } |
|
35 |
|
36 .sd-nametag { |
|
37 border:1px solid #000; |
|
38 text-align:center; |
|
39 width:25em; |
|
40 margin:20px; |
|
41 |
|
42 background-color:#00f; |
|
43 |
|
44 border-radius: 10px; |
|
45 -webkit-border-radius: 10px; |
|
46 -moz-border-radius: 10px; |
|
47 |
|
48 box-shadow: 3px 3px 3px #888; |
|
49 -moz-box-shadow: 3px 3px 3px #888; |
|
50 -webkit-box-shadow: 3px 3px 3px #888; |
|
51 } |
|
52 |
|
53 .sd-nametag .sd-hd, .sd-nametag .sd-ft { |
|
54 padding:5px; |
|
55 text-align:center; |
|
56 font-size:108%; |
|
57 font-weight:900; |
|
58 color:#fff; |
|
59 } |
|
60 |
|
61 .sd-nametag .sd-hd { |
|
62 border-top-right-radius: 10px; |
|
63 border-top-left-radius: 10px; |
|
64 -moz-border-radius-topright: 10px; |
|
65 -moz-border-radius-topleft: 10px; |
|
66 -webkit-border-top-right-radius: 10px; |
|
67 -webkit-border-top-left-radius: 10px; |
|
68 } |
|
69 |
|
70 .sd-nametag .sd-ft { |
|
71 border-bottom-right-radius: 10px; |
|
72 border-bottom-left-radius: 10px; |
|
73 -moz-border-radius-bottomright: 10px; |
|
74 -moz-border-radius-bottomleft: 10px; |
|
75 -webkit-border-bottom-right-radius: 10px; |
|
76 -webkit-border-bottom-left-radius: 10px; |
|
77 } |
|
78 |
|
79 .sd-nametag .sd-bd { |
|
80 background-color:#fff; |
|
81 padding:1em; |
|
82 } |
|
83 |
|
84 .sd-nametag .sd-bd .sd-name, |
|
85 .sd-nametag .sd-bd .sd-personality { |
|
86 font-size:108%; |
|
87 font-weight:900; |
|
88 font-family:monospace; |
|
89 text-decoration:underline; |
|
90 color:#00a; |
|
91 } |
|
92 |
|
93 .sd-nametag .sd-bd .sd-personality.sd-max { |
|
94 color:#f00; |
|
95 } |
|
96 |
|
97 </style> |
|
98 |
|
99 <!--end custom header content for this example--> |
|
100 |
|
101 </head> |
|
102 |
|
103 <body class=" yui-skin-sam"> |
|
104 |
|
105 <h1>Attribute Based Speed Dating</h1> |
|
106 |
|
107 <div class="exampleIntro"> |
|
108 <p> |
|
109 This example builds on the <a href="attribute-basic.html">"Basic Configuration" example</a>, |
|
110 showing how you can use attribute to model objects in your application. |
|
111 </p> |
|
112 |
|
113 <p> |
|
114 As with the basic example, it is geared towards users who want to create their own classes from |
|
115 scratch and add attribute support. In most cases you should consider extending the |
|
116 <a href="http://developer.yahoo.com/yui/3/base/index.html"><code>Base</code></a> class when you need attribute support, instead |
|
117 of augmenting Attribute directly. <a href="http://developer.yahoo.com/yui/3/base/index.html"><code>Base</code></a> does the work described |
|
118 in this example for you, in addition to making it easier for users to extend you class. |
|
119 </p> |
|
120 </div> |
|
121 |
|
122 <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
123 |
|
124 <div id="speeddate"> |
|
125 |
|
126 <h1>Speed Dating With Attributes</h1> |
|
127 |
|
128 <div id="john"> |
|
129 <button type="button" class="hi">Hi I'm John</button> |
|
130 <button type="button" class="taken" disabled="disabled">I like Jane</button> |
|
131 <div class="shirt"></div> |
|
132 </div> |
|
133 |
|
134 <div id="jane"> |
|
135 <button type="button" disabled="disabled" class="hi">Hey, I'm Jane</button> |
|
136 <button type="button" class="upgrade" disabled="disabled">No way!, I save whales too!</button> |
|
137 <button type="button" class="taken" disabled="disabled">I like John</button> |
|
138 <div class="shirt"></div> |
|
139 </div> |
|
140 </div> |
|
141 |
|
142 <script type="text/javascript"> |
|
143 |
|
144 // Get a new instance of YUI and |
|
145 // load it with the required set of modules |
|
146 |
|
147 YUI({base:"../../build/", timeout: 10000}).use("node", "attribute", "substitute", function(Y) { |
|
148 |
|
149 // Setup custom class which we want to |
|
150 // add managed attribute support to |
|
151 |
|
152 function SpeedDater(cfg) { |
|
153 |
|
154 // When constructed, setup the initial attributes for the |
|
155 // instance, by calling the addAttrs method. |
|
156 |
|
157 var attrs = { |
|
158 // Add 3 attributes: name, personality, available |
|
159 name : { |
|
160 writeOnce:true |
|
161 }, |
|
162 |
|
163 personality : { |
|
164 value:50 |
|
165 }, |
|
166 |
|
167 available : { |
|
168 value:true |
|
169 } |
|
170 }; |
|
171 |
|
172 this.addAttrs(attrs, cfg); |
|
173 } |
|
174 |
|
175 // Setup static property to hold attribute config |
|
176 |
|
177 SpeedDater.NAMETAG = '<div class="sd-nametag"><div class="sd-hd">Hello!</div><div class="sd-bd">I\'m <span class="sd-name">{name}</span> and my PersonalityQuotientIndex is <span class="sd-personality">{personality}</span></div><div class="sd-ft sd-availability">{available}</div></div>'; |
|
178 |
|
179 SpeedDater.prototype.applyNameTag = function(where) { |
|
180 |
|
181 var tokens = { |
|
182 name: this.get("name"), |
|
183 available: (this.get("available")) ? "I'm still looking " : "Sorry, I'm taken", |
|
184 personality: this.get("personality") |
|
185 }; |
|
186 |
|
187 this.nameTag = Y.Node.create(Y.substitute(SpeedDater.NAMETAG, tokens)); |
|
188 |
|
189 Y.one(where).appendChild(this.nameTag); |
|
190 }; |
|
191 |
|
192 SpeedDater.prototype.updateNameTag = function() { |
|
193 |
|
194 var taken = (this.get("available")) ? "I'm still looking " : "Sorry, I'm taken"; |
|
195 var name = this.get("name"); |
|
196 var personality = this.get("personality"); |
|
197 |
|
198 this.nameTag.query(".sd-name").set("innerHTML", name); |
|
199 this.nameTag.query(".sd-availability").set("innerHTML", taken); |
|
200 |
|
201 var personalityEl = this.nameTag.query(".sd-personality"); |
|
202 personalityEl.set("innerHTML", personality); |
|
203 |
|
204 if (personality > 90) { |
|
205 personalityEl.addClass("sd-max"); |
|
206 } |
|
207 } |
|
208 |
|
209 // Augment custom class with Attribute |
|
210 Y.augment(SpeedDater, Y.Attribute); |
|
211 |
|
212 var john, jane; |
|
213 |
|
214 Y.on("click", function() { |
|
215 |
|
216 if (!john) { |
|
217 |
|
218 // Set both name and personality during construction |
|
219 john = new SpeedDater({ |
|
220 name: "John", |
|
221 personality: 76.43 |
|
222 }); |
|
223 john.applyNameTag("#john .shirt"); |
|
224 |
|
225 Y.one("#jane .hi").set("disabled", false); |
|
226 } |
|
227 |
|
228 }, "#john .hi"); |
|
229 |
|
230 Y.on("click", function() { |
|
231 |
|
232 if (!jane) { |
|
233 |
|
234 // Set name during construction |
|
235 jane = new SpeedDater({ |
|
236 name: "Jane" |
|
237 }); |
|
238 |
|
239 // Set personality after construction |
|
240 jane.set("personality", 82); |
|
241 |
|
242 jane.applyNameTag("#jane .shirt"); |
|
243 |
|
244 Y.all("#jane button").set("disabled", false); |
|
245 Y.all("#john button").set("disabled", false); |
|
246 } |
|
247 |
|
248 }, "#jane .hi"); |
|
249 |
|
250 Y.on("click", function() { |
|
251 |
|
252 john.set("available", false); |
|
253 john.updateNameTag(); |
|
254 |
|
255 }, "#john .taken"); |
|
256 |
|
257 Y.on("click", function() { |
|
258 |
|
259 jane.set("available", false); |
|
260 jane.updateNameTag(); |
|
261 |
|
262 }, "#jane .taken"); |
|
263 |
|
264 Y.on("click", function() { |
|
265 |
|
266 jane.set("personality", 98); |
|
267 jane.updateNameTag(); |
|
268 |
|
269 }, "#jane .upgrade"); |
|
270 |
|
271 }); |
|
272 </script> |
|
273 |
|
274 <!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
275 |
|
276 </body> |
|
277 </html> |