-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factor generic configuration stuff out of BlockBased into a new base …
…class JBrowse/Component that will be used by more than just tracks
- Loading branch information
Showing
2 changed files
with
76 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters