• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!--
2Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6Code distributed by Google as part of the polymer project is also
7subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
8-->
9
10<!--
11
12`core-dropdown-base` is a base class for implementing controls that displays
13an overlay when tapped on.
14
15The child element with the class `dropdown` will be used as the drop-down. It
16should be a `core-dropdown` or other overlay element.
17
18@group Polymer Core Elements
19@element core-dropdown-base
20@status unstable
21@homepage github.io
22-->
23
24<link href="../polymer/polymer.html" rel="import">
25
26<polymer-element name="core-dropdown-base" tabindex="0">
27<script>
28
29  Polymer({
30
31    publish: {
32
33      /**
34       * True if the menu is open.
35       *
36       * @attribute opened
37       * @type boolean
38       * @default false
39       */
40      opened: false
41
42    },
43
44    eventDelegates: {
45      'tap': 'toggleOverlay'
46    },
47
48    overlayListeners: {
49      'core-overlay-open': 'openAction'
50    },
51
52    get dropdown() {
53      if (!this._dropdown) {
54        this._dropdown = this.querySelector('.dropdown');
55        for (var l in this.overlayListeners) {
56          this.addElementListener(this._dropdown, l, this.overlayListeners[l]);
57        }
58      }
59      return this._dropdown;
60    },
61
62    attached: function() {
63      // find the dropdown on attach
64      // FIXME: Support MO?
65      this.dropdown;
66    },
67
68    addElementListener: function(node, event, methodName, capture) {
69      var fn = this._makeBoundListener(methodName);
70      if (node && fn) {
71        Polymer.addEventListener(node, event, fn, capture);
72      }
73    },
74
75    removeElementListener: function(node, event, methodName, capture) {
76      var fn = this._makeBoundListener(methodName);
77      if (node && fn) {
78        Polymer.removeEventListener(node, event, fn, capture);
79      }
80    },
81
82    _makeBoundListener: function(methodName) {
83      var self = this, method = this[methodName];
84      if (!method) {
85        return;
86      }
87      var bound = '_bound' + methodName;
88      if (!this[bound]) {
89        this[bound] = function(e) {
90          method.call(self, e);
91        };
92      }
93      return this[bound];
94    },
95
96    openedChanged: function() {
97      if (this.disabled) {
98        return;
99      }
100      var dropdown = this.dropdown;
101      if (dropdown) {
102        dropdown.opened = this.opened;
103      }
104    },
105
106    openAction: function(e) {
107      this.opened = !!e.detail;
108    },
109
110    toggleOverlay: function(event) {
111      if (!this.dropdown.contains(event.target) && !this.disabled) {
112        this.opened = !this.opened;
113      }
114    }
115
116  });
117
118</script>
119</polymer-element>
120