Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

fix(panel): consolidate redundant validations #9631

Merged
merged 2 commits into from
May 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 32 additions & 55 deletions src/components/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2793,65 +2793,15 @@ MdPanelPosition.prototype.addPanelPosition = function(xPosition, yPosition) {
'relative positioning. Set relativeTo first.');
}

this._validateXPosition(xPosition);
this._validateYPosition(yPosition);
validatePosition(MdPanelPosition.xPosition, xPosition);
validatePosition(MdPanelPosition.yPosition, yPosition);

this._positions.push({
x: xPosition,
y: yPosition,
x: xPosition,
y: yPosition
});
return this;
};


/**
* Ensures that yPosition is a valid position name. Throw an exception if not.
* @param {string} yPosition
*/
MdPanelPosition.prototype._validateYPosition = function(yPosition) {
// empty is ok
if (yPosition == null) {
return;
}

var positionKeys = Object.keys(MdPanelPosition.yPosition);
var positionValues = [];
for (var key, i = 0; key = positionKeys[i]; i++) {
var position = MdPanelPosition.yPosition[key];
positionValues.push(position);

if (position === yPosition) {
return;
}
}

throw new Error('mdPanel: Panel y position only accepts the following ' +
'values:\n' + positionValues.join(' | '));
};


/**
* Ensures that xPosition is a valid position name. Throw an exception if not.
* @param {string} xPosition
*/
MdPanelPosition.prototype._validateXPosition = function(xPosition) {
// empty is ok
if (xPosition == null) {
return;
}

var positionKeys = Object.keys(MdPanelPosition.xPosition);
var positionValues = [];
for (var key, i = 0; key = positionKeys[i]; i++) {
var position = MdPanelPosition.xPosition[key];
positionValues.push(position);
if (position === xPosition) {
return;
}
}

throw new Error('mdPanel: Panel x Position only accepts the following ' +
'values:\n' + positionValues.join(' | '));
return this;
};


Expand Down Expand Up @@ -3553,6 +3503,33 @@ function getComputedTranslations(el, property) {
return output;
}

/*
* Ensures that a value is a valid position name. Throw an exception if not.
* @param {Object} positionMap Object against which the value will be checked.
* @param {string} value
*/
function validatePosition(positionMap, value) {
// empty is ok
if (value === null || angular.isUndefined(value)) {
return;
}

var positionKeys = Object.keys(positionMap);
var positionValues = [];

for (var key, i = 0; key = positionKeys[i]; i++) {
var position = positionMap[key];
positionValues.push(position);

if (position === value) {
return;
}
}

throw new Error('Panel position only accepts the following values:\n' +
positionValues.join(' | '));
}

/**
* Adds units to a number value.
* @param {string|number} value
Expand Down