define(['libs/underscore', 'viz/trackster/util'], function(_, util_mod) { /** * A configuration setting. Currently key is used as id. */ var ConfigSetting = Backbone.Model.extend({ initialize: function(options) { // Use key as id for now. var key = this.get('key'); this.set('id', key); // Set defaults based on key. var defaults = _.find(ConfigSetting.known_settings_defaults, function(s) { return s.key === key; }); if (defaults) { this.set(_.extend({}, defaults, options)); } // If type color and no value, get random color. if (this.get('type') === 'color' && !this.get('value')) { this.set('value', util_mod.get_random_color()); } // When value updated, cast is appropriately. this.on('change:value', this.cast_value, this); }, /** * Cast value from string to appropriate type. */ cast_value: function() { var type = this.get('type'), value = this.get('value'); if (type === 'float') { value = parseFloat( value ); } else if (type === 'int') { value = parseInt( value, 10 ); } // TODO: handle casting from string to bool? this.set('value'); } }, { // This is a master list of default settings for known settings. known_settings_defaults: [ { key: 'name', label: 'Name', type: 'text', default_value: '' }, { key: 'color', label: 'Color', type: 'color', default_value: null }, { key: 'min_value', label: 'Min Value', type: 'float', default_value: null }, { key: 'max_value', label: 'Max Value', type: 'float', default_value: null }, { key: 'mode', type: 'string', default_value: this.mode, hidden: true }, { key: 'height', type: 'int', default_value: 32, hidden: true }, { key: 'pos_color', label: 'Positive Color', type: 'color', default_value: "#FF8C00" }, { key: 'neg_color', label: 'Negative Color', type: 'color', default_value: "#4169E1" }, { key: 'block_color', label: 'Block color', type: 'color', default_value: null }, { key: 'label_color', label: 'Label color', type: 'color', default_value: 'black' }, { key: 'show_insertions', label: 'Show insertions', type: 'bool', default_value: false }, { key: 'show_counts', label: 'Show summary counts', type: 'bool', default_value: true }, { key: 'mode', type: 'string', default_value: this.mode, hidden: true }, { key: 'reverse_strand_color', label: 'Antisense strand color', type: 'color', default_value: null }, { key: 'show_differences', label: 'Show differences only', type: 'bool', default_value: true }, { key: 'mode', type: 'string', default_value: this.mode, hidden: true } ] }); /** * Collection of config settings. */ var ConfigSettingCollection = Backbone.Collection.extend({ model: ConfigSetting, /** * Save settings as a dictionary of key-value pairs. * This function is needed for backwards compatibility. */ to_key_value_dict: function() { var rval = {}; this.each(function(setting) { rval[setting.get('key')] = setting.get('value'); }); return rval; }, /** * Returns value for a given key. Returns undefined if there is no setting with the specified key. */ get_value: function(key) { var s = this.get(key); if (s) { return s.get('value'); } return undefined; } }, { /** * Utility function that creates a ConfigSettingsCollection from a standard dictionary * with configuration key-value pairs. */ from_config_dict: function(config_dict) { // Create a list of key-value dicts suitable for sending to a collection. var settings_list = _.map(_.keys(config_dict), function(key) { return { key: key, value: config_dict[key] }; }); return new ConfigSettingCollection(settings_list); } }); /** * Viewer for config settings collection. */ var ConfigSettingCollectionView = Backbone.View.extend({ className: 'config-settings-view', /** * Renders form for editing configuration settings. */ render: function() { var track_config = this.model; var container = this.$el; var param; // Function to process parameters recursively function handle_params( params, container ) { for ( var index = 0; index < params.length; index++ ) { param = params[index]; // Hidden params have no representation in the form if ( param.hidden ) { continue; } // Build row for param var id = 'param_' + index; var value = track_config.values[ param.key ]; var row = $("
").appendTo( container ); row.append( $('