mirror of
https://github.com/nasa/openmct.git
synced 2025-06-19 23:53:49 +00:00
[Edit] Avoid rebuilding toolbar on edit
Avoid rebuilding the toolbar in Edit mode whenever any change occurs to avoid losing the binding to the current selection state; needed for color picker to work properly for WTD-881.
This commit is contained in:
@ -20,9 +20,10 @@ define(
|
|||||||
* @param {Function} commit callback to invoke after changes
|
* @param {Function} commit callback to invoke after changes
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function EditToolbar(structure, selection, commit) {
|
function EditToolbar(structure, commit) {
|
||||||
var toolbarStructure = Object.create(structure || {}),
|
var toolbarStructure = Object.create(structure || {}),
|
||||||
toolbarState,
|
toolbarState,
|
||||||
|
selection,
|
||||||
properties = [];
|
properties = [];
|
||||||
|
|
||||||
// Generate a new key for an item's property
|
// Generate a new key for an item's property
|
||||||
@ -155,35 +156,79 @@ define(
|
|||||||
return section && section.items && section.items.length > 0;
|
return section && section.items && section.items.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare a toolbar section based on current selection
|
// Prepare a toolbar section
|
||||||
function convertSection(section) {
|
function convertSection(section) {
|
||||||
var converted = Object.create(section || {});
|
var converted = Object.create(section || {});
|
||||||
converted.items =
|
converted.items =
|
||||||
((section || {}).items || [])
|
((section || {}).items || [])
|
||||||
.map(convertItem)
|
.map(convertItem);
|
||||||
.filter(isApplicable);
|
|
||||||
return converted;
|
return converted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show/hide controls in this section per applicability
|
||||||
|
function refreshSectionApplicability(section) {
|
||||||
|
var count = 0;
|
||||||
|
// Show/hide each item
|
||||||
|
(section.items || []).forEach(function (item) {
|
||||||
|
item.hidden = !isApplicable(item);
|
||||||
|
count += item.hidden ? 0 : 1;
|
||||||
|
});
|
||||||
|
// Hide this section if there are no applicable items
|
||||||
|
section.hidden = !count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show/hide controls if they are applicable
|
||||||
|
function refreshApplicability() {
|
||||||
|
toolbarStructure.sections.forEach(refreshSectionApplicability);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh toolbar state to match selection
|
||||||
|
function refreshState() {
|
||||||
|
toolbarState = properties.map(initializeState);
|
||||||
|
}
|
||||||
|
|
||||||
toolbarStructure.sections =
|
toolbarStructure.sections =
|
||||||
((structure || {}).sections || [])
|
((structure || {}).sections || [])
|
||||||
.map(convertSection)
|
.map(convertSection)
|
||||||
.filter(nonEmpty);
|
.filter(nonEmpty);
|
||||||
|
|
||||||
toolbarState = properties.map(initializeState);
|
toolbarState = [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
/**
|
/**
|
||||||
*
|
* Set the current selection. Visisbility of sections
|
||||||
|
* and items in the toolbar will be updated to match this.
|
||||||
|
* @param {Array} s the new selection
|
||||||
|
*/
|
||||||
|
setSelection: function (s) {
|
||||||
|
selection = s;
|
||||||
|
refreshApplicability();
|
||||||
|
refreshState();
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Get the structure of the toolbar, as appropriate to
|
||||||
|
* pass to `mct-toolbar`.
|
||||||
|
* @returns the toolbar structure
|
||||||
*/
|
*/
|
||||||
getStructure: function () {
|
getStructure: function () {
|
||||||
return toolbarStructure;
|
return toolbarStructure;
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Get the current state of the toolbar, as appropriate
|
||||||
|
* to two-way bind to the state handled by `mct-toolbar`.
|
||||||
|
* @returns {Array} state of the toolbar
|
||||||
|
*/
|
||||||
getState: function () {
|
getState: function () {
|
||||||
return toolbarState;
|
return toolbarState;
|
||||||
},
|
},
|
||||||
updateState: function (key, value) {
|
/**
|
||||||
updateProperties(properties[key], value);
|
* Update state within the current selection.
|
||||||
|
* @param {number} index the index of the corresponding
|
||||||
|
* element in the state array
|
||||||
|
* @param value the new value to convey to the selection
|
||||||
|
*/
|
||||||
|
updateState: function (index, value) {
|
||||||
|
updateProperties(properties[index], value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,7 @@ define(
|
|||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function EditToolbarRepresenter(scope, element, attrs) {
|
function EditToolbarRepresenter(scope, element, attrs) {
|
||||||
var definition,
|
var toolbar,
|
||||||
unwatch,
|
|
||||||
toolbar,
|
|
||||||
toolbarObject = {};
|
toolbarObject = {};
|
||||||
|
|
||||||
// Mark changes as ready to persist
|
// Mark changes as ready to persist
|
||||||
@ -29,17 +27,20 @@ define(
|
|||||||
|
|
||||||
// Handle changes to the current selection
|
// Handle changes to the current selection
|
||||||
function updateSelection(selection) {
|
function updateSelection(selection) {
|
||||||
// Make sure selection is array-like
|
// Only update if there is a toolbar to update
|
||||||
selection = Array.isArray(selection) ?
|
if (toolbar) {
|
||||||
selection :
|
// Make sure selection is array-like
|
||||||
(selection ? [selection] : []);
|
selection = Array.isArray(selection) ?
|
||||||
|
selection :
|
||||||
|
(selection ? [selection] : []);
|
||||||
|
|
||||||
// Instantiate a new toolbar...
|
// Update the toolbar's selection
|
||||||
toolbar = new EditToolbar(definition, selection, commit);
|
toolbar.setSelection(selection);
|
||||||
|
|
||||||
// ...and expose its structure/state
|
// ...and expose its structure/state
|
||||||
toolbarObject.structure = toolbar.getStructure();
|
toolbarObject.structure = toolbar.getStructure();
|
||||||
toolbarObject.state = toolbar.getState();
|
toolbarObject.state = toolbar.getState();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get state (to watch it)
|
// Get state (to watch it)
|
||||||
@ -50,7 +51,7 @@ define(
|
|||||||
// Update selection models to match changed toolbar state
|
// Update selection models to match changed toolbar state
|
||||||
function updateState(state) {
|
function updateState(state) {
|
||||||
// Update underlying state based on toolbar changes
|
// Update underlying state based on toolbar changes
|
||||||
state.forEach(function (value, index) {
|
(state || []).forEach(function (value, index) {
|
||||||
toolbar.updateState(index, value);
|
toolbar.updateState(index, value);
|
||||||
});
|
});
|
||||||
// Commit the changes.
|
// Commit the changes.
|
||||||
@ -58,9 +59,11 @@ define(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize toolbar (expose object to parent scope)
|
// Initialize toolbar (expose object to parent scope)
|
||||||
function initialize() {
|
function initialize(definition) {
|
||||||
// If we have been asked to expose toolbar state...
|
// If we have been asked to expose toolbar state...
|
||||||
if (attrs.toolbar) {
|
if (attrs.toolbar) {
|
||||||
|
// Initialize toolbar object
|
||||||
|
toolbar = new EditToolbar(definition, commit);
|
||||||
// Expose toolbar state under that name
|
// Expose toolbar state under that name
|
||||||
scope.$parent[attrs.toolbar] = toolbarObject;
|
scope.$parent[attrs.toolbar] = toolbarObject;
|
||||||
}
|
}
|
||||||
@ -68,12 +71,12 @@ define(
|
|||||||
|
|
||||||
// Represent a domain object using this definition
|
// Represent a domain object using this definition
|
||||||
function represent(representation) {
|
function represent(representation) {
|
||||||
|
// Get the newest toolbar definition from the view
|
||||||
|
var definition = (representation || {}).toolbar || {};
|
||||||
// Expose the toolbar object to the parent scope
|
// Expose the toolbar object to the parent scope
|
||||||
initialize();
|
initialize(definition);
|
||||||
// Clear any existing selection
|
// Clear any existing selection
|
||||||
scope.selection = [];
|
scope.selection = [];
|
||||||
// Get the newest toolbar definition from the view
|
|
||||||
definition = (representation || {}).toolbar || {};
|
|
||||||
// Initialize toolbar to an empty selection
|
// Initialize toolbar to an empty selection
|
||||||
updateSelection([]);
|
updateSelection([]);
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,12 @@
|
|||||||
<div class="form">
|
<div class="form">
|
||||||
<span ng-repeat="section in structure.sections"
|
<span ng-repeat="section in structure.sections"
|
||||||
class="control-group coordinates"
|
class="control-group coordinates"
|
||||||
|
ng-if="!section.hidden"
|
||||||
title="{{section.description}}">
|
title="{{section.description}}">
|
||||||
|
|
||||||
<ng-form ng-repeat="item in section.items"
|
<ng-form ng-repeat="item in section.items"
|
||||||
ng-class="{ 'input-labeled': item.name }"
|
ng-class="{ 'input-labeled': item.name }"
|
||||||
|
ng-hide="item.hidden"
|
||||||
class="inline"
|
class="inline"
|
||||||
title="{{item.description}}"
|
title="{{item.description}}"
|
||||||
name="mctFormInner">
|
name="mctFormInner">
|
||||||
|
Reference in New Issue
Block a user