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

Commit

Permalink
fix(panel): allow numbers in offset methods (#9609)
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 #9604.
  • Loading branch information
crisbeto authored and mmalerba committed Feb 9, 2018
1 parent 4d99e36 commit 0d276f3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/components/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ angular
* @description
* Sets the value of the offset in the x-direction.
*
* @param {string} offsetX
* @param {string|number} offsetX
* @returns {!MdPanelPosition}
*/

Expand All @@ -824,7 +824,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 @@ -2844,23 +2844,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 @@ -2976,9 +2976,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 @@ -3508,7 +3507,6 @@ function getElement(el) {
return angular.element(queryResult);
}


/**
* Gets the computed values for an element's translateX and translateY in px.
* @param {!angular.JQLite|!Element} el
Expand Down Expand Up @@ -3536,3 +3534,12 @@ function getComputedTranslations(el, property) {

return output;
}

/**
* 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 @@ -1940,6 +1940,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 @@ -2009,6 +2029,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 0d276f3

Please sign in to comment.