| 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.Button and goog.ui.ToggleButton</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.Button'); |
| 21 |
goog.require('goog.ui.ButtonRenderer'); |
| 22 |
goog.require('goog.ui.ButtonSide'); |
| 23 |
goog.require('goog.ui.CustomButton'); |
| 24 |
goog.require('goog.ui.CustomButtonRenderer'); |
| 25 |
goog.require('goog.ui.FlatButtonRenderer'); |
| 26 |
goog.require('goog.ui.LinkButtonRenderer'); |
| 27 |
goog.require('goog.ui.ToggleButton'); |
| 28 |
goog.require('goog.ui.decorate'); |
| 29 |
goog.require('goog.dom.TagName'); |
| 30 |
</script> |
| 31 |
<link rel="stylesheet" href="css/demo.css"> |
| 32 |
<link rel="stylesheet" href="../css/button.css"> |
| 33 |
<link rel="stylesheet" href="../css/custombutton.css"> |
| 34 |
<link rel="stylesheet" href="../css/flatbutton.css"> |
| 35 |
<link rel="stylesheet" href="../css/linkbutton.css"> |
| 36 |
<style> |
| 37 |
/* Base class for all icon elements. */ |
| 38 |
.icon { |
| 39 |
height: 16px; |
| 40 |
width: 16px; |
| 41 |
margin: 0 1px; |
| 42 |
background-image: url(../images/toolbar_icons.gif); |
| 43 |
background-repeat: no-repeat; |
| 44 |
vertical-align: middle; |
| 45 |
} |
| 46 |
|
| 47 |
/* "Highlight" icon. */ |
| 48 |
.highlight-icon{ |
| 49 |
background-position: -64px; |
| 50 |
} |
| 51 |
|
| 52 |
/* "Insert Image" icon. */ |
| 53 |
.insert-image-icon { |
| 54 |
background-position: -80px; |
| 55 |
} |
| 56 |
|
| 57 |
/* Custom style for the "default" button. */ |
| 58 |
.default-button { |
| 59 |
font-weight: bold; |
| 60 |
} |
| 61 |
</style> |
| 62 |
</head> |
| 63 |
<body> |
| 64 |
<h1>goog.ui.Button</h1> |
| 65 |
<fieldset> |
| 66 |
<legend> |
| 67 |
The first <strong>Button</strong> was created programmatically, |
| 68 |
the second by decorating an <strong><input></strong> element: |
| 69 |
</legend> |
| 70 |
<div id="b1"></div> |
| 71 |
<label> |
| 72 |
Enable button:<input type="checkbox" id="b1_enable" checked/> |
| 73 |
</label> |
| 74 |
<br/> |
| 75 |
<br/> |
| 76 |
<input type="button" id="b2" title="Tooltip extracted from title" |
| 77 |
value="My Button" disabled="true"/><br/> |
| 78 |
<label>Enable button:<input type="checkbox" id="b2_enable"/></label> |
| 79 |
<br/> |
| 80 |
</fieldset> |
| 81 |
<br/> |
| 82 |
|
| 83 |
<h2>goog.ui.FlatButtonRenderer</h2> |
| 84 |
<fieldset> |
| 85 |
<legend> |
| 86 |
Buttons made with <strong><div></strong>'s instead of |
| 87 |
<strong><input></strong>'s or <strong><button></strong>'s |
| 88 |
The first rendered, the second decorated: |
| 89 |
</legend> |
| 90 |
<div id="fb1"></div> |
| 91 |
<label> |
| 92 |
Enable button:<input type="checkbox" id="fb1_enable" checked/> |
| 93 |
</label> |
| 94 |
<br/> |
| 95 |
<br/> |
| 96 |
<div id="fb2" class="goog-flat-button goog-flat-button-disabled" |
| 97 |
title="Tooltip extracted from title">My Flat Button</div><br/> |
| 98 |
<label>Enable button:<input type="checkbox" id="fb2_enable"/></label> |
| 99 |
<br/> |
| 100 |
<br/> |
| 101 |
<div id="fb3" class="goog-flat-button goog-flat-button-collapse-right"> |
| 102 |
Combined |
| 103 |
</div><div id="fb4" class="goog-flat-button goog-flat-button-collapse-left"> |
| 104 |
Buttons |
| 105 |
</div> |
| 106 |
<br/> |
| 107 |
</fieldset> |
| 108 |
<br/> |
| 109 |
|
| 110 |
<h2>goog.ui.LinkButtonRenderer</h2> |
| 111 |
<fieldset> |
| 112 |
<legend> |
| 113 |
Like FlatButtonRenderer, except the style makes the button appear to be a |
| 114 |
link. |
| 115 |
</legend> |
| 116 |
<div id="lb"></div> |
| 117 |
<label> |
| 118 |
Enable button:<input type="checkbox" id="lb_enable" checked/> |
| 119 |
</label> |
| 120 |
</fieldset> |
| 121 |
|
| 122 |
<h2>goog.ui.CustomButton & goog.ui.ToggleButton</h2> |
| 123 |
<fieldset> |
| 124 |
<legend> |
| 125 |
These buttons were rendered using <strong>goog.ui.CustomButton</strong>: |
| 126 |
|
| 127 |
</legend> |
| 128 |
<br/> |
| 129 |
These buttons were created programmatically:<br/> |
| 130 |
<div id="cb1"></div> |
| 131 |
<br/> |
| 132 |
These buttons were created by decorating some DIVs, and they dispatch |
| 133 |
state transition events (watch the event log):<br/> |
| 134 |
<div id="cb2"> |
| 135 |
<!-- On FF2, if you don't enclose the contents of the button in a SPAN, |
| 136 |
the bold element isn't rendered. Works on every other browser. --> |
| 137 |
<div id="foo" class="goog-custom-button" title="Title specified in HTML"> |
| 138 |
<span>Decorated <b>Button</b>, yay!</span> |
| 139 |
</div> |
| 140 |
<div id="bar" class="goog-custom-button goog-custom-button-disabled" |
| 141 |
title="Initialized to DISABLED in HTML...">Decorated Disabled</div> |
| 142 |
<div id="fee" class="goog-custom-button">Another Button</div> |
| 143 |
<div id="btn1" |
| 144 |
class="goog-custom-button goog-custom-button-collapse-right"> |
| 145 |
Archive |
| 146 |
</div><div id="btn2" class="goog-custom-button goog-custom-button-collapse-right goog-custom-button-collapse-left"> |
| 147 |
Delete |
| 148 |
</div><div id="btn3" |
| 149 |
class="goog-custom-button goog-custom-button-collapse-left"> |
| 150 |
Report Spam |
| 151 |
</div> |
| 152 |
</div> |
| 153 |
<br/> |
| 154 |
Use these <strong>ToggleButton</strong>s to hide/show and enable/disable |
| 155 |
the middle button:<br/> |
| 156 |
<div id="toggleEnable" class="goog-toggle-button" |
| 157 |
title="Click here to enable/disable the button above">Enable</div> |
| 158 |
|
| 159 |
<div id="hideShow" |
| 160 |
class="goog-toggle-button goog-custom-button-checked" |
| 161 |
title="Click here to hide/show the button above">Show</div> |
| 162 |
<br/><br/> |
| 163 |
Combined toggle buttons:<br/> |
| 164 |
<div id="btn4" class="goog-toggle-button goog-custom-button-collapse-right"> |
| 165 |
Bold |
| 166 |
</div><div id="btn5" class="goog-toggle-button goog-custom-button-collapse-right goog-custom-button-collapse-left"> |
| 167 |
Italics |
| 168 |
</div><div id="btn6" class="goog-toggle-button goog-custom-button-collapse-left goog-custom-button-checked"> |
| 169 |
Underlined |
| 170 |
</div> |
| 171 |
<br/> |
| 172 |
<br/> |
| 173 |
These buttons have icons, and the second one has an extra CSS class:<br/> |
| 174 |
<div id="iconbuttons"></div> |
| 175 |
<br/> |
| 176 |
<span class="hint"> |
| 177 |
The button with the <span style="font-weight:bold;color:orange;">orange |
| 178 |
outline</span> has keyboard focus. Hit Enter to activate focused buttons. |
| 179 |
</span> |
| 180 |
</fieldset> |
| 181 |
<br/> |
| 182 |
<div id="perf"></div> |
| 183 |
<!-- Event log. --> |
| 184 |
<fieldset class="goog-debug-panel"> |
| 185 |
<legend>Event Log</legend> |
| 186 |
<div id="log"></div> |
| 187 |
</fieldset> |
| 188 |
<script type="text/javascript"> |
| 189 |
var timer = goog.now(); |
| 190 |
|
| 191 |
// Set up a logger. |
| 192 |
goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL); |
| 193 |
var logger = goog.log.getLogger('demo'); |
| 194 |
var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log')); |
| 195 |
logconsole.setCapturing(true); |
| 196 |
|
| 197 |
var EVENTS = goog.object.getValues(goog.ui.Component.EventType); |
| 198 |
goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.'); |
| 199 |
|
| 200 |
function logEvent(e) { |
| 201 |
goog.log.info(logger, '"' + e.target.getCaption() + '" dispatched: ' + e.type); |
| 202 |
} |
| 203 |
|
| 204 |
// Create the first button programmatically. |
| 205 |
var b1 = new goog.ui.Button('Hello!'); |
| 206 |
b1.render(goog.dom.getElement('b1')); |
| 207 |
b1.setTooltip('I changed the tooltip using setTooltip() ' + |
| 208 |
'after the button was rendered.'); |
| 209 |
goog.events.listen(b1, EVENTS, logEvent); |
| 210 |
|
| 211 |
goog.events.listen(goog.dom.getElement('b1_enable'), |
| 212 |
goog.events.EventType.CLICK, |
| 213 |
function(e) { |
| 214 |
b1.setEnabled(e.target.checked); |
| 215 |
}); |
| 216 |
|
| 217 |
goog.events.listen(b1, goog.ui.Component.EventType.ACTION, |
| 218 |
function(e) { |
| 219 |
var newCaption = window.prompt('Enter new caption for button:'); |
| 220 |
b1.setCaption(newCaption || 'Empty'); |
| 221 |
}); |
| 222 |
|
| 223 |
// Create the second button by decorating an element. |
| 224 |
var b2 = new goog.ui.Button(); |
| 225 |
b2.decorate(goog.dom.getElement('b2')); |
| 226 |
goog.events.listen(b2, EVENTS, logEvent); |
| 227 |
|
| 228 |
goog.events.listen(goog.dom.getElement('b2_enable'), |
| 229 |
goog.events.EventType.CLICK, |
| 230 |
function(e) { |
| 231 |
b2.setEnabled(e.target.checked); |
| 232 |
}); |
| 233 |
|
| 234 |
goog.events.listen(b2, goog.ui.Component.EventType.ACTION, |
| 235 |
function(e) { |
| 236 |
alert('The value of the button is: ' + b2.getValue()); |
| 237 |
}); |
| 238 |
|
| 239 |
|
| 240 |
// Create flat buttons that use divs instead of button or input elements. |
| 241 |
// Render 1st flat button. |
| 242 |
var fb1 = new goog.ui.Button('Hello!', |
| 243 |
goog.ui.FlatButtonRenderer.getInstance()); |
| 244 |
fb1.render(goog.dom.getElement('fb1')); |
| 245 |
fb1.setTooltip('I changed the tooltip using setTooltip() ' + |
| 246 |
'after the button was rendered.'); |
| 247 |
goog.events.listen(fb1, EVENTS, logEvent); |
| 248 |
|
| 249 |
goog.events.listen(goog.dom.getElement('fb1_enable'), |
| 250 |
goog.events.EventType.CLICK, |
| 251 |
function(e) { |
| 252 |
fb1.setEnabled(e.target.checked); |
| 253 |
}); |
| 254 |
|
| 255 |
goog.events.listen(fb1, goog.ui.Component.EventType.ACTION, |
| 256 |
function(e) { |
| 257 |
var newCaption = window.prompt('Enter new caption for button:'); |
| 258 |
fb1.setCaption(newCaption || 'Empty'); |
| 259 |
}); |
| 260 |
|
| 261 |
// Decorate 2nd flat button. |
| 262 |
var fb2 = goog.ui.decorate(goog.dom.getElement('fb2')); |
| 263 |
goog.events.listen(fb2, EVENTS, logEvent); |
| 264 |
|
| 265 |
goog.events.listen(goog.dom.getElement('fb2_enable'), |
| 266 |
goog.events.EventType.CLICK, |
| 267 |
function(e) { |
| 268 |
fb2.setEnabled(e.target.checked); |
| 269 |
}); |
| 270 |
|
| 271 |
goog.events.listen(fb2, goog.ui.Component.EventType.ACTION, |
| 272 |
function(e) { |
| 273 |
alert('The caption of the button is: ' + fb2.getCaption()); |
| 274 |
}); |
| 275 |
|
| 276 |
// Decorate 3rd and 4th flat buttons. |
| 277 |
goog.ui.decorate(goog.dom.getElement('fb3')); |
| 278 |
goog.ui.decorate(goog.dom.getElement('fb4')); |
| 279 |
|
| 280 |
// Create buttons that look like links. |
| 281 |
var lb = new goog.ui.Button('Hello!', |
| 282 |
goog.ui.LinkButtonRenderer.getInstance()); |
| 283 |
lb.render(goog.dom.getElement('lb')); |
| 284 |
lb.setTooltip('I changed the tooltip using setTooltip() ' + |
| 285 |
'after the button was rendered.'); |
| 286 |
goog.events.listen(lb, EVENTS, logEvent); |
| 287 |
|
| 288 |
goog.events.listen(goog.dom.getElement('lb_enable'), |
| 289 |
goog.events.EventType.CLICK, |
| 290 |
function(e) { |
| 291 |
lb.setEnabled(e.target.checked); |
| 292 |
}); |
| 293 |
|
| 294 |
goog.events.listen(lb, goog.ui.Component.EventType.ACTION, |
| 295 |
function(e) { |
| 296 |
var newCaption = window.prompt('Enter new caption for button:'); |
| 297 |
lb.setCaption(newCaption || 'Empty'); |
| 298 |
}); |
| 299 |
|
| 300 |
// Create some custom buttons. |
| 301 |
var disabledButton, leftButton, centerButton, rightButton; |
| 302 |
var customButtons = [ |
| 303 |
new goog.ui.CustomButton('Button'), |
| 304 |
new goog.ui.CustomButton('Another Button'), |
| 305 |
disabledButton = new goog.ui.CustomButton('Disabled Button'), |
| 306 |
new goog.ui.CustomButton('Yet Another Button'), |
| 307 |
leftButton = new goog.ui.CustomButton('Left'), |
| 308 |
centerButton = new goog.ui.CustomButton('Center'), |
| 309 |
rightButton = new goog.ui.CustomButton('Right'), |
| 310 |
]; |
| 311 |
disabledButton.setEnabled(false); |
| 312 |
leftButton.setCollapsed(goog.ui.ButtonSide.END); |
| 313 |
centerButton.setCollapsed(goog.ui.ButtonSide.BOTH); |
| 314 |
rightButton.setCollapsed(goog.ui.ButtonSide.START); |
| 315 |
goog.array.forEach(customButtons, function(b) { |
| 316 |
b.render(goog.dom.getElement('cb1')); |
| 317 |
goog.events.listen(b, goog.ui.Component.EventType.ACTION, |
| 318 |
function(e) { |
| 319 |
var newCaption = window.prompt('Enter new caption for button:'); |
| 320 |
b.setCaption(newCaption || 'Empty'); |
| 321 |
}); |
| 322 |
goog.events.listen(b, EVENTS, logEvent); |
| 323 |
}); |
| 324 |
|
| 325 |
// Decorate some custom buttons. |
| 326 |
var cb2 = []; |
| 327 |
var decoratedButtons = goog.array.map( |
| 328 |
['foo', 'bar', 'fee', 'btn1', 'btn2', 'btn3'], |
| 329 |
goog.dom.getElement); |
| 330 |
goog.array.forEach(decoratedButtons, function(element) { |
| 331 |
// Since the elements to be decorated each have the correct "marker" CSS |
| 332 |
// class ("goog-custom-button"), we can use the renderer registry to get |
| 333 |
// the appropriate control instance to decorate them. |
| 334 |
var button = goog.ui.decorate(element); |
| 335 |
button.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 336 |
cb2.push(button); |
| 337 |
goog.events.listen(button, EVENTS, logEvent); |
| 338 |
}); |
| 339 |
|
| 340 |
// Decorate toggle buttons. |
| 341 |
var toggleEnableElem = goog.dom.getElement('toggleEnable'); |
| 342 |
var toggleEnable = goog.ui.decorate(toggleEnableElem); |
| 343 |
toggleEnable.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 344 |
goog.events.listen(toggleEnable, EVENTS, logEvent); |
| 345 |
|
| 346 |
goog.events.listen(toggleEnable, goog.ui.Component.EventType.ACTION, |
| 347 |
function(e) { |
| 348 |
cb2[1].setEnabled(e.target.isChecked()); |
| 349 |
}); |
| 350 |
|
| 351 |
var hideShowElem = goog.dom.getElement('hideShow'); |
| 352 |
var hideShow = new goog.ui.decorate(hideShowElem); |
| 353 |
hideShow.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 354 |
goog.events.listen(hideShow, EVENTS, logEvent); |
| 355 |
|
| 356 |
goog.events.listen(hideShow, goog.ui.Component.EventType.ACTION, |
| 357 |
function(e) { |
| 358 |
cb2[1].setVisible(e.target.isChecked()); |
| 359 |
}); |
| 360 |
|
| 361 |
// Decorate combined toggle buttons. |
| 362 |
var cb3 = []; |
| 363 |
var combinedButtons = goog.array.map(['btn4', 'btn5', 'btn6'], |
| 364 |
goog.dom.getElement); |
| 365 |
goog.array.forEach(combinedButtons, function(element) { |
| 366 |
var button = goog.ui.decorate(element); |
| 367 |
button.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 368 |
cb3.push(button); |
| 369 |
goog.events.listen(button, EVENTS, logEvent); |
| 370 |
}); |
| 371 |
|
| 372 |
|
| 373 |
// Use a DIV with a background image as the icon, and a SPAN as the caption. |
| 374 |
var iconbutton1 = new goog.ui.ToggleButton([ |
| 375 |
goog.dom.createDom(goog.dom.TagName.DIV, 'icon insert-image-icon goog-inline-block'), |
| 376 |
goog.dom.createDom(goog.dom.TagName.SPAN, {'style': 'vertical-align:middle'}, |
| 377 |
'Insert Image') |
| 378 |
]); |
| 379 |
iconbutton1.render(goog.dom.getElement('iconbuttons')); |
| 380 |
goog.events.listen(iconbutton1, EVENTS, logEvent); |
| 381 |
|
| 382 |
// Add a custom style, too. |
| 383 |
var iconbutton2 = new goog.ui.ToggleButton([ |
| 384 |
goog.dom.createDom(goog.dom.TagName.DIV, 'icon highlight-icon goog-inline-block'), |
| 385 |
goog.dom.createDom(goog.dom.TagName.SPAN, {'style': 'vertical-align:middle'}, |
| 386 |
'Highlight Text') |
| 387 |
]); |
| 388 |
iconbutton2.addClassName('default-button'); |
| 389 |
iconbutton2.render(goog.dom.getElement('iconbuttons')); |
| 390 |
goog.events.listen(iconbutton2, EVENTS, logEvent); |
| 391 |
|
| 392 |
goog.dom.setTextContent(goog.dom.getElement('perf'), |
| 393 |
(goog.now() - timer) + 'ms'); |
| 394 |
</script> |
| 395 |
</body> |
| 396 |
</html> |