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

Commit 0896ba3

Browse files
crisbetojelbourn
authored andcommitted
feat(panel): allow passing in a function to the offset methods (#9615)
* Allows for a function to be passed to the `withOffsetX` and `withOffsetY` methods which will help when trying to determine the offset dynamically. * Switches to assigning the offset before the position, in order to avoid tiny jumps in the UI on slower browsers. Fixes #9608.
1 parent 72d0685 commit 0896ba3

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/components/panel/panel.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,12 @@ MdPanelRef.prototype._updatePosition = function(init) {
13371337
var positionConfig = this.config['position'];
13381338

13391339
if (positionConfig) {
1340+
// Use the vendor prefixed version of transform.
1341+
// Note that the offset should be assigned before the position, in
1342+
// order to avoid tiny jumps in the panel's position, on slower browsers.
1343+
var prefixedTransform = this._$mdConstant.CSS.TRANSFORM;
1344+
this.panelEl.css(prefixedTransform, positionConfig.getTransform());
1345+
13401346
positionConfig._setPanelPosition(this.panelEl);
13411347

13421348
// Hide the panel now that position is known.
@@ -1360,10 +1366,6 @@ MdPanelRef.prototype._updatePosition = function(init) {
13601366
MdPanelPosition.absPosition.RIGHT,
13611367
positionConfig.getRight()
13621368
);
1363-
1364-
// Use the vendor prefixed version of transform.
1365-
var prefixedTransform = this._$mdConstant.CSS.TRANSFORM;
1366-
this.panelEl.css(prefixedTransform, positionConfig.getTransform());
13671369
}
13681370
};
13691371

@@ -2003,7 +2005,7 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
20032005
/**
20042006
* Sets the value of the offset in the x-direction. This will add to any
20052007
* previously set offsets.
2006-
* @param {string} offsetX
2008+
* @param {string|function(MdPanelPosition): string} offsetX
20072009
* @returns {!MdPanelPosition}
20082010
*/
20092011
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
@@ -2015,7 +2017,7 @@ MdPanelPosition.prototype.withOffsetX = function(offsetX) {
20152017
/**
20162018
* Sets the value of the offset in the y-direction. This will add to any
20172019
* previously set offsets.
2018-
* @param {string} offsetY
2020+
* @param {string|function(MdPanelPosition): string} offsetY
20192021
* @returns {!MdPanelPosition}
20202022
*/
20212023
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
@@ -2117,8 +2119,11 @@ MdPanelPosition.prototype.getActualPosition = function() {
21172119
MdPanelPosition.prototype._reduceTranslateValues =
21182120
function(translateFn, values) {
21192121
return values.map(function(translation) {
2120-
return translateFn + '(' + translation + ')';
2121-
}).join(' ');
2122+
// TODO(crisbeto): this should add the units after #9609 is merged.
2123+
var translationValue = angular.isFunction(translation) ?
2124+
translation(this) : translation;
2125+
return translateFn + '(' + translationValue + ')';
2126+
}, this).join(' ');
21222127
};
21232128

21242129

src/components/panel/panel.spec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,34 @@ describe('$mdPanel', function() {
13721372
.toBeApproximately(parseInt(left) + parseInt(offset));
13731373
});
13741374

1375+
it('horizontally with a function', function() {
1376+
var left = '50px';
1377+
var offset = '-15px';
1378+
var obj = {
1379+
getOffsetX: function() {
1380+
return offset;
1381+
}
1382+
};
1383+
1384+
spyOn(obj, 'getOffsetX').and.callThrough();
1385+
1386+
var position = mdPanelPosition
1387+
.absolute()
1388+
.left(left)
1389+
.withOffsetX(obj.getOffsetX);
1390+
1391+
config['position'] = position;
1392+
1393+
openPanel(config);
1394+
1395+
var panelRect = document.querySelector(PANEL_EL)
1396+
.getBoundingClientRect();
1397+
1398+
expect(obj.getOffsetX).toHaveBeenCalledWith(position);
1399+
expect(panelRect.left)
1400+
.toBeApproximately(parseInt(left) + parseInt(offset));
1401+
});
1402+
13751403
it('horizontally with centering', function() {
13761404
var offset = '15px';
13771405

@@ -1414,6 +1442,34 @@ describe('$mdPanel', function() {
14141442
.toBeApproximately(parseInt(top) + parseInt(offset));
14151443
});
14161444

1445+
it('vertically with a function', function() {
1446+
var top = '50px';
1447+
var offset = '-15px';
1448+
var obj = {
1449+
getOffsetY: function() {
1450+
return offset;
1451+
}
1452+
};
1453+
1454+
spyOn(obj, 'getOffsetY').and.callThrough();
1455+
1456+
var position = mdPanelPosition
1457+
.absolute()
1458+
.top(top)
1459+
.withOffsetY(obj.getOffsetY);
1460+
1461+
config['position'] = position;
1462+
1463+
openPanel(config);
1464+
1465+
var panelRect = document.querySelector(PANEL_EL)
1466+
.getBoundingClientRect();
1467+
1468+
expect(obj.getOffsetY).toHaveBeenCalledWith(position);
1469+
expect(panelRect.top)
1470+
.toBeApproximately(parseInt(top) + parseInt(offset));
1471+
});
1472+
14171473
it('vertically with centering', function() {
14181474
var offset = '15px';
14191475

0 commit comments

Comments
 (0)