OLD | NEW |
1 | 1 |
2 | 2 |
3 Polymer('paper-toggle-button', { | 3 Polymer('paper-toggle-button', { |
4 | 4 |
5 /** | 5 /** |
6 * Fired when the checked state changes due to user interaction. | 6 * Fired when the checked state changes due to user interaction. |
7 * | 7 * |
8 * @event change | 8 * @event change |
9 */ | 9 */ |
10 | 10 |
11 /** | 11 /** |
12 * Fired when the checked state changes. | 12 * Fired when the checked state changes. |
13 * | 13 * |
14 * @event core-change | 14 * @event core-change |
15 */ | 15 */ |
16 | 16 |
17 /** | 17 /** |
18 * Gets or sets the state, `true` is checked and `false` is unchecked. | 18 * Gets or sets the state, `true` is checked and `false` is unchecked. |
19 * | 19 * |
20 * @attribute checked | 20 * @attribute checked |
21 * @type boolean | 21 * @type boolean |
22 * @default false | 22 * @default false |
23 */ | 23 */ |
24 checked: false, | 24 checked: false, |
25 | 25 |
26 /** | 26 /** |
27 * If true, the toggle button is disabled. A disabled toggle button cannot | 27 * If true, the toggle button is disabled. A disabled toggle button cannot |
28 * be tapped or dragged to change the checked state. | 28 * be tapped or dragged to change the checked state. |
29 * | 29 * |
30 * @attribute disabled | 30 * @attribute disabled |
31 * @type boolean | 31 * @type boolean |
32 * @default false | 32 * @default false |
33 */ | 33 */ |
34 disabled: false, | 34 disabled: false, |
35 | 35 |
| 36 eventDelegates: { |
| 37 down: 'downAction', |
| 38 up: 'upAction', |
| 39 tap: 'tap', |
| 40 trackstart: 'trackStart', |
| 41 trackx: 'trackx', |
| 42 trackend: 'trackEnd' |
| 43 }, |
| 44 |
| 45 downAction: function(e) { |
| 46 var rect = this.$.ink.getBoundingClientRect(); |
| 47 this.$.ink.downAction({ |
| 48 x: rect.left + rect.width / 2, |
| 49 y: rect.top + rect.height / 2 |
| 50 }); |
| 51 }, |
| 52 |
| 53 upAction: function(e) { |
| 54 this.$.ink.upAction(); |
| 55 }, |
| 56 |
| 57 tap: function() { |
| 58 if (this.disabled) { |
| 59 return; |
| 60 } |
| 61 this.checked = !this.checked; |
| 62 this.fire('change'); |
| 63 }, |
| 64 |
36 trackStart: function(e) { | 65 trackStart: function(e) { |
37 this._w = this.$.toggleBar.offsetLeft + this.$.toggleBar.offsetWidth; | 66 if (this.disabled) { |
| 67 return; |
| 68 } |
| 69 this._w = this.$.toggleBar.offsetWidth / 2; |
38 e.preventTap(); | 70 e.preventTap(); |
39 }, | 71 }, |
40 | 72 |
41 trackx: function(e) { | 73 trackx: function(e) { |
42 this._x = Math.min(this._w, | 74 this._x = Math.min(this._w, |
43 Math.max(0, this.checked ? this._w + e.dx : e.dx)); | 75 Math.max(0, this.checked ? this._w + e.dx : e.dx)); |
44 this.$.toggleRadio.classList.add('dragging'); | 76 this.$.toggleButton.classList.add('dragging'); |
45 var s = this.$.toggleRadio.style; | 77 var s = this.$.toggleButton.style; |
46 s.webkitTransform = s.transform = 'translate3d(' + this._x + 'px,0,0)'; | 78 s.webkitTransform = s.transform = 'translate3d(' + this._x + 'px,0,0)'; |
47 }, | 79 }, |
48 | 80 |
49 trackEnd: function() { | 81 trackEnd: function() { |
50 var s = this.$.toggleRadio.style; | 82 var s = this.$.toggleButton.style; |
51 s.transform = s.webkitTransform = ''; | 83 s.transform = s.webkitTransform = ''; |
52 this.$.toggleRadio.classList.remove('dragging'); | 84 this.$.toggleButton.classList.remove('dragging'); |
53 var old = this.checked; | 85 var old = this.checked; |
54 this.checked = Math.abs(this._x) > this._w / 2; | 86 this.checked = Math.abs(this._x) > this._w / 2; |
55 if (this.checked !== old) { | 87 if (this.checked !== old) { |
56 this.fire('change'); | 88 this.fire('change'); |
57 } | 89 } |
58 }, | 90 }, |
59 | 91 |
60 checkedChanged: function() { | 92 checkedChanged: function() { |
61 this.setAttribute('aria-pressed', Boolean(this.checked)); | 93 this.setAttribute('aria-pressed', Boolean(this.checked)); |
62 this.fire('core-change'); | 94 this.fire('core-change'); |
63 }, | |
64 | |
65 changeAction: function(e) { | |
66 e.stopPropagation(); | |
67 this.fire('change'); | |
68 }, | |
69 | |
70 stopPropagation: function(e) { | |
71 e.stopPropagation(); | |
72 } | 95 } |
73 | 96 |
74 }); | 97 }); |
75 | 98 |
OLD | NEW |