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

Commit 761493d

Browse files
crisbetohansl
authored andcommitted
fix(panel): take offsets into account when checking if element is on screen (#9662)
Accounts for the computed offsets of the panel element when determining whether it is on screen. Fixes #9628.
1 parent 62df3c8 commit 761493d

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/components/panel/panel.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,11 +2471,15 @@ MdPanelPosition.prototype.getTransform = function() {
24712471
MdPanelPosition.prototype._isOnscreen = function(panelEl) {
24722472
// this works because we always use fixed positioning for the panel,
24732473
// which is relative to the viewport.
2474-
// TODO(gmoothart): take into account _translateX and _translateY to the
2475-
// extent feasible.
2476-
24772474
var left = parseInt(this.getLeft());
24782475
var top = parseInt(this.getTop());
2476+
2477+
if (this._translateX.length || this._translateY.length) {
2478+
var offsets = getComputedTranslations(panelEl);
2479+
left += offsets.x;
2480+
top += offsets.y;
2481+
}
2482+
24792483
var right = left + panelEl[0].offsetWidth;
24802484
var bottom = top + panelEl[0].offsetHeight;
24812485

@@ -2982,3 +2986,30 @@ function getElement(el) {
29822986
document.querySelector(el) : el;
29832987
return angular.element(queryResult);
29842988
}
2989+
2990+
/**
2991+
* Gets the computed values for an element's translateX and translateY in px.
2992+
* @param {!angular.JQLite|!Element} el
2993+
* @return {{x: number, y: number}}
2994+
*/
2995+
function getComputedTranslations(el) {
2996+
// The transform being returned by `getComputedStyle` is in the format:
2997+
// `matrix(a, b, c, d, translateX, translateY)` if defined and `none`
2998+
// if the element doesn't have a transform.
2999+
var transform = getComputedStyle(el[0] || el).transform;
3000+
var openIndex = transform.indexOf('(');
3001+
var closeIndex = transform.lastIndexOf(')');
3002+
var output = { x: 0, y: 0 };
3003+
3004+
if (openIndex > -1 && closeIndex > -1) {
3005+
var parsedValues = transform
3006+
.substring(openIndex + 1, closeIndex)
3007+
.split(', ')
3008+
.slice(-2);
3009+
3010+
output.x = parseInt(parsedValues[0]);
3011+
output.y = parseInt(parsedValues[1]);
3012+
}
3013+
3014+
return output;
3015+
}

src/components/panel/panel.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,40 @@ describe('$mdPanel', function() {
20252025
});
20262026
});
20272027

2028+
it('takes the x offset into account', function() {
2029+
var position = mdPanelPosition
2030+
.relativeTo(myButton)
2031+
.withOffsetX(window.innerWidth + 'px')
2032+
.addPanelPosition(xPosition.ALIGN_START, yPosition.ALIGN_TOPS)
2033+
.addPanelPosition(xPosition.ALIGN_END, yPosition.ALIGN_TOPS);
2034+
2035+
config['position'] = position;
2036+
2037+
openPanel(config);
2038+
2039+
expect(position.getActualPosition()).toEqual({
2040+
x: xPosition.ALIGN_END,
2041+
y: yPosition.ALIGN_TOPS
2042+
});
2043+
});
2044+
2045+
it('takes the y offset into account', function() {
2046+
var position = mdPanelPosition
2047+
.relativeTo(myButton)
2048+
.withOffsetY(window.innerHeight + 'px')
2049+
.addPanelPosition(xPosition.ALIGN_START, yPosition.ALIGN_BOTTOMS)
2050+
.addPanelPosition(xPosition.ALIGN_START, yPosition.ALIGN_TOPS);
2051+
2052+
config['position'] = position;
2053+
2054+
openPanel(config);
2055+
2056+
expect(position.getActualPosition()).toEqual({
2057+
x: xPosition.ALIGN_START,
2058+
y: yPosition.ALIGN_TOPS
2059+
});
2060+
});
2061+
20282062
it('should choose last position if none are on-screen', function() {
20292063
var position = mdPanelPosition
20302064
.relativeTo(myButton)

0 commit comments

Comments
 (0)