-
Notifications
You must be signed in to change notification settings - Fork 332
/
paper-switch.js
168 lines (138 loc) · 4.78 KB
/
paper-switch.js
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
128
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
/**
* @module ember-paper
*/
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { assert } from '@ember/debug';
import { get, computed } from '@ember/object';
import { run } from '@ember/runloop';
import { htmlSafe } from '@ember/string';
import layout from '../templates/components/paper-switch';
import FocusableMixin from 'ember-paper/mixins/focusable-mixin';
import RippleMixin from 'ember-paper/mixins/ripple-mixin';
import ColorMixin from 'ember-paper/mixins/color-mixin';
import ProxiableMixin from 'ember-paper/mixins/proxiable-mixin';
import { invokeAction } from 'ember-invoke-action';
/* global Hammer */
/**
* @class PaperSwitch
* @extends Ember.Component
* @uses FocusableMixin
* @uses RippleMixin
* @uses ColorMixin
* @uses ProxiableMixin
*/
export default Component.extend(FocusableMixin, RippleMixin, ColorMixin, ProxiableMixin, {
layout,
tagName: 'md-switch',
classNames: ['paper-switch', 'md-default-theme'],
classNameBindings: ['value:md-checked', 'dragging:md-dragging'],
toggle: true,
constants: service(),
/* Ripple Overrides */
rippleContainerSelector: '.md-thumb',
center: true,
dimBackground: false,
fitRipple: true,
value: false,
disabled: false,
dragging: false,
thumbContainerStyle: computed('dragging', 'dragAmount', function() {
if (!this.get('dragging')) {
return htmlSafe('');
}
let translate = Math.max(0, Math.min(100, this.get('dragAmount') * 100));
let transformProp = `translate3d(${translate}%, 0, 0)`;
return htmlSafe(`transform: ${transformProp};-webkit-transform: ${transformProp}`);
}),
didInsertElement() {
this._super(...arguments);
// Only setup if the switch is not disabled
if (!this.get('disabled')) {
this._setupSwitch();
}
},
init() {
this._super(...arguments);
assert('{{paper-switch}} requires an `onChange` action or null for no action.', this.get('onChange') !== undefined);
},
willDestroyElement() {
this._super(...arguments);
this._teardownSwitch();
},
didUpdateAttrs() {
this._super(...arguments);
if (!this.get('disabled') && !this._switchContainerHammer) {
this._setupSwitch();
} else if (!this.get('disabled') && this._switchContainerHammer) {
this._switchContainerHammer.set({ enable: true });
} else if (this.get('disabled') && this._switchContainerHammer) {
this._switchContainerHammer.set({ enable: false });
}
},
_setupSwitch() {
this.set('switchWidth', this.element.querySelector('.md-thumb-container').offsetWidth);
let switchContainer = this.element.querySelector('.md-container');
let switchHammer = new Hammer(switchContainer);
this._switchContainerHammer = switchHammer;
// Enable dragging the switch container
switchHammer.get('pan').set({ threshold: 1 });
switchHammer.on('panstart', run.bind(this, this._dragStart))
.on('panmove', run.bind(this, this._drag))
.on('panend', run.bind(this, this._dragEnd));
// Enable tapping gesture on the switch
this._switchHammer = new Hammer(this.element);
this._switchHammer.on('tap', run.bind(this, this._dragEnd));
this._onClickHandleNativeClick = run.bind(this, this._handleNativeClick);
this.element.querySelector('.md-container')
.addEventListener('click', this._onClickHandleNativeClick);
},
_handleNativeClick() {
return get(this, 'bubbles');
},
_teardownSwitch() {
if (this._switchContainerHammer) {
this._switchContainerHammer.destroy();
this._switchHammer.destroy();
}
this.element.querySelector('.md-container')
.removeEventListener('click', this._onClickHandleNativeClick);
this._onClickHandleNativeClick = null;
},
_dragStart() {
this.set('dragAmount', +this.get('value'));
this.set('dragging', true);
},
_drag(event) {
if (!this.get('disabled')) {
// Set the amount the switch has been dragged
this.set('dragAmount', +this.get('value') + event.deltaX / this.get('switchWidth'));
}
},
_dragEnd() {
if (!this.get('disabled')) {
let value = this.get('value');
let dragAmount = this.get('dragAmount');
if (!this.get('dragging') || (value && dragAmount < 0.5) || (!value && dragAmount > 0.5)) {
invokeAction(this, 'onChange', !value);
}
this.set('dragging', false);
this.set('dragAmount', null);
}
},
focusIn() {
// Focusing in w/o being pressed should use the default behavior
if (!this.get('pressed')) {
this._super(...arguments);
}
},
keyPress(ev) {
if (ev.which === this.get('constants.KEYCODE.SPACE') || ev.which === this.get('constants.KEYCODE.ENTER')) {
ev.preventDefault();
this._dragEnd();
}
},
processProxy() {
invokeAction(this, 'onChange', !this.get('value'));
}
});