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

Commit a23b43b

Browse files
crisbetoSplaktar
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 8111cf4 commit a23b43b

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

Diff for: 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

@@ -2861,23 +2861,23 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
28612861
/**
28622862
* Sets the value of the offset in the x-direction. This will add to any
28632863
* previously set offsets.
2864-
* @param {string|function(MdPanelPosition): string} offsetX
2864+
* @param {string|number|function(MdPanelPosition): string} offsetX
28652865
* @returns {!MdPanelPosition}
28662866
*/
28672867
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
2868-
this._translateX.push(offsetX);
2868+
this._translateX.push(addUnits(offsetX));
28692869
return this;
28702870
};
28712871

28722872

28732873
/**
28742874
* Sets the value of the offset in the y-direction. This will add to any
28752875
* previously set offsets.
2876-
* @param {string|function(MdPanelPosition): string} offsetY
2876+
* @param {string|number|function(MdPanelPosition): string} offsetY
28772877
* @returns {!MdPanelPosition}
28782878
*/
28792879
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
2880-
this._translateY.push(offsetY);
2880+
this._translateY.push(addUnits(offsetY));
28812881
return this;
28822882
};
28832883

@@ -2993,9 +2993,8 @@ MdPanelPosition.prototype.getActualPosition = function() {
29932993
MdPanelPosition.prototype._reduceTranslateValues =
29942994
function(translateFn, values) {
29952995
return values.map(function(translation) {
2996-
// TODO(crisbeto): this should add the units after #9609 is merged.
29972996
var translationValue = angular.isFunction(translation) ?
2998-
translation(this) : translation;
2997+
addUnits(translation(this)) : translation;
29992998
return translateFn + '(' + translationValue + ')';
30002999
}, this).join(' ');
30013000
};
@@ -3525,7 +3524,6 @@ function getElement(el) {
35253524
return angular.element(queryResult);
35263525
}
35273526

3528-
35293527
/**
35303528
* Gets the computed values for an element's translateX and translateY in px.
35313529
* @param {!angular.JQLite|!Element} el
@@ -3553,3 +3551,12 @@ function getComputedTranslations(el, property) {
35533551

35543552
return output;
35553553
}
3554+
3555+
/**
3556+
* Adds units to a number value.
3557+
* @param {string|number} value
3558+
* @return {string}
3559+
*/
3560+
function addUnits(value) {
3561+
return angular.isNumber(value) ? value + 'px' : value;
3562+
}

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

+66-2
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,26 @@ describe('$mdPanel', function() {
19421942
.toBeApproximately(middleOfPage + parseInt(offset));
19431943
});
19441944

1945+
it('horizontally with an integer value', function() {
1946+
var left = '50px';
1947+
var offset = -15;
1948+
1949+
var position = mdPanelPosition
1950+
.absolute()
1951+
.left(left)
1952+
.withOffsetX(offset);
1953+
1954+
config['position'] = position;
1955+
1956+
openPanel(config);
1957+
1958+
var panelRect = document.querySelector(PANEL_EL)
1959+
.getBoundingClientRect();
1960+
1961+
expect(panelRect.left)
1962+
.toBeApproximately(parseInt(left) + offset);
1963+
});
1964+
19451965
it('vertically', function() {
19461966
var top = '50px';
19471967
var offset = '-15px';
@@ -2012,6 +2032,50 @@ describe('$mdPanel', function() {
20122032
.toBeApproximately(middleOfPage + parseInt(offset));
20132033
});
20142034

2035+
it('vertically with an integer value', function() {
2036+
var top = '50px';
2037+
var offset = -15;
2038+
2039+
var position = mdPanelPosition
2040+
.absolute()
2041+
.top(top)
2042+
.withOffsetY(offset);
2043+
2044+
config['position'] = position;
2045+
2046+
openPanel(config);
2047+
2048+
var panelRect = document.querySelector(PANEL_EL)
2049+
.getBoundingClientRect();
2050+
2051+
expect(panelRect.top)
2052+
.toBeApproximately(parseInt(top) + offset);
2053+
});
2054+
2055+
it('with a function that does not return units', function() {
2056+
var left = '50px';
2057+
var offset = -15;
2058+
var obj = {
2059+
getOffsetX: function() {
2060+
return offset;
2061+
}
2062+
};
2063+
2064+
var position = mdPanelPosition
2065+
.absolute()
2066+
.left(left)
2067+
.withOffsetX(obj.getOffsetX);
2068+
2069+
config['position'] = position;
2070+
2071+
openPanel(config);
2072+
2073+
var panelRect = document.querySelector(PANEL_EL)
2074+
.getBoundingClientRect();
2075+
2076+
expect(panelRect.left).toBeApproximately(parseInt(left) + offset);
2077+
});
2078+
20152079
it('should apply offsets to the panel inner wrapper, instead of directly ' +
20162080
'on the panel element', function() {
20172081
var position = mdPanelPosition
@@ -2264,7 +2328,7 @@ describe('$mdPanel', function() {
22642328
myButton = '<button>myButton</button>';
22652329
attachToBody(myButton);
22662330
myButton = angular.element(document.querySelector('button'));
2267-
myButton.css('margin', '100px');
2331+
myButton.css('margin', '120px');
22682332
myButtonRect = myButton[0].getBoundingClientRect();
22692333

22702334
xPosition = $mdPanel.xPosition;
@@ -2822,7 +2886,7 @@ describe('$mdPanel', function() {
28222886
expect(panelRect.right).toBeApproximately(myButtonRect.right);
28232887
});
28242888

2825-
it('offset to the right of an element', function() {
2889+
it('offset to the left of an element', function() {
28262890
var position = mdPanelPosition
28272891
.relativeTo(myButton)
28282892
.addPanelPosition(xPosition.OFFSET_END, null);

0 commit comments

Comments
 (0)