2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-17 19:27:28 +00:00
|
|
|
import Helper from '@ember/component/helper';
|
2020-12-08 09:27:28 +00:00
|
|
|
|
|
|
|
export default class DomPosition extends Helper {
|
2021-11-24 14:53:12 +00:00
|
|
|
compute([target], { from, offset = false }) {
|
2022-09-15 08:43:17 +00:00
|
|
|
return (e) => {
|
2021-11-24 14:53:12 +00:00
|
|
|
if (typeof target === 'function') {
|
|
|
|
let rect;
|
|
|
|
let $el;
|
|
|
|
if (offset) {
|
|
|
|
$el = e.currentTarget;
|
|
|
|
rect = {
|
|
|
|
width: $el.offsetWidth,
|
|
|
|
left: $el.offsetLeft,
|
|
|
|
height: $el.offsetHeight,
|
|
|
|
top: $el.offsetTop,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
$el = e.target;
|
|
|
|
rect = $el.getBoundingClientRect();
|
|
|
|
if (typeof from !== 'undefined') {
|
|
|
|
const fromRect = from.getBoundingClientRect();
|
|
|
|
rect.x = rect.x - fromRect.x;
|
|
|
|
rect.y = rect.y - fromRect.y;
|
|
|
|
}
|
2020-12-11 09:38:33 +00:00
|
|
|
}
|
|
|
|
return target(rect);
|
2022-03-15 12:54:56 +00:00
|
|
|
} else {
|
|
|
|
// if we are using this as part of an on-resize
|
|
|
|
// provide and easy way to map coords to CSS props
|
|
|
|
const $el = e.target;
|
|
|
|
const rect = $el.getBoundingClientRect();
|
2022-09-15 08:43:17 +00:00
|
|
|
target.forEach(([prop, value]) => {
|
|
|
|
$el.style[value] = `${rect[prop]}px`;
|
|
|
|
});
|
2021-11-24 14:53:12 +00:00
|
|
|
}
|
|
|
|
};
|
2020-12-08 09:27:28 +00:00
|
|
|
}
|
|
|
|
}
|