Skip to content

Commit 1b695cc

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 angular#9604.
1 parent 7cdd32a commit 1b695cc

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

Diff for: src/components/panel/panel.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ angular
576576
* @description
577577
* Sets the value of the offset in the x-direction.
578578
*
579-
* @param {string} offsetX
579+
* @param {string|number} offsetX
580580
* @returns {!MdPanelPosition}
581581
*/
582582

@@ -586,7 +586,7 @@ angular
586586
* @description
587587
* Sets the value of the offset in the y-direction.
588588
*
589-
* @param {string} offsetY
589+
* @param {string|number} offsetY
590590
* @returns {!MdPanelPosition}
591591
*/
592592

@@ -2003,23 +2003,23 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
20032003
/**
20042004
* Sets the value of the offset in the x-direction. This will add to any
20052005
* previously set offsets.
2006-
* @param {string} offsetX
2006+
* @param {string|number} offsetX
20072007
* @returns {!MdPanelPosition}
20082008
*/
20092009
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
2010-
this._translateX.push(offsetX);
2010+
this._translateX.push(addUnits(offsetX));
20112011
return this;
20122012
};
20132013

20142014

20152015
/**
20162016
* Sets the value of the offset in the y-direction. This will add to any
20172017
* previously set offsets.
2018-
* @param {string} offsetY
2018+
* @param {string|number} offsetY
20192019
* @returns {!MdPanelPosition}
20202020
*/
20212021
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
2022-
this._translateY.push(offsetY);
2022+
this._translateY.push(addUnits(offsetY));
20232023
return this;
20242024
};
20252025

@@ -2553,3 +2553,12 @@ function getElement(el) {
25532553
document.querySelector(el) : el;
25542554
return angular.element(queryResult);
25552555
}
2556+
2557+
/**
2558+
* Adds units to a number value.
2559+
* @param {number} value
2560+
* @return {string}
2561+
*/
2562+
function addUnits(value) {
2563+
return angular.isNumber(value) ? value + 'px' : value;
2564+
}

Diff for: src/components/panel/panel.spec.js

+40
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,26 @@ describe('$mdPanel', function() {
13941394
.toBeApproximately(middleOfPage + parseInt(offset));
13951395
});
13961396

1397+
it('horizontally with an integer value', function() {
1398+
var left = '50px';
1399+
var offset = -15;
1400+
1401+
var position = mdPanelPosition
1402+
.absolute()
1403+
.left(left)
1404+
.withOffsetX(offset);
1405+
1406+
config['position'] = position;
1407+
1408+
openPanel(config);
1409+
1410+
var panelRect = document.querySelector(PANEL_EL)
1411+
.getBoundingClientRect();
1412+
1413+
expect(panelRect.left)
1414+
.toBeApproximately(parseInt(left) + offset);
1415+
});
1416+
13971417
it('vertically', function() {
13981418
var top = '50px';
13991419
var offset = '-15px';
@@ -1435,6 +1455,26 @@ describe('$mdPanel', function() {
14351455
expect(middleOfPanel)
14361456
.toBeApproximately(middleOfPage + parseInt(offset));
14371457
});
1458+
1459+
it('vertically with an integer value', function() {
1460+
var top = '50px';
1461+
var offset = -15;
1462+
1463+
var position = mdPanelPosition
1464+
.absolute()
1465+
.top(top)
1466+
.withOffsetY(offset);
1467+
1468+
config['position'] = position;
1469+
1470+
openPanel(config);
1471+
1472+
var panelRect = document.querySelector(PANEL_EL)
1473+
.getBoundingClientRect();
1474+
1475+
expect(panelRect.top)
1476+
.toBeApproximately(parseInt(top) + offset);
1477+
});
14381478
});
14391479

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

0 commit comments

Comments
 (0)