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

Commit 07a594c

Browse files
committed
fix(panel): allow numbers in offset methods
Allows for numbers to be pass to the `withOffsetX` and `withOffsetY` methods, assuming that the units are pixels. Fixes #9604.
1 parent 1b9245a commit 07a594c

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

src/components/panel/panel.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ angular
666666
* @description
667667
* Sets the value of the offset in the x-direction.
668668
*
669-
* @param {string} offsetX
669+
* @param {string|number} offsetX
670670
* @returns {!MdPanelPosition}
671671
*/
672672

@@ -676,7 +676,7 @@ angular
676676
* @description
677677
* Sets the value of the offset in the y-direction.
678678
*
679-
* @param {string} offsetY
679+
* @param {string|number} offsetY
680680
* @returns {!MdPanelPosition}
681681
*/
682682

@@ -2392,23 +2392,23 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
23922392
/**
23932393
* Sets the value of the offset in the x-direction. This will add to any
23942394
* previously set offsets.
2395-
* @param {string|function(MdPanelPosition): string} offsetX
2395+
* @param {string|number|function(MdPanelPosition): string} offsetX
23962396
* @returns {!MdPanelPosition}
23972397
*/
23982398
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
2399-
this._translateX.push(offsetX);
2399+
this._translateX.push(addUnits(offsetX));
24002400
return this;
24012401
};
24022402

24032403

24042404
/**
24052405
* Sets the value of the offset in the y-direction. This will add to any
24062406
* previously set offsets.
2407-
* @param {string|function(MdPanelPosition): string} offsetY
2407+
* @param {string|number|function(MdPanelPosition): string} offsetY
24082408
* @returns {!MdPanelPosition}
24092409
*/
24102410
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
2411-
this._translateY.push(offsetY);
2411+
this._translateY.push(addUnits(offsetY));
24122412
return this;
24132413
};
24142414

@@ -2510,9 +2510,8 @@ MdPanelPosition.prototype.getActualPosition = function() {
25102510
MdPanelPosition.prototype._reduceTranslateValues =
25112511
function(translateFn, values) {
25122512
return values.map(function(translation) {
2513-
// TODO(crisbeto): this should add the units after #9609 is merged.
25142513
var translationValue = angular.isFunction(translation) ?
2515-
translation(this) : translation;
2514+
addUnits(translation(this)) : translation;
25162515
return translateFn + '(' + translationValue + ')';
25172516
}, this).join(' ');
25182517
};
@@ -2987,7 +2986,7 @@ function getElement(el) {
29872986
return angular.element(queryResult);
29882987
}
29892988

2990-
/**
2989+
/*
29912990
* Gets the computed values for an element's translateX and translateY in px.
29922991
* @param {!angular.JQLite|!Element} el
29932992
* @return {{x: number, y: number}}
@@ -3013,3 +3012,12 @@ function getComputedTranslations(el) {
30133012

30143013
return output;
30153014
}
3015+
3016+
/**
3017+
* Adds units to a number value.
3018+
* @param {string|number} value
3019+
* @return {string}
3020+
*/
3021+
function addUnits(value) {
3022+
return angular.isNumber(value) ? value + 'px' : value;
3023+
}

src/components/panel/panel.spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,26 @@ describe('$mdPanel', function() {
15951595
.toBeApproximately(middleOfPage + parseInt(offset));
15961596
});
15971597

1598+
it('horizontally with an integer value', function() {
1599+
var left = '50px';
1600+
var offset = -15;
1601+
1602+
var position = mdPanelPosition
1603+
.absolute()
1604+
.left(left)
1605+
.withOffsetX(offset);
1606+
1607+
config['position'] = position;
1608+
1609+
openPanel(config);
1610+
1611+
var panelRect = document.querySelector(PANEL_EL)
1612+
.getBoundingClientRect();
1613+
1614+
expect(panelRect.left)
1615+
.toBeApproximately(parseInt(left) + offset);
1616+
});
1617+
15981618
it('vertically', function() {
15991619
var top = '50px';
16001620
var offset = '-15px';
@@ -1664,6 +1684,50 @@ describe('$mdPanel', function() {
16641684
expect(middleOfPanel)
16651685
.toBeApproximately(middleOfPage + parseInt(offset));
16661686
});
1687+
1688+
it('vertically with an integer value', function() {
1689+
var top = '50px';
1690+
var offset = -15;
1691+
1692+
var position = mdPanelPosition
1693+
.absolute()
1694+
.top(top)
1695+
.withOffsetY(offset);
1696+
1697+
config['position'] = position;
1698+
1699+
openPanel(config);
1700+
1701+
var panelRect = document.querySelector(PANEL_EL)
1702+
.getBoundingClientRect();
1703+
1704+
expect(panelRect.top)
1705+
.toBeApproximately(parseInt(top) + offset);
1706+
});
1707+
1708+
it('with a function that does not return units', function() {
1709+
var left = '50px';
1710+
var offset = -15;
1711+
var obj = {
1712+
getOffsetX: function() {
1713+
return offset;
1714+
}
1715+
};
1716+
1717+
var position = mdPanelPosition
1718+
.absolute()
1719+
.left(left)
1720+
.withOffsetX(obj.getOffsetX);
1721+
1722+
config['position'] = position;
1723+
1724+
openPanel(config);
1725+
1726+
var panelRect = document.querySelector(PANEL_EL)
1727+
.getBoundingClientRect();
1728+
1729+
expect(panelRect.left).toBeApproximately(parseInt(left) + offset);
1730+
});
16671731
});
16681732

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

0 commit comments

Comments
 (0)