Created
April 16, 2018 07:33
-
-
Save 197291/846eb7b3d247c949a7b88bbf746411ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getCoords(elem: any) { | |
var box = elem.getBoundingClientRect(); | |
return { | |
top: box.top + window.pageYOffset, | |
left: box.left + window.pageXOffset, | |
right: box.right + window.pageXOffset, | |
bottom: box.bottom + window.pageYOffset, | |
width: box.width, | |
height: box.height | |
}; | |
} | |
export function getWidthHeightDocs() { | |
return { | |
height: document.documentElement.clientHeight, | |
width: document.documentElement.clientWidth | |
}; | |
} | |
export const createPopoverCoordsFromEvent = (target, popupParams: PopupParams, targetCoords?): Coords => { | |
targetCoords = targetCoords || getCoords(target); | |
const coords = Object.assign({}, targetCoords, { | |
top: targetCoords.top + targetCoords.height / 2 | |
}); | |
const documentSizes = getWidthHeightDocs(); | |
const restOfHeight = documentSizes.height - coords.top; | |
const restOfWidth = documentSizes.width - coords.right; | |
const { width, height, topOffset, bottomOffset } = popupParams; | |
const leftOffset = 35; | |
const rightOffset = 30; | |
let result: Coords = {}; | |
if (restOfHeight > height + topOffset) { | |
result = Object.assign({}, result, { | |
top: Math.floor(coords.top) - (topOffset * -1) + 'px', | |
trgBottom: false | |
}); | |
} else { | |
let botOffset = restOfHeight < bottomOffset * -1 ? restOfHeight : bottomOffset * -1; | |
let topPosition = Math.floor(coords.top) - height + botOffset; | |
topPosition = topPosition < 0 ? 0 : topPosition; | |
result = Object.assign({}, result, { | |
top: topPosition + 'px', | |
trgBottom: true | |
}); | |
} | |
if (restOfWidth > width + leftOffset) { | |
result = Object.assign({}, result, { | |
left: Math.floor(coords.right) + leftOffset + 'px', | |
trgRight: false | |
}); | |
} else { | |
result = Object.assign({}, result, { | |
left: Math.floor(coords.left) - width - rightOffset + 'px', | |
trgRight: true | |
}); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment