dat.gui/controller.number.js

132 lines
3.8 KiB
JavaScript
Raw Normal View History

2011-01-25 05:31:20 +00:00
// TODO: How do we intercept the press up/down event on number fields?
// TODO: Provide alternate controllers for non-html5 browsers?
// TODO: Firefox is retarded?
var NumberController = function() {
this.type = "number";
Controller.apply(this, arguments);
var _this = this;
// If we simply click and release a number field, we want to highlight it.
// This variable keeps track of whether or not we've draggedNumberField.
var draggedNumberField = false;
var clickedNumberField = false;
2011-01-25 05:31:20 +00:00
var y = py = 0;
2011-01-25 05:31:20 +00:00
var min = arguments[2];
var max = arguments[3];
var step = arguments[4];
if (!step) {
if (min && max) {
step = (max-min)*0.01;
} else {
step = 1;
}
}
2011-01-25 05:31:20 +00:00
var numberField = document.createElement('input');
numberField.setAttribute('id', this.propertyName);
// Little up and down arrows are pissing me off.
numberField.setAttribute('type', 'text');
2011-01-25 05:31:20 +00:00
numberField.setAttribute('value', this.getValue());
2011-01-24 20:53:33 +00:00
2011-01-25 05:31:20 +00:00
if (step) numberField.setAttribute('step', step);
2011-01-25 05:31:20 +00:00
this.domElement.appendChild(numberField);
2011-01-25 05:31:20 +00:00
var slider;
2011-01-25 06:51:06 +00:00
if (min && max &&
(navigator.appVersion.indexOf("chrome") != -1 || navigator.appVersion.indexOf("Safari") != -1)) {
2011-01-25 05:31:20 +00:00
slider = document.createElement('input');
slider.setAttribute('type', 'range');
2011-01-25 05:31:20 +00:00
slider.setAttribute('value', this.getValue());
slider.setAttribute('min', min);
slider.setAttribute('max', max);
slider.setAttribute('step', step);
slider.addEventListener('change', function(e) {
updateValue(this.value);
}, false);
this.domElement.appendChild(slider);
2011-01-24 22:02:28 +00:00
}
2011-01-25 05:31:20 +00:00
numberField.addEventListener('blur', function(e) {
var val = parseFloat(this.value);
2011-01-25 05:31:20 +00:00
if (!isNaN(val)) {
updateValue(val);
2011-01-24 20:53:33 +00:00
}
2011-01-24 22:02:28 +00:00
}, false);
2011-01-25 05:31:20 +00:00
numberField.addEventListener('mousewheel', function(e) {
e.preventDefault();
updateValue(_this.getValue() + Math.abs(e.wheelDeltaY)/e.wheelDeltaY*step);
return false;
}, false);
numberField.addEventListener('mousedown', function(e) {
2011-01-25 06:51:06 +00:00
py = y = e.pageY;
clickedNumberField = true;
2011-01-25 06:51:06 +00:00
document.addEventListener('mousemove', dragNumberField, false);
2011-01-25 05:31:20 +00:00
}, false);
document.addEventListener('mouseup', function(e) {
2011-01-25 06:51:06 +00:00
document.removeEventListener('mousemove', dragNumberField, false);
_this.makeSelectable(GUI.domElement);
_this.makeSelectable(numberField);
if (clickedNumberField && !draggedNumberField) {
numberField.focus();
numberField.select();
}
draggedNumberField = false;
clickedNumberField = false;
}, false);
2011-01-25 05:31:20 +00:00
2011-01-25 06:51:06 +00:00
if(navigator.appVersion.indexOf('chrome') != -1) {
document.addEventListener('mouseout', function(e) {
document.removeEventListener('mousemove', dragNumberField, false);
}, false);
}
2011-01-25 05:31:20 +00:00
var dragNumberField = function(e) {
draggedNumberField = true;
2011-01-25 05:31:20 +00:00
e.preventDefault();
// We don't want to be highlighting this field as we scroll.
// Or any other fields in this gui for that matter ...
// TODO: Make makeUselectable go through each element and child element.
2011-01-25 05:31:20 +00:00
_this.makeUnselectable(GUI.domElement);
_this.makeUnselectable(numberField);
2011-01-25 05:31:20 +00:00
py = y;
y = e.pageY;
var dy = py - y;
var newVal = _this.getValue() + dy*step;
2011-01-25 05:31:20 +00:00
updateValue(newVal);
return false;
}
2011-01-24 22:32:44 +00:00
2011-01-25 05:31:20 +00:00
var updateValue = function(val) {
val = parseFloat(val);
2011-01-25 05:31:20 +00:00
if (min && val <= min) {
val = min;
} else if (max && val >= max) {
val = max;
}
2011-01-24 22:02:28 +00:00
_this.setValue(val);
2011-01-25 05:31:20 +00:00
numberField.value = _this.getValue();
if (slider) slider.value = _this.getValue();
2011-01-24 22:02:28 +00:00
}
};
NumberController.prototype = new Controller();
2011-01-25 05:31:20 +00:00
NumberController.prototype.constructor = NumberController;