dat.gui/elements/Gui.js

232 lines
4.6 KiB
JavaScript
Raw Normal View History

2014-09-08 01:31:51 +00:00
( function( scope ) {
2014-09-08 03:36:20 +00:00
/* globals Path */
2014-08-27 00:01:15 +00:00
var Gui = function( params ) {
2014-08-27 00:01:15 +00:00
if ( !ready ) {
Gui.error( 'Gui not ready. Put your code inside Gui.ready()' );
}
2014-09-08 03:36:20 +00:00
params = params || {};
2014-09-08 03:36:20 +00:00
// Properties
2014-09-08 03:36:20 +00:00
this.vars = {};
this.localStorage = params.localStorage || false;
2014-09-08 03:36:20 +00:00
// Make domElement
2014-09-08 03:36:20 +00:00
this.panel = document.createElement( 'dat-gui' );
this.panel.autoPlace = params.autoPlace !== false;
2014-09-08 03:36:20 +00:00
if ( this.panel.autoPlace ) {
document.body.appendChild( this.panel );
}
2014-09-08 03:36:20 +00:00
};
2014-09-08 03:36:20 +00:00
// Instance methods
// -------------------------------
2014-09-08 03:36:20 +00:00
Gui.prototype.add = function( object, path ) {
2014-09-08 03:36:20 +00:00
// Make controller
2014-09-08 03:36:20 +00:00
var value = Path.get( path ).getValueFrom( object );
2014-09-08 03:36:20 +00:00
if ( value === null || value === undefined ) {
return Gui.error( object + ' doesn\'t have a value for path "' + path + '".' );
}
2014-09-08 03:36:20 +00:00
var args = Array.prototype.slice.call( arguments, 2 );
var controller;
2014-09-08 03:36:20 +00:00
if ( args[ 0 ] instanceof Array || typeof args[ 0 ] == 'object' ) {
controller = document.createElement( 'dat-gui-option' );
} else {
controller = Gui.getController( value );
}
2014-09-08 03:36:20 +00:00
if ( !controller ) {
return Gui.error( 'Unrecognized type:', value );
}
2014-09-08 03:36:20 +00:00
controller.watch( object, path );
controller.init.apply( controller, args );
2014-09-08 03:36:20 +00:00
// Make row
2014-09-08 03:36:20 +00:00
var row = document.createElement( 'gui-row' );
row.name = path;
2014-09-08 03:36:20 +00:00
controller.row = row;
2014-09-08 03:36:20 +00:00
controller.name = function( name ) {
row.name = name;
};
2014-09-08 03:36:20 +00:00
controller.comment = function( comment ) {
row.comment = comment;
2014-09-08 03:36:20 +00:00
};
row.appendChild( controller );
this.panel.appendChild( row );
2014-08-27 00:01:15 +00:00
return controller;
2014-08-27 00:01:15 +00:00
};
2014-09-08 22:01:44 +00:00
Gui.prototype.remember = function( object ) {};
2014-09-08 03:36:20 +00:00
Gui.prototype.var = function() {
2014-09-08 03:36:20 +00:00
var name, initialValue, args;
2014-08-27 00:01:15 +00:00
if ( arguments.length == 1 ) {
2014-09-08 03:36:20 +00:00
name = arguments[ 0 ];
return this.vars[ name ];
}
2014-09-08 03:36:20 +00:00
initialValue = arguments[ 1 ];
name = arguments[ 0 ];
2014-09-08 03:36:20 +00:00
args = [ this.vars, name ];
args = args.concat( Array.prototype.slice.call( arguments, 2 ) );
2014-09-08 03:36:20 +00:00
this.vars[ name ] = initialValue;
2014-09-08 03:36:20 +00:00
return this.add.apply( this, args );
2014-09-08 03:36:20 +00:00
};
2014-09-08 03:36:20 +00:00
Gui.prototype.listenAll = function() {
2014-08-27 00:01:15 +00:00
Gui.warn( 'controller.listenAll() is deprecated. All controllers are listened for free.' );
2014-08-27 00:01:15 +00:00
};
2014-09-01 03:54:59 +00:00
2014-08-27 00:01:15 +00:00
// Register custom controllers
// -------------------------------
2014-08-27 00:01:15 +00:00
var controllers = {};
2014-08-27 00:01:15 +00:00
Gui.register = function( elementName, test ) {
2014-08-27 00:01:15 +00:00
controllers[ elementName ] = test;
2014-08-27 00:01:15 +00:00
};
2014-08-27 00:01:15 +00:00
// Returns a controller based on a value
// -------------------------------
2014-08-27 00:01:15 +00:00
Gui.getController = function( value ) {
2014-08-27 00:01:15 +00:00
for ( var type in controllers ) {
2014-08-27 00:01:15 +00:00
var test = controllers[ type ];
2014-08-27 00:01:15 +00:00
if ( test( value ) ) {
2014-08-27 00:01:15 +00:00
return document.createElement( type );
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
}
2014-08-27 00:01:15 +00:00
}
2014-08-27 00:01:15 +00:00
};
2014-08-27 00:01:15 +00:00
// Gui ready handler ... * shakes fist at polymer *
// -------------------------------
2014-08-27 00:01:15 +00:00
var ready = false;
var readyHandlers = [];
2014-08-27 00:01:15 +00:00
document.addEventListener( 'polymer-ready', function() {
2014-08-27 00:01:15 +00:00
ready = true;
readyHandlers.forEach( function( fnc ) {
2014-08-27 00:01:15 +00:00
fnc();
2014-09-08 01:31:51 +00:00
} );
} );
2014-08-27 00:01:15 +00:00
Gui.ready = function( fnc ) {
if ( ready ) {
fnc();
} else {
readyHandlers.push( fnc );
}
};
// Console
// -------------------------------
Gui.error = function() {
var args = Array.prototype.slice.apply( arguments );
args.unshift( 'dat-gui ::' );
console.error.apply( console, args );
};
Gui.warn = function() {
var args = Array.prototype.slice.apply( arguments );
args.unshift( 'dat-gui ::' );
console.warn.apply( console, args );
};
// Old namespaces
// -------------------------------
var dat = {};
dat.gui = {};
dat.gui.GUI = Gui;
dat.GUI = dat.gui.GUI;
dat.color = {};
dat.color.Color = function() {};
dat.dom = {};
dat.dom.dom = function() {};
2014-09-08 01:31:51 +00:00
dat.controllers = {};
dat.controllers.Controller = constructor( 'dat-gui-base' );
dat.controllers.NumberController = constructor( 'dat-gui-number' );
dat.controllers.FunctionController = constructor( 'dat-gui-function' );
dat.controllers.ColorController = constructor( 'dat-gui-color' );
dat.controllers.BooleanController = constructor( 'dat-gui-boolean' );
dat.controllers.OptionController = constructor( 'dat-gui-option' );
2014-09-08 01:31:51 +00:00
dat.controllers.NumberControllerBox = dat.controllers.NumberController;
dat.controllers.NumberControllerSlider = dat.controllers.NumberController;
2014-09-08 01:31:51 +00:00
function constructor( elementName ) {
2014-09-08 01:31:51 +00:00
return function( object, path ) {
var el = document.createElement( elementName );
el.watch( object, path );
return el;
};
}
2014-08-27 00:01:15 +00:00
// Export
// -------------------------------
2014-08-27 00:01:15 +00:00
scope.dat = dat;
scope.Gui = Gui;
2014-09-08 22:01:44 +00:00
2014-08-27 00:01:15 +00:00
})( this );