{"version":3,"sources":["popup/calc-drawing-params.ts"],"names":[],"mappings":";AA8DA;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,KAAA;;;;;EA+BzC;AAyCD;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,KAAA;;;;;EAM/C;AAwHD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,KAAA,EAAE,gBAAgB,KAAA,EAAE,sBAAsB,KAAA;;;;;;EAuFpF","file":"calc-drawing-params.d.ts","sourcesContent":["/* This Source Code Form is subject to the terms of the Mozilla Public\n * License, v. 2.0. If a copy of the MPL was not distributed with this\n * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n\n// @ts-nocheck\n\nimport { POPUP_MAIN_OFFSET } from '../vars';\n\nconst VIEWPORT_ACCURACY_FACTOR = 0.99;\nconst VIEWPORT_PADDING = 20;\n\nconst DEFAULT_DIRECTIONS = [\n    'bottom-left', 'bottom-center', 'bottom-right',\n    'top-left', 'top-center', 'top-right',\n    'right-top', 'right-center', 'right-bottom',\n    'left-top', 'left-center', 'left-bottom',\n];\n\n/**\n * @typedef {Object} DrawingParams\n * @property {String} direction Направление отрисовки\n * @property {Number} left Положение по горизонтали\n * @property {Number} top Положение по вертикали\n */\n\n/**\n * @typedef {Object} PopupDimension\n * @property {Number} width Ширина попапа\n * @property {Number} height Высота попапа\n * @property {Number} area Площадь попапа\n */\n\n/**\n * @typedef {Object} Rect\n * @property {Number} top\n * @property {Number} left\n * @property {Number} [bottom]\n * @property {Number} [right]\n * @property {Number} [width]\n * @property {Number} [height]\n */\n\n/**\n * @param {String} direction Направление отрисовки\n * @returns {String|Boolean}\n */\nfunction getMainDirection(direction) {\n    const deliveryPosition = direction.indexOf('-');\n\n    return (deliveryPosition !== -1) && direction.substr(0, deliveryPosition);\n}\n\n/**\n * @param {String} direction Направление отрисовки\n * @returns {String|Boolean}\n */\nfunction getSecondaryDirection(direction) {\n    const deliveryPosition = direction.indexOf('-');\n\n    return (deliveryPosition !== -1) && direction.substr(deliveryPosition + 1);\n}\n\n/**\n * Высчитывает координаты блока, который является таргетом поп-апа.\n *\n * @param {PopupHash} popup Набор параметров попапа\n * @returns {Rect}\n */\nexport function calcTargetDimensions(popup) {\n    if (!popup.isTargetAnchor && popup.targetPosition) {\n        return {\n            left: popup.targetPosition.left,\n            top: popup.targetPosition.top,\n            width: 0,\n            height: 0,\n        };\n    }\n\n    const anchor = popup.targetAnchor;\n    const anchorRect = anchor.getBoundingClientRect();\n    const { fitContainer } = popup;\n\n    if (fitContainer) {\n        const fitContainerRect = fitContainer.getBoundingClientRect();\n\n        return {\n            left: (anchorRect.left - fitContainerRect.left) + fitContainer.scrollLeft,\n            top: (anchorRect.top - fitContainerRect.top) + fitContainer.scrollTop,\n            width: anchorRect.width,\n            height: anchorRect.height,\n        };\n    }\n\n    return {\n        left: anchorRect.left + window.pageXOffset,\n        top: anchorRect.top + window.pageYOffset,\n        width: anchorRect.width,\n        height: anchorRect.height,\n    };\n}\n\n/**\n * Рассчитывает размеры и координаты расположения viewport.\n *\n * @returns {Rect}\n */\nfunction calcViewportDimensions() {\n    const winTop = window.pageYOffset;\n    const winLeft = window.pageXOffset;\n    const winWidth = window.innerWidth;\n    const winHeight = window.innerHeight;\n\n    return {\n        top: winTop,\n        left: winLeft,\n        bottom: winTop + winHeight,\n        right: winLeft + winWidth,\n    };\n}\n\n/**\n * Высчитывает размеры и координаты расположения контейнера, в котором рендерится Popup.\n *\n * @param {HTMLElement} container контейнер, в котором будет позиционирован Popup\n * @returns {Rect}\n */\nfunction calcContainerDimensions(container) {\n    const containerTop = container.scrollTop;\n    const containerLeft = container.scrollLeft;\n    const containerHeight = container.offsetHeight;\n    const containerWidth = container.offsetWidth;\n\n    return {\n        top: containerTop,\n        left: containerLeft,\n        bottom: containerTop + containerHeight,\n        right: containerLeft + containerWidth,\n    };\n}\n\n/**\n * Определяет контейнер, в котором рендерится Popup и возвращает его размеры и координаты.\n *\n * @param {PopupHash} popup Набор параметров попапа\n * @returns {Rect}\n */\nexport function calcFitContainerDimensions(popup) {\n    const { fitContainer } = popup;\n\n    return fitContainer\n        ? calcContainerDimensions(fitContainer)\n        : calcViewportDimensions();\n}\n\n/**\n * Рассчитывает пересечения Popup и контейнера, в котором он рендерится.\n *\n * @param {Point} position Позиция\n * @param {Rect} fitContainerDimensions Размеры контейнера, в который будет\n * рендерится Popup\n * @param {PopupDimension} popupDimensions Размеры попапа\n * @param {PopupHash} popupOffsetFitContainer Минимально допустимое смещение в\n * пикселях всплывающего окна от края его контейнера\n * @returns {Number}\n */\nfunction calcFitContainerFactor(\n    position,\n    fitContainerDimensions,\n    popupDimensions,\n    popupOffsetFitContainer,\n) {\n    const intersectionLeft = Math.max(\n        position.left,\n        fitContainerDimensions.left + popupOffsetFitContainer,\n    );\n\n    const intersectionRight = Math.min(\n        position.left + popupDimensions.width,\n        fitContainerDimensions.right - popupOffsetFitContainer,\n    );\n\n    const intersectionTop = Math.max(\n        position.top,\n        fitContainerDimensions.top + popupOffsetFitContainer,\n    );\n\n    const intersectionBottom = Math.min(\n        position.top + popupDimensions.height,\n        fitContainerDimensions.bottom - popupOffsetFitContainer,\n    );\n\n    if ((intersectionLeft < intersectionRight) && (intersectionTop < intersectionBottom)) {\n        return (\n            (intersectionRight - intersectionLeft) * (intersectionBottom - intersectionTop)\n        ) / popupDimensions.area;\n    }\n\n    return 0;\n}\n\n/**\n * Возвращает значения для позиционирования Popup в конкретной позиции.\n *\n * @param {String} direction Направление отрисовки\n * @param {Rect} targetDimensions Область отрисовки якорного элемента\n * @param {PopupDimension} popupDimensions Размеры попапа\n * @param {PopupHash} popup Набор параметров попапа\n * @returns {Point}\n */\nfunction calcPos(direction, targetDimensions, popupDimensions, popup) {\n    const result = {};\n    const mainOffset = popup.offset.main || (popup.isHaveTooltip && POPUP_MAIN_OFFSET) || 0;\n    const secondaryOffset = popup.offset.second;\n\n    const mainDirection = getMainDirection(direction);\n    const secondaryDirection = getSecondaryDirection(direction);\n\n    switch (mainDirection) {\n    case 'bottom':\n        result.top = targetDimensions.top + targetDimensions.height + mainOffset;\n        break;\n    case 'top':\n        result.top = targetDimensions.top - popupDimensions.height - mainOffset;\n        break;\n    case 'left':\n        result.left = targetDimensions.left - popupDimensions.width - mainOffset;\n        break;\n    case 'right':\n        result.left = targetDimensions.left + targetDimensions.width + mainOffset;\n        break;\n    default:\n        break;\n    }\n\n    switch (secondaryDirection) {\n    case 'right':\n        result.left = (targetDimensions.left + targetDimensions.width) - popupDimensions.width - secondaryOffset;\n        break;\n    case 'left':\n        result.left = targetDimensions.left + secondaryOffset;\n        break;\n    case 'bottom':\n        result.top = (targetDimensions.top + targetDimensions.height) - popupDimensions.height - secondaryOffset;\n        break;\n    case 'top':\n        result.top = targetDimensions.top + secondaryOffset;\n        break;\n    case 'center':\n        switch (mainDirection) {\n        case 'top':\n        case 'bottom':\n            result.left = targetDimensions.left + (\n                (targetDimensions.width / 2) - (popupDimensions.width / 2)\n            );\n            break;\n        case 'left':\n        case 'right':\n            result.top = targetDimensions.top + (\n                (targetDimensions.height / 2) - (popupDimensions.height / 2)\n            );\n            break;\n        default:\n            break;\n        }\n        break;\n    default:\n        break;\n    }\n\n    return result;\n}\n\n/**\n * Рассчитывает наилучшие значения для позиционирования Popup.\n *\n * @param {PopupHash} popup Набор параметров попапа\n * @param {Rect} targetDimensions Область отрисовки якорного элемента\n * @param {Rect} fitContainerDimensions Размеры вьюпорта\n * @returns {DrawingParams}\n */\nexport function calcBestDrawingParams(popup, targetDimensions, fitContainerDimensions) {\n    const popupDimensions = {\n        width: popup.width,\n        height: popup.height,\n        area: popup.width * popup.height,\n    };\n    const directions = popup.directions\n        ? popup.directions\n        : (popup.isHeightAvailable && ['top-left']) || DEFAULT_DIRECTIONS;\n    let i = 0;\n    let direction = '';\n    let heightToEdge = 0;\n    let height = 0;\n    let overflow = false;\n    let newPopupDimensions = {};\n    let position = {};\n    let fitContainerFactor = 0;\n    let bestDirection = '';\n    let bestPosition = {};\n    let bestHeight = 0;\n    let bestFitContainerFactor = 0;\n\n    while (directions[i]) {\n        direction = directions[i];\n        i += 1;\n\n        if (getMainDirection(direction) === 'top') {\n            heightToEdge = targetDimensions.top - fitContainerDimensions.top;\n        } else if (getMainDirection(direction) === 'bottom') {\n            heightToEdge = fitContainerDimensions.bottom - (targetDimensions.top + targetDimensions.height);\n        }\n\n        if (popup.isHeightAdaptive) {\n            if (popup.contentHeight > heightToEdge) {\n                height = heightToEdge;\n                overflow = true;\n            } else {\n                height = popup.contentHeight;\n                overflow = false;\n            }\n\n            newPopupDimensions = {\n                width: popup.width,\n                height,\n                area: popup.width * height,\n            };\n        }\n\n        position = calcPos(direction, targetDimensions, height ? newPopupDimensions : popupDimensions, popup);\n        fitContainerFactor = calcFitContainerFactor(\n            position,\n            fitContainerDimensions,\n            popupDimensions,\n            popup.offset.fitContainer,\n        );\n\n        if (i === 1\n            || fitContainerFactor > bestFitContainerFactor\n            || (!bestFitContainerFactor && popup.bestDirection)\n        ) {\n            bestFitContainerFactor = fitContainerFactor;\n            bestDirection = direction;\n            bestPosition = position;\n            bestHeight = height;\n        }\n\n        if (bestFitContainerFactor > VIEWPORT_ACCURACY_FACTOR) {\n            break;\n        }\n    }\n\n    if (popup.isHeightAdaptive && popup.contentHeight > heightToEdge) {\n        if (getMainDirection(bestDirection) === 'top') {\n            bestPosition.top += VIEWPORT_PADDING;\n            bestHeight -= VIEWPORT_PADDING;\n        } else if (getMainDirection(bestDirection) === 'bottom') {\n            bestHeight -= VIEWPORT_PADDING;\n        }\n    }\n\n    return {\n        direction: bestDirection,\n        left: bestPosition.left,\n        top: bestPosition.top,\n        height: bestHeight || 'auto',\n        overflow,\n    };\n}\n"]}