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U,IAAI,SAAS,KAAK,MAAM,IAAI,aAAa,CAAC;YACpD,CAAC,UAAU,IAAI,SAAS,KAAK,OAAO,IAAI,cAAc,CAAC;YACvD,CAAC,CAAC,UAAU,IAAI,SAAS,KAAK,MAAM,IAAI,YAAY,CAAC;YACrD,CAAC,CAAC,UAAU,IAAI,SAAS,KAAK,OAAO,IAAI,eAAe,CAAC,CAAC,CAAC;QAE/D,IAAI,WAAW,IAAI,mBAAmB,IAAI,gBAAgB,EAAE;YAC1D,IAAI,WAAW,IAAI,mBAAmB,EAAE;gBACtC,SAAS,GAAG,SAAS,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;aAClC;YAED,IAAI,gBAAgB,EAAE;gBACpB,SAAS,GAAG,oBAAoB,CAAC,SAAS,CAAC,CAAC;aAC7C;YAED,IAAI,CAAC,SAAS,GAAG,SAAS,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YAEhE,IAAI,CAAC,OAAO,CAAC,MAAM,mCACd,IAAI,CAAC,OAAO,CAAC,MAAM,GACnB,gBAAgB,CACjB,IAAI,CAAC,QAAQ,CAAC,MAAM,EACpB,IAAI,CAAC,OAAO,CAAC,IAAI,EACjB,IAAI,CAAC,SAAS,CACf,CACF,CAAC;SACH;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { Data } from '../models';\nimport {\n computeAutoPlacement,\n getBoundaries,\n getClientRect,\n getOppositeVariation,\n getTargetOffsets,\n isModifierEnabled\n} from '../utils';\n\nexport function flip(data: Data): Data {\n data.offsets.target = getClientRect(data.offsets.target);\n\n if (!isModifierEnabled(data.options, 'flip')) {\n\n data.offsets.target = {\n ...data.offsets.target,\n ...getTargetOffsets(\n data.instance.target,\n data.offsets.host,\n data.placement\n )\n };\n\n return data;\n }\n\n const boundaries = getBoundaries(\n data.instance.target,\n data.instance.host,\n 0, // padding\n 'viewport',\n false // positionFixed\n );\n\n let placement = data.placement.split(' ')[0];\n let variation = data.placement.split(' ')[1] || '';\n\n const offsetsHost = data.offsets.host;\n const target = data.instance.target;\n const host = data.instance.host;\n\n const adaptivePosition = computeAutoPlacement('auto', offsetsHost, target, host, data.options.allowedPositions);\n const flipOrder = [placement, adaptivePosition];\n\n flipOrder.forEach((step, index) => {\n if (placement !== step || flipOrder.length === index + 1) {\n return;\n }\n\n placement = data.placement.split(' ')[0];\n\n // using floor because the host offsets may contain decimals we are not going to consider here\n const overlapsRef =\n (placement === 'left' &&\n Math.floor(data.offsets.target.right ?? 0) > Math.floor(data.offsets.host.left ?? 0)) ||\n (placement === 'right' &&\n Math.floor(data.offsets.target.left ?? 0) < Math.floor(data.offsets.host.right ?? 0)) ||\n (placement === 'top' &&\n Math.floor(data.offsets.target.bottom ?? 0) > Math.floor(data.offsets.host.top ?? 0)) ||\n (placement === 'bottom' &&\n Math.floor(data.offsets.target.top ?? 0) < Math.floor(data.offsets.host.bottom ?? 0));\n\n const overflowsLeft = Math.floor(data.offsets.target.left ?? 0) < Math.floor(boundaries.left ?? 0);\n const overflowsRight = Math.floor(data.offsets.target.right ?? 0) > Math.floor(boundaries.right ?? 0);\n const overflowsTop = Math.floor(data.offsets.target.top ?? 0) < Math.floor(boundaries.top ?? 0);\n const overflowsBottom = Math.floor(data.offsets.target.bottom ?? 0) > Math.floor(boundaries.bottom ?? 0);\n\n const overflowsBoundaries =\n (placement === 'left' && overflowsLeft) ||\n (placement === 'right' && overflowsRight) ||\n (placement === 'top' && overflowsTop) ||\n (placement === 'bottom' && overflowsBottom);\n\n // flip the variation if required\n const isVertical = ['top', 'bottom'].indexOf(placement) !== -1;\n const flippedVariation =\n ((isVertical && variation === 'left' && overflowsLeft) ||\n (isVertical && variation === 'right' && overflowsRight) ||\n (!isVertical && variation === 'left' && overflowsTop) ||\n (!isVertical && variation === 'right' && overflowsBottom));\n\n if (overlapsRef || overflowsBoundaries || flippedVariation) {\n if (overlapsRef || overflowsBoundaries) {\n placement = flipOrder[index + 1];\n }\n\n if (flippedVariation) {\n variation = getOppositeVariation(variation);\n }\n\n data.placement = placement + (variation ? ` ${variation}` : '');\n\n data.offsets.target = {\n ...data.offsets.target,\n ...getTargetOffsets(\n data.instance.target,\n data.offsets.host,\n data.placement\n )\n };\n }\n });\n\n return data;\n}\n"]} |