Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- };
Add Comment
Please, Sign In to add comment