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

Commit b894397

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 421fed4 commit b894397

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

src/components/panel/panel.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ angular
617617
* @description
618618
* Sets the value of the offset in the x-direction.
619619
*
620-
* @param {string} offsetX
620+
* @param {string|number} offsetX
621621
* @returns {!MdPanelPosition}
622622
*/
623623

@@ -627,7 +627,7 @@ angular
627627
* @description
628628
* Sets the value of the offset in the y-direction.
629629
*
630-
* @param {string} offsetY
630+
* @param {string|number} offsetY
631631
* @returns {!MdPanelPosition}
632632
*/
633633

@@ -2166,23 +2166,23 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
21662166
/**
21672167
* Sets the value of the offset in the x-direction. This will add to any
21682168
* previously set offsets.
2169-
* @param {string|function(MdPanelPosition): string} offsetX
2169+
* @param {string|number|function(MdPanelPosition): string} offsetX
21702170
* @returns {!MdPanelPosition}
21712171
*/
21722172
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
2173-
this._translateX.push(offsetX);
2173+
this._translateX.push(addUnits(offsetX));
21742174
return this;
21752175
};
21762176

21772177

21782178
/**
21792179
* Sets the value of the offset in the y-direction. This will add to any
21802180
* previously set offsets.
2181-
* @param {string|function(MdPanelPosition): string} offsetY
2181+
* @param {string|number|function(MdPanelPosition): string} offsetY
21822182
* @returns {!MdPanelPosition}
21832183
*/
21842184
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
2185-
this._translateY.push(offsetY);
2185+
this._translateY.push(addUnits(offsetY));
21862186
return this;
21872187
};
21882188

@@ -2280,9 +2280,8 @@ MdPanelPosition.prototype.getActualPosition = function() {
22802280
MdPanelPosition.prototype._reduceTranslateValues =
22812281
function(translateFn, values) {
22822282
return values.map(function(translation) {
2283-
// TODO(crisbeto): this should add the units after #9609 is merged.
22842283
var translationValue = angular.isFunction(translation) ?
2285-
translation(this) : translation;
2284+
addUnits(translation(this)) : translation;
22862285
return translateFn + '(' + translationValue + ')';
22872286
}, this).join(' ');
22882287
};
@@ -2719,3 +2718,12 @@ function getElement(el) {
27192718
document.querySelector(el) : el;
27202719
return angular.element(queryResult);
27212720
}
2721+
2722+
/**
2723+
* Adds units to a number value.
2724+
* @param {string|number} value
2725+
* @return {string}
2726+
*/
2727+
function addUnits(value) {
2728+
return angular.isNumber(value) ? value + 'px' : value;
2729+
}

src/components/panel/panel.spec.js

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

1425+
it('horizontally with an integer value', function() {
1426+
var left = '50px';
1427+
var offset = -15;
1428+
1429+
var position = mdPanelPosition
1430+
.absolute()
1431+
.left(left)
1432+
.withOffsetX(offset);
1433+
1434+
config['position'] = position;
1435+
1436+
openPanel(config);
1437+
1438+
var panelRect = document.querySelector(PANEL_EL)
1439+
.getBoundingClientRect();
1440+
1441+
expect(panelRect.left)
1442+
.toBeApproximately(parseInt(left) + offset);
1443+
});
1444+
14251445
it('vertically', function() {
14261446
var top = '50px';
14271447
var offset = '-15px';
@@ -1491,6 +1511,50 @@ describe('$mdPanel', function() {
14911511
expect(middleOfPanel)
14921512
.toBeApproximately(middleOfPage + parseInt(offset));
14931513
});
1514+
1515+
it('vertically with an integer value', function() {
1516+
var top = '50px';
1517+
var offset = -15;
1518+
1519+
var position = mdPanelPosition
1520+
.absolute()
1521+
.top(top)
1522+
.withOffsetY(offset);
1523+
1524+
config['position'] = position;
1525+
1526+
openPanel(config);
1527+
1528+
var panelRect = document.querySelector(PANEL_EL)
1529+
.getBoundingClientRect();
1530+
1531+
expect(panelRect.top)
1532+
.toBeApproximately(parseInt(top) + offset);
1533+
});
1534+
1535+
it('with a function that does not return units', function() {
1536+
var left = '50px';
1537+
var offset = -15;
1538+
var obj = {
1539+
getOffsetX: function() {
1540+
return offset;
1541+
}
1542+
};
1543+
1544+
var position = mdPanelPosition
1545+
.absolute()
1546+
.left(left)
1547+
.withOffsetX(obj.getOffsetX);
1548+
1549+
config['position'] = position;
1550+
1551+
openPanel(config);
1552+
1553+
var panelRect = document.querySelector(PANEL_EL)
1554+
.getBoundingClientRect();
1555+
1556+
expect(panelRect.left).toBeApproximately(parseInt(left) + offset);
1557+
});
14941558
});
14951559

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

0 commit comments

Comments
 (0)