@mapbox/react-map-gl
Version:
A React wrapper for MapboxGL-js and overlay API.
124 lines (112 loc) • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getDynamicPosition = getDynamicPosition;
exports.ANCHOR_POSITION = void 0;
var ANCHOR_POSITION = {
top: {
x: 0.5,
y: 0
},
'top-left': {
x: 0,
y: 0
},
'top-right': {
x: 1,
y: 0
},
bottom: {
x: 0.5,
y: 1
},
'bottom-left': {
x: 0,
y: 1
},
'bottom-right': {
x: 1,
y: 1
},
left: {
x: 0,
y: 0.5
},
right: {
x: 1,
y: 0.5
}
};
exports.ANCHOR_POSITION = ANCHOR_POSITION;
var ANCHOR_TYPES = Object.keys(ANCHOR_POSITION);
/**
* Calculate the dynamic position for a popup to fit in a container.
* @param {Number} x - x position of the anchor on screen
* @param {Number} y - y position of the anchor on screen
* @param {Number} width - width of the container
* @param {Number} height - height of the container
* @param {Number} padding - extra space from the edge in pixels
* @param {Number} selfWidth - width of the popup
* @param {Number} selfHeight - height of the popup
* @param {String} anchor - type of the anchor, one of 'top', 'bottom',
'left', 'right', 'top-left', 'top-right', 'bottom-left' , and 'bottom-right'
* @returns {String} position - one of 'top', 'bottom',
'left', 'right', 'top-left', 'top-right', 'bottom-left' , and 'bottom-right'
*/
function getDynamicPosition(_ref) {
var x = _ref.x,
y = _ref.y,
width = _ref.width,
height = _ref.height,
selfWidth = _ref.selfWidth,
selfHeight = _ref.selfHeight,
anchor = _ref.anchor,
_ref$padding = _ref.padding,
padding = _ref$padding === void 0 ? 0 : _ref$padding;
var _ANCHOR_POSITION$anch = ANCHOR_POSITION[anchor],
anchorX = _ANCHOR_POSITION$anch.x,
anchorY = _ANCHOR_POSITION$anch.y; // anchorY: top - 0, center - 0.5, bottom - 1
var top = y - anchorY * selfHeight;
var bottom = top + selfHeight; // If needed, adjust anchorY at 0.5 step between [0, 1]
var yStep = 0.5;
if (top < padding) {
// Top edge is outside, try move down
while (top < padding && anchorY >= yStep) {
anchorY -= yStep;
top += yStep * selfHeight;
}
} else if (bottom > height - padding) {
// bottom edge is outside, try move up
while (bottom > height - padding && anchorY <= 1 - yStep) {
anchorY += yStep;
bottom -= yStep * selfHeight;
}
} // anchorX: left - 0, center - 0.5, right - 1
var left = x - anchorX * selfWidth;
var right = left + selfWidth; // If needed, adjust anchorX at 0.5 step between [0, 1]
var xStep = 0.5;
if (anchorY === 0.5) {
// If y is centered, then x cannot also be centered
anchorX = Math.floor(anchorX);
xStep = 1;
}
if (left < padding) {
// Left edge is outside, try move right
while (left < padding && anchorX >= xStep) {
anchorX -= xStep;
left += xStep * selfWidth;
}
} else if (right > width - padding) {
// Right edge is outside, try move left
while (right > width - padding && anchorX <= 1 - xStep) {
anchorX += xStep;
right -= xStep * selfWidth;
}
} // Find the name of the new anchor position
return ANCHOR_TYPES.find(function (positionType) {
var anchorPosition = ANCHOR_POSITION[positionType];
return anchorPosition.x === anchorX && anchorPosition.y === anchorY;
}) || anchor;
}
//# sourceMappingURL=dynamic-position.js.map