import { ThyPlacement , ConnectedPositionOffset } from './interface' ; import { POSITION_MAP } from './overlay-position-map' ; import { ConnectionPositionPair } from '@angular/cdk/overlay' ; const connectionFallbackPositionsMap = { top : [ 'topLeft' , 'topRight' , 'bottom' , 'bottomLeft' , 'bottomRight' ] , topLeft : [ 'top' , 'topRight' , 'bottomLeft' , 'bottom' , 'bottomRight' ] , topRight : [ 'top' , 'topLeft' , 'bottomRight' , 'bottom' , 'bottomLeft' ] , bottom : [ 'bottomLeft' , 'bottomRight' , 'top' , 'topLeft' , 'topRight' ] , bottomLeft : [ 'bottom' , 'bottomRight' , 'topLeft' , 'top' , 'topRight' ] , bottomRight : [ 'bottom' , 'bottomLeft' , 'topRight' , 'top' , 'topLeft' ] , left : [ 'leftTop' , 'leftBottom' , 'right' , 'rightTop' , 'rightBottom' ] , leftTop : [ 'left' , 'leftBottom' , 'rightTop' , 'right' , 'rightBottom' ] , leftBottom : [ 'left' , 'leftTop' , 'rightBottom' , 'right' , 'rightTop' ] , right : [ 'rightTop' , 'rightBottom' , 'left' , 'leftTop' , 'leftBottom' ] , rightTop : [ 'right' , 'rightBottom' , 'leftTop' , 'left' , 'leftBottom' ] , rightBottom : [ 'right' , 'rightTop' , 'leftBottom' , 'left' , 'leftTop' ] } ; export function buildConnectedPositionOffset ( placement , offset ) { const connectedPositionOffset = { } ; if ( placement . startsWith ( 'top' ) ) { connectedPositionOffset . offsetY = - offset ; } else if ( placement . startsWith ( 'bottom' ) ) { connectedPositionOffset . offsetY = offset ; } else if ( placement . startsWith ( 'left' ) ) { connectedPositionOffset . offsetX = - offset ; } else if ( placement . startsWith ( 'right' ) ) { connectedPositionOffset . offsetX = offset ; } else { } return connectedPositionOffset ; } export function buildConnectedPositionPair ( placement , offset ? , panelClassPrefix ? ) { const position = { ... POSITION_MAP [ placement ] , ... ( offset ? buildConnectedPositionOffset ( placement , offset ) : null ) } ; if ( panelClassPrefix ) { position . panelClass = `${ panelClassPrefix }-${ placement }` ; } return position ; } export function getFallbackPlacements ( placement ) { return connectionFallbackPositionsMap [ placement ] || [ ] ; } example returns export function getFlexiblePositions ( placement , offset ? , panelClassPrefix ? ) { const fallbackPlacements = getFallbackPlacements ( placement ) ; return [ placement , ... fallbackPlacements ] . map ( placementName => { return buildConnectedPositionPair ( placementName , offset , panelClassPrefix ) ; } ) ; } export function getPlacementByPosition ( position ) { const keyList = [ 'originX' , 'originY' , 'overlayX' , 'overlayY' ] ; for ( const placement in POSITION_MAP ) { if ( keyList . every ( key => position [ key ] === POSITION_MAP [ placement ] [ key ] ) ) { return placement ; } } }