dat.gui/src/dat/controllers/Controller.js

128 lines
3.0 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 13:24:30 +00:00
/**
* @class An "abstract" class that represents a given property of an object.
*
* @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 Controller {
constructor(object, property) {
2011-11-07 21:29:37 +00:00
this.initialValue = object[property];
/**
* Those who extend this class will put their DOM elements in here.
* @type {DOMElement}
*/
this.domElement = document.createElement('div');
/**
* The object to manipulate
* @type {Object}
*/
this.object = object;
/**
* The name of the property to manipulate
* @type {String}
*/
this.property = property;
/**
* The function to be called on change.
* @type {Function}
* @ignore
*/
this.__onChange = undefined;
/**
* The function to be called on finishing change.
* @type {Function}
* @ignore
*/
this.__onFinishChange = undefined;
2015-08-14 16:27:54 +00:00
}
/**
* Specify that a function fire every time someone changes the value with
* this Controller.
*
* @param {Function} fnc This function will be called whenever the value
* is modified via this Controller.
2015-08-14 20:29:30 +00:00
* @returns {Controller} this
2015-08-14 16:27:54 +00:00
*/
onChange(fnc) {
this.__onChange = fnc;
return this;
}
/**
* Specify that a function fire every time someone "finishes" changing
* the value wih this Controller. Useful for values that change
* incrementally like numbers or strings.
*
* @param {Function} fnc This function will be called whenever
* someone "finishes" changing the value via this Controller.
2015-08-14 20:29:30 +00:00
* @returns {Controller} this
2015-08-14 16:27:54 +00:00
*/
onFinishChange(fnc) {
this.__onFinishChange = fnc;
return this;
}
/**
* Change the value of <code>object[property]</code>
*
* @param {Object} newValue The new value of <code>object[property]</code>
*/
setValue(newValue) {
this.object[this.property] = newValue;
if (this.__onChange) {
this.__onChange.call(this, newValue);
2015-08-14 13:24:30 +00:00
}
2015-08-14 20:29:30 +00:00
2015-08-14 16:27:54 +00:00
this.updateDisplay();
return this;
}
/**
* Gets the value of <code>object[property]</code>
*
* @returns {Object} The current value of <code>object[property]</code>
*/
getValue() {
return this.object[this.property];
}
/**
* Refreshes the visual display of a Controller in order to keep sync
* with the object's current value.
2015-08-14 20:29:30 +00:00
* @returns {Controller} this
2015-08-14 16:27:54 +00:00
*/
updateDisplay() {
return this;
}
/**
* @returns {Boolean} true if the value has deviated from initialValue
*/
isModified() {
2015-08-14 22:16:18 +00:00
return this.initialValue !== this.getValue();
2015-08-14 16:27:54 +00:00
}
}
2011-11-07 21:29:37 +00:00
2015-08-14 22:16:18 +00:00
export default Controller;