@descope/sdk-mixins
Version:
Descope JavaScript SDK mixins
77 lines (74 loc) • 3.08 kB
JavaScript
;
const limitCoordinateToScreenBoundaries = (ele, x, y, boundaries = {}) => {
var _a, _b, _c, _d;
return [
Math.min(Math.max(x, (boundaries.left === 'all' ? ele.offsetWidth : (_a = boundaries.left) !== null && _a !== void 0 ? _a : 0) -
ele.offsetWidth), window.innerWidth -
(boundaries.right === 'all' ? ele.offsetWidth : (_b = boundaries.right) !== null && _b !== void 0 ? _b : 0)),
Math.min(Math.max(y, (boundaries.top === 'all' ? ele.offsetHeight : (_c = boundaries.top) !== null && _c !== void 0 ? _c : 0) -
ele.offsetHeight), window.innerHeight -
(boundaries.bottom === 'all' ? ele.offsetHeight : (_d = boundaries.bottom) !== null && _d !== void 0 ? _d : 0)),
];
};
const dragElement = (ele, triggerEle, keepVisible) => {
let deltaX = 0;
let deltaY = 0;
let currentX = 0;
let currentY = 0;
function elementDrag(e) {
e.preventDefault();
// calculate the new cursor position:
deltaX = currentX - e.clientX;
deltaY = currentY - e.clientY;
currentX = e.clientX;
currentY = e.clientY;
// set the element's new position:
const [left, top] = limitCoordinateToScreenBoundaries(ele, ele.offsetLeft - deltaX, ele.offsetTop - deltaY, keepVisible);
// eslint-disable-next-line no-param-reassign
ele.style.top = `${top}px`;
// eslint-disable-next-line no-param-reassign
ele.style.left = `${left}px`;
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
function dragMouseDown(e) {
e.preventDefault();
// get the mouse cursor position at startup:
currentX = e.clientX;
currentY = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
if (triggerEle) {
// if provided, the triggerEle is where you move the div from
// eslint-disable-next-line no-param-reassign
triggerEle.onmousedown = dragMouseDown;
}
else {
// otherwise, move the DIV from anywhere inside the DIV:
// eslint-disable-next-line no-param-reassign
ele.onmousedown = dragMouseDown;
}
};
const addOnResize = (ele) => {
// eslint-disable-next-line no-param-reassign
ele.onmousemove = (e) => {
var _a;
// eslint-disable-next-line prefer-destructuring
const target = e.target;
if ((target.w && target.w !== target.offsetWidth) ||
(target.h && target.h !== target.offsetHeight)) {
(_a = ele.onresize) === null || _a === void 0 ? void 0 : _a.call(ele, e);
}
target.w = target.offsetWidth;
target.h = target.offsetHeight;
};
};
exports.addOnResize = addOnResize;
exports.dragElement = dragElement;
exports.limitCoordinateToScreenBoundaries = limitCoordinateToScreenBoundaries;
//# sourceMappingURL=helpers.js.map