| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<!-- |
| 4 |
Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 5 |
|
| 6 |
Use of this source code is governed by the Apache License, Version 2.0. |
| 7 |
See the COPYING file for details. |
| 8 |
--> |
| 9 |
<head> |
| 10 |
<title>goog.ui.Control Demo</title> |
| 11 |
<script src="../base.js"></script> |
| 12 |
<script> |
| 13 |
goog.require('goog.array'); |
| 14 |
goog.require('goog.debug.DivConsole'); |
| 15 |
goog.require('goog.debug.LogManager'); |
| 16 |
goog.require('goog.events'); |
| 17 |
goog.require('goog.events.EventType'); |
| 18 |
goog.require('goog.log'); |
| 19 |
goog.require('goog.object'); |
| 20 |
goog.require('goog.ui.Control'); |
| 21 |
goog.require('goog.ui.ControlRenderer'); |
| 22 |
goog.require('goog.ui.decorate'); |
| 23 |
goog.require('goog.ui.registry'); |
| 24 |
goog.require('goog.dom.TagName'); |
| 25 |
</script> |
| 26 |
<link rel="stylesheet" href="css/demo.css"> |
| 27 |
<style> |
| 28 |
/* Demo styles for goog.ui.Control. */ |
| 29 |
.goog-control { |
| 30 |
position: relative; |
| 31 |
width: 20ex; |
| 32 |
margin: 2px; |
| 33 |
border: 2px solid #036; |
| 34 |
padding: 2px; |
| 35 |
font: normal 9pt "Trebuchet MS", Tahoma, Arial, sans-serif; |
| 36 |
color: #036; |
| 37 |
background-color:#69c; |
| 38 |
cursor: pointer; |
| 39 |
outline: none; |
| 40 |
-moz-outline: none; |
| 41 |
} |
| 42 |
|
| 43 |
.goog-control-disabled { |
| 44 |
border-color: #888; |
| 45 |
color: #888; |
| 46 |
background-color: #ccc; |
| 47 |
} |
| 48 |
|
| 49 |
.goog-control-hover { |
| 50 |
border-color: #369; |
| 51 |
color: #369; |
| 52 |
background-color: #9cf; |
| 53 |
} |
| 54 |
|
| 55 |
.goog-control-active, |
| 56 |
.goog-control-selected, |
| 57 |
.goog-control-checked { |
| 58 |
border-color: #9cf; |
| 59 |
color: #9cf; |
| 60 |
background-color: #369; |
| 61 |
} |
| 62 |
|
| 63 |
.goog-control-focused { |
| 64 |
border-color: orange; |
| 65 |
} |
| 66 |
|
| 67 |
/* Custom control styles. */ |
| 68 |
.goog-custom-control { |
| 69 |
position: relative; |
| 70 |
width: 25ex; |
| 71 |
margin: 4px 0; |
| 72 |
border: 2px solid #eee; |
| 73 |
padding: 0; |
| 74 |
cursor: pointer; |
| 75 |
vertical-align: middle; |
| 76 |
outline: none; |
| 77 |
-moz-outline: none; |
| 78 |
} |
| 79 |
|
| 80 |
.goog-custom-control-outer-box { |
| 81 |
position: relative; |
| 82 |
margin: 0; |
| 83 |
border: 2px solid #ddd; |
| 84 |
padding: 0; |
| 85 |
vertical-align: middle; |
| 86 |
} |
| 87 |
|
| 88 |
.goog-custom-control-inner-box { |
| 89 |
position: relative; |
| 90 |
margin: 0; |
| 91 |
border: 2px solid #ccc; |
| 92 |
padding: 0; |
| 93 |
vertical-align: middle; |
| 94 |
} |
| 95 |
|
| 96 |
.goog-custom-control-content { |
| 97 |
position: relative; |
| 98 |
margin: 0; |
| 99 |
border: 2px solid #bbb; |
| 100 |
padding: 4px; |
| 101 |
font-family: Georgia, Times, serif; |
| 102 |
color: #ddd; |
| 103 |
background-color: #aaa; |
| 104 |
} |
| 105 |
|
| 106 |
.goog-custom-control-hover { |
| 107 |
border-color: #eef; |
| 108 |
} |
| 109 |
|
| 110 |
.goog-custom-control-hover .goog-custom-control-outer-box { |
| 111 |
border-color: #ddf; |
| 112 |
} |
| 113 |
|
| 114 |
.goog-custom-control-hover .goog-custom-control-inner-box { |
| 115 |
border-color: #ccf; |
| 116 |
} |
| 117 |
|
| 118 |
.goog-custom-control-hover .goog-custom-control-content { |
| 119 |
border-color: #bbf; |
| 120 |
background-color: #aaf; |
| 121 |
} |
| 122 |
|
| 123 |
.goog-custom-control-active .goog-custom-control-content { |
| 124 |
color: #fff; |
| 125 |
} |
| 126 |
|
| 127 |
.goog-custom-control-focused { |
| 128 |
border-style: dashed; |
| 129 |
border-color: orange; |
| 130 |
} |
| 131 |
|
| 132 |
/* "Insert" button styles. */ |
| 133 |
.goog-edit-insert-icon { |
| 134 |
height: 16px; |
| 135 |
width: 16px; |
| 136 |
margin: 0; |
| 137 |
border: 0; |
| 138 |
padding: 0; |
| 139 |
background: url(../images/toolbar_icons.gif) no-repeat -80px; |
| 140 |
vertical-align: middle; |
| 141 |
} |
| 142 |
|
| 143 |
.goog-edit-insert-caption { |
| 144 |
margin: 0; |
| 145 |
border: 0; |
| 146 |
padding: 0 2px; |
| 147 |
vertical-align: middle; |
| 148 |
font-weight: bold; |
| 149 |
} |
| 150 |
|
| 151 |
/* Extra CSS styles. */ |
| 152 |
.extra-red { |
| 153 |
border: 1px solid red; |
| 154 |
background-color: #fcc; |
| 155 |
} |
| 156 |
|
| 157 |
.extra-blue { |
| 158 |
border: 1px solid blue; |
| 159 |
background-color: #ccf; |
| 160 |
} |
| 161 |
|
| 162 |
.extra-thick-border { |
| 163 |
border-width: 4px; |
| 164 |
} |
| 165 |
</style> |
| 166 |
</head> |
| 167 |
<body> |
| 168 |
<h1>goog.ui.Control</h1> |
| 169 |
<table border="0" cellpadding="0" cellspacing="4" width="100%"> |
| 170 |
<tbody> |
| 171 |
<tr valign="top"> |
| 172 |
<td width="67%"> |
| 173 |
<!-- Left pane --> |
| 174 |
<fieldset> |
| 175 |
<legend>This control was created programmatically: </legend> |
| 176 |
<div id="c1"></div> |
| 177 |
<br/> |
| 178 |
This control dispatches ENTER, LEAVE, and ACTION events on |
| 179 |
mouseover, mouseout, and mouseup, respectively. It supports |
| 180 |
keyboard focus. |
| 181 |
</fieldset> |
| 182 |
<br/> |
| 183 |
<fieldset> |
| 184 |
<legend>This was created by decorating a SPAN: </legend> |
| 185 |
<span id="c2" |
| 186 |
class="goog-inline-block goog-control goog-control-disabled" |
| 187 |
style="vertical-align:middle;"> |
| 188 |
Decorated Example |
| 189 |
</span> |
| 190 |
<br/> |
| 191 |
<span id="hint" class="hint"> |
| 192 |
You need to enable this component first. |
| 193 |
</span> |
| 194 |
<br/> |
| 195 |
<br/> |
| 196 |
This control is configured to dispatch state transition events in |
| 197 |
addition to ENTER, LEAVE, and ACTION. It also supports keyboard |
| 198 |
focus. Watch the event log as you interact with this component. |
| 199 |
<br/> |
| 200 |
<br/> |
| 201 |
<label> |
| 202 |
Click here to toggle the component's enabled status: |
| 203 |
<input type="checkbox" id="enable"/> |
| 204 |
</label> |
| 205 |
<br/> |
| 206 |
<label> |
| 207 |
Click here to toggle the component's visiblity: |
| 208 |
<input type="checkbox" id="visible" checked/> |
| 209 |
</label> |
| 210 |
</fieldset> |
| 211 |
<br/> |
| 212 |
<h3>Custom Renderers</h3> |
| 213 |
<fieldset> |
| 214 |
<legend> |
| 215 |
This control was created using a custom renderer: |
| 216 |
</legend> |
| 217 |
<div id="c3"></div> |
| 218 |
</fieldset> |
| 219 |
<br/> |
| 220 |
<fieldset> |
| 221 |
<legend> |
| 222 |
This was created by decorating a DIV via a custom renderer: |
| 223 |
</legend> |
| 224 |
<div id="c4" class="goog-custom-control"> |
| 225 |
<span class="goog-inline-block goog-edit-insert-icon"> |
| 226 |
|
| 227 |
</span> |
| 228 |
<span class="goog-inline-block goog-edit-insert-caption"> |
| 229 |
Insert Picture |
| 230 |
</span> |
| 231 |
</div> |
| 232 |
</fieldset> |
| 233 |
<br/> |
| 234 |
<h3>Extra CSS Styling</h3> |
| 235 |
<fieldset> |
| 236 |
<legend> |
| 237 |
These controls have extra CSS classes applied: |
| 238 |
</legend> |
| 239 |
<div id="c5"></div> |
| 240 |
<div id="c6"></div> |
| 241 |
<br/> |
| 242 |
Use the <b>addClassName</b> API to add additional CSS class names |
| 243 |
to controls, before or after they're rendered or decorated. |
| 244 |
</fieldset> |
| 245 |
<br/> |
| 246 |
<h3>Right-to-Left Rendering</h3> |
| 247 |
<fieldset> |
| 248 |
<legend> |
| 249 |
These controls are rendered right-to-left: |
| 250 |
</legend> |
| 251 |
<p>These right-to-left controls were progammatically created:</p> |
| 252 |
<div id="bidi" style="position:relative;background-color:#ccc;" |
| 253 |
dir="rtl"></div> |
| 254 |
<p>These right-to-left controls were decorated:</p> |
| 255 |
<div style="position:relative;background-color:#ccc;" dir="rtl"> |
| 256 |
<div id="c9" class="goog-inline-block goog-control"> |
| 257 |
Hello, world |
| 258 |
</div><div id="c10" class="goog-inline-block goog-control"> |
| 259 |
Sample control |
| 260 |
</div> |
| 261 |
</div> |
| 262 |
<p class="hint"> |
| 263 |
On pre-FF3 Gecko, <b>margin-left</b> and <b>margin-right</b> are |
| 264 |
ignored, so controls render right next to each other. |
| 265 |
A workaround is to include some <b>&nbsp;</b>s in between |
| 266 |
controls. |
| 267 |
</p> |
| 268 |
</fieldset> |
| 269 |
</td> |
| 270 |
<td width="33%"> |
| 271 |
<!-- Event log. --> |
| 272 |
<fieldset class="goog-debug-panel"> |
| 273 |
<legend>Event Log</legend> |
| 274 |
<div id="log"></div> |
| 275 |
</fieldset> |
| 276 |
<br/> |
| 277 |
<span class="hint"> |
| 278 |
The control with the |
| 279 |
<span style="font-weight:bold;color:orange;">orange outline</span> |
| 280 |
has keyboard focus. |
| 281 |
</span> |
| 282 |
</td> |
| 283 |
</tr> |
| 284 |
</tbody> |
| 285 |
</table> |
| 286 |
<div id="perf"></div> |
| 287 |
<script> |
| 288 |
var timer = goog.now(); |
| 289 |
|
| 290 |
// Set up a logger. |
| 291 |
goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL); |
| 292 |
var logger = goog.log.getLogger('demo'); |
| 293 |
var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log')); |
| 294 |
logconsole.setCapturing(true); |
| 295 |
|
| 296 |
var EVENTS = goog.object.getValues(goog.ui.Component.EventType); |
| 297 |
goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.'); |
| 298 |
|
| 299 |
function logEvent(e) { |
| 300 |
goog.log.info(logger, '"' + e.target.getCaption() + '" dispatched: ' + e.type); |
| 301 |
} |
| 302 |
|
| 303 |
// Create the first control programmatically, and make it focusable. |
| 304 |
var c1 = new goog.ui.Control( |
| 305 |
goog.dom.createDom(goog.dom.TagName.SPAN, null, 'Hello, ', |
| 306 |
goog.dom.createDom(goog.dom.TagName.B, null, 'world'), |
| 307 |
'!')); |
| 308 |
c1.render(goog.dom.getElement('c1')); |
| 309 |
goog.events.listen(c1, EVENTS, logEvent); |
| 310 |
|
| 311 |
// Create the second control by decorating an existing element. We can |
| 312 |
// use goog.ui.decorate to get a Control instance and have it |
| 313 |
// decorate the element, since the element to be decorated has the |
| 314 |
// appropriate "marker" class name. |
| 315 |
var c2 = goog.ui.decorate(goog.dom.getElement('c2')); |
| 316 |
c2.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 317 |
goog.events.listen(c2, EVENTS, logEvent); |
| 318 |
|
| 319 |
// Define a simple custom renderer for the third control. |
| 320 |
function SampleCustomRenderer() { |
| 321 |
goog.ui.ControlRenderer.call(this); |
| 322 |
} |
| 323 |
goog.inherits(SampleCustomRenderer, goog.ui.ControlRenderer); |
| 324 |
|
| 325 |
// See goog.ui.ControlRenderer#createDom for documentation. |
| 326 |
SampleCustomRenderer.prototype.createDom = function(control) { |
| 327 |
var baseClass = this.getCssClass(); |
| 328 |
var classNames = this.getClassNamesForState(control.getState()); |
| 329 |
var dom = control.dom_; |
| 330 |
return dom.createDom(goog.dom.TagName.DIV, classNames.join(' '), |
| 331 |
dom.createDom(goog.dom.TagName.DIV, baseClass + '-outer-box', |
| 332 |
dom.createDom(goog.dom.TagName.DIV, baseClass + '-inner-box', |
| 333 |
dom.createDom(goog.dom.TagName.DIV, baseClass + '-content', |
| 334 |
control.getContent())))); |
| 335 |
}; |
| 336 |
|
| 337 |
// See goog.ui.ControlRenderer#decorate for documentation. |
| 338 |
SampleCustomRenderer.prototype.decorate = function(control, element) { |
| 339 |
var baseClass = this.getCssClass(); |
| 340 |
var dom = control.dom_; |
| 341 |
element.appendChild( |
| 342 |
dom.createDom(goog.dom.TagName.DIV, baseClass + '-outer-box', |
| 343 |
dom.createDom(goog.dom.TagName.DIV, baseClass + '-inner-box', |
| 344 |
dom.createDom(goog.dom.TagName.DIV, baseClass + '-content', |
| 345 |
element.childNodes)))); |
| 346 |
return SampleCustomRenderer.superClass_.decorate.call(this, control, |
| 347 |
element); |
| 348 |
}; |
| 349 |
|
| 350 |
// See goog.ui.ControlRenderer#getContent for documentation. |
| 351 |
SampleCustomRenderer.prototype.getContent = function(element) { |
| 352 |
if (element) { |
| 353 |
return SampleCustomRenderer.superClass_.getContent.call(this, |
| 354 |
goog.dom.getElementsByTagNameAndClass('div', |
| 355 |
this.getCssClass() + '-content', element)[0]); |
| 356 |
} |
| 357 |
return null; |
| 358 |
}; |
| 359 |
|
| 360 |
// See goog.ui.ControlRenderer#setContent for documentation. |
| 361 |
SampleCustomRenderer.prototype.setContent = function(element, content) { |
| 362 |
if (element) { |
| 363 |
return SampleCustomRenderer.superClass_.setContent.call(this, |
| 364 |
goog.dom.getElementsByTagNameAndClass('div', |
| 365 |
this.getCssClass() + '-content', element)[0], |
| 366 |
content); |
| 367 |
} |
| 368 |
}; |
| 369 |
|
| 370 |
// See goog.ui.ControlRenderer#getCssClass for documentation. |
| 371 |
SampleCustomRenderer.prototype.getCssClass = function() { |
| 372 |
return 'goog-custom-control'; |
| 373 |
}; |
| 374 |
|
| 375 |
// Create the singleton instance of our custom renderer. |
| 376 |
var customRenderer = new SampleCustomRenderer(); |
| 377 |
|
| 378 |
// Create the third control using the custom renderer. |
| 379 |
var c3 = new goog.ui.Control('Custom Renderer', customRenderer); |
| 380 |
c3.render(goog.dom.getElement('c3')); |
| 381 |
goog.events.listen(c3, EVENTS, logEvent); |
| 382 |
|
| 383 |
// Register a decorator factory function for controls to be decorated using |
| 384 |
// our custom renderer. |
| 385 |
goog.ui.registry.setDecoratorByClassName('goog-custom-control', function() { |
| 386 |
// Elements that have the 'goog-custom-control' marker class will be |
| 387 |
// decorated by instances of goog.ui.Control using our custom renderer. |
| 388 |
return new goog.ui.Control(null, customRenderer); |
| 389 |
}); |
| 390 |
|
| 391 |
// Create the fourth control by decorating a DIV. |
| 392 |
var customElem = goog.dom.getElement('c4'); |
| 393 |
// getDecorator will use the decorator factory function registered above to |
| 394 |
// create a control to decorate our element. |
| 395 |
var c4 = goog.ui.registry.getDecorator(customElem); |
| 396 |
c4.addClassName('extra-red'); |
| 397 |
c4.addClassName('extra-thick-border'); |
| 398 |
c4.decorate(customElem); |
| 399 |
goog.events.listen(c4, EVENTS, logEvent); |
| 400 |
|
| 401 |
var c5 = new goog.ui.Control('Custom Red'); |
| 402 |
c5.addClassName('extra-red'); |
| 403 |
c5.render(goog.dom.getElement('c5')); |
| 404 |
goog.events.listen(c5, EVENTS, logEvent); |
| 405 |
|
| 406 |
var c6 = new goog.ui.Control('Custom Blue'); |
| 407 |
c6.addClassName('extra-blue'); |
| 408 |
c6.render(goog.dom.getElement('c6')); |
| 409 |
c6.addClassName('extra-thick-border'); |
| 410 |
goog.events.listen(c6, EVENTS, logEvent); |
| 411 |
|
| 412 |
// BiDi examples: |
| 413 |
var c7 = new goog.ui.Control('Hello, world'); |
| 414 |
c7.addClassName('goog-inline-block'); |
| 415 |
c7.render(goog.dom.getElement('bidi')); |
| 416 |
goog.events.listen(c7, EVENTS, logEvent); |
| 417 |
|
| 418 |
var c8 = new goog.ui.Control('Sample control'); |
| 419 |
c8.addClassName('goog-inline-block'); |
| 420 |
c8.render(goog.dom.getElement('bidi')); |
| 421 |
goog.events.listen(c8, EVENTS, logEvent); |
| 422 |
|
| 423 |
var c9 = goog.ui.decorate(goog.dom.getElement('c9')); |
| 424 |
goog.events.listen(c9, EVENTS, logEvent); |
| 425 |
|
| 426 |
var c10 = goog.ui.decorate(goog.dom.getElement('c10')); |
| 427 |
goog.events.listen(c10, EVENTS, logEvent); |
| 428 |
|
| 429 |
// Add some event handling to the second control, which dispatches state |
| 430 |
// transition events. |
| 431 |
|
| 432 |
goog.events.listen(c2, goog.ui.Component.EventType.HIGHLIGHT, |
| 433 |
function(e) { |
| 434 |
goog.dom.setTextContent(goog.dom.getElement('hint'), |
| 435 |
'Click to activate.'); |
| 436 |
}); |
| 437 |
|
| 438 |
goog.events.listen(c2, [goog.ui.Component.EventType.ENABLE, |
| 439 |
goog.ui.Component.EventType.UNHIGHLIGHT], |
| 440 |
function(e) { |
| 441 |
goog.dom.setTextContent(goog.dom.getElement('hint'), |
| 442 |
'Mouse over for instructions.'); |
| 443 |
}); |
| 444 |
|
| 445 |
goog.events.listen(c2, goog.ui.Component.EventType.DISABLE, |
| 446 |
function(e) { |
| 447 |
goog.dom.setTextContent(goog.dom.getElement('hint'), |
| 448 |
'You need to enable this component first.'); |
| 449 |
}); |
| 450 |
|
| 451 |
goog.events.listen(c2, goog.ui.Component.EventType.ACTIVATE, |
| 452 |
function(e) { |
| 453 |
goog.dom.setTextContent(goog.dom.getElement('hint'), |
| 454 |
'Release the mouse button to deactivate.'); |
| 455 |
}); |
| 456 |
|
| 457 |
goog.events.listen(c2, goog.ui.Component.EventType.DEACTIVATE, |
| 458 |
function(e) { |
| 459 |
goog.dom.setTextContent(goog.dom.getElement('hint'), 'You got it!'); |
| 460 |
}); |
| 461 |
|
| 462 |
goog.events.listen(goog.dom.getElement('enable'), |
| 463 |
goog.events.EventType.CLICK, |
| 464 |
function(e) { |
| 465 |
c2.setEnabled(e.target.checked); |
| 466 |
}); |
| 467 |
|
| 468 |
goog.events.listen(goog.dom.getElement('visible'), |
| 469 |
goog.events.EventType.CLICK, |
| 470 |
function(e) { |
| 471 |
c2.setVisible(e.target.checked); |
| 472 |
}); |
| 473 |
|
| 474 |
goog.dom.setTextContent(goog.dom.getElement('perf'), |
| 475 |
(goog.now() - timer) + 'ms'); |
| 476 |
</script> |
| 477 |
</body> |
| 478 |
</html> |