dat.gui/src/dat/controllers/StringController.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2011-11-07 21:29:37 +00:00
/**
* 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
*/
2015-08-14 15:28:30 +00:00
import Controller from './Controller';
import dom from '../dom/dom';
2015-08-14 13:24:30 +00:00
/**
* @class Provides a text input to alter the string property of an object.
*
* @extends dat.controllers.Controller
*
* @param {Object} object The object to be manipulated
* @param {string} property The name of the property to be manipulated
*
* @member dat.controllers
*/
2015-08-14 16:27:54 +00:00
class StringController extends Controller {
constructor(object, property) {
super(object, property);
2015-08-14 13:24:30 +00:00
2015-08-14 22:16:18 +00:00
const _this = this;
2011-11-07 21:29:37 +00:00
2015-08-15 01:28:21 +00:00
function onChange() {
_this.setValue(_this.__input.value);
}
function onBlur() {
if (_this.__onFinishChange) {
_this.__onFinishChange.call(_this, _this.getValue());
}
}
2015-08-14 16:27:54 +00:00
this.__input = document.createElement('input');
this.__input.setAttribute('type', 'text');
2011-11-07 21:29:37 +00:00
2015-08-14 16:27:54 +00:00
dom.bind(this.__input, 'keyup', onChange);
dom.bind(this.__input, 'change', onChange);
dom.bind(this.__input, 'blur', onBlur);
2015-08-14 22:16:18 +00:00
dom.bind(this.__input, 'keydown', function(e) {
2015-08-14 16:27:54 +00:00
if (e.keyCode === 13) {
this.blur();
}
});
2015-08-14 13:24:30 +00:00
2015-08-14 16:27:54 +00:00
this.updateDisplay();
2011-11-07 21:29:37 +00:00
2015-08-14 16:27:54 +00:00
this.domElement.appendChild(this.__input);
}
2011-11-07 21:29:37 +00:00
2015-08-14 16:27:54 +00:00
updateDisplay() {
// Stops the caret from moving on account of:
// keyup -> setValue -> updateDisplay
if (!dom.isActive(this.__input)) {
this.__input.value = this.getValue();
2015-08-14 13:24:30 +00:00
}
2015-08-14 16:27:54 +00:00
return super.updateDisplay();
}
}
2011-11-07 21:29:37 +00:00
2015-08-14 15:28:30 +00:00
export default StringController;