dat.gui/elements/gui-panel/gui-panel.js

157 lines
3.2 KiB
JavaScript
Raw Normal View History

/* globals Polymer, Path, Gui */
2014-09-04 04:14:36 +00:00
'use strict';
2014-09-03 16:16:11 +00:00
// [ ] scrolling when docked
// [ ] scrolling when window short and not docked
2014-08-27 00:01:15 +00:00
Polymer('gui-panel', {
docked: false,
open: true,
touch: ('ontouchstart' in window) ||
2014-09-04 04:14:36 +00:00
(!!window.DocumentTouch && document instanceof window.DocumentTouch),
2014-08-27 00:01:15 +00:00
ready: function() {
2014-08-27 00:01:15 +00:00
this.defined = {};
2014-08-27 00:01:15 +00:00
},
2014-08-27 00:01:15 +00:00
2014-09-07 20:18:36 +00:00
define: function() {
2014-08-27 00:01:15 +00:00
if ( arguments.length == 1 ) {
var name = arguments[ 0 ];
2014-09-07 20:18:36 +00:00
return this.defined[ name ];
2014-08-27 00:01:15 +00:00
}
2014-09-07 20:18:36 +00:00
var initialValue = arguments[ 1 ];
var name = arguments[ 0 ];
2014-08-27 00:01:15 +00:00
2014-09-07 20:18:36 +00:00
var args = [ this.defined, name ];
2014-08-27 00:01:15 +00:00
args = args.concat( Array.prototype.slice.call( arguments, 2 ) );
2014-09-07 20:18:36 +00:00
this.defined[ name ] = initialValue;
2014-08-27 00:01:15 +00:00
return this.add.apply(this, args);
2014-08-27 00:01:15 +00:00
},
2014-08-27 00:01:15 +00:00
add: function(object, path) {
2014-08-27 00:01:15 +00:00
// Make controller
2014-08-27 00:01:15 +00:00
var value = Path.get(path).getValueFrom(object);
2014-08-27 00:01:15 +00:00
2014-09-04 04:14:36 +00:00
if (value === null || value === undefined) {
return Gui.error(object +
' doesn\'t have a value for path "' + path + '".');
}
2014-08-27 00:01:15 +00:00
var args = Array.prototype.slice.call( arguments, 2 );
var controller;
2014-08-27 00:01:15 +00:00
if ( args[ 0 ] instanceof Array || typeof args[ 0 ] == 'object' ) {
controller = document.createElement( 'controller-option' );
} else {
controller = Gui.getController( value );
}
if ( !controller ) {
return Gui.error( 'Unrecognized type:', value );
}
2014-08-27 00:01:15 +00:00
controller.watch(object, path);
controller.init.apply(controller, args);
2014-08-27 00:01:15 +00:00
// Make row
2014-08-27 00:01:15 +00:00
var row = document.createElement('gui-row');
row.name = path;
2014-08-27 00:01:15 +00:00
controller.row = row;
2014-08-27 00:01:15 +00:00
controller.name = function(name) {
row.name = name;
};
2014-08-27 00:01:15 +00:00
controller.comment = function(comment) {
row.comment = comment;
};
2014-08-27 00:01:15 +00:00
row.appendChild(controller);
this.appendChild(row);
2014-08-27 00:01:15 +00:00
return controller;
2014-08-27 00:01:15 +00:00
},
2014-08-27 00:01:15 +00:00
// Observers
// -------------------------------
2014-08-27 00:01:15 +00:00
openChanged: function() {
2014-08-27 00:01:15 +00:00
if (this.open || this.docked) {
2014-08-27 00:01:15 +00:00
// let the style sheet take care of things
2014-08-27 00:01:15 +00:00
this.$.container.style.transform = '';
this.$.panel.style.transform = '';
2014-08-27 00:01:15 +00:00
} else {
2014-08-27 00:01:15 +00:00
// todo: need the rest of the vendor prefixes ...
// wish i could pipe javascript variables into styl.
2014-08-27 00:01:15 +00:00
var y = -this.$.controllers.offsetHeight + 'px';
this.$.container.style.transform = 'translate3d(0, ' + y + ', 0)';
2014-08-27 00:01:15 +00:00
}
2014-08-27 00:01:15 +00:00
},
2014-08-27 00:01:15 +00:00
dockedChanged: function() {
2014-08-27 00:01:15 +00:00
this.openChanged();
2014-08-27 00:01:15 +00:00
},
2014-08-27 00:01:15 +00:00
// Events
// -------------------------------
2014-08-27 00:01:15 +00:00
tapClose: function() {
this.open = !this.open;
},
2014-08-27 00:01:15 +00:00
2014-09-07 20:18:36 +00:00
toggleOpen: function() {
2014-08-27 00:01:15 +00:00
this.open = !this.open;
},
2014-09-02 17:13:35 +00:00
// checkHeight: function() {
2014-08-27 00:01:15 +00:00
2014-09-02 17:13:35 +00:00
// if ( window.innerHeight < this.$.controllers.offsetHeight ) {
// this.docked = true;
// } else {
// this.docked = false;
// }
// if ( window.innerHeight < this.$.controllers.offsetHeight) {
// this.docked = true;
// } else {
// this.docked = false;
// }
2014-08-27 00:01:15 +00:00
// },
2014-08-27 00:01:15 +00:00
// Legacy
// -------------------------------
2014-08-27 00:01:15 +00:00
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-08-27 00:01:15 +00:00
// todo: domElement
2014-08-27 00:01:15 +00:00
});