dat.gui/src/dat/controllers/BooleanController.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 checkbox input to alter the boolean property of an object.
*
2015-08-14 13:24:30 +00:00
* @extends dat.controllers.Controller
*
* @param {Object} object The object to be manipulated
* @param {string} property The name of the property to be manipulated
*/
2015-08-14 16:27:54 +00:00
class BooleanController extends Controller {
constructor(object, property) {
super(object, property);
2011-11-07 21:29:37 +00:00
2015-08-14 22:16:18 +00:00
const _this = this;
2011-11-08 06:17:40 +00:00
this.__prev = this.getValue();
2011-11-07 21:29:37 +00:00
this.__checkbox = document.createElement('input');
this.__checkbox.setAttribute('type', 'checkbox');
2015-08-14 16:27:54 +00:00
function onChange() {
_this.setValue(!_this.__prev);
}
2011-11-07 21:29:37 +00:00
dom.bind(this.__checkbox, 'change', onChange, false);
this.domElement.appendChild(this.__checkbox);
// Match original value
this.updateDisplay();
2015-08-14 16:27:54 +00:00
}
2011-11-07 21:29:37 +00:00
2015-08-14 16:27:54 +00:00
setValue(v) {
2015-08-14 22:16:18 +00:00
const toReturn = super.setValue(v);
2015-08-14 16:27:54 +00:00
if (this.__onFinishChange) {
this.__onFinishChange.call(this, this.getValue());
2011-11-07 21:29:37 +00:00
}
2015-08-14 16:27:54 +00:00
this.__prev = this.getValue();
return toReturn;
}
updateDisplay() {
if (this.getValue() === true) {
this.__checkbox.setAttribute('checked', 'checked');
this.__checkbox.checked = true;
this.__prev = true;
2015-08-14 16:27:54 +00:00
} else {
this.__checkbox.checked = false;
this.__prev = false;
2015-08-14 13:24:30 +00:00
}
2011-11-07 21:29:37 +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 BooleanController;