OLD | NEW |
| (Empty) |
1 | |
2 | |
3 Polymer.IronButtonState = { | |
4 | |
5 properties: { | |
6 | |
7 /** | |
8 * If true, the user is currently holding down the button. | |
9 * | |
10 * @attribute pressed | |
11 * @type boolean | |
12 * @default false | |
13 */ | |
14 pressed: { | |
15 type: Boolean, | |
16 readOnly: true, | |
17 reflectToAttribute: true, | |
18 observer: '_pressedChanged' | |
19 }, | |
20 | |
21 /** | |
22 * If true, the button toggles the active state with each tap or press | |
23 * of the spacebar. | |
24 * | |
25 * @attribute toggles | |
26 * @type boolean | |
27 * @default false | |
28 */ | |
29 toggles: { | |
30 type: Boolean, | |
31 reflectToAttribute: true | |
32 }, | |
33 | |
34 /** | |
35 * If true, the button is a toggle and is currently in the active state. | |
36 * | |
37 * @attribute active | |
38 * @type boolean | |
39 * @default false | |
40 */ | |
41 active: { | |
42 type: Boolean, | |
43 notify: true, | |
44 reflectToAttribute: true, | |
45 observer: '_activeChanged' | |
46 } | |
47 | |
48 }, | |
49 | |
50 listeners: { | |
51 mousedown: '_downHandler', | |
52 mouseup: '_upHandler', | |
53 keydown: '_keyDownHandler', | |
54 keyup: '_keyUpHandler', | |
55 tap: '_tapHandler' | |
56 }, | |
57 | |
58 _tapHandler: function() { | |
59 if (this.toggles) { | |
60 // a tap is needed to toggle the active state | |
61 this._userActivate(!this.active); | |
62 } else { | |
63 this.active = false; | |
64 } | |
65 }, | |
66 | |
67 // to emulate native checkbox, (de-)activations from a user interaction fire | |
68 // 'change' events | |
69 _userActivate: function(active) { | |
70 this.active = active; | |
71 this.fire('change'); | |
72 }, | |
73 | |
74 _downHandler: function() { | |
75 this._setPressed(true); | |
76 }, | |
77 | |
78 _upHandler: function(e) { | |
79 this._setPressed(false); | |
80 }, | |
81 | |
82 _keyDownHandler: function(e) { | |
83 switch(e.keyCode) { | |
84 case this.keyCodes.ENTER_KEY: | |
85 this._asyncClick(); | |
86 break; | |
87 | |
88 case this.keyCodes.SPACE: | |
89 this._setPressed(true); | |
90 break; | |
91 | |
92 } | |
93 }, | |
94 | |
95 _keyUpHandler: function(e) { | |
96 if (e.keyCode == this.keyCodes.SPACE) { | |
97 if (this.pressed) { | |
98 this._asyncClick(); | |
99 } | |
100 this._setPressed(false); | |
101 } | |
102 }, | |
103 | |
104 // trigger click asynchronously, the asynchrony is useful to allow one | |
105 // event handler to unwind before triggering another event | |
106 _asyncClick: function() { | |
107 this.async(function() { | |
108 this.click(); | |
109 }, 1); | |
110 }, | |
111 | |
112 // any of these changes are considered a change to button state | |
113 | |
114 _pressedChanged: function(pressed) { | |
115 this._changedButtonState(); | |
116 }, | |
117 | |
118 _activeChanged: function(active) { | |
119 this.setAttribute('aria-pressed', active ? 'true' : 'false'); | |
120 this._changedButtonState(); | |
121 }, | |
122 | |
123 _controlStateChanged: function() { | |
124 if (this.disabled) { | |
125 this._setPressed(false); | |
126 this.active = false; | |
127 } else { | |
128 this._changedButtonState(); | |
129 } | |
130 }, | |
131 | |
132 // provide hook for follow-on behaviors to react to button-state | |
133 | |
134 _changedButtonState: function() { | |
135 if (this._buttonStateChanged) { | |
136 this._buttonStateChanged(); // abstract | |
137 } | |
138 } | |
139 | |
140 }; | |
141 | |
OLD | NEW |