| 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> |
| 11 |
goog.ui.ImagelessButtonRenderer Demo |
| 12 |
</title> |
| 13 |
<script src="../base.js"></script> |
| 14 |
<script> |
| 15 |
goog.require('goog.array'); |
| 16 |
goog.require('goog.debug.DivConsole'); |
| 17 |
goog.require('goog.debug.LogManager'); |
| 18 |
goog.require('goog.events'); |
| 19 |
goog.require('goog.log'); |
| 20 |
goog.require('goog.object'); |
| 21 |
goog.require('goog.ui.CustomButton'); |
| 22 |
goog.require('goog.ui.ImagelessButtonRenderer'); |
| 23 |
goog.require('goog.ui.ToggleButton'); |
| 24 |
goog.require('goog.ui.decorate'); |
| 25 |
goog.require('goog.dom.TagName'); |
| 26 |
</script> |
| 27 |
<link rel="stylesheet" href="css/demo.css"> |
| 28 |
<link rel="stylesheet" href="../css/imagelessbutton.css"> |
| 29 |
<style> |
| 30 |
/* Base class for all icon elements. */ |
| 31 |
.icon { |
| 32 |
height: 16px; |
| 33 |
width: 16px; |
| 34 |
margin: 0 4px 0 0; |
| 35 |
background-image: url(../images/toolbar_icons.gif); |
| 36 |
background-repeat: no-repeat; |
| 37 |
vertical-align: middle; |
| 38 |
} |
| 39 |
|
| 40 |
/* "Highlight" icon. */ |
| 41 |
.highlight-icon{ |
| 42 |
background-position: -64px; |
| 43 |
} |
| 44 |
|
| 45 |
/* "Insert Image" icon. */ |
| 46 |
.insert-image-icon { |
| 47 |
background-position: -80px; |
| 48 |
} |
| 49 |
|
| 50 |
/* Custom style for the "default" button. */ |
| 51 |
.default-button { |
| 52 |
font-weight: bold; |
| 53 |
} |
| 54 |
</style> |
| 55 |
</head> |
| 56 |
<body> |
| 57 |
<h1>goog.ui.ImagelessButtonRenderer</h1> |
| 58 |
<fieldset> |
| 59 |
<legend> |
| 60 |
These buttons were rendered using |
| 61 |
<strong>goog.ui.ImagelessButtonRenderer</strong>: |
| 62 |
</legend> |
| 63 |
<br/> |
| 64 |
These buttons were created programmatically:<br/> |
| 65 |
<div id="cb1"></div> |
| 66 |
<br/> |
| 67 |
These buttons were created by decorating some DIVs, and they dispatch |
| 68 |
state transition events (watch the event log):<br/> |
| 69 |
<div id="cb2"> |
| 70 |
<!-- On FF2, if you don't enclose the contents of the button in a SPAN, |
| 71 |
the bold element isn't rendered. Works on every other browser. --> |
| 72 |
<div id="foo" class="goog-imageless-button" |
| 73 |
title="Title specified in HTML"> |
| 74 |
<span>Decorated <b>Button</b>, yay!</span> |
| 75 |
</div> |
| 76 |
<div id="bar" class="goog-imageless-button goog-imageless-button-disabled" |
| 77 |
title="Initialized to DISABLED in HTML...">Decorated Disabled</div> |
| 78 |
<div id="fee" class="goog-imageless-button">Another Button</div> |
| 79 |
<div id="btn1" |
| 80 |
class="goog-imageless-button goog-imageless-button-collapse-right"> |
| 81 |
Archive |
| 82 |
</div><div id="btn2" |
| 83 |
class="goog-imageless-button goog-imageless-button-collapse-right goog-imageless-button-collapse-left"> |
| 84 |
Delete |
| 85 |
</div><div id="btn3" |
| 86 |
class="goog-imageless-button goog-imageless-button-collapse-left"> |
| 87 |
Report Spam |
| 88 |
</div> |
| 89 |
</div> |
| 90 |
<br/> |
| 91 |
Use these <strong>ToggleButton</strong>s to hide/show and enable/disable |
| 92 |
the middle button:<br/> |
| 93 |
<div id="toggleEnable" class="goog-imageless-toggle-button" |
| 94 |
title="Click here to enable/disable the button above">Enable</div> |
| 95 |
|
| 96 |
<div id="hideShow" |
| 97 |
class="goog-imageless-toggle-button goog-imageless-button-checked" |
| 98 |
title="Click here to hide/show the button above">Show</div> |
| 99 |
|
| 100 |
<br/><br/> |
| 101 |
Combined toggle buttons<br/> |
| 102 |
<div id="btn4" class="goog-imageless-toggle-button goog-imageless-button-collapse-right"> |
| 103 |
Bold |
| 104 |
</div><div id="btn5" class="goog-imageless-toggle-button goog-imageless-button-collapse-right goog-imageless-button-collapse-left"> |
| 105 |
Italics |
| 106 |
</div><div id="btn6" class="goog-imageless-toggle-button goog-imageless-button-collapse-left goog-imageless-button-checked"> |
| 107 |
Underlined |
| 108 |
</div> |
| 109 |
<br/><br/> |
| 110 |
These buttons have icons, and the second one has an extra CSS class:<br/> |
| 111 |
<div id="iconbuttons"></div> |
| 112 |
<br/> |
| 113 |
</fieldset> |
| 114 |
<br/> |
| 115 |
<div id="perf"></div> |
| 116 |
<!-- Event log. --> |
| 117 |
<fieldset class="goog-debug-panel"> |
| 118 |
<legend>Event Log</legend> |
| 119 |
<div id="log"></div> |
| 120 |
</fieldset> |
| 121 |
|
| 122 |
<script type="text/javascript"> |
| 123 |
var timer = goog.now(); |
| 124 |
|
| 125 |
// Set up a logger. |
| 126 |
goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL); |
| 127 |
var logger = goog.log.getLogger('demo'); |
| 128 |
var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log')); |
| 129 |
logconsole.setCapturing(true); |
| 130 |
|
| 131 |
var EVENTS = goog.object.getValues(goog.ui.Component.EventType); |
| 132 |
goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.'); |
| 133 |
|
| 134 |
function logEvent(e) { |
| 135 |
goog.log.info(logger, '"' + e.target.getCaption() + '" dispatched: ' + e.type); |
| 136 |
} |
| 137 |
|
| 138 |
// Create some buttons using CustomButton with the |
| 139 |
// ImagelessButtonRenderer renderer. |
| 140 |
var disabledButton; |
| 141 |
var customButtons = [ |
| 142 |
new goog.ui.CustomButton('Button', |
| 143 |
goog.ui.ImagelessButtonRenderer.getInstance()), |
| 144 |
new goog.ui.CustomButton('Another Button', |
| 145 |
goog.ui.ImagelessButtonRenderer.getInstance()), |
| 146 |
disabledButton = new goog.ui.CustomButton('Disabled Button', |
| 147 |
goog.ui.ImagelessButtonRenderer.getInstance()), |
| 148 |
new goog.ui.CustomButton('Yet Another Button', |
| 149 |
goog.ui.ImagelessButtonRenderer.getInstance()) |
| 150 |
]; |
| 151 |
disabledButton.setEnabled(false); |
| 152 |
goog.array.forEach(customButtons, function(b) { |
| 153 |
b.render(goog.dom.getElement('cb1')); |
| 154 |
goog.events.listen(b, goog.ui.Component.EventType.ACTION, |
| 155 |
function(e) { |
| 156 |
var newCaption = window.prompt('Enter new caption for button:'); |
| 157 |
b.setCaption(newCaption || 'Empty'); |
| 158 |
}); |
| 159 |
goog.events.listen(b, EVENTS, logEvent); |
| 160 |
}); |
| 161 |
|
| 162 |
// Decorate some buttons. |
| 163 |
var cb2 = []; |
| 164 |
var decoratedButtons = goog.array.map([ |
| 165 |
'foo', 'bar', 'fee', 'btn1', 'btn2', 'btn3', 'btn4', 'btn5', 'btn6' |
| 166 |
], goog.dom.getElement); |
| 167 |
goog.array.forEach(decoratedButtons, function(element) { |
| 168 |
// Since the elements to be decorated each have the correct "marker" CSS |
| 169 |
// class ("goog-imageless-button"), we can use the renderer |
| 170 |
// registry to get the appropriate control instance to decorate them. |
| 171 |
var button = goog.ui.decorate(element); |
| 172 |
|
| 173 |
button.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 174 |
cb2.push(button); |
| 175 |
goog.events.listen(button, EVENTS, logEvent); |
| 176 |
}); |
| 177 |
|
| 178 |
// Decorate toggle buttons. |
| 179 |
var toggleEnableElem = goog.dom.getElement('toggleEnable'); |
| 180 |
var toggleEnable = goog.ui.decorate(toggleEnableElem); |
| 181 |
toggleEnable.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 182 |
|
| 183 |
goog.events.listen(toggleEnable, EVENTS, logEvent); |
| 184 |
|
| 185 |
goog.events.listen(toggleEnable, goog.ui.Component.EventType.ACTION, |
| 186 |
function(e) { |
| 187 |
cb2[1].setEnabled(e.target.isChecked()); |
| 188 |
}); |
| 189 |
|
| 190 |
var hideShowElem = goog.dom.getElement('hideShow'); |
| 191 |
var hideShow = new goog.ui.decorate(hideShowElem); |
| 192 |
hideShow.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 193 |
goog.events.listen(hideShow, EVENTS, logEvent); |
| 194 |
|
| 195 |
goog.events.listen(hideShow, goog.ui.Component.EventType.ACTION, |
| 196 |
function(e) { |
| 197 |
cb2[1].setVisible(e.target.isChecked()); |
| 198 |
}); |
| 199 |
|
| 200 |
|
| 201 |
// Use a DIV with a background image as the icon, and a SPAN as the caption. |
| 202 |
var iconbutton1 = new goog.ui.ToggleButton([ |
| 203 |
goog.dom.createDom(goog.dom.TagName.DIV, 'icon insert-image-icon goog-inline-block'), |
| 204 |
'Insert Image' |
| 205 |
], goog.ui.ImagelessButtonRenderer.getInstance()); |
| 206 |
iconbutton1.render(goog.dom.getElement('iconbuttons')); |
| 207 |
goog.events.listen(iconbutton1, EVENTS, logEvent); |
| 208 |
|
| 209 |
// Add a custom style, too. |
| 210 |
var iconbutton2 = new goog.ui.ToggleButton([ |
| 211 |
goog.dom.createDom(goog.dom.TagName.DIV, 'icon highlight-icon goog-inline-block'), |
| 212 |
'Highlight Text' |
| 213 |
], goog.ui.ImagelessButtonRenderer.getInstance()); |
| 214 |
iconbutton2.addClassName('default-button'); |
| 215 |
iconbutton2.render(goog.dom.getElement('iconbuttons')); |
| 216 |
goog.events.listen(iconbutton2, EVENTS, logEvent); |
| 217 |
|
| 218 |
goog.dom.setTextContent(goog.dom.getElement('perf'), |
| 219 |
(goog.now() - timer) + 'ms'); |
| 220 |
</script> |
| 221 |
</body> |
| 222 |
</html> |