Skip to content

Commit

Permalink
fix(panel): allow numbers in offset methods
Browse files Browse the repository at this point in the history
Allows for numbers to be pass to the `withOffsetX` and `withOffsetY` methods, assuming that the units are pixels.

Fixes angular#9604.
  • Loading branch information
crisbeto committed Sep 21, 2016
1 parent 421fed4 commit b894397
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/components/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ angular
* @description
* Sets the value of the offset in the x-direction.
*
* @param {string} offsetX
* @param {string|number} offsetX
* @returns {!MdPanelPosition}
*/

Expand All @@ -627,7 +627,7 @@ angular
* @description
* Sets the value of the offset in the y-direction.
*
* @param {string} offsetY
* @param {string|number} offsetY
* @returns {!MdPanelPosition}
*/

Expand Down Expand Up @@ -2166,23 +2166,23 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
/**
* Sets the value of the offset in the x-direction. This will add to any
* previously set offsets.
* @param {string|function(MdPanelPosition): string} offsetX
* @param {string|number|function(MdPanelPosition): string} offsetX
* @returns {!MdPanelPosition}
*/
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
this._translateX.push(offsetX);
this._translateX.push(addUnits(offsetX));
return this;
};


/**
* Sets the value of the offset in the y-direction. This will add to any
* previously set offsets.
* @param {string|function(MdPanelPosition): string} offsetY
* @param {string|number|function(MdPanelPosition): string} offsetY
* @returns {!MdPanelPosition}
*/
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
this._translateY.push(offsetY);
this._translateY.push(addUnits(offsetY));
return this;
};

Expand Down Expand Up @@ -2280,9 +2280,8 @@ MdPanelPosition.prototype.getActualPosition = function() {
MdPanelPosition.prototype._reduceTranslateValues =
function(translateFn, values) {
return values.map(function(translation) {
// TODO(crisbeto): this should add the units after #9609 is merged.
var translationValue = angular.isFunction(translation) ?
translation(this) : translation;
addUnits(translation(this)) : translation;
return translateFn + '(' + translationValue + ')';
}, this).join(' ');
};
Expand Down Expand Up @@ -2719,3 +2718,12 @@ function getElement(el) {
document.querySelector(el) : el;
return angular.element(queryResult);
}

/**
* Adds units to a number value.
* @param {string|number} value
* @return {string}
*/
function addUnits(value) {
return angular.isNumber(value) ? value + 'px' : value;
}
64 changes: 64 additions & 0 deletions src/components/panel/panel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,26 @@ describe('$mdPanel', function() {
.toBeApproximately(middleOfPage + parseInt(offset));
});

it('horizontally with an integer value', function() {
var left = '50px';
var offset = -15;

var position = mdPanelPosition
.absolute()
.left(left)
.withOffsetX(offset);

config['position'] = position;

openPanel(config);

var panelRect = document.querySelector(PANEL_EL)
.getBoundingClientRect();

expect(panelRect.left)
.toBeApproximately(parseInt(left) + offset);
});

it('vertically', function() {
var top = '50px';
var offset = '-15px';
Expand Down Expand Up @@ -1491,6 +1511,50 @@ describe('$mdPanel', function() {
expect(middleOfPanel)
.toBeApproximately(middleOfPage + parseInt(offset));
});

it('vertically with an integer value', function() {
var top = '50px';
var offset = -15;

var position = mdPanelPosition
.absolute()
.top(top)
.withOffsetY(offset);

config['position'] = position;

openPanel(config);

var panelRect = document.querySelector(PANEL_EL)
.getBoundingClientRect();

expect(panelRect.top)
.toBeApproximately(parseInt(top) + offset);
});

it('with a function that does not return units', function() {
var left = '50px';
var offset = -15;
var obj = {
getOffsetX: function() {
return offset;
}
};

var position = mdPanelPosition
.absolute()
.left(left)
.withOffsetX(obj.getOffsetX);

config['position'] = position;

openPanel(config);

var panelRect = document.querySelector(PANEL_EL)
.getBoundingClientRect();

expect(panelRect.left).toBeApproximately(parseInt(left) + offset);
});
});

describe('should absolutely position the panel at', function() {
Expand Down

0 comments on commit b894397

Please sign in to comment.