Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat(panel): allow passing in a function to the offset methods (#9615)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
crisbeto authored and jelbourn committed Sep 21, 2016
1 parent 72d0685 commit 0896ba3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/components/panel/panel.js
Expand Up @@ -1337,6 +1337,12 @@ MdPanelRef.prototype._updatePosition = function(init) {
var positionConfig = this.config['position'];

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

positionConfig._setPanelPosition(this.panelEl);

// Hide the panel now that position is known.
Expand All @@ -1360,10 +1366,6 @@ MdPanelRef.prototype._updatePosition = function(init) {
MdPanelPosition.absPosition.RIGHT,
positionConfig.getRight()
);

// Use the vendor prefixed version of transform.
var prefixedTransform = this._$mdConstant.CSS.TRANSFORM;
this.panelEl.css(prefixedTransform, positionConfig.getTransform());
}
};

Expand Down Expand Up @@ -2003,7 +2005,7 @@ MdPanelPosition.prototype._validateXPosition = function(xPosition) {
/**
* Sets the value of the offset in the x-direction. This will add to any
* previously set offsets.
* @param {string} offsetX
* @param {string|function(MdPanelPosition): string} offsetX
* @returns {!MdPanelPosition}
*/
MdPanelPosition.prototype.withOffsetX = function(offsetX) {
Expand All @@ -2015,7 +2017,7 @@ MdPanelPosition.prototype.withOffsetX = function(offsetX) {
/**
* Sets the value of the offset in the y-direction. This will add to any
* previously set offsets.
* @param {string} offsetY
* @param {string|function(MdPanelPosition): string} offsetY
* @returns {!MdPanelPosition}
*/
MdPanelPosition.prototype.withOffsetY = function(offsetY) {
Expand Down Expand Up @@ -2117,8 +2119,11 @@ MdPanelPosition.prototype.getActualPosition = function() {
MdPanelPosition.prototype._reduceTranslateValues =
function(translateFn, values) {
return values.map(function(translation) {
return translateFn + '(' + translation + ')';
}).join(' ');
// TODO(crisbeto): this should add the units after #9609 is merged.
var translationValue = angular.isFunction(translation) ?
translation(this) : translation;
return translateFn + '(' + translationValue + ')';
}, this).join(' ');
};


Expand Down
56 changes: 56 additions & 0 deletions src/components/panel/panel.spec.js
Expand Up @@ -1372,6 +1372,34 @@ describe('$mdPanel', function() {
.toBeApproximately(parseInt(left) + parseInt(offset));
});

it('horizontally with a function', function() {
var left = '50px';
var offset = '-15px';
var obj = {
getOffsetX: function() {
return offset;
}
};

spyOn(obj, 'getOffsetX').and.callThrough();

var position = mdPanelPosition
.absolute()
.left(left)
.withOffsetX(obj.getOffsetX);

config['position'] = position;

openPanel(config);

var panelRect = document.querySelector(PANEL_EL)
.getBoundingClientRect();

expect(obj.getOffsetX).toHaveBeenCalledWith(position);
expect(panelRect.left)
.toBeApproximately(parseInt(left) + parseInt(offset));
});

it('horizontally with centering', function() {
var offset = '15px';

Expand Down Expand Up @@ -1414,6 +1442,34 @@ describe('$mdPanel', function() {
.toBeApproximately(parseInt(top) + parseInt(offset));
});

it('vertically with a function', function() {
var top = '50px';
var offset = '-15px';
var obj = {
getOffsetY: function() {
return offset;
}
};

spyOn(obj, 'getOffsetY').and.callThrough();

var position = mdPanelPosition
.absolute()
.top(top)
.withOffsetY(obj.getOffsetY);

config['position'] = position;

openPanel(config);

var panelRect = document.querySelector(PANEL_EL)
.getBoundingClientRect();

expect(obj.getOffsetY).toHaveBeenCalledWith(position);
expect(panelRect.top)
.toBeApproximately(parseInt(top) + parseInt(offset));
});

it('vertically with centering', function() {
var offset = '15px';

Expand Down

0 comments on commit 0896ba3

Please sign in to comment.