Cross Reference: /yui3/src/widget/docs/widget-build.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<style type="text/css" scoped>
/* Standard Module Widget CSS */
.yui3-standardmodule-hidden {
display:none;
}
.yui3-standardmodule {
margin:10px;
}
.yui3-standardmodule-content {
padding:3px;
border:1px solid #666;
}
.yui3-standardmodule-content .yui3-widget-hd {
padding:5px;
border:2px solid #aa0000;
background-color:#fff;
overflow:auto;
}
.yui3-standardmodule-content .yui3-widget-bd {
padding:5px;
border:2px solid #0000aa;
background-color:#fff;
overflow:auto;
}
.yui3-standardmodule-content .yui3-widget-ft {
padding:5px;
border:2px solid #00aa00;
background-color:#fff;
overflow:auto;
}
/* Positionable Widget CSS */
.yui3-positionable {
position:absolute;
}
.yui3-positionable-content {
text-align:center;
border:1px solid #000;
background-color:#999966;
color:#fff;
padding:10px;
}
.yui3-positionable-hidden {
visibility:hidden;
}
/* Alignable Widget CSS */
.yui3-alignable {
position:absolute;
}
.yui3-alignable-content {
text-align:center;
border:1px solid #000000;
background-color:#004C6D;
color:#fff;
padding:1px;
}
.yui3-alignable-hidden {
visibility:hidden;
}
/* Example Layout CSS */
.widget-build-example {
border:1px solid #ccc;
padding:5px;
}
.widget-build-example button, .widget-build-example label, .widget-build-example select, .widget-build-example input {
margin-right:5px;
}
.widget-build-example button.fail {
color:#cc0000;
margin-left:10px;
}
.widget-build-example .filler {
color:#999;
}
#x, #y {
width:3em;
}
#widget-build-examples dd {
margin-left:0;
}
#widget-build-examples dt {
margin-bottom:1em;
margin-top:1em;
}
#widget2-example, #widget3-example {
height:15em;
}
#widget2-example select, #widget3-example select {
width:100%;
}
#alignment p {
margin:0;
padding:2px;
color:#dddd00;
}
</style>
<div class="intro">
<p>This example shows how you can mix and match the `WidgetPosition`, `WidgetPositionAlign`, `WidgetStack` and `WidgetStdMod` extensions to build custom versions of the `Widget` class, using `Base.create`.</p>
</div>
<div class="example">
{{>widget-build-source}}
</div>
<h2>Creating Custom Widget Classes</h2>
<p>The `Base` class provides a `create` method which can be used to create custom versions of classes which derive from `Base` by adding extension classes to them.</p>
<p>Widget currently ships with four such extensions: `WidgetPosition`, `WidgetStack`, `WidgetPositionAlign` and `WidgetStdMod`.
These extensions are used to create the basic `Overlay` widget, but can also be used individually, to create custom versions of the base `Widget` class.</p>
<h2>Widget with WidgetStdMod support</h2>
<p>Adding the `WidgetStdMod` extension to Widget, creates a statically positioned Widget, with support for standard module format sections - header, body and footer, which maybe useful in portal type use cases, where the positioning/stacking capabilities which come bundled with Overlay are not required.</p>
<p>To create a custom class, we use <a href="{{apiDocs}}/Base.html#method_Base.create">`Base.create`</a>, which is described in detail on the documention page for <a href="../base/index.html#extensions">Base</a>.</p>
<p>We pass in `Widget` as the main class we want to add extensions to, and `WidgetStdMod` as the extension we'd like added to the main class:</p>
```
var StandardModule = Y.Base.create("standardModule", Y.Widget, [Y.WidgetStdMod]);
// Render from Markup
var stdmod = new StandardModule({
contentBox: "#widget1",
width:"12em",
height:"12em"
});
stdmod.render();
```
<p>`Base.create` will:</p>
<ol>
<li>Create a new class which extends `Widget`</li>
<li>Aggregate known `Base` and `Widget` fields, such as `ATTRS` and `HTML_PARSER` from `WidgetStdMod` on the new class</li>
<li>Augment prototype methods from `WidgetStdMod` onto the new class prototype</li>
</ol>
<p>The first argument to create is the `NAME` of the new class we are creating, just like the `NAME` we define when extending the Widget class directly.</p>
<p>Note that the `Widget` class is unchanged, allowing you to still create `Widget` instances without any standard module support, along with `StandardModule` instances which have standard module support.</p>
<h3>Testing It Out</h3>
<p>The example attempts to set content on an instance of the newly created `StandardModule` class, using the `setStdModContent` method which is added by the extension (which would otherwise not exist on the Widget instance).</p>
```
var contentInput = Y.one("#content");
var sectionInput = Y.one("#section");
// This should work, since the StandardModule widget has settable
// header/body/footer sections
Y.on("submit", function(e) {
e.preventDefault();
var content = Y.Escape.html(contentInput.get("value"));
var section = sectionInput.get("value");
stdmod.setStdModContent(section, content);
}, "#widget1-example");
```
<p>To verify that no unrequested features are added, we also attempt to move the instance using the `move` method, which is not part of the base Widget class, and would be added by the `WidgetPosition` extension. This verifies that the other example classes we'll create, which do create new classes which use `WidgetPosition`, have not modified the base Widget class.</p>
```
// This shoud fail, since the StandardModule widget is not positionable
Y.on("click", function(e) {
try {
stdmod.move([0,0]);
} catch (e) {
alert("move() is " + typeof stdmod.move + ", stdmod.hasImpl(Y.WidgetPosition) : "
+ stdmod.hasImpl(Y.WidgetPosition));
}
}, "#tryMove");
```
<p>Note that `Base.create` adds a `hasImpl` method to the built class, which allows you to query whether or not it has a particular extension applied.</p>
<h3>CSS Considerations</h3>
<p>We need to define the CSS which goes with this new `StandardModule` class we have created. The only rule really required out of the box is the rule which handles visibility (`yui-standardmodule-hidden`). The "standardmodule" used in the class name comes from the `NAME` property we set up for the new class, and is used to prefix all state related classes added to the widgets bounding box.
Since the `StandardModule` class is not positionable, we use `display:none` to define the `hidden` state.</p>
```
/* Visibility - How to handle visibility for this new widget */
.yui3-standardmodule-hidden {
display:none;
}
```
<p>The other "yui-standardmodule" rules are only used to create the required look/feel for this particular example, and do not impact the StandardModule widget's functionality.</p>
<h2>Widget with WidgetPosition and WidgetStack support</h2>
<p>As with `StandardModule`, we use `Base.create` to create the new `Positionable` widget class. This time we add `WidgetPosition` and `WidgetStack` support to the base `Widget` class to create a basic XY positionable widget, with shimming and z-index support.</p>
```
var Positionable = Y.Base.create("positionable", Y.Widget,
[Y.WidgetPosition, Y.WidgetStack]);
// Render from markup
var positionable = new Positionable({
contentBox: "#widget2",
width:"10em",
height:"10em",
zIndex:1
});
positionable.render("#widget2-example");
var xy = Y.one("#widget2-example > p").getXY();
positionable.move(xy[0], xy[1]);
```
<p>We <strong>don't</strong> add `WidgetPositionAlign` or `WidgetStdMod` support, so the widget doesn't have extended positioning support (align, center) or standard module support. Hence we position it manually using the `move` method which the `WidgetPosition` extension provides.</p>
<h3>Testing It Out</h3>
<p>We should now be able to invoke the `move` method on an instance of the newly created `Positionable` class:</p>
```
// This should work, since Positionable has basic XY Positioning support
Y.on("submit", function(e) {
e.preventDefault();
var x = parseInt(xInput.get("value"));
var y = parseInt(yInput.get("value"));
positionable.move(x,y);
}, "#widget2-example");
```
<p>And, as with the `StandardModule` class, we should not be allowed to invoke any methods from an extension which we didn't request:</p>
```
// This should fail, since Positionable does not have Standard Module sections
Y.on("click", function(e) {
try {
positionable.setStdModContent("header", "new content");
} catch (e) {
alert("setStdModContent() is " + typeof positionable.setStdModContent +
", positionable.hasImpl(Y.WidgetStdMod) : " + positionable.hasImpl(Y.WidgetStdMod));
}
}, "#tryContent");
```
<h3>CSS Considerations</h3>
<p>Since now we have a positionable widget, with z-index support, we set the widget to be absolutely positioned by default, and control it's hidden state using `visibility` as opposed to `display`</p>
```
/* Define absolute positioning as the default for positionable widgets */
.yui3-positionable {
position:absolute;
}
/*
In order to be able to position the widget when hidden, we define hidden
to use visibility, as opposed to display
*/
.yui3-positionable-hidden {
visibility:hidden;
}
```
<h2>Widget with WidgetPosition, WidgetStack and WidgetPositionAlign support</h2>
<p>Lastly, we'll attempt to create a new widget class, which, in addition to basic positioning and stacking support, also has extended positioning support, allowing us to align it with other elements on the page.</p>
<p>Again, we use `Base.create` to create our new `Alignable` widget class, by combining `WidgetPosition`, `WidgetStack` and `WidgetPositionAlign` with the base widget class:</p>
```
var Alignable = Y.Base.create("alignable", Y.Widget,
[Y.WidgetPosition, Y.WidgetPositionAlign, Y.WidgetStack]);
var alignable = new Alignable({
width:"14em",
align : {
node: "#widget3-example",
points: ["cc", "cc"]
},
zIndex:1
});
alignable.get("contentBox").set("innerHTML",
'<strong>Alignable Widget</strong><div id="alignment"><p>#widget3-example</p> \
<p>[center, center]</p></div>');
alignable.render("#widget3-example");
```
<h3>Testing It Out</h3>
<p>We'll attempt to align an instance of the `Alignable` class, using some of the additional attributes which `WidgetPositionAlign` adds to the base `Widget` class: `align` and `centered`:</p>
```
// Align left-center egde of widget to
// right-center edge of the node with id "widget3-example"
alignable.set("align", {node:"#widget3-example", points:["lc", "rc"]});
// Align top-right corner of widget to
// bottom-right corner of the node with id "widget3-example"
alignable.set("align", {node:"#widget3-example", points:["tr", "br"]});
// Center the widget in the node with id "widget3-example"
alignable.set("centered", "widget3-example");
// Align the right-center edge of the widget to
// the right center edge of the viewport (since a node is not provided to 'align')
alignable.set("align", {points:["rc", "rc"]});
// Center the widget in the viewport (wince a node is not provided to 'centered')
alignable.set("centered", true);
// Return the node to it's original alignment
// (centered in the node with id "widget3-example")
// NOTE: centered is a shortcut for align : { points:["cc", "cc"] }
alignable.set("align", {node:"#widget3-example", points:["cc", "cc"]});
```
<h3>CSS Considerations</h3>
<p>The `Alignable` widget class, has the same core CSS rules as the `Positionable` class, to define how it is positioned and how it is hidden:</p>
```
/* Define absolute positioning as the default for alignable widgets */
.yui3-alignable {
position:absolute;
}
/*
In order to be able to position the widget when hidden, we define hidden
to use visibility, as opposed to display
*/
.yui3-alignable-hidden {
visibility:hidden;
}
```
<h2>Complete Example Source</h2>
```
{{>widget-build-source}}
```