Blame view

bower_components/iron-behaviors/iron-button-state.html 5.23 KB
73bcce88   luigser   COMPONENTS
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
  <!--
  @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="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
  <link rel="import" href="iron-control-state.html">
  
  <script>
  
    /**
     * @demo demo/index.html
     * @polymerBehavior Polymer.IronButtonState
     */
    Polymer.IronButtonStateImpl = {
  
      properties: {
  
        /**
         * If true, the user is currently holding down the button.
         */
        pressed: {
          type: Boolean,
          readOnly: true,
          value: false,
          reflectToAttribute: true,
          observer: '_pressedChanged'
        },
  
        /**
         * If true, the button toggles the active state with each tap or press
         * of the spacebar.
         */
        toggles: {
          type: Boolean,
          value: false,
          reflectToAttribute: true
        },
  
        /**
         * If true, the button is a toggle and is currently in the active state.
         */
        active: {
          type: Boolean,
          value: false,
          notify: true,
          reflectToAttribute: true
        },
  
        /**
         * True if the element is currently being pressed by a "pointer," which
         * is loosely defined as mouse or touch input (but specifically excluding
         * keyboard input).
         */
        pointerDown: {
          type: Boolean,
          readOnly: true,
          value: false
        },
  
        /**
         * True if the input device that caused the element to receive focus
         * was a keyboard.
         */
        receivedFocusFromKeyboard: {
          type: Boolean,
          readOnly: true
        },
  
        /**
         * The aria attribute to be set if the button is a toggle and in the
         * active state.
         */
        ariaActiveAttribute: {
          type: String,
          value: 'aria-pressed',
          observer: '_ariaActiveAttributeChanged'
        }
      },
  
      listeners: {
        down: '_downHandler',
        up: '_upHandler',
        tap: '_tapHandler'
      },
  
      observers: [
        '_detectKeyboardFocus(focused)',
        '_activeChanged(active, ariaActiveAttribute)'
      ],
  
      keyBindings: {
        'enter:keydown': '_asyncClick',
        'space:keydown': '_spaceKeyDownHandler',
        'space:keyup': '_spaceKeyUpHandler',
      },
  
      _mouseEventRe: /^mouse/,
  
      _tapHandler: function() {
        if (this.toggles) {
         // a tap is needed to toggle the active state
          this._userActivate(!this.active);
        } else {
          this.active = false;
        }
      },
  
      _detectKeyboardFocus: function(focused) {
        this._setReceivedFocusFromKeyboard(!this.pointerDown && focused);
      },
  
      // to emulate native checkbox, (de-)activations from a user interaction fire
      // 'change' events
      _userActivate: function(active) {
        if (this.active !== active) {
          this.active = active;
          this.fire('change');
        }
      },
  
73bcce88   luigser   COMPONENTS
128
      _downHandler: function(event) {
73bcce88   luigser   COMPONENTS
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
        this._setPointerDown(true);
        this._setPressed(true);
        this._setReceivedFocusFromKeyboard(false);
      },
  
      _upHandler: function() {
        this._setPointerDown(false);
        this._setPressed(false);
      },
  
      _spaceKeyDownHandler: function(event) {
        var keyboardEvent = event.detail.keyboardEvent;
        keyboardEvent.preventDefault();
        keyboardEvent.stopImmediatePropagation();
        this._setPressed(true);
      },
  
      _spaceKeyUpHandler: function() {
        if (this.pressed) {
          this._asyncClick();
        }
        this._setPressed(false);
      },
  
      // trigger click asynchronously, the asynchrony is useful to allow one
      // event handler to unwind before triggering another event
      _asyncClick: function() {
        this.async(function() {
          this.click();
        }, 1);
      },
  
      // any of these changes are considered a change to button state
  
      _pressedChanged: function(pressed) {
        this._changedButtonState();
      },
  
      _ariaActiveAttributeChanged: function(value, oldValue) {
        if (oldValue && oldValue != value && this.hasAttribute(oldValue)) {
          this.removeAttribute(oldValue);
        }
      },
  
      _activeChanged: function(active, ariaActiveAttribute) {
        if (this.toggles) {
          this.setAttribute(this.ariaActiveAttribute,
                            active ? 'true' : 'false');
        } else {
          this.removeAttribute(this.ariaActiveAttribute);
        }
        this._changedButtonState();
      },
  
      _controlStateChanged: function() {
        if (this.disabled) {
          this._setPressed(false);
        } else {
          this._changedButtonState();
        }
      },
  
      // provide hook for follow-on behaviors to react to button-state
  
      _changedButtonState: function() {
        if (this._buttonStateChanged) {
          this._buttonStateChanged(); // abstract
        }
      }
  
    };
  
    /** @polymerBehavior */
    Polymer.IronButtonState = [
      Polymer.IronA11yKeysBehavior,
      Polymer.IronButtonStateImpl
    ];
  
  </script>