dat.gui/elements/Gui.js

147 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-09-08 01:31:51 +00:00
( function( scope ) {
'use strict';
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
var Gui = function( params ) {
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
if ( !ready ) {
Gui.error( 'Gui not ready. Put your code inside Gui.ready()' );
}
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
params = params || {};
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
var panel = document.createElement( 'gui-panel' );
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
panel.autoPlace = params.autoPlace !== false;
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
if ( panel.autoPlace ) {
document.body.appendChild( panel );
}
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
return panel;
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-09-01 03:54:59 +00:00
2014-09-08 01:31:51 +00:00
// Register custom controllers
// -------------------------------
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
var controllers = {};
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
Gui.register = function( elementName, test ) {
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
controllers[ elementName ] = test;
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-09-08 01:31:51 +00:00
// Returns a controller based on a value
// -------------------------------
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
Gui.getController = function( value ) {
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
for ( var type in controllers ) {
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
var test = controllers[ type ];
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
if ( test( value ) ) {
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +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-09-08 01:31:51 +00:00
}
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-09-08 01:31:51 +00:00
// Gui ready handler ... * shakes fist at polymer *
// -------------------------------
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
var ready = false;
var readyHandlers = [];
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
document.addEventListener( 'polymer-ready', function() {
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
ready = true;
readyHandlers.forEach( function( fnc ) {
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
fnc();
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
} );
} );
Gui.ready = function( fnc ) {
2014-08-27 00:01:15 +00:00
2014-09-08 02:43:10 +00:00
if ( ready ) {
fnc();
} else {
readyHandlers.push( fnc );
}
2014-09-08 01:31:51 +00:00
};
2014-09-08 01:31:51 +00:00
// Error
// -------------------------------
2014-09-08 01:31:51 +00:00
Gui.error = function() {
var args = Array.prototype.slice.apply( arguments );
args.unshift( 'dat-gui ::' );
console.error.apply( console, args );
};
2014-09-08 01:31:51 +00:00
Gui.warn = function() {
var args = Array.prototype.slice.apply( arguments );
args.unshift( 'dat-gui ::' );
console.warn.apply( console, args );
};
2014-09-08 01:31:51 +00:00
// Old namespaces
// -------------------------------
2014-09-08 01:31:51 +00:00
var dat = {};
2014-09-08 01:31:51 +00:00
dat.gui = {};
dat.gui.GUI = Gui;
dat.GUI = dat.gui.GUI;
2014-09-08 01:31:51 +00:00
dat.color = {};
dat.color.Color = function() {};
2014-09-08 01:31:51 +00:00
dat.dom = {};
dat.dom.dom = function() {};
2014-09-08 01:31:51 +00:00
dat.controllers = {};
dat.controllers.Controller = constructor( 'controller-base' );
dat.controllers.NumberController = constructor( 'controller-number' );
dat.controllers.FunctionController = constructor( 'controller-function' );
dat.controllers.ColorController = constructor( 'controller-color' );
dat.controllers.BooleanController = constructor( 'controller-boolean' );
dat.controllers.OptionController = constructor( 'controller-option' );
dat.controllers.NumberControllerBox = dat.controllers.NumberController;
dat.controllers.NumberControllerSlider = dat.controllers.NumberController;
function constructor( elementName ) {
return function( object, path ) {
var el = document.createElement( elementName );
el.watch( object, path );
return el;
};
}
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
// Export
// -------------------------------
2014-08-27 00:01:15 +00:00
2014-09-08 01:31:51 +00:00
scope.dat = dat;
scope.Gui = Gui;
2014-08-27 00:01:15 +00:00
2014-09-08 01:38:29 +00:00
} )( this );