Skip to content

Commit

Permalink
factor generic configuration stuff out of BlockBased into a new base …
Browse files Browse the repository at this point in the history
…class JBrowse/Component that will be used by more than just tracks
  • Loading branch information
rbuels committed Mar 1, 2013
1 parent 5557262 commit 9d067bc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 37 deletions.
73 changes: 73 additions & 0 deletions src/JBrowse/Component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* A JBrowse component keeps a reference to the main browser object, and is configurable.
*/

define([
'dojo/_base/declare'
],
function(
declare
) {

return declare( null, {

constructor: function( args ) {
args = args || {};

// merge our config with the config defaults
this.config = args.config || {};
this.config = this._mergeConfigs( dojo.clone( this._defaultConfig() ), this.config );

this.browser = args.browser;
},

_defaultConfig: function() {
return {};
},

_mergeConfigs: function(a, b) {
for (var prop in b) {
if ((prop in a)
&& ("object" == typeof b[prop])
&& ("object" == typeof a[prop]) ) {
this._mergeConfigs( a[prop], b[prop] );
} else if( typeof a[prop] == 'undefined' || typeof b[prop] != 'undefined' ){
a[prop] = b[prop];
}
}
return a;
},

_compileConfigurationPath: function( path ) {
var confVal = this.config;

if( typeof path == 'string' )
path = path.split('.');
while( path.length && confVal )
confVal = confVal[ path.shift() ];

if( path.length )
return function() { return null; };

return typeof confVal == 'function'
? confVal
: function() { return confVal; };
},

/**
* Given a dot-separated string configuration path into the config
* (e.g. "style.bg_color"), get the value of the configuration.
* If args are given, evaluate the configuration using them.
* Otherwise, return a function that returns the value of the
* configuration when called.
*/
getConf: function( path, args ) {
var func = this.compiledConfig[path];
if( ! func ) {
func = this.compiledConfig[path] = this._compileConfigurationPath( path );
}

return args ? func.apply( this, args ) : func;
}
});
});
40 changes: 3 additions & 37 deletions src/JBrowse/View/Track/BlockBased.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define( [
'dijit/CheckedMenuItem',
'dijit/MenuSeparator',
'JBrowse/Util',
'JBrowse/Component',
'JBrowse/Errors',
'JBrowse/View/TrackConfigEditor',
'JBrowse/View/ConfirmDialog'
Expand All @@ -29,12 +30,13 @@ define( [
dijitCheckedMenuItem,
dijitMenuSeparator,
Util,
Component,
Errors,
TrackConfigEditor,
ConfirmDialog
) {

return declare( null,
return declare( Component,
/**
* @lends JBrowse.View.Track.BlockBased.prototype
*/
Expand All @@ -46,10 +48,6 @@ return declare( null,
constructor: function( args ) {
args = args || {};

// merge our config with the config defaults
this.config = args.config || {};
this.config = Util.deepUpdate( dojo.clone( this._defaultConfig() ), this.config );

this.refSeq = args.refSeq;
this.name = args.label || this.config.label;
this.key = args.key || this.config.key || this.name;
Expand Down Expand Up @@ -822,38 +820,6 @@ return declare( null,
: confVal;
},

_compileConfigurationPath: function( path ) {
var confVal = this.config;

if( typeof path == 'string' )
path = path.split('.');
while( path.length && confVal )
confVal = confVal[ path.shift() ];

if( path.length )
return function() { return null; };

return typeof confVal == 'function'
? confVal
: function() { return confVal; };
},

/**
* Given a dot-separated string configuration path into the config
* (e.g. "style.bg_color"), get the value of the configuration.
* If args are given, evaluate the configuration using them.
* Otherwise, return a function that returns the value of the
* configuration when called.
*/
getConf: function( path, args ) {
var func = this.compiledConfig[path];
if( ! func ) {
func = this.compiledConfig[path] = this._compileConfigurationPath( path );
}

return args ? func.apply( this, args ) : func;
},

/**
* Like getConf, but get a conf value that explicitly can vary
* feature by feature. Provides a uniform function signature for
Expand Down

0 comments on commit 9d067bc

Please sign in to comment.