| OLD | NEW |
| (Empty) |
| 1 <!-- | |
| 2 @license | |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
| 7 Code distributed by Google as part of the polymer project is also | |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
| 9 --> | |
| 10 | |
| 11 <link rel="import" href="../polymer/polymer.html"> | |
| 12 <link rel="import" href="../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html
"> | |
| 13 <link rel="import" href="iron-control-state.html"> | |
| 14 | |
| 15 <script> | |
| 16 | |
| 17 /** @polymerBehavior Polymer.IronButtonState */ | |
| 18 Polymer.IronButtonStateImpl = { | |
| 19 | |
| 20 properties: { | |
| 21 | |
| 22 /** | |
| 23 * If true, the user is currently holding down the button. | |
| 24 * | |
| 25 * @attribute pressed | |
| 26 * @type boolean | |
| 27 * @default false | |
| 28 */ | |
| 29 pressed: { | |
| 30 type: Boolean, | |
| 31 readOnly: true, | |
| 32 value: false, | |
| 33 reflectToAttribute: true, | |
| 34 observer: '_pressedChanged' | |
| 35 }, | |
| 36 | |
| 37 /** | |
| 38 * If true, the button toggles the active state with each tap or press | |
| 39 * of the spacebar. | |
| 40 * | |
| 41 * @attribute toggles | |
| 42 * @type boolean | |
| 43 * @default false | |
| 44 */ | |
| 45 toggles: { | |
| 46 type: Boolean, | |
| 47 value: false, | |
| 48 reflectToAttribute: true | |
| 49 }, | |
| 50 | |
| 51 /** | |
| 52 * If true, the button is a toggle and is currently in the active state. | |
| 53 * | |
| 54 * @attribute active | |
| 55 * @type boolean | |
| 56 * @default false | |
| 57 */ | |
| 58 active: { | |
| 59 type: Boolean, | |
| 60 value: false, | |
| 61 notify: true, | |
| 62 reflectToAttribute: true, | |
| 63 observer: '_activeChanged' | |
| 64 }, | |
| 65 | |
| 66 /** | |
| 67 * True if the element is currently being pressed by a "pointer," which | |
| 68 * is loosely defined as mouse or touch input (but specifically excluding | |
| 69 * keyboard input). | |
| 70 */ | |
| 71 pointerDown: { | |
| 72 type: Boolean, | |
| 73 readOnly: true, | |
| 74 value: false | |
| 75 }, | |
| 76 | |
| 77 /** | |
| 78 * True if the input device that caused the element to receive focus | |
| 79 * was a keyboard. | |
| 80 */ | |
| 81 receivedFocusFromKeyboard: { | |
| 82 type: Boolean, | |
| 83 readOnly: true | |
| 84 } | |
| 85 }, | |
| 86 | |
| 87 listeners: { | |
| 88 down: '_downHandler', | |
| 89 up: '_upHandler', | |
| 90 tap: '_tapHandler' | |
| 91 }, | |
| 92 | |
| 93 observers: [ | |
| 94 '_detectKeyboardFocus(focused)' | |
| 95 ], | |
| 96 | |
| 97 keyBindings: { | |
| 98 'enter:keydown': '_asyncClick', | |
| 99 'space:keydown': '_spaceKeyDownHandler', | |
| 100 'space:keyup': '_spaceKeyUpHandler', | |
| 101 }, | |
| 102 | |
| 103 _tapHandler: function() { | |
| 104 if (this.toggles) { | |
| 105 // a tap is needed to toggle the active state | |
| 106 this._userActivate(!this.active); | |
| 107 } else { | |
| 108 this.active = false; | |
| 109 } | |
| 110 }, | |
| 111 | |
| 112 _detectKeyboardFocus: function(focused) { | |
| 113 this._setReceivedFocusFromKeyboard(!this.pointerDown && focused); | |
| 114 }, | |
| 115 | |
| 116 // to emulate native checkbox, (de-)activations from a user interaction fire | |
| 117 // 'change' events | |
| 118 _userActivate: function(active) { | |
| 119 this.active = active; | |
| 120 this.fire('change'); | |
| 121 }, | |
| 122 | |
| 123 _downHandler: function() { | |
| 124 this._setPointerDown(true); | |
| 125 this._setPressed(true); | |
| 126 this._setReceivedFocusFromKeyboard(false); | |
| 127 }, | |
| 128 | |
| 129 _upHandler: function() { | |
| 130 this._setPointerDown(false); | |
| 131 this._setPressed(false); | |
| 132 }, | |
| 133 | |
| 134 _spaceKeyDownHandler: function(event) { | |
| 135 var keyboardEvent = event.detail.keyboardEvent; | |
| 136 keyboardEvent.preventDefault(); | |
| 137 keyboardEvent.stopImmediatePropagation(); | |
| 138 this._setPressed(true); | |
| 139 }, | |
| 140 | |
| 141 _spaceKeyUpHandler: function() { | |
| 142 if (this.pressed) { | |
| 143 this._asyncClick(); | |
| 144 } | |
| 145 this._setPressed(false); | |
| 146 }, | |
| 147 | |
| 148 // trigger click asynchronously, the asynchrony is useful to allow one | |
| 149 // event handler to unwind before triggering another event | |
| 150 _asyncClick: function() { | |
| 151 this.async(function() { | |
| 152 this.click(); | |
| 153 }, 1); | |
| 154 }, | |
| 155 | |
| 156 // any of these changes are considered a change to button state | |
| 157 | |
| 158 _pressedChanged: function(pressed) { | |
| 159 this._changedButtonState(); | |
| 160 }, | |
| 161 | |
| 162 _activeChanged: function(active) { | |
| 163 if (this.toggles) { | |
| 164 this.setAttribute('aria-pressed', active ? 'true' : 'false'); | |
| 165 } else { | |
| 166 this.removeAttribute('aria-pressed'); | |
| 167 } | |
| 168 this._changedButtonState(); | |
| 169 }, | |
| 170 | |
| 171 _controlStateChanged: function() { | |
| 172 if (this.disabled) { | |
| 173 this._setPressed(false); | |
| 174 } else { | |
| 175 this._changedButtonState(); | |
| 176 } | |
| 177 }, | |
| 178 | |
| 179 // provide hook for follow-on behaviors to react to button-state | |
| 180 | |
| 181 _changedButtonState: function() { | |
| 182 if (this._buttonStateChanged) { | |
| 183 this._buttonStateChanged(); // abstract | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 }; | |
| 188 | |
| 189 /** @polymerBehavior Polymer.IronButtonState */ | |
| 190 Polymer.IronButtonState = [ | |
| 191 Polymer.IronA11yKeysBehavior, | |
| 192 Polymer.IronButtonStateImpl | |
| 193 ]; | |
| 194 | |
| 195 </script> | |
| OLD | NEW |