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