-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathNumberControllerBox.js
More file actions
128 lines (108 loc) · 3.53 KB
/
NumberControllerBox.js
File metadata and controls
128 lines (108 loc) · 3.53 KB
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
/**
* dat-gui JavaScript Controller Library
* http://code.google.com/p/dat-gui
*
* Copyright 2011 Data Arts Team, Google Creative Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
import NumberController from './NumberController';
import dom from '../dom/dom';
import common from '../utils/common';
function roundToDecimal(value, decimals) {
const tenTo = Math.pow(10, decimals);
return Math.round(value * tenTo) / tenTo;
}
/**
* @class Represents a given property of an object that is a number and
* provides an input element with which to manipulate it.
*
* @extends dat.controllers.Controller
* @extends dat.controllers.NumberController
*
* @param {Object} object The object to be manipulated
* @param {string} property The name of the property to be manipulated
* @param {Object} [params] Optional parameters
* @param {Number} [params.min] Minimum allowed value
* @param {Number} [params.max] Maximum allowed value
* @param {Number} [params.step] Increment by which to change value
*/
class NumberControllerBox extends NumberController {
constructor(object, property, params) {
super(object, property, params);
this.__truncationSuspended = false;
const _this = this;
/**
* {Number} Previous mouse y position
* @ignore
*/
let prevY;
function onChange() {
const attempted = parseFloat(_this.__input.value);
if (!common.isNaN(attempted)) {
_this.setValue(attempted);
}
}
function onFinish() {
if (_this.__onFinishChange) {
_this.__onFinishChange.call(_this, _this.getValue());
}
}
function onBlur() {
onFinish();
}
function onMouseDrag(e) {
const diff = prevY - e.clientY;
_this.setValue(_this.getValue() + diff * _this.__impliedStep);
prevY = e.clientY;
}
function onMouseUp() {
dom.unbind(window, 'mousemove', onMouseDrag);
dom.unbind(window, 'mouseup', onMouseUp);
onFinish();
}
function onMouseDown(e) {
dom.bind(window, 'mousemove', onMouseDrag);
dom.bind(window, 'mouseup', onMouseUp);
prevY = e.clientY;
}
this.__input = document.createElement('input');
this.__input.setAttribute('type', 'text');
// Makes it so manually specified values are not truncated.
dom.bind(this.__input, 'change', onChange);
dom.bind(this.__input, 'blur', onBlur);
dom.bind(this.__input, 'mousedown', onMouseDown);
dom.bind(this.__input, 'keydown', function(e) {
// When pressing enter, you can be as precise as you want.
const step = _this.__step || 1;
switch (e.keyCode) {
case 13:
_this.__truncationSuspended = true;
this.blur();
_this.__truncationSuspended = false;
onFinish();
break;
case 38:
var newVal = _this.getValue() + step;
_this.setValue(newVal);
break;
case 40: // down
var newVal = _this.getValue() - step;
_this.setValue(newVal);
break;
}
});
this.updateDisplay();
this.domElement.appendChild(this.__input);
}
updateDisplay(force) {
if (!force && dom.isActive(this.__input)) return this;
this.__input.value = this.__truncationSuspended ? this.getValue() : roundToDecimal(this.getValue(), this.__precision);
return super.updateDisplay();
}
}
export default NumberControllerBox;