aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/demos/menubutton.html
blob: bfb685da16ece988905969a3a9592bddf6b7c0c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
  <title>goog.ui.MenuButton Demo</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.array');
    goog.require('goog.events');
    goog.require('goog.events.EventType');
    goog.require('goog.object');
    goog.require('goog.debug.DivConsole');
    goog.require('goog.debug.Logger');
    goog.require('goog.debug.LogManager');
    goog.require('goog.ui.CustomButton');
    goog.require('goog.ui.Menu');
    goog.require('goog.ui.MenuButton');
    goog.require('goog.ui.MenuItem');
    goog.require('goog.ui.Separator');
    goog.require('goog.ui.decorate');
  </script>
  <link rel="stylesheet" href="css/demo.css">
  <link rel="stylesheet" href="../css/custombutton.css">
  <link rel="stylesheet" href="../css/menu.css">
  <link rel="stylesheet" href="../css/menuitem.css">
  <link rel="stylesheet" href="../css/menuseparator.css">
  <link rel="stylesheet" href="../css/menubutton.css">
  <style>
    /* Base class for all icon elements. */
    .icon {
      height: 16px;
      width: 16px;
      margin: 0 1px;
      background-image: url(../images/toolbar_icons.gif);
      background-repeat: no-repeat;
      vertical-align: middle;
    }

    /* "Format" icon. */
    .format-icon {
      background-position: -64px;
    }

    .positioning-frame {
      height: 250px;
      overflow: auto;
      width: 100%;
    }
  </style>
</head>
<body>
  <h1>goog.ui.MenuButton</h1>
  <table border="0" cellpadding="0" cellspacing="4" width="100%">
    <tbody>
      <tr valign="top">
        <td width="67%">
          <fieldset>
            <legend>
              These <strong>MenuButton</strong>s were created programmatically:
              &nbsp;
            </legend>
            <table border="0" cellpadding="0" cellspacing="4">
              <tbody>
                <tr valign="middle">
                  <td>
                    <div id="menuButtons"></div>
                  </td>
                  <td>
                    Enable first button:
                    <input type="checkbox" id="b1_enable" checked>
                    &nbsp;
                    Show second button:
                    <input type="checkbox" id="b2_show" checked>
                    &nbsp;
                  </td>
                </tr>
              </tbody>
            </table>
            <label>
            </label>
            <br>
          </fieldset>
          <fieldset>
            <legend>
              This <strong>MenuButton</strong> decorates an element:&nbsp;
            </legend>
            <table border="0" cellpadding="0" cellspacing="4">
              <tbody>
                <tr valign="middle">
                  <td>
                    <div id="formatButton" class="goog-menu-button"
                        title="Format">
                      <!-- These elements will become the button's caption. -->
                      <div class="icon format-icon goog-inline-block"></div>
                      <span style="vertical-align:middle">Format</span>
                      <!-- This DIV will be auto-decorated with a menu. -->
                      <div id="formatMenu" class="goog-menu">
                        <div class="goog-menuitem">Bold</div>
                        <div class="goog-menuitem">Italic</div>
                        <div class="goog-menuitem">Underline</div>
                        <div class="goog-menuseparator"></div>
                        <div class="goog-menuitem goog-menuitem-disabled">
                          Strikethrough
                        </div>
                        <div class="goog-menuseparator"></div>
                        <div class="goog-menuitem">Font...</div>
                        <div class="goog-menuitem">Color...</div>
                      </div>
                    </div>
                  </td>
                  <td>
                    Enable button:
                    <input type="checkbox" id="formatButton_enable" checked>
                    &nbsp;
                    Show button:
                    <input type="checkbox" id="formatButton_show" checked>
                    &nbsp;
                  </td>
                </tr>
              </tbody>
            </table>
            <label>
            </label>
            <br>
          </fieldset>
          <fieldset>
            <legend>
              This <strong>MenuButton</strong> accompanies a
              <strong>CustomButton</strong> to form a combo button:
              &nbsp;
            </legend>
            <div id="comboButtons"></div>
            <label>
            </label>
            <br>
          </fieldset>
          <fieldset>
            <legend>
              These <strong>MenuButtons</strong> demonstrate
              menu positioning options:
              &nbsp;
            </legend>
            <div></div>

            <input type="checkbox" id="pos_option_scroll">
            <label for="pos_option_scroll">
              Scroll on overflow
            </label>
            <iframe class="positioning-frame" id="positioning-frame"
                src="menubutton_frame.html">
            </iframe>
            <label>
            </label>
            <br>
          </fieldset>
        </td>
        <td width="33%">
          <!-- Event log. -->
          <fieldset class="goog-debug-panel">
            <legend>Event Log</legend>
            <div id="log"></div>
          </fieldset>
        </td>
      </tr>
    </tbody>
  </table>
  <br>
  <div id="perf"></div>
  <script>
    var timer = goog.now();

    // Set up a logger.
    goog.debug.LogManager.getRoot().setLevel(goog.debug.Logger.Level.ALL);
    var logger = goog.debug.Logger.getLogger('demo');
    var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
    logconsole.setCapturing(true);

    var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
    logger.fine('Listening for: ' + EVENTS.join(', ') + '.');

    function logEvent(e) {
      var component = e.target;
      var caption = (typeof component.getCaption == 'function') ?
          component.getCaption() : component.getId();
      logger.info('"' + caption + '" dispatched: ' + e.type);
    }

    // Create a sample menu.
    var m1 = new goog.ui.Menu();
    m1.setId('FileMenu');
    goog.array.forEach(['New', 'Open', 'Save', 'Save as', null, 'Change label'],
        function(label) {
          var item;
          if (label) {
            item = new goog.ui.MenuItem(label + '...');
            item.setId(label);
            item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
            if (label == 'Save as') {
              item.setEnabled(false);
            }
          } else {
            item = new goog.ui.MenuSeparator();
          }
          m1.addItem(item);
        });

    // Create the first button programmatically.
    var b1 = new goog.ui.MenuButton('File', m1);
    b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
    b1.setId('FileButton');
    b1.render(goog.dom.getElement('menuButtons'));
    b1.setTooltip('File menu demo');
    goog.events.listen(b1, EVENTS, logEvent);

    goog.events.listen(goog.dom.getElement('b1_enable'),
        goog.events.EventType.CLICK,
        function(e) {
          b1.setEnabled(e.target.checked);
        });

    goog.events.listen(b1, goog.ui.Component.EventType.ACTION,
        function(e) {
          if (e.target && e.target.getCaption() == 'Change label...') {
            var label = window.prompt('Enter new menu label:', b1.getCaption());
            b1.setCaption(label || 'Empty');
          }
        });

    // Create another sample menu.
    var m2 = new goog.ui.Menu();
    m2.setId('EditMenu');
    goog.array.forEach(['Cut', 'Copy', 'Paste', null, 'Paste special'],
        function(label) {
          var item;
          if (label) {
            item = new goog.ui.MenuItem(label + '...');
            item.setId(label);
            item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
          } else {
            item = new goog.ui.MenuSeparator();
          }
          m2.addItem(item);
        });

    // Create the second button programmatically.
    var b2 = new goog.ui.MenuButton('Edit', m2);
    b2.setId('EditButton');
    b2.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
    b2.render(goog.dom.getElement('menuButtons'));
    b2.setTooltip('Edit menu demo');
    goog.events.listen(b2, EVENTS, logEvent);

    goog.events.listen(goog.dom.getElement('b2_show'),
        goog.events.EventType.CLICK,
        function(e) {
          b2.setVisible(e.target.checked);
        });

    // Create another sample menu.
    var m3 = new goog.ui.Menu();
    m3.setId('WindowMenu');
    goog.array.forEach(['Tile', 'Cascade', null, 'Zoom in', 'Zoom out'],
        function(label) {
          var item;
          if (label) {
            item = new goog.ui.MenuItem(label + '...');
            item.setId(label);
            item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
          } else {
            item = new goog.ui.MenuSeparator();
          }
          m3.addItem(item);
        });
    var unhighlightable = new goog.ui.MenuItem('Zoom control:');
    // Disabled implies unhighlightable, unless the menu is specifically set
    // to allow highlighting disabled items (but it doesn't by default).
    unhighlightable.setEnabled(false);
    unhighlightable.setId('unhighlightable');
    m3.addItemAt(unhighlightable, 3);

    // Create a third button programmatically.
    var b3 = new goog.ui.MenuButton('Window', m3);
    b3.setId('WindowButton');
    b3.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
    b3.render(goog.dom.getElement('menuButtons'));
    b3.setTooltip('Windowm menu demo');
    goog.events.listen(b3, EVENTS, logEvent);

    // Decorate a menu button.  Note that since one of the child nodes of the
    // menu button element can be decorated as a menu, it is auto-decorated and
    // attached to the button.
    var formatButton = goog.ui.decorate(goog.dom.getElement('formatButton'));

    goog.events.listen(goog.dom.getElement('formatButton_show'),
        goog.events.EventType.CLICK,
        function(e) {
          formatButton.setVisible(e.target.checked);
        });

    goog.events.listen(goog.dom.getElement('formatButton_enable'),
        goog.events.EventType.CLICK,
        function(e) {
          formatButton.setEnabled(e.target.checked);
        });

    goog.events.listen(formatButton, EVENTS, logEvent);


    // Create a combo button with a custom button and a menu button.
    // Create a sample menu.
    var cbb1 = new goog.ui.CustomButton('Add to Family');
    cbb1.render(goog.dom.getElement('comboButtons'));
    cbb1.addClassName('goog-custom-button-collapse-right');
    var cbm1 = new goog.ui.Menu();
    cbm1.setId('ComboMenu');
    goog.array.forEach(['Friends', 'Family', 'Coworkers'],
        function(label) {
          var item = new goog.ui.MenuItem(label);
          item.setId(label);
          item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
          cbm1.addItem(item);
        });
    var cb1 = new goog.ui.Button('Take action', cbm1);
    var cbmb1 = new goog.ui.MenuButton(null, cbm1);
    cbmb1.setPositionElement(cbb1.getElement());
    cbmb1.addClassName('goog-menu-button-collapse-left');
    cbmb1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
    cbmb1.setId('ComboMenuButton');
    cbmb1.render(goog.dom.getElement('comboButtons'));
    cbmb1.setTooltip('Combo button');
    goog.events.listen(cbmb1, EVENTS, logEvent);


    // Create two buttons with menus for the positioning demo inside the
    // positioning iframe.
    var positioningFrame = goog.dom.getElement('positioning-frame');
    var shortPosButton, longPosButton;
    goog.events.listen(positioningFrame, 'load', function() {
      var frameDocument = goog.dom.getFrameContentDocument(positioningFrame);
      var frameDomHelper = new goog.dom.DomHelper(frameDocument);

      var shortMenu = new goog.ui.Menu(frameDomHelper);
      shortMenu.setId('PositionMenuShort');
      for (var i = 1; i <= 5; i++) {
        shortMenu.addItem(new goog.ui.MenuItem('Item ' + i + '...'));
      }
      shortPosButton = new goog.ui.MenuButton('Short Menu', shortMenu);
      shortPosButton.setId('PositingButtonShort');
      shortPosButton.render(frameDocument.body);
      shortMenu.getElement().style.overflowY = 'auto';

      var longMenu = new goog.ui.Menu(frameDomHelper);
      longMenu.setId('PositionMenuLong');
      for (var i = 1; i <= 15; i++) {
        longMenu.addItem(new goog.ui.MenuItem('Item ' + i + '...'));
      }
      longPosButton = new goog.ui.MenuButton('Long Menu', longMenu);
      longPosButton.setId('PositingButtonLong');
      longPosButton.render(frameDocument.body);
      longMenu.getElement().style.overflowY = 'auto';
    });
    function updateScrollOption() {
      var checked = goog.dom.getElement('pos_option_scroll').checked;
      shortPosButton.setScrollOnOverflow(checked);
      longPosButton.setScrollOnOverflow(checked);
    }
    goog.events.listen(goog.dom.getElement('pos_option_scroll'), 'click',
        updateScrollOption);


    goog.dom.setTextContent(goog.dom.getElement('perf'),
        (goog.now() - timer) + 'ms');
  </script>
</body>
</html>