@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
50 lines (38 loc) • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _consts = require("../consts");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var isInside = function isInside(p, canBe) {
if (p === _consts.POSITIONS.TOP && canBe[p]) {
return _consts.POSITIONS.TOP;
}
if (p === _consts.POSITIONS.BOTTOM && canBe[p]) {
return _consts.POSITIONS.BOTTOM;
}
return false;
};
var calculateVerticalPosition = function calculateVerticalPosition(desiredPositions, pos) {
var _canBeInWindow, _canBeInDocument;
var canBeInWindow = (_canBeInWindow = {}, _defineProperty(_canBeInWindow, _consts.POSITIONS.TOP, pos.containerTop - pos.popoverHeight > pos.windowScrollTop), _defineProperty(_canBeInWindow, _consts.POSITIONS.BOTTOM, pos.containerTop - pos.windowScrollTop + pos.containerHeight + pos.popoverHeight < pos.windowHeight), _canBeInWindow);
var canBeInDocument = (_canBeInDocument = {}, _defineProperty(_canBeInDocument, _consts.POSITIONS.TOP, pos.containerTop - pos.popoverHeight >= 0), _defineProperty(_canBeInDocument, _consts.POSITIONS.BOTTOM, pos.containerTop + pos.containerHeight + pos.popoverHeight < pos.documentHeight), _canBeInDocument);
var possibleWindowPositions = desiredPositions.map(function (p) {
return isInside(p, canBeInWindow);
}).filter(function (p) {
return typeof p === "string";
});
var possibleDocumentPositions = desiredPositions.map(function (p) {
return isInside(p, canBeInDocument);
}).filter(function (p) {
return typeof p === "string";
}); // ordering in POSITIONS const is important
var posPosition = possibleWindowPositions[0] || possibleDocumentPositions[0];
if (typeof posPosition === "string") {
return posPosition;
}
return _consts.POSITIONS.BOTTOM;
};
var _default = calculateVerticalPosition;
exports.default = _default;