-
Notifications
You must be signed in to change notification settings - Fork 51
/
range-slider.js
186 lines (156 loc) · 4.16 KB
/
range-slider.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* eslint ember/closure-actions: 0 */
import { A } from '@ember/array';
import Component from '@ember/component';
import { run } from '@ember/runloop';
import { isEmpty } from '@ember/utils';
import { observer, computed } from '@ember/object';
import Ember from 'ember';
import noUiSlider from 'noUiSlider';
const {
Logger: { warn }
} = Ember;
export default Component.extend({
attributeBindings: ['disabledOrUndefined:disabled'],
slider: null,
start: undefined,
step: undefined,
margin: undefined,
padding: undefined,
limit: undefined,
pips: undefined,
animate: true,
snap: false,
connect: false,
disabled: false,
orientation: 'horizontal',
direction: 'ltr',
behaviour: 'tap',
tooltips: false,
multitouch: false,
keyboardSupport: true,
min: 0,
max: 100,
range: computed('min', 'max', function() {
return {
min: this.get('min'),
max: this.get('max')
};
}),
formatTo(value) {
return value;
},
formatFrom(value) {
return +value;
},
format: computed('formatTo', 'formatFrom', function() {
return {
to: this.get('formatTo'),
from: this.get('formatFrom')
};
}),
didInsertElement() {
this.setup();
},
setup() {
let element = this.get('element');
let { noUiSlider: slider } = element;
let properties = this.getProperties(
'start', 'step', 'margin', 'padding',
'limit', 'range', 'connect',
'orientation', 'direction',
'behaviour', 'animate', 'snap',
'pips', 'format', 'tooltips',
'multitouch', 'cssPrefix',
'cssClasses', 'keyboardSupport'
);
let sliderEvents = A(['change', 'set', 'slide', 'update']);
// We first check if the element has a slider already created
if (slider && slider.destroy) {
slider.destroy();
}
try {
slider = noUiSlider.create(element, properties, true);
} catch (err) {
warn(`[ember-cli-nouislider]: ${err}`);
}
this.slider = slider;
sliderEvents.forEach(event => {
const eventActionName = `on-${event}`;
if (!isEmpty(this.get(eventActionName))) {
slider.on(event, () => {
run(this, function() {
const val = this.get('slider').get();
const action = this.get(eventActionName);
if (typeof(action) === 'string') {
// Note that `sendAction` is deprecated and this will trigger a deprecation message.
this.sendAction(eventActionName, val);
} else if (typeof(action) === 'function') {
action(val);
}
});
});
}
});
slider.on('start', () => {
run(this, function() {
this.onStart();
if (!isEmpty(this.get(`on-start`))) {
let val = this.get("slider").get();
this.sendAction(`on-start`, val);
}
});
});
slider.on('end', () => {
run(this, function() {
this.onEnd();
if (!isEmpty(this.get(`on-end`))) {
let val = this.get("slider").get();
this.sendAction(`on-end`, val);
}
});
});
},
onStart() {
this.sliding = true;
},
onEnd() {
delete this.sliding;
},
didUpdateAttrs() {
this.update();
},
update() {
let { slider } = this;
let properties = this.getProperties(
'margin', 'limit', 'step',
'range', 'animate', 'snap',
'start', 'padding', 'keyboardSupport'
);
if (slider) {
slider.updateOptions(properties);
}
},
willDestroyElement() {
this.teardown();
},
teardown() {
let { slider } = this;
// Remove all event listeners
slider.off();
slider.destroy();
},
setValue: observer('start', function() {
let { slider } = this;
if (slider && !this.sliding) {
let value = this.get('start');
slider.set(value);
}
}),
// disabled can't be just `false` - this leads to an attribute of disabled="false"
disabledOrUndefined: computed('disabled', function() {
if (this.get('disabled')) {
return true;
}
return undefined;
})
});