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

fix(panel): allow numbers in offset methods #9609

Merged
merged 1 commit into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
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
25 changes: 16 additions & 9 deletions src/components/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ angular
* @description
* Sets the value of the offset in the x-direction.
*
* @param {string} offsetX
* @param {string|number} offsetX
* @returns {!MdPanelPosition}
*/

Expand All @@ -686,7 +686,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 @@ -2532,23 +2532,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 @@ -2664,9 +2664,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 @@ -3186,7 +3185,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 @@ -3214,3 +3212,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 @@ -1804,6 +1804,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 @@ -1873,6 +1893,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