define(['libs/underscore', 'viz/trackster/util', 'utils/config'], function(_, util_mod, config_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 (this.get('value') === undefined) { // Use default to set value. this.set_value(this.get('default_value')); // If no default value for color config, set random color. if (!this.get('value') && this.get('type') === 'color') { // For color setting, set random color. this.set('value', util_mod.get_random_color()); } } }, /** * Cast and set value. This should be instead of * setting.set('value', new_value) */ set_value: function(value, options) { var type = this.get('type'); if (type === 'float') { value = parseFloat(value); } else if (type === 'int') { value = parseInt(value, 10); } // TODO: handle casting from string to bool? this.set({value: value}, options); } }, { // 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; }, /** * Set value for a setting. */ set_value: function(key, value, options) { var s = this.get(key); if (s) { return s.set_value(value, options); } return undefined; }, /** * Set default value for a setting. */ set_default_value: function(key, default_value) { var s = this.get(key); if (s) { return s.set('default_value', default_value); } return undefined; } }, { /** * Utility function that creates a ConfigSettingsCollection from a set of models * and a saved_values dictionary. */ from_models_and_saved_values: function(models, saved_values) { // If there are saved values, copy models and update with saved values. if (saved_values) { models = _.map(models, function(m) { return _.extend({}, m, { value: saved_values[m.key] }); }); } return new ConfigSettingCollection(models); } }); /** * Viewer for config settings collection. */ var ConfigSettingCollectionView = Backbone.View.extend({ className: 'config-settings-view', /** * Renders form for editing configuration settings. */ render: function() { var container = this.$el; this.collection.each(function(param, index) { // Hidden params have no representation in the form if (param.get('hidden')) { return; } // Build row for param. var id = 'param_' + index, type = param.get('type'), value = param.get('value'); var row = $("
").appendTo(container); row.append($('