[Time Conductor] Recognize domain changes in plot

This commit is contained in:
Victor Woeltjen
2015-10-27 11:39:23 -07:00
parent 0bd1d53d25
commit b375ede217
2 changed files with 86 additions and 29 deletions

View File

@ -46,21 +46,9 @@ define(
*
*/
function PlotAxis(axisType, metadatas, defaultValue) {
var keys = {},
options = [];
// Look through all metadata objects and assemble a list
// of all possible domain or range options
function buildOptionsForMetadata(m) {
(m[axisType] || []).forEach(function (option) {
if (!keys[option.key]) {
keys[option.key] = true;
options.push(option);
}
});
}
(metadatas || []).forEach(buildOptionsForMetadata);
this.axisType = axisType;
this.defaultValue = defaultValue;
this.optionKeys = {};
/**
* The currently chosen option for this axis. An
@ -68,7 +56,7 @@ define(
* directly form the plot template.
* @memberof platform/features/plot.PlotAxis#
*/
this.active = options[0] || defaultValue;
this.active = defaultValue;
/**
* The set of options applicable for this axis;
@ -77,17 +65,62 @@ define(
* human-readable names respectively)
* @memberof platform/features/plot.PlotAxis#
*/
this.options = options;
this.options = [];
// Initialize options from metadata objects
this.updateMetadata(metadatas);
}
PlotAxis.prototype.choose = function (key) {
var i;
for (i = 0; i < this.options.length; i += 1) {
if (this.options[i].key === key) {
this.active = this.options[i];
return;
}
/**
* Update axis options to reflect current metadata.
* @memberof platform/features/plot.PlotAxis
*/
PlotAxis.prototype.updateMetadata = function (metadatas) {
var axisType = this.axisType,
optionKeys = this.optionKeys,
newOptions = {},
toAdd = [];
function isValid(option) {
return option && optionKeys[option.key];
}
metadatas.forEach(function (m) {
(m[axisType + 's'] || []).forEach(function (option) {
var key = option.key;
if (!optionKeys[key] && !newOptions[key]) {
toAdd.push(option);
}
newOptions[option.key] = true;
});
});
optionKeys = this.optionKeys = newOptions;
// General approach here is to avoid changing object
// instances unless something has really changed, since
// Angular is watching; don't want to trigger extra digests.
if (!this.options.every(isValid)) {
this.options = this.options.filter(isValid);
}
if (toAdd.length > 0) {
this.options = this.options.concat(toAdd);
}
if (!isValid(this.active)) {
this.active = this.options[0] || this.defaultValue;
}
};
PlotAxis.prototype.chooseOption = function (key) {
var self = this;
this.options.forEach(function (option) {
if (option.key === key) {
self.active = option;
}
});
};
return PlotAxis;