-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularInput.js
326 lines (281 loc) · 10.8 KB
/
circularInput.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = factory();
} else {
window.circularInput = factory();
}
})(function () {
'use strict';
var elementsAmount = 0,
uid = 'circularInput' + new Date().getTime(),
cache = [];
function isNodeList (object) {
var stringRepr = Object.prototype.toString.call(object);
return typeof object === 'object' &&
/^\[object (HTMLCollection|NodeList|Object)\]$/.test(stringRepr) &&
(typeof object.length === 'number') &&
(object.length === 0 || (typeof object[0] === 'object' && object[0].nodeType > 0));
}
function stringToDom (string) {
var div = document.createElement('div');
div.innerHTML = string;
return div.childNodes;
}
function stringToSvg (string) {
var div,
frag;
div = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
div.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg">' + string + '</svg>';
frag = document.createDocumentFragment();
while (div.firstChild.firstChild) {
frag.appendChild(div.firstChild.firstChild);
}
return frag;
}
function extend (rootObj, extendingObj) {
for (var i in extendingObj) {
if (extendingObj.hasOwnProperty(i)) {
rootObj[i] = extendingObj[i];
}
}
return rootObj;
}
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees) * Math.PI / 180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
function describeArc(x, y, radius, startAngle, endAngle){
var start = polarToCartesian(x, y, radius, endAngle);
var end = polarToCartesian(x, y, radius, startAngle);
var arcSweep = endAngle - startAngle <= 180 ? '0' : '1';
var d = [
'M', start.x, start.y,
'A', radius, radius, 0, arcSweep, 0, end.x, end.y
].join(' ');
return d;
}
var triggerEvent = (function () {
if (document.createEvent) {
return function (target, type) {
var event = document.createEvent('Event');
event.initEvent(type, true, true);
target.dispatchEvent(event);
}
} else {
return function (target, type) {
var event = document.createEventObject();
target.fireEvent('on' + type, event);
}
}
})();
function getDecimalPartLength (number) {
var numberParts = number.toString().split('.');
if (numberParts.length === 1) {
return 0;
} else if (numberParts.length > 1){
return numberParts.pop().length;
}
}
function roundTo (value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
function circularInput (element, options) {
if (!(this instanceof circularInput)) { // force 'new' keyword
return new circularInput(element, options);
}
if (isNodeList(element)) {
var elementsArray = [];
for (var i = 0; i < element.length; i++) {
elementsArray.push(new circularInput(element[i], options));
}
return elementsArray;
}
if (typeof element[uid] !== 'undefined') {
return cache[element[uid]]; // return existing object
} else {
element[uid] = elementsAmount;
cache[elementsAmount] = this;
elementsAmount++;
}
this.input = element;
this.svgEl = null;
this.indicator = null;
this.activeArc = null;
this.options = extend({}, circularInput.DEFAULTS);
this.options = extend(this.options, this.getDomOptions());
this.options = extend(this.options, options);
this.value = this.input.value ? Number(this.input.value) : this.options.min;
this.pxPerStep = this.options.sensivity / ((this.options.max - this.options.min) / this.options.step);
this.decimalPartLength = getDecimalPartLength(this.options.step);
this.init();
return this;
}
circularInput.DEFAULTS = {
min: 0,
max: 100,
step: 5,
sensivity: 100,
theme: '',
svgDefs: '',
};
circularInput.CLASS_NAMES = {
meter: 'circ-input__meter',
circle: 'circ-input__circle',
basicArc: 'circ-input__basic-arc',
activeArc: 'circ-input__active-arc',
indicator: 'circ-input__indicator'
};
circularInput.DISPLAY = {
arcAngle: 300,
arcRadius: 14,
boxSize: 36
};
circularInput.domStrings = {
container: '<div class="circ-input"></div>',
svg : ['<svg class="circ-input__svg" ',
'viewBox="0 0 ', circularInput.DISPLAY.boxSize, ' ', circularInput.DISPLAY.boxSize , '" ',
'version="1.1" xmlns="http://www.w3.org/2000/svg">',
'<defs></defs>',
'<path class="', circularInput.CLASS_NAMES.meter,'"></path>',
'<circle ',
'cx="', circularInput.DISPLAY.boxSize/2, '" ',
'cy="', circularInput.DISPLAY.boxSize/2, '" r="14" ',
'class="', circularInput.CLASS_NAMES.circle,'"></circle>',
'<path class="', circularInput.CLASS_NAMES.basicArc, '"></path>',
'<path class="', circularInput.CLASS_NAMES.activeArc, '"></path>',
'<line class="', circularInput.CLASS_NAMES.indicator, '" x1="0" y1="0" x2="0" y2="', circularInput.DISPLAY.arcRadius, '"></line>',
'</svg>'].join('')
};
circularInput.prototype = {
getDomOptions: function () {
var domOptions = {},
optionNames = Object.keys(circularInput.DEFAULTS);
for (var i = 0; i < optionNames.length; i++) {
var optionName = optionNames[i],
optionValue = this.input.getAttribute('data-' + optionName);
if (optionValue !== null) {
domOptions[optionName] = Number(optionValue);
}
}
return domOptions;
},
init: function () {
this.createDOM();
this.addEventHandlers();
this.updateView();
},
createDOM: function () {
var container = stringToDom(circularInput.domStrings.container)[0],
circle,
meter,
basicArc,
defs;
this.input.parentNode.insertBefore(container, this.input);
container.innerHTML = circularInput.domStrings.svg;
container.appendChild(this.input);
this.svgEl = container.firstChild;
this.indicator = container.getElementsByClassName(circularInput.CLASS_NAMES.indicator)[0];
this.activeArc = container.getElementsByClassName(circularInput.CLASS_NAMES.activeArc)[0];
defs = container.getElementsByTagName('defs')[0];
circle = container.getElementsByClassName(circularInput.CLASS_NAMES.circle)[0];
circle.setAttribute('d', this.describeArc(359));
meter = container.getElementsByClassName(circularInput.CLASS_NAMES.basicArc)[0];
meter.setAttribute('d', this.describeArc(circularInput.DISPLAY.arcAngle));
basicArc = container.getElementsByClassName(circularInput.CLASS_NAMES.meter)[0];
basicArc.setAttribute('d', this.describeArc(circularInput.DISPLAY.arcAngle, circularInput.DISPLAY.arcRadius + 2));
if (this.options.theme) {
container.className += ' circ-input--' + this.options.theme;
}
defs.appendChild(stringToSvg(this.options.svgDefs));
},
addEventHandlers: function () {
var self = this;
this.svgEl.addEventListener('mousedown', function (e) {
e.preventDefault(); // http://stackoverflow.com/questions/9506041/javascript-events-mouseup-not-firing-after-mousemove
var initalMouseY = e.pageY,
initalValue = self.value,
unbindListeners = function () {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', unbindListeners);
},
handleMouseMove = function (e) {
self.mouseMoveValueChange(e, initalValue, initalMouseY);
}
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', unbindListeners);
});
this.input.addEventListener('blur', function () {
self.keyboardInputValueChange();
});
this.svgEl.addEventListener('touchmove', function (e) {
e.preventDefault();
});
},
mouseMoveValueChange: function (e, initalValue, initalMouseY) {
var mouseMoveDiff,
valueDiffAfterMove,
finalValue;
e.preventDefault();
mouseMoveDiff = -(e.pageY - initalMouseY); // if mouse goes up, value increases
valueDiffAfterMove = Math.round(mouseMoveDiff / this.pxPerStep) * this.options.step;
finalValue = initalValue + valueDiffAfterMove;
this.setValue(finalValue);
},
keyboardInputValueChange: function () {
var value = parseFloat(this.input.value);
this.setValue(value);
},
updateView: function () {
this.updateCricleView();
this.updateInputView();
},
updateCricleView: function () {
var valueProgress,
activeAngle,
rotation,
translation;
valueProgress = this.value / (this.options.max - this.options.min);
activeAngle = valueProgress * circularInput.DISPLAY.arcAngle;
rotation = this.getIndicatorRotation(activeAngle);
translation = this.getIndicatorTranslation();
this.indicator.setAttribute('transform', translation + ' ' + rotation);
this.activeArc.setAttribute('d', this.describeArc(activeAngle));
},
setValue: function (value) {
this.value = this.validateValue(value);
this.updateView();
},
updateInputView: function () {
this.input.value = this.value.toFixed(this.decimalPartLength);
triggerEvent(this.input, 'change');
},
getIndicatorRotation: function (angle) {
var additionalRotation = (360 - circularInput.DISPLAY.arcAngle) / 2;
return 'rotate(' + (angle + additionalRotation) + ' 0 0)';
},
getIndicatorTranslation: function () {
var translationVal = circularInput.DISPLAY.boxSize / 2;
return 'translate(' + translationVal + ' ' + translationVal + ')';
},
describeArc: function (angle, radius) {
var additionalRotation = 90 + (360 - circularInput.DISPLAY.arcAngle) / 2,
x = circularInput.DISPLAY.boxSize / 2,
y = circularInput.DISPLAY.boxSize / 2,
radius = typeof radius !== 'undefined' ? radius : circularInput.DISPLAY.arcRadius;
return describeArc(x, y, radius, additionalRotation, angle + additionalRotation);
},
validateValue: function (value) {
value = this.options.min + Math.round((value - this.options.min) / this.options.step) * this.options.step;
value = value <= this.options.max ? value : this.options.max;
value = value >= this.options.min ? value : this.options.min;
return roundTo(value, this.decimalPartLength);
}
};
return circularInput;
});