| 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.MenuButton 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.CustomButton'); |
| 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/custombutton.css"> |
| 29 |
<link rel="stylesheet" href="../css/menu.css"> |
| 30 |
<link rel="stylesheet" href="../css/menuitem.css"> |
| 31 |
<link rel="stylesheet" href="../css/menuseparator.css"> |
| 32 |
<link rel="stylesheet" href="../css/menubutton.css"> |
| 33 |
<style> |
| 34 |
/* Base class for all icon elements. */ |
| 35 |
.icon { |
| 36 |
height: 16px; |
| 37 |
width: 16px; |
| 38 |
margin: 0 1px; |
| 39 |
background-image: url(../images/toolbar_icons.gif); |
| 40 |
background-repeat: no-repeat; |
| 41 |
vertical-align: middle; |
| 42 |
} |
| 43 |
|
| 44 |
/* "Format" icon. */ |
| 45 |
.format-icon { |
| 46 |
background-position: -64px; |
| 47 |
} |
| 48 |
|
| 49 |
.positioning-frame { |
| 50 |
height: 250px; |
| 51 |
overflow: auto; |
| 52 |
width: 100%; |
| 53 |
} |
| 54 |
</style> |
| 55 |
</head> |
| 56 |
<body> |
| 57 |
<h1>goog.ui.MenuButton</h1> |
| 58 |
<table border="0" cellpadding="0" cellspacing="4" width="100%"> |
| 59 |
<tbody> |
| 60 |
<tr valign="top"> |
| 61 |
<td width="67%"> |
| 62 |
<fieldset> |
| 63 |
<legend> |
| 64 |
These <strong>MenuButton</strong>s were created programmatically: |
| 65 |
|
| 66 |
</legend> |
| 67 |
<table border="0" cellpadding="0" cellspacing="4"> |
| 68 |
<tbody> |
| 69 |
<tr valign="middle"> |
| 70 |
<td> |
| 71 |
<div id="menuButtons"></div> |
| 72 |
</td> |
| 73 |
<td> |
| 74 |
Enable first button: |
| 75 |
<input type="checkbox" id="b1_enable" checked> |
| 76 |
|
| 77 |
Show second button: |
| 78 |
<input type="checkbox" id="b2_show" checked> |
| 79 |
|
| 80 |
</td> |
| 81 |
</tr> |
| 82 |
</tbody> |
| 83 |
</table> |
| 84 |
<label> |
| 85 |
</label> |
| 86 |
<br> |
| 87 |
</fieldset> |
| 88 |
<fieldset> |
| 89 |
<legend> |
| 90 |
This <strong>MenuButton</strong> decorates an element: |
| 91 |
</legend> |
| 92 |
<table border="0" cellpadding="0" cellspacing="4"> |
| 93 |
<tbody> |
| 94 |
<tr valign="middle"> |
| 95 |
<td> |
| 96 |
<div id="formatButton" class="goog-menu-button" |
| 97 |
title="Format"> |
| 98 |
<!-- These elements will become the button's caption. --> |
| 99 |
<div class="icon format-icon goog-inline-block"></div> |
| 100 |
<span style="vertical-align:middle">Format</span> |
| 101 |
<!-- This DIV will be auto-decorated with a menu. --> |
| 102 |
<div id="formatMenu" class="goog-menu"> |
| 103 |
<div class="goog-menuitem">Bold</div> |
| 104 |
<div class="goog-menuitem">Italic</div> |
| 105 |
<div class="goog-menuitem">Underline</div> |
| 106 |
<div class="goog-menuseparator"></div> |
| 107 |
<div class="goog-menuitem goog-menuitem-disabled"> |
| 108 |
Strikethrough |
| 109 |
</div> |
| 110 |
<div class="goog-menuseparator"></div> |
| 111 |
<div class="goog-menuitem">Font...</div> |
| 112 |
<div class="goog-menuitem">Color...</div> |
| 113 |
</div> |
| 114 |
</div> |
| 115 |
</td> |
| 116 |
<td> |
| 117 |
Enable button: |
| 118 |
<input type="checkbox" id="formatButton_enable" checked> |
| 119 |
|
| 120 |
Show button: |
| 121 |
<input type="checkbox" id="formatButton_show" checked> |
| 122 |
|
| 123 |
</td> |
| 124 |
</tr> |
| 125 |
</tbody> |
| 126 |
</table> |
| 127 |
<label> |
| 128 |
</label> |
| 129 |
<br> |
| 130 |
</fieldset> |
| 131 |
<fieldset> |
| 132 |
<legend> |
| 133 |
This <strong>MenuButton</strong> accompanies a |
| 134 |
<strong>CustomButton</strong> to form a combo button: |
| 135 |
|
| 136 |
</legend> |
| 137 |
<div id="comboButtons"></div> |
| 138 |
<label> |
| 139 |
</label> |
| 140 |
<br> |
| 141 |
</fieldset> |
| 142 |
<fieldset> |
| 143 |
<legend> |
| 144 |
These <strong>MenuButtons</strong> demonstrate |
| 145 |
menu positioning options: |
| 146 |
|
| 147 |
</legend> |
| 148 |
<div></div> |
| 149 |
|
| 150 |
<input type="checkbox" id="pos_option_scroll"> |
| 151 |
<label for="pos_option_scroll"> |
| 152 |
Scroll on overflow |
| 153 |
</label> |
| 154 |
<iframe class="positioning-frame" id="positioning-frame" |
| 155 |
src="menubutton_frame.html"> |
| 156 |
</iframe> |
| 157 |
<label> |
| 158 |
</label> |
| 159 |
<br> |
| 160 |
</fieldset> |
| 161 |
</td> |
| 162 |
<td width="33%"> |
| 163 |
<!-- Event log. --> |
| 164 |
<fieldset class="goog-debug-panel"> |
| 165 |
<legend>Event Log</legend> |
| 166 |
<div id="log"></div> |
| 167 |
</fieldset> |
| 168 |
</td> |
| 169 |
</tr> |
| 170 |
</tbody> |
| 171 |
</table> |
| 172 |
<br> |
| 173 |
<div id="perf"></div> |
| 174 |
<script> |
| 175 |
var timer = goog.now(); |
| 176 |
|
| 177 |
// Set up a logger. |
| 178 |
goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL); |
| 179 |
var logger = goog.log.getLogger('demo'); |
| 180 |
var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log')); |
| 181 |
logconsole.setCapturing(true); |
| 182 |
|
| 183 |
var EVENTS = goog.object.getValues(goog.ui.Component.EventType); |
| 184 |
goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.'); |
| 185 |
|
| 186 |
function logEvent(e) { |
| 187 |
var component = e.target; |
| 188 |
var caption = (typeof component.getCaption == 'function') ? |
| 189 |
component.getCaption() : component.getId(); |
| 190 |
goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type); |
| 191 |
} |
| 192 |
|
| 193 |
// Create a sample menu. |
| 194 |
var m1 = new goog.ui.Menu(); |
| 195 |
m1.setId('FileMenu'); |
| 196 |
goog.array.forEach(['New', 'Open', 'Save', 'Save as', null, 'Change label'], |
| 197 |
function(label) { |
| 198 |
var item; |
| 199 |
if (label) { |
| 200 |
item = new goog.ui.MenuItem(label + '...'); |
| 201 |
item.setId(label); |
| 202 |
item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 203 |
if (label == 'Save as') { |
| 204 |
item.setEnabled(false); |
| 205 |
} |
| 206 |
} else { |
| 207 |
item = new goog.ui.MenuSeparator(); |
| 208 |
} |
| 209 |
m1.addItem(item); |
| 210 |
}); |
| 211 |
|
| 212 |
// Create the first button programmatically. |
| 213 |
var b1 = new goog.ui.MenuButton('File', m1); |
| 214 |
b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 215 |
b1.setId('FileButton'); |
| 216 |
b1.render(goog.dom.getElement('menuButtons')); |
| 217 |
b1.setTooltip('File menu demo'); |
| 218 |
goog.events.listen(b1, EVENTS, logEvent); |
| 219 |
|
| 220 |
goog.events.listen(goog.dom.getElement('b1_enable'), |
| 221 |
goog.events.EventType.CLICK, |
| 222 |
function(e) { |
| 223 |
b1.setEnabled(e.target.checked); |
| 224 |
}); |
| 225 |
|
| 226 |
goog.events.listen(b1, goog.ui.Component.EventType.ACTION, |
| 227 |
function(e) { |
| 228 |
if (e.target && e.target.getCaption() == 'Change label...') { |
| 229 |
var label = window.prompt('Enter new menu label:', b1.getCaption()); |
| 230 |
b1.setCaption(label || 'Empty'); |
| 231 |
} |
| 232 |
}); |
| 233 |
|
| 234 |
// Create another sample menu. |
| 235 |
var m2 = new goog.ui.Menu(); |
| 236 |
m2.setId('EditMenu'); |
| 237 |
goog.array.forEach(['Cut', 'Copy', 'Paste', null, 'Paste special'], |
| 238 |
function(label) { |
| 239 |
var item; |
| 240 |
if (label) { |
| 241 |
item = new goog.ui.MenuItem(label + '...'); |
| 242 |
item.setId(label); |
| 243 |
item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 244 |
} else { |
| 245 |
item = new goog.ui.MenuSeparator(); |
| 246 |
} |
| 247 |
m2.addItem(item); |
| 248 |
}); |
| 249 |
|
| 250 |
// Create the second button programmatically. |
| 251 |
var b2 = new goog.ui.MenuButton('Edit', m2); |
| 252 |
b2.setId('EditButton'); |
| 253 |
b2.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 254 |
b2.render(goog.dom.getElement('menuButtons')); |
| 255 |
b2.setTooltip('Edit menu demo'); |
| 256 |
goog.events.listen(b2, EVENTS, logEvent); |
| 257 |
|
| 258 |
goog.events.listen(goog.dom.getElement('b2_show'), |
| 259 |
goog.events.EventType.CLICK, |
| 260 |
function(e) { |
| 261 |
b2.setVisible(e.target.checked); |
| 262 |
}); |
| 263 |
|
| 264 |
// Create another sample menu. |
| 265 |
var m3 = new goog.ui.Menu(); |
| 266 |
m3.setId('WindowMenu'); |
| 267 |
goog.array.forEach(['Tile', 'Cascade', null, 'Zoom in', 'Zoom out'], |
| 268 |
function(label) { |
| 269 |
var item; |
| 270 |
if (label) { |
| 271 |
item = new goog.ui.MenuItem(label + '...'); |
| 272 |
item.setId(label); |
| 273 |
item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 274 |
} else { |
| 275 |
item = new goog.ui.MenuSeparator(); |
| 276 |
} |
| 277 |
m3.addItem(item); |
| 278 |
}); |
| 279 |
var unhighlightable = new goog.ui.MenuItem('Zoom control:'); |
| 280 |
// Disabled implies unhighlightable, unless the menu is specifically set |
| 281 |
// to allow highlighting disabled items (but it doesn't by default). |
| 282 |
unhighlightable.setEnabled(false); |
| 283 |
unhighlightable.setId('unhighlightable'); |
| 284 |
m3.addItemAt(unhighlightable, 3); |
| 285 |
|
| 286 |
// Create a third button programmatically. |
| 287 |
var b3 = new goog.ui.MenuButton('Window', m3); |
| 288 |
b3.setId('WindowButton'); |
| 289 |
b3.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 290 |
b3.render(goog.dom.getElement('menuButtons')); |
| 291 |
b3.setTooltip('Windowm menu demo'); |
| 292 |
goog.events.listen(b3, EVENTS, logEvent); |
| 293 |
|
| 294 |
// Decorate a menu button. Note that since one of the child nodes of the |
| 295 |
// menu button element can be decorated as a menu, it is auto-decorated and |
| 296 |
// attached to the button. |
| 297 |
var formatButton = goog.ui.decorate(goog.dom.getElement('formatButton')); |
| 298 |
|
| 299 |
goog.events.listen(goog.dom.getElement('formatButton_show'), |
| 300 |
goog.events.EventType.CLICK, |
| 301 |
function(e) { |
| 302 |
formatButton.setVisible(e.target.checked); |
| 303 |
}); |
| 304 |
|
| 305 |
goog.events.listen(goog.dom.getElement('formatButton_enable'), |
| 306 |
goog.events.EventType.CLICK, |
| 307 |
function(e) { |
| 308 |
formatButton.setEnabled(e.target.checked); |
| 309 |
}); |
| 310 |
|
| 311 |
goog.events.listen(formatButton, EVENTS, logEvent); |
| 312 |
|
| 313 |
|
| 314 |
// Create a combo button with a custom button and a menu button. |
| 315 |
// Create a sample menu. |
| 316 |
var cbb1 = new goog.ui.CustomButton('Add to Family'); |
| 317 |
cbb1.render(goog.dom.getElement('comboButtons')); |
| 318 |
cbb1.addClassName('goog-custom-button-collapse-right'); |
| 319 |
var cbm1 = new goog.ui.Menu(); |
| 320 |
cbm1.setId('ComboMenu'); |
| 321 |
goog.array.forEach(['Friends', 'Family', 'Coworkers'], |
| 322 |
function(label) { |
| 323 |
var item = new goog.ui.MenuItem(label); |
| 324 |
item.setId(label); |
| 325 |
item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 326 |
cbm1.addItem(item); |
| 327 |
}); |
| 328 |
var cb1 = new goog.ui.Button('Take action', cbm1); |
| 329 |
var cbmb1 = new goog.ui.MenuButton(null, cbm1); |
| 330 |
cbmb1.setPositionElement(cbb1.getElement()); |
| 331 |
cbmb1.addClassName('goog-menu-button-collapse-left'); |
| 332 |
cbmb1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true); |
| 333 |
cbmb1.setId('ComboMenuButton'); |
| 334 |
cbmb1.render(goog.dom.getElement('comboButtons')); |
| 335 |
cbmb1.setTooltip('Combo button'); |
| 336 |
goog.events.listen(cbmb1, EVENTS, logEvent); |
| 337 |
|
| 338 |
|
| 339 |
// Create two buttons with menus for the positioning demo inside the |
| 340 |
// positioning iframe. |
| 341 |
var positioningFrame = goog.dom.getElement('positioning-frame'); |
| 342 |
var shortPosButton, longPosButton; |
| 343 |
goog.events.listen(positioningFrame, 'load', function() { |
| 344 |
var frameDocument = goog.dom.getFrameContentDocument(positioningFrame); |
| 345 |
var frameDomHelper = new goog.dom.DomHelper(frameDocument); |
| 346 |
|
| 347 |
var shortMenu = new goog.ui.Menu(frameDomHelper); |
| 348 |
shortMenu.setId('PositionMenuShort'); |
| 349 |
for (var i = 1; i <= 5; i++) { |
| 350 |
shortMenu.addItem(new goog.ui.MenuItem('Item ' + i + '...')); |
| 351 |
} |
| 352 |
shortPosButton = new goog.ui.MenuButton('Short Menu', shortMenu); |
| 353 |
shortPosButton.setId('PositingButtonShort'); |
| 354 |
shortPosButton.render(frameDocument.body); |
| 355 |
shortMenu.getElement().style.overflowY = 'auto'; |
| 356 |
|
| 357 |
var longMenu = new goog.ui.Menu(frameDomHelper); |
| 358 |
longMenu.setId('PositionMenuLong'); |
| 359 |
for (var i = 1; i <= 15; i++) { |
| 360 |
longMenu.addItem(new goog.ui.MenuItem('Item ' + i + '...')); |
| 361 |
} |
| 362 |
longPosButton = new goog.ui.MenuButton('Long Menu', longMenu); |
| 363 |
longPosButton.setId('PositingButtonLong'); |
| 364 |
longPosButton.render(frameDocument.body); |
| 365 |
longMenu.getElement().style.overflowY = 'auto'; |
| 366 |
}); |
| 367 |
function updateScrollOption() { |
| 368 |
var checked = goog.dom.getElement('pos_option_scroll').checked; |
| 369 |
shortPosButton.setScrollOnOverflow(checked); |
| 370 |
longPosButton.setScrollOnOverflow(checked); |
| 371 |
} |
| 372 |
goog.events.listen(goog.dom.getElement('pos_option_scroll'), 'click', |
| 373 |
updateScrollOption); |
| 374 |
|
| 375 |
|
| 376 |
goog.dom.setTextContent(goog.dom.getElement('perf'), |
| 377 |
(goog.now() - timer) + 'ms'); |
| 378 |
</script> |
| 379 |
</body> |
| 380 |
</html> |