| 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.ImagelessMenuButtonRenderer 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.ImagelessMenuButtonRenderer'); |
| 21 |
goog.require('goog.ui.Menu'); |
| 22 |
goog.require('goog.ui.MenuButton'); |
| 23 |
goog.require('goog.ui.MenuItem'); |
| 24 |
goog.require('goog.ui.Separator'); |
| 25 |
goog.require('goog.ui.decorate'); |
| 26 |
</script> |
| 27 |
<link rel="stylesheet" href="css/demo.css"> |
| 28 |
<link rel="stylesheet" href="../css/menu.css"> |
| 29 |
<link rel="stylesheet" href="../css/menuitem.css"> |
| 30 |
<link rel="stylesheet" href="../css/menuseparator.css"> |
| 31 |
<link rel="stylesheet" href="../css/menubutton.css"> |
| 32 |
<link rel="stylesheet" href="../css/imagelessbutton.css"> |
| 33 |
<link rel="stylesheet" href="../css/imagelessmenubutton.css"> |
| 34 |
<style> |
| 35 |
/* Base class for all icon elements. */ |
| 36 |
.icon { |
| 37 |
height: 16px; |
| 38 |
width: 16px; |
| 39 |
margin: 0 1px; |
| 40 |
background-image: url(../images/toolbar_icons.gif); |
| 41 |
background-repeat: no-repeat; |
| 42 |
vertical-align: middle; |
| 43 |
} |
| 44 |
|
| 45 |
/* "Format" icon. */ |
| 46 |
.format-icon{ |
| 47 |
background-position: -64px; |
| 48 |
} |
| 49 |
</style> |
| 50 |
</head> |
| 51 |
<body> |
| 52 |
<h1>goog.ui.ImagelessMenuButtonRenderer</h1> |
| 53 |
<table border="0" cellpadding="0" cellspacing="4" width="100%"> |
| 54 |
<tbody> |
| 55 |
<tr valign="top"> |
| 56 |
<td width="67%"> |
| 57 |
<fieldset> |
| 58 |
<legend> |
| 59 |
These <strong>MenuButton</strong>s were created programmatically: |
| 60 |
|
| 61 |
</legend> |
| 62 |
<table border="0" cellpadding="0" cellspacing="4"> |
| 63 |
<tbody> |
| 64 |
<tr valign="middle"> |
| 65 |
<td> |
| 66 |
<div id="menuButtons"></div> |
| 67 |
</td> |
| 68 |
<td> |
| 69 |
Enable first button: |
| 70 |
<input type="checkbox" id="b1_enable" checked> |
| 71 |
|
| 72 |
Show second button: |
| 73 |
<input type="checkbox" id="b2_show" checked> |
| 74 |
|
| 75 |
</td> |
| 76 |
</tr> |
| 77 |
</tbody> |
| 78 |
</table> |
| 79 |
<label> |
| 80 |
</label> |
| 81 |
<br> |
| 82 |
</fieldset> |
| 83 |
<fieldset> |
| 84 |
<legend> |
| 85 |
This <strong>MenuButton</strong> decorates an element: |
| 86 |
</legend> |
| 87 |
<table border="0" cellpadding="0" cellspacing="4"> |
| 88 |
<tbody> |
| 89 |
<tr valign="middle"> |
| 90 |
<td> |
| 91 |
<div id="formatButton" class="goog-imageless-menu-button" |
| 92 |
title="Format"> |
| 93 |
<!-- These elements will become the button's caption. --> |
| 94 |
<div class="icon format-icon goog-inline-block"></div> |
| 95 |
<span style="vertical-align:middle">Format</span> |
| 96 |
<!-- This DIV will be auto-decorated with a menu. --> |
| 97 |
<div id="formatMenu" class="goog-menu"> |
| 98 |
<div class="goog-menuitem">Bold</div> |
| 99 |
<div class="goog-menuitem">Italic</div> |
| 100 |
<div class="goog-menuitem">Underline</div> |
| 101 |
<div class="goog-menuseparator"></div> |
| 102 |
<div class="goog-menuitem goog-menuitem-disabled"> |
| 103 |
Strikethrough |
| 104 |
</div> |
| 105 |
<div class="goog-menuseparator"></div> |
| 106 |
<div class="goog-menuitem">Font...</div> |
| 107 |
<div class="goog-menuitem">Color...</div> |
| 108 |
</div> |
| 109 |
</div> |
| 110 |
</td> |
| 111 |
<td> |
| 112 |
Enable button: |
| 113 |
<input type="checkbox" id="formatButton_enable" checked> |
| 114 |
|
| 115 |
Show button: |
| 116 |
<input type="checkbox" id="formatButton_show" checked> |
| 117 |
|
| 118 |
</td> |
| 119 |
</tr> |
| 120 |
</tbody> |
| 121 |
</table> |
| 122 |
<label> |
| 123 |
</label> |
| 124 |
<br> |
| 125 |
</fieldset> |
| 126 |
</td> |
| 127 |
<td width="33%"> |
| 128 |
<!-- Event log. --> |
| 129 |
<fieldset class="goog-debug-panel"> |
| 130 |
<legend>Event Log</legend> |
| 131 |
<div id="log"></div> |
| 132 |
</fieldset> |
| 133 |
</td> |
| 134 |
</tr> |
| 135 |
</tbody> |
| 136 |
</table> |
| 137 |
<br> |
| 138 |
<div id="perf"></div> |
| 139 |
<script> |
| 140 |
var timer = goog.now(); |
| 141 |
|
| 142 |
// Set up a logger. |
| 143 |
goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL); |
| 144 |
var logger = goog.log.getLogger('demo'); |
| 145 |
var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log')); |
| 146 |
logconsole.setCapturing(true); |
| 147 |
|
| 148 |
var EVENTS = goog.object.getValues(goog.ui.Component.EventType); |
| 149 |
goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.'); |
| 150 |
|
| 151 |
function logEvent(e) { |
| 152 |
var component = e.target; |
| 153 |
var caption = (typeof component.getCaption == 'function') ? |
| 154 |
component.getCaption() : component.getId(); |
| 155 |
goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type); |
| 156 |
} |
| 157 |
|
| 158 |
// Create a sample menu. |
| 159 |
var m1 = new goog.ui.Menu(); |
| 160 |
m1.setId('FileMenu'); |
| 161 |
goog.array.forEach(['New', 'Open', 'Save', 'Save as', null, 'Change label'], |
| 162 |
function(label) { |
| 163 |
var item; |
| 164 |
if (label) { |
| 165 |
item = new goog.ui.MenuItem(label + '...'); |
| 166 |
item.setId(label); |
| 167 |
item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 168 |
if (label == 'Save as') { |
| 169 |
item.setEnabled(false); |
| 170 |
} |
| 171 |
} else { |
| 172 |
item = new goog.ui.MenuSeparator(); |
| 173 |
} |
| 174 |
m1.addItem(item); |
| 175 |
}); |
| 176 |
|
| 177 |
// Create the first button programmatically. |
| 178 |
var b1 = new goog.ui.MenuButton('File', m1, |
| 179 |
new goog.ui.ImagelessMenuButtonRenderer()); |
| 180 |
b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 181 |
b1.setId('FileButton'); |
| 182 |
b1.render(goog.dom.getElement('menuButtons')); |
| 183 |
b1.setTooltip('File menu demo'); |
| 184 |
goog.events.listen(b1, EVENTS, logEvent); |
| 185 |
|
| 186 |
goog.events.listen(goog.dom.getElement('b1_enable'), |
| 187 |
goog.events.EventType.CLICK, |
| 188 |
function(e) { |
| 189 |
b1.setEnabled(e.target.checked); |
| 190 |
}); |
| 191 |
|
| 192 |
goog.events.listen(b1, goog.ui.Component.EventType.ACTION, |
| 193 |
function(e) { |
| 194 |
if (e.target && e.target.getCaption() == 'Change label...') { |
| 195 |
var label = window.prompt('Enter new menu label:', b1.getCaption()); |
| 196 |
b1.setCaption(label || 'Empty'); |
| 197 |
} |
| 198 |
}); |
| 199 |
|
| 200 |
// Create another sample menu. |
| 201 |
var m2 = new goog.ui.Menu(); |
| 202 |
m2.setId('EditMenu'); |
| 203 |
goog.array.forEach(['Cut', 'Copy', 'Paste', null, 'Paste special'], |
| 204 |
function(label) { |
| 205 |
var item; |
| 206 |
if (label) { |
| 207 |
item = new goog.ui.MenuItem(label + '...'); |
| 208 |
item.setId(label); |
| 209 |
item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 210 |
} else { |
| 211 |
item = new goog.ui.MenuSeparator(); |
| 212 |
} |
| 213 |
m2.addItem(item); |
| 214 |
}); |
| 215 |
|
| 216 |
// Create the second button programmatically. |
| 217 |
var b2 = new goog.ui.MenuButton('Edit', m2, |
| 218 |
new goog.ui.ImagelessMenuButtonRenderer()); |
| 219 |
b2.setId('EditButton'); |
| 220 |
b2.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 221 |
b2.render(goog.dom.getElement('menuButtons')); |
| 222 |
b2.setTooltip('Edit menu demo'); |
| 223 |
goog.events.listen(b2, EVENTS, logEvent); |
| 224 |
|
| 225 |
goog.events.listen(goog.dom.getElement('b2_show'), |
| 226 |
goog.events.EventType.CLICK, |
| 227 |
function(e) { |
| 228 |
b2.setVisible(e.target.checked); |
| 229 |
}); |
| 230 |
|
| 231 |
// Create another sample menu. |
| 232 |
var m3 = new goog.ui.Menu(); |
| 233 |
m3.setId('WindowMenu'); |
| 234 |
goog.array.forEach(['Tile', 'Cascade', null, 'Zoom in', 'Zoom out'], |
| 235 |
function(label) { |
| 236 |
var item; |
| 237 |
if (label) { |
| 238 |
item = new goog.ui.MenuItem(label + '...'); |
| 239 |
item.setId(label); |
| 240 |
item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 241 |
} else { |
| 242 |
item = new goog.ui.MenuSeparator(); |
| 243 |
} |
| 244 |
m3.addItem(item); |
| 245 |
}); |
| 246 |
var unhighlightable = new goog.ui.MenuItem('Zoom control:'); |
| 247 |
// Disabled implies unhighlightable, unless the menu is specifically set |
| 248 |
// to allow highlighting disabled items (but it doesn't by default). |
| 249 |
unhighlightable.setEnabled(false); |
| 250 |
unhighlightable.setId('unhighlightable'); |
| 251 |
m3.addItemAt(unhighlightable, 3); |
| 252 |
|
| 253 |
// Create a third button programmatically. |
| 254 |
var b3 = new goog.ui.MenuButton('Window', m3, |
| 255 |
new goog.ui.ImagelessMenuButtonRenderer()); |
| 256 |
b3.setId('WindowButton'); |
| 257 |
b3.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 258 |
b3.render(goog.dom.getElement('menuButtons')); |
| 259 |
b3.setTooltip('Windowm menu demo'); |
| 260 |
goog.events.listen(b3, EVENTS, logEvent); |
| 261 |
|
| 262 |
// Decorate a menu button. Note that since one of the child nodes of the |
| 263 |
// menu button element can be decorated as a menu, it is auto-decorated and |
| 264 |
// attached to the button. |
| 265 |
var formatButton = goog.ui.decorate(goog.dom.getElement('formatButton')); |
| 266 |
|
| 267 |
goog.events.listen(goog.dom.getElement('formatButton_show'), |
| 268 |
goog.events.EventType.CLICK, |
| 269 |
function(e) { |
| 270 |
formatButton.setVisible(e.target.checked); |
| 271 |
}); |
| 272 |
|
| 273 |
goog.events.listen(goog.dom.getElement('formatButton_enable'), |
| 274 |
goog.events.EventType.CLICK, |
| 275 |
function(e) { |
| 276 |
formatButton.setEnabled(e.target.checked); |
| 277 |
}); |
| 278 |
|
| 279 |
goog.events.listen(formatButton, EVENTS, logEvent); |
| 280 |
|
| 281 |
goog.dom.setTextContent(goog.dom.getElement('perf'), |
| 282 |
(goog.now() - timer) + 'ms'); |
| 283 |
</script> |
| 284 |
</body> |
| 285 |
</html> |