-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
312 lines (259 loc) · 5.98 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Module dependencies
*/
var
Emitter = require('emitter'),
events = require('event'),
domify = require('domify'),
rotate = require('rotate'),
extend = require('extend'),
template = require('./template');
/**
* Expose `Dial`
*/
module.exports = Dial;
/**
* A constructor for a Dial object,
* creating a radial UI dial controlling
* an input field
*
* Options
*
* - `min` minimum value
* - `max` maximum value
* - `float` boolean determing whether floats are allowed
* - `increment` value to increment by
* - `value` starting value
*
* @param {HTMLInputElement} input
* @param {Object} options
* @api public
*/
function Dial (input, options) {
this.input = input;
this.options = extend(false,
Dial.defaults,
optionsFromAttrs(this.input),
options
);
Emitter(this);
this.value = this.options.value;
this
.setDefaultIncrement()
.render()
.bind()
.hideInput();
this.input.parentNode.appendChild(this.el);
this.rotate();
}
/**
* Defaults for each dial.
* Increment is handled in setDefaultIncrement
*/
Dial.defaults = {
min: 0,
max: 10,
value: 5,
float: false,
increment: null
};
/**
* Creates the dial's `el` property
*
* @return {Dial}
* @api public
*/
Dial.prototype.render = function () {
this.el = domify(template);
return this;
};
/**
* Binds mouse events for the dial element,
* and on document if not already bound
*
* @return {Dial}
* @api public
*/
Dial.prototype.bind = function () {
var that = this;
events.bind(this.el, 'mousedown', mouseDownHandler);
events.bind(this.input, 'change', inputChangeHandler);
// Bind mouseup and movemouse handlers
// to document for better dragging
if (!Dial.docEventsBound) {
events.bind(document, 'mouseup', onMouseUp);
events.bind(document, 'mousemove', onMouseMove);
Dial.docEventsBound = true;
}
/**
* Unbinds dial's events, not on prototype
* due to unbinding event on input element in scope
*
* @return {Dial}
* @api public
*/
this.unbind = function () {
events.unbind(this.el, 'mousedown');
events.unbind(this.input, 'change', inputChangeHandler);
return this;
};
function mouseDownHandler () {
onMouseDown.apply(that, arguments);
}
function inputChangeHandler () {
that.set(this.value);
}
return this;
};
/**
* Removes dial element, reveals original input element,
* unbinds dial events.
*
* @return {Dial}
* @api public
*/
Dial.prototype.destroy = function () {
var parent = this.el.parentElement;
this.unbind();
this.showInput();
parent.removeChild(this.el);
return this;
};
/**
* Sets the dial's value if in allowed range,
* rounds to integer if needed, updates the corresponding
* input element, rotates dial and fires a change event
*
* @param {Number} val
* @return {Dial}
* @api public
*/
Dial.prototype.set = function (val) {
if (val < this.options.min || val > this.options.max) {
return;
}
if (!this.options.float) {
val = ~~val;
}
this.input.value = val;
this.value = val;
this.emit('change', val);
this.rotate();
return this;
};
/**
* Rotates the dial el based off of current
* dial's value
*
* @return {Dial}
* @api public
*/
Dial.prototype.rotate = function () {
var MAX_DEG = 270;
var shift = 405;
var relative = (MAX_DEG / (this.options.max - this.options.min)) *
(this.value - this.options.min);
var degrees = relative - MAX_DEG + shift;
rotate(this.el.children[0], degrees);
return this;
};
/**
* Hides original input element
*
* @return this;
*/
Dial.prototype.hideInput = function () {
this.originalDisplay = this.originalDisplay || this.input.style.display;
this.input.style.display = 'none';
return this;
};
/**
* Shows original input element
*
* @return this;
*/
Dial.prototype.showInput = function () {
this.input.style.display = this.originalDisplay;
return this;
};
/**
* If no increment specific in config or attributes,
* set an increment of a 1/25th of the allowable range.
* Round to integer if necessary.
*
* @return this
* @api public
*/
Dial.prototype.setDefaultIncrement = function () {
var options = this.options;
var increment;
if (!options.increment) {
increment = (options.max - options.min) / 25;
if (!options.float) {
increment = ~~increment || 1;
}
options.increment = increment;
}
return this;
};
/**
* Takes an input element and parses attributes
* and returns an object containing the attributes
* if not empty.
*
* @param {HTMLInputElement} input
* @return {Object}
* @api private
*/
function optionsFromAttrs (input) {
var options = {};
var min = parseFloatAttr(input, 'min');
var max = parseFloatAttr(input, 'max');
var value = parseFloatAttr(input, 'value');
var float = !!input.getAttribute('data-float');
var inc = parseFloatAttr(input, 'increment');
min != undefined && (options.min = min);
max != undefined && (options.max = max);
float != undefined && (options.float = float);
inc != undefined && (options.increment = inc);
value !== '' && (options.value = value);
return options;
}
/**
* Parses an attribute on an element
* and returns as float if it exists
*
* @param {HTMLInputElement} el
* @param {String} attr
* @return {Mixed}
* @api private
*/
function parseFloatAttr (el, attr) {
var value = attr === 'value' ?
el.value :
el.getAttribute('data-' + attr);
value = value && parseFloat(value);
return value;
}
function onMouseMove (e) {
var dial = Dial.activeDial;
if (!dial) { return; }
var options = dial.options;
var curPos = e.pageX;
var lastPos = dial.pos !== undefined ? dial.pos : curPos;
// Increase
if (curPos > lastPos) {
dial.set(dial.value + options.increment);
// Decrease
} else if (curPos < lastPos) {
dial.set(dial.value - options.increment);
}
dial.pos = curPos;
}
function onMouseDown (e) {
Dial.activeDial = this;
e.preventDefault();
}
function onMouseUp (e) {
Dial.activeDial = null;
}