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

Commit 0d276f3

Browse files
crisbetommalerba
authored andcommitted
fix(panel): allow numbers in offset methods (#9609)
Allows for numbers to be pass to the `withOffsetX` and `withOffsetY` methods, assuming that the units are pixels. Fixes #9604.
1 parent 4d99e36 commit 0d276f3

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

src/components/panel/panel.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ angular
814814
* @description
815815
* Sets the value of the offset in the x-direction.
816816
*
817-
* @param {string} offsetX
817+
* @param {string|number} offsetX
818818
* @returns {!MdPanelPosition}
819819
*/
820820

@@ -824,7 +824,7 @@ angular
824824
* @description
825825
* Sets the value of the offset in the y-direction.
826826
*
827-
* @param {string} offsetY
827+
* @param {string|number} offsetY
828828
* @returns {!MdPanelPosition}
829829
*/
830830

@@ -2844,23 +2844,23 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
28442844
/**
28452845
* Sets the value of the offset in the x-direction. This will add to any
28462846
* previously set offsets.
2847-
* @param {string|function(MdPanelPosition): string} offsetX
2847+
* @param {string|number|function(MdPanelPosition): string} offsetX
28482848
* @returns {!MdPanelPosition}
28492849
*/
28502850
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
2851-
this._translateX.push(offsetX);
2851+
this._translateX.push(addUnits(offsetX));
28522852
return this;
28532853
};
28542854

28552855

28562856
/**
28572857
* Sets the value of the offset in the y-direction. This will add to any
28582858
* previously set offsets.
2859-
* @param {string|function(MdPanelPosition): string} offsetY
2859+
* @param {string|number|function(MdPanelPosition): string} offsetY
28602860
* @returns {!MdPanelPosition}
28612861
*/
28622862
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
2863-
this._translateY.push(offsetY);
2863+
this._translateY.push(addUnits(offsetY));
28642864
return this;
28652865
};
28662866

@@ -2976,9 +2976,8 @@ MdPanelPosition.prototype.getActualPosition = function() {
29762976
MdPanelPosition.prototype._reduceTranslateValues =
29772977
function(translateFn, values) {
29782978
return values.map(function(translation) {
2979-
// TODO(crisbeto): this should add the units after #9609 is merged.
29802979
var translationValue = angular.isFunction(translation) ?
2981-
translation(this) : translation;
2980+
addUnits(translation(this)) : translation;
29822981
return translateFn + '(' + translationValue + ')';
29832982
}, this).join(' ');
29842983
};
@@ -3508,7 +3507,6 @@ function getElement(el) {
35083507
return angular.element(queryResult);
35093508
}
35103509

3511-
35123510
/**
35133511
* Gets the computed values for an element's translateX and translateY in px.
35143512
* @param {!angular.JQLite|!Element} el
@@ -3536,3 +3534,12 @@ function getComputedTranslations(el, property) {
35363534

35373535
return output;
35383536
}
3537+
3538+
/**
3539+
* Adds units to a number value.
3540+
* @param {string|number} value
3541+
* @return {string}
3542+
*/
3543+
function addUnits(value) {
3544+
return angular.isNumber(value) ? value + 'px' : value;
3545+
}

src/components/panel/panel.spec.js

+64
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,26 @@ describe('$mdPanel', function() {
19401940
.toBeApproximately(middleOfPage + parseInt(offset));
19411941
});
19421942

1943+
it('horizontally with an integer value', function() {
1944+
var left = '50px';
1945+
var offset = -15;
1946+
1947+
var position = mdPanelPosition
1948+
.absolute()
1949+
.left(left)
1950+
.withOffsetX(offset);
1951+
1952+
config['position'] = position;
1953+
1954+
openPanel(config);
1955+
1956+
var panelRect = document.querySelector(PANEL_EL)
1957+
.getBoundingClientRect();
1958+
1959+
expect(panelRect.left)
1960+
.toBeApproximately(parseInt(left) + offset);
1961+
});
1962+
19431963
it('vertically', function() {
19441964
var top = '50px';
19451965
var offset = '-15px';
@@ -2009,6 +2029,50 @@ describe('$mdPanel', function() {
20092029
expect(middleOfPanel)
20102030
.toBeApproximately(middleOfPage + parseInt(offset));
20112031
});
2032+
2033+
it('vertically with an integer value', function() {
2034+
var top = '50px';
2035+
var offset = -15;
2036+
2037+
var position = mdPanelPosition
2038+
.absolute()
2039+
.top(top)
2040+
.withOffsetY(offset);
2041+
2042+
config['position'] = position;
2043+
2044+
openPanel(config);
2045+
2046+
var panelRect = document.querySelector(PANEL_EL)
2047+
.getBoundingClientRect();
2048+
2049+
expect(panelRect.top)
2050+
.toBeApproximately(parseInt(top) + offset);
2051+
});
2052+
2053+
it('with a function that does not return units', function() {
2054+
var left = '50px';
2055+
var offset = -15;
2056+
var obj = {
2057+
getOffsetX: function() {
2058+
return offset;
2059+
}
2060+
};
2061+
2062+
var position = mdPanelPosition
2063+
.absolute()
2064+
.left(left)
2065+
.withOffsetX(obj.getOffsetX);
2066+
2067+
config['position'] = position;
2068+
2069+
openPanel(config);
2070+
2071+
var panelRect = document.querySelector(PANEL_EL)
2072+
.getBoundingClientRect();
2073+
2074+
expect(panelRect.left).toBeApproximately(parseInt(left) + offset);
2075+
});
20122076
});
20132077

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

0 commit comments

Comments
 (0)