Skip to content

Commit

Permalink
feat(layer-selector-ui): update add/remove statematrix part functions
Browse files Browse the repository at this point in the history
Now you can specify states parts should be added/removed from.
  • Loading branch information
AleksueiR committed Apr 22, 2015
1 parent 04b1cf8 commit 98ee47b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/js/RAMP/Modules/filterManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ define([
}
);

LayerItem.addStateMatrixPart(options.stateMatrix, "notices", LayerItem.notices.USER, true);
LayerItem.addStateMatrixPart(options.stateMatrix, "notices", LayerItem.notices.USER, [], true);
LayerItem.removeStateMatrixPart(options.stateMatrix, "controls", LayerItem.controls.METADATA);
}

Expand Down
50 changes: 37 additions & 13 deletions src/js/RAMP/Modules/layerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,6 @@ define([
],
toggles: [
LayerItem.toggles.EYE,
LayerItem.toggles.BOX,
LayerItem.toggles.QUERY
],
notices: []
Expand Down Expand Up @@ -688,7 +687,7 @@ define([
],
toggles: [
LayerItem.toggles.EYE,
LayerItem.toggles.BOX
LayerItem.toggles.QUERY
],
notices: [
LayerItem.notices.UPDATE
Expand All @@ -715,7 +714,7 @@ define([
toggles: [
LayerItem.toggles.ZOOM,
LayerItem.toggles.EYE,
LayerItem.toggles.BOX
LayerItem.toggles.QUERY
],
notices: [
LayerItem.notices.SCALE
Expand Down Expand Up @@ -754,23 +753,44 @@ define([
LayerItem.state.UPDATING
];

/**
* Helper function to check if `states` parameter is false or [].
*
* @param {Array} [states] state names
* @method getStateNames
* @private
*/
function getStateNames(states) {
if (typeof states === 'undefined' || !states || states.length === 0) {
states = Object
.getOwnPropertyNames(LayerItem.state)
.map(function (key) { return LayerItem.state[key]; });
}

return states;
}

/**
* Modifies a given state matrix by adding specified partKey to the specified partType collection.
*
* @param {Object} stateMatrix matrix to modify
* @param {String} partType type of the parts to modify: `controls`, `toggles`, `notices`
* @param {String} partKey part key to be inserted into the collection
* @param {Boolean} prepend indicates if the part key should be prepended or appended
* @param {Array} [states] array of state names to insert the part into; if false or [], all states are assumed
* @param {Boolean} [prepend] indicates if the part key should be prepended or appended
* @method addStateMatrixPart
* @static
* @private
*/
LayerItem.addStateMatrixPart = function (stateMatrix, partType, partKey, prepend) {
LayerItem.addStateMatrixPart = function (stateMatrix, partType, partKey, states, prepend) {
states = getStateNames(states);

UtilDict.forEachEntry(stateMatrix, function (state, data) {
if (prepend) {
data[partType].unshift(partKey);
} else {
data[partType].push(partKey);
if (states.indexOf(state) !== -1) {
if (prepend) {
data[partType].unshift(partKey);
} else {
data[partType].push(partKey);
}
}
});
};
Expand All @@ -781,13 +801,17 @@ define([
* @param {Object} stateMatrix matrix to modify
* @param {String} partType type of the parts to modify: `controls`, `toggles`, `notices`
* @param {String} partKey part key to be removed into the collection
* @param {Array} [states] array of state names to remove the part from; if false or [], all states are assumed
* @method addStateMatrixPart
* @static
* @private
*/
LayerItem.removeStateMatrixPart = function (stateMatrix, partType, partKey) {
LayerItem.removeStateMatrixPart = function (stateMatrix, partType, partKey, states) {
states = getStateNames(states);

UtilDict.forEachEntry(stateMatrix, function (state, data) {
UtilArray.remove(data[partType], partKey);
if (states.indexOf(state) !== -1) {
UtilArray.remove(data[partType], partKey);
}
});
};

Expand Down

0 comments on commit 98ee47b

Please sign in to comment.