Skip to content

Commit cfd857e

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 a1f2e17 commit cfd857e

File tree

3 files changed

+80
-37
lines changed

3 files changed

+80
-37
lines changed

scripts/sauce/setup-tunnel.sh

-28
This file was deleted.

src/components/panel/panel.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ angular
676676
* @description
677677
* Sets the value of the offset in the x-direction.
678678
*
679-
* @param {string} offsetX
679+
* @param {string|number} offsetX
680680
* @returns {!MdPanelPosition}
681681
*/
682682

@@ -686,7 +686,7 @@ angular
686686
* @description
687687
* Sets the value of the offset in the y-direction.
688688
*
689-
* @param {string} offsetY
689+
* @param {string|number} offsetY
690690
* @returns {!MdPanelPosition}
691691
*/
692692

@@ -2532,23 +2532,23 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
25322532
/**
25332533
* Sets the value of the offset in the x-direction. This will add to any
25342534
* previously set offsets.
2535-
* @param {string|function(MdPanelPosition): string} offsetX
2535+
* @param {string|number|function(MdPanelPosition): string} offsetX
25362536
* @returns {!MdPanelPosition}
25372537
*/
25382538
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
2539-
this._translateX.push(offsetX);
2539+
this._translateX.push(addUnits(offsetX));
25402540
return this;
25412541
};
25422542

25432543

25442544
/**
25452545
* Sets the value of the offset in the y-direction. This will add to any
25462546
* previously set offsets.
2547-
* @param {string|function(MdPanelPosition): string} offsetY
2547+
* @param {string|number|function(MdPanelPosition): string} offsetY
25482548
* @returns {!MdPanelPosition}
25492549
*/
25502550
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
2551-
this._translateY.push(offsetY);
2551+
this._translateY.push(addUnits(offsetY));
25522552
return this;
25532553
};
25542554

@@ -2664,9 +2664,8 @@ MdPanelPosition.prototype.getActualPosition = function() {
26642664
MdPanelPosition.prototype._reduceTranslateValues =
26652665
function(translateFn, values) {
26662666
return values.map(function(translation) {
2667-
// TODO(crisbeto): this should add the units after #9609 is merged.
26682667
var translationValue = angular.isFunction(translation) ?
2669-
translation(this) : translation;
2668+
addUnits(translation(this)) : translation;
26702669
return translateFn + '(' + translationValue + ')';
26712670
}, this).join(' ');
26722671
};
@@ -3186,7 +3185,6 @@ function getElement(el) {
31863185
return angular.element(queryResult);
31873186
}
31883187

3189-
31903188
/**
31913189
* Gets the computed values for an element's translateX and translateY in px.
31923190
* @param {!angular.JQLite|!Element} el
@@ -3214,3 +3212,12 @@ function getComputedTranslations(el, property) {
32143212

32153213
return output;
32163214
}
3215+
3216+
/**
3217+
* Adds units to a number value.
3218+
* @param {string|number} value
3219+
* @return {string}
3220+
*/
3221+
function addUnits(value) {
3222+
return angular.isNumber(value) ? value + 'px' : value;
3223+
}

src/components/panel/panel.spec.js

+64
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,26 @@ describe('$mdPanel', function() {
18041804
.toBeApproximately(middleOfPage + parseInt(offset));
18051805
});
18061806

1807+
it('horizontally with an integer value', function() {
1808+
var left = '50px';
1809+
var offset = -15;
1810+
1811+
var position = mdPanelPosition
1812+
.absolute()
1813+
.left(left)
1814+
.withOffsetX(offset);
1815+
1816+
config['position'] = position;
1817+
1818+
openPanel(config);
1819+
1820+
var panelRect = document.querySelector(PANEL_EL)
1821+
.getBoundingClientRect();
1822+
1823+
expect(panelRect.left)
1824+
.toBeApproximately(parseInt(left) + offset);
1825+
});
1826+
18071827
it('vertically', function() {
18081828
var top = '50px';
18091829
var offset = '-15px';
@@ -1873,6 +1893,50 @@ describe('$mdPanel', function() {
18731893
expect(middleOfPanel)
18741894
.toBeApproximately(middleOfPage + parseInt(offset));
18751895
});
1896+
1897+
it('vertically with an integer value', function() {
1898+
var top = '50px';
1899+
var offset = -15;
1900+
1901+
var position = mdPanelPosition
1902+
.absolute()
1903+
.top(top)
1904+
.withOffsetY(offset);
1905+
1906+
config['position'] = position;
1907+
1908+
openPanel(config);
1909+
1910+
var panelRect = document.querySelector(PANEL_EL)
1911+
.getBoundingClientRect();
1912+
1913+
expect(panelRect.top)
1914+
.toBeApproximately(parseInt(top) + offset);
1915+
});
1916+
1917+
it('with a function that does not return units', function() {
1918+
var left = '50px';
1919+
var offset = -15;
1920+
var obj = {
1921+
getOffsetX: function() {
1922+
return offset;
1923+
}
1924+
};
1925+
1926+
var position = mdPanelPosition
1927+
.absolute()
1928+
.left(left)
1929+
.withOffsetX(obj.getOffsetX);
1930+
1931+
config['position'] = position;
1932+
1933+
openPanel(config);
1934+
1935+
var panelRect = document.querySelector(PANEL_EL)
1936+
.getBoundingClientRect();
1937+
1938+
expect(panelRect.left).toBeApproximately(parseInt(left) + offset);
1939+
});
18761940
});
18771941

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

0 commit comments

Comments
 (0)