Blame view

bower_components/paper-dropdown-menu/paper-dropdown-menu.html 11.7 KB
73bcce88   luigser   COMPONENTS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  <!--
  @license
  Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
  This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  Code distributed by Google as part of the polymer project is also
  subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  -->
  
  <link rel="import" href="../polymer/polymer.html">
  <link rel="import" href="../paper-styles/default-theme.html">
  <link rel="import" href="../paper-input/paper-input.html">
  <link rel="import" href="../paper-menu-button/paper-menu-button.html">
  <link rel="import" href="../paper-ripple/paper-ripple.html">
  <link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
  <link rel="import" href="../iron-behaviors/iron-control-state.html">
  <link rel="import" href="../iron-behaviors/iron-button-state.html">
  <link rel="import" href="../iron-icons/iron-icons.html">
  <link rel="import" href="../iron-icon/iron-icon.html">
c5169e0e   Renato De Donato   a new hope
21
  <link rel="import" href="../iron-selector/iron-selectable.html">
eb240478   Luigi Serra   public room cards...
22
23
  <link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
  <link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.html">
73bcce88   luigser   COMPONENTS
24
25
  
  <!--
eb240478   Luigi Serra   public room cards...
26
27
  Material design: [Dropdown menus](https://www.google.com/design/spec/components/buttons.html#buttons-dropdown-buttons)
  
73bcce88   luigser   COMPONENTS
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  `paper-dropdown-menu` is similar to a native browser select element.
  `paper-dropdown-menu` works with selectable content. The currently selected
  item is displayed in the control. If no item is selected, the `label` is
  displayed instead.
  
  The child element with the class `dropdown-content` will be used as the dropdown
  menu. It could be a `paper-menu` or element that triggers `iron-select` when
  selecting its children.
  
  Example:
  
      <paper-dropdown-menu label="Your favourite pastry">
        <paper-menu class="dropdown-content">
          <paper-item>Croissant</paper-item>
          <paper-item>Donut</paper-item>
          <paper-item>Financier</paper-item>
          <paper-item>Madeleine</paper-item>
        </paper-menu>
      </paper-dropdown-menu>
  
  This example renders a dropdown menu with 4 options.
  
e619a3b0   Luigi Serra   Controllet cross ...
50
51
52
  Similarly to using `iron-select`, `iron-deselect` events will cause the
  current selection of the `paper-dropdown-menu` to be cleared.
  
73bcce88   luigser   COMPONENTS
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  ### Styling
  
  The following custom properties and mixins are also available for styling:
  
  Custom property | Description | Default
  ----------------|-------------|----------
  `--paper-dropdown-menu` | A mixin that is applied to the element host | `{}`
  `--paper-dropdown-menu-disabled` | A mixin that is applied to the element host when disabled | `{}`
  `--paper-dropdown-menu-ripple` | A mixin that is applied to the internal ripple | `{}`
  `--paper-dropdown-menu-button` | A mixin that is applied to the internal menu button | `{}`
  `--paper-dropdown-menu-input` | A mixin that is applied to the internal paper input | `{}`
  `--paper-dropdown-menu-icon` | A mixin that is applied to the internal icon | `{}`
  
  You can also use any of the `paper-input-container` and `paper-menu-button`
  style mixins and custom properties to style the internal input and menu button
  respectively.
  
  @group Paper Elements
  @element paper-dropdown-menu
  @hero hero.svg
  @demo demo/index.html
  -->
  
  <dom-module id="paper-dropdown-menu">
c5169e0e   Renato De Donato   a new hope
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    <style>
      :host {
        display: inline-block;
        position: relative;
        text-align: left;
        cursor: pointer;
  
        /* NOTE(cdata): Both values are needed, since some phones require the
         * value to be `transparent`.
         */
        -webkit-tap-highlight-color: rgba(0,0,0,0);
        -webkit-tap-highlight-color: transparent;
  
        --paper-input-container-input: {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          max-width: 100%;
          box-sizing: border-box;
73bcce88   luigser   COMPONENTS
96
          cursor: pointer;
c5169e0e   Renato De Donato   a new hope
97
        };
73bcce88   luigser   COMPONENTS
98
  
c5169e0e   Renato De Donato   a new hope
99
100
        @apply(--paper-dropdown-menu);
      }
73bcce88   luigser   COMPONENTS
101
  
c5169e0e   Renato De Donato   a new hope
102
103
104
      :host([disabled]) {
        @apply(--paper-dropdown-menu-disabled);
      }
73bcce88   luigser   COMPONENTS
105
  
c5169e0e   Renato De Donato   a new hope
106
107
108
      :host([noink]) paper-ripple {
        display: none;
      }
73bcce88   luigser   COMPONENTS
109
  
c5169e0e   Renato De Donato   a new hope
110
111
112
      :host([no-label-float]) paper-ripple {
        top: 8px;
      }
73bcce88   luigser   COMPONENTS
113
  
c5169e0e   Renato De Donato   a new hope
114
115
116
117
118
      paper-ripple {
        top: 12px;
        left: 0px;
        bottom: 8px;
        right: 0px;
73bcce88   luigser   COMPONENTS
119
  
c5169e0e   Renato De Donato   a new hope
120
121
        @apply(--paper-dropdown-menu-ripple);
      }
73bcce88   luigser   COMPONENTS
122
  
c5169e0e   Renato De Donato   a new hope
123
124
125
126
127
      paper-menu-button {
        display: block;
        padding: 0;
        @apply(--paper-dropdown-menu-button);
      }
73bcce88   luigser   COMPONENTS
128
  
c5169e0e   Renato De Donato   a new hope
129
130
131
      paper-input {
        @apply(--paper-dropdown-menu-input);
      }
73bcce88   luigser   COMPONENTS
132
  
c5169e0e   Renato De Donato   a new hope
133
134
      iron-icon {
        color: var(--disabled-text-color);
73bcce88   luigser   COMPONENTS
135
  
c5169e0e   Renato De Donato   a new hope
136
137
        @apply(--paper-dropdown-menu-icon);
      }
73bcce88   luigser   COMPONENTS
138
  
c5169e0e   Renato De Donato   a new hope
139
140
    </style>
    <template>
73bcce88   luigser   COMPONENTS
141
142
      <paper-menu-button
        id="menuButton"
c5169e0e   Renato De Donato   a new hope
143
144
        vertical-align="top"
        horizontal-align="right"
73bcce88   luigser   COMPONENTS
145
146
147
148
        vertical-offset="[[_computeMenuVerticalOffset(noLabelFloat)]]"
        disabled="[[disabled]]"
        no-animations="[[noAnimations]]"
        on-iron-select="_onIronSelect"
e619a3b0   Luigi Serra   Controllet cross ...
149
        on-iron-deselect="_onIronDeselect"
73bcce88   luigser   COMPONENTS
150
151
152
        opened="{{opened}}">
        <div class="dropdown-trigger">
          <paper-ripple></paper-ripple>
73bcce88   luigser   COMPONENTS
153
          <paper-input
eb240478   Luigi Serra   public room cards...
154
            invalid="[[invalid]]"
73bcce88   luigser   COMPONENTS
155
156
157
158
159
160
161
162
163
164
165
166
167
            readonly
            disabled="[[disabled]]"
            value="[[selectedItemLabel]]"
            placeholder="[[placeholder]]"
            always-float-label="[[alwaysFloatLabel]]"
            no-label-float="[[noLabelFloat]]"
            label="[[label]]">
            <iron-icon icon="arrow-drop-down" suffix></iron-icon>
          </paper-input>
        </div>
        <content id="content" select=".dropdown-content"></content>
      </paper-menu-button>
    </template>
c5169e0e   Renato De Donato   a new hope
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
  </dom-module>
  <script>
    (function() {
      'use strict';
  
      Polymer({
        is: 'paper-dropdown-menu',
  
        /**
         * Fired when the dropdown opens.
         *
         * @event paper-dropdown-open
         */
  
        /**
         * Fired when the dropdown closes.
         *
         * @event paper-dropdown-close
         */
  
        behaviors: [
          Polymer.IronControlState,
          Polymer.IronButtonState,
          Polymer.IronFormElementBehavior,
          Polymer.IronValidatableBehavior
        ],
  
        properties: {
a1a3bc73   Luigi Serra   graphs updates
196
          /**
c5169e0e   Renato De Donato   a new hope
197
198
199
           * The derived "label" of the currently selected item. This value
           * is the `label` property on the selected item if set, or else the
           * trimmed text content of the selected item.
73bcce88   luigser   COMPONENTS
200
           */
c5169e0e   Renato De Donato   a new hope
201
202
203
204
          selectedItemLabel: {
            type: String,
            notify: true,
            readOnly: true
73bcce88   luigser   COMPONENTS
205
206
207
          },
  
          /**
c5169e0e   Renato De Donato   a new hope
208
209
210
211
212
           * The last selected item. An item is selected if the dropdown menu has
           * a child with class `dropdown-content`, and that child triggers an
           * `iron-select` event with the selected `item` in the `detail`.
           *
           * @type {?Object}
eb240478   Luigi Serra   public room cards...
213
           */
c5169e0e   Renato De Donato   a new hope
214
215
216
217
          selectedItem: {
            type: Object,
            notify: true,
            readOnly: true
eb240478   Luigi Serra   public room cards...
218
219
220
          },
  
          /**
c5169e0e   Renato De Donato   a new hope
221
222
223
           * The value for this element that will be used when submitting in
           * a form. It is read only, and will always have the same value
           * as `selectedItemLabel`.
73bcce88   luigser   COMPONENTS
224
           */
c5169e0e   Renato De Donato   a new hope
225
226
227
228
          value: {
            type: String,
            notify: true,
            readOnly: true
73bcce88   luigser   COMPONENTS
229
230
231
          },
  
          /**
c5169e0e   Renato De Donato   a new hope
232
           * The label for the dropdown.
73bcce88   luigser   COMPONENTS
233
           */
c5169e0e   Renato De Donato   a new hope
234
235
          label: {
            type: String
73bcce88   luigser   COMPONENTS
236
237
238
          },
  
          /**
c5169e0e   Renato De Donato   a new hope
239
           * The placeholder for the dropdown.
73bcce88   luigser   COMPONENTS
240
           */
c5169e0e   Renato De Donato   a new hope
241
242
          placeholder: {
            type: String
73bcce88   luigser   COMPONENTS
243
244
245
          },
  
          /**
c5169e0e   Renato De Donato   a new hope
246
           * True if the dropdown is open. Otherwise, false.
73bcce88   luigser   COMPONENTS
247
           */
c5169e0e   Renato De Donato   a new hope
248
249
250
251
          opened: {
            type: Boolean,
            notify: true,
            value: false
73bcce88   luigser   COMPONENTS
252
253
254
          },
  
          /**
c5169e0e   Renato De Donato   a new hope
255
256
           * Set to true to disable the floating label. Bind this to the
           * `<paper-input-container>`'s `noLabelFloat` property.
73bcce88   luigser   COMPONENTS
257
           */
c5169e0e   Renato De Donato   a new hope
258
259
260
261
          noLabelFloat: {
              type: Boolean,
              value: false,
              reflectToAttribute: true
73bcce88   luigser   COMPONENTS
262
263
264
          },
  
          /**
c5169e0e   Renato De Donato   a new hope
265
266
           * Set to true to always float the label. Bind this to the
           * `<paper-input-container>`'s `alwaysFloatLabel` property.
73bcce88   luigser   COMPONENTS
267
           */
c5169e0e   Renato De Donato   a new hope
268
269
270
          alwaysFloatLabel: {
            type: Boolean,
            value: false
a1a3bc73   Luigi Serra   graphs updates
271
          },
73bcce88   luigser   COMPONENTS
272
  
a1a3bc73   Luigi Serra   graphs updates
273
          /**
c5169e0e   Renato De Donato   a new hope
274
275
           * Set to true to disable animations when opening and closing the
           * dropdown.
a1a3bc73   Luigi Serra   graphs updates
276
           */
c5169e0e   Renato De Donato   a new hope
277
278
279
          noAnimations: {
            type: Boolean,
            value: false
a1a3bc73   Luigi Serra   graphs updates
280
          }
c5169e0e   Renato De Donato   a new hope
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
        },
  
        listeners: {
          'tap': '_onTap'
        },
  
        keyBindings: {
          'up down': 'open',
          'esc': 'close'
        },
  
        hostAttributes: {
          role: 'group',
          'aria-haspopup': 'true'
        },
  
        observers: [
          '_selectedItemChanged(selectedItem)'
        ],
  
        attached: function() {
          // NOTE(cdata): Due to timing, a preselected value in a `IronSelectable`
          // child will cause an `iron-select` event to fire while the element is
          // still in a `DocumentFragment`. This has the effect of causing
          // handlers not to fire. So, we double check this value on attached:
          var contentElement = this.contentElement;
          if (contentElement && contentElement.selectedItem) {
            this._setSelectedItem(contentElement.selectedItem);
          }
        },
  
        /**
         * The content element that is contained by the dropdown menu, if any.
         */
        get contentElement() {
          return Polymer.dom(this.$.content).getDistributedNodes()[0];
        },
  
        /**
         * Show the dropdown content.
         */
        open: function() {
          this.$.menuButton.open();
        },
  
        /**
         * Hide the dropdown content.
         */
        close: function() {
          this.$.menuButton.close();
        },
  
        /**
         * A handler that is called when `iron-select` is fired.
         *
         * @param {CustomEvent} event An `iron-select` event.
         */
        _onIronSelect: function(event) {
          this._setSelectedItem(event.detail.item);
        },
  
        /**
         * A handler that is called when `iron-deselect` is fired.
         *
         * @param {CustomEvent} event An `iron-deselect` event.
         */
        _onIronDeselect: function(event) {
          this._setSelectedItem(null);
        },
  
        /**
         * A handler that is called when the dropdown is tapped.
         *
         * @param {CustomEvent} event A tap event.
         */
        _onTap: function(event) {
          if (Polymer.Gestures.findOriginalTarget(event) === this) {
            this.open();
          }
        },
  
        /**
         * Compute the label for the dropdown given a selected item.
         *
         * @param {Element} selectedItem A selected Element item, with an
         * optional `label` property.
         */
        _selectedItemChanged: function(selectedItem) {
          var value = '';
          if (!selectedItem) {
            value = '';
          } else {
            value = selectedItem.label || selectedItem.textContent.trim();
          }
  
          this._setValue(value);
          this._setSelectedItemLabel(value);
        },
  
        /**
         * Compute the vertical offset of the menu based on the value of
         * `noLabelFloat`.
         *
         * @param {boolean} noLabelFloat True if the label should not float
         * above the input, otherwise false.
         */
        _computeMenuVerticalOffset: function(noLabelFloat) {
          // NOTE(cdata): These numbers are somewhat magical because they are
          // derived from the metrics of elements internal to `paper-input`'s
          // template. The metrics will change depending on whether or not the
          // input has a floating label.
          return noLabelFloat ? -4 : 8;
        },
  
        /**
         * Returns false if the element is required and does not have a selection,
         * and true otherwise.
         * @return {Boolean} true if `required` is false, or if `required` is true
         * and the element has a valid selection.
         */
        _getValidity: function() {
          return this.disabled || !this.required || (this.required && this.value);
        }
      });
    })();
  </script>