devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
232 lines (231 loc) • 8.74 kB
JavaScript
/**
* DevExtreme (esm/__internal/ui/popup/m_popup_drag.js)
* Version: 26.1.3
* Build date: Wed Jun 10 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import {
locate,
move
} from "../../../common/core/animation/translator";
import eventsEngine from "../../../common/core/events/core/events_engine";
import {
end as dragEndEvent,
move as dragMoveEvent,
start as dragStartEvent
} from "../../../common/core/events/drag";
import {
addNamespace
} from "../../../common/core/events/utils";
import domAdapter from "../../../core/dom_adapter";
import {
fitIntoRange
} from "../../../core/utils/math";
import {
getOffset,
getOuterHeight,
getOuterWidth
} from "../../../core/utils/size";
import {
isWindow
} from "../../../core/utils/type";
const KEYBOARD_DRAG_STEP = 5;
export default class PopupDrag {
constructor(configuration) {
this._namespace = "overlayDrag";
this.init(configuration)
}
init(configuration) {
const {
positionController: positionController,
draggableElement: draggableElement,
handle: handle,
dragEnabled: dragEnabled
} = configuration;
this._positionController = positionController;
this._draggableElement = draggableElement;
this._handle = handle;
this._dragEnabled = dragEnabled;
this.unsubscribe();
if (!dragEnabled) {
return
}
this.subscribe()
}
moveDown(e) {
this._moveTo(5, 0, e)
}
moveUp(e) {
this._moveTo(-5, 0, e)
}
moveLeft(e) {
this._moveTo(0, -5, e)
}
moveRight(e) {
this._moveTo(0, 5, e)
}
subscribe() {
const eventNames = this._getEventNames();
eventsEngine.on(this._handle, eventNames.startEventName, e => {
this._dragStartHandler(e)
});
eventsEngine.on(this._handle, eventNames.updateEventName, e => {
this._dragUpdateHandler(e)
});
eventsEngine.on(this._handle, eventNames.endEventName, e => {
this._dragEndHandler(e)
})
}
unsubscribe() {
const eventNames = this._getEventNames();
eventsEngine.off(this._handle, eventNames.startEventName);
eventsEngine.off(this._handle, eventNames.updateEventName);
eventsEngine.off(this._handle, eventNames.endEventName)
}
_getEventNames() {
const startEventName = addNamespace(dragStartEvent, this._namespace);
const updateEventName = addNamespace(dragMoveEvent, this._namespace);
const endEventName = addNamespace(dragEndEvent, this._namespace);
return {
startEventName: startEventName,
updateEventName: updateEventName,
endEventName: endEventName
}
}
_dragStartHandler(e) {
const allowedOffsets = this._getAllowedOffsets();
this._prevOffset = {
x: 0,
y: 0
};
e.targetElements = [];
e.maxTopOffset = allowedOffsets.top;
e.maxBottomOffset = allowedOffsets.bottom;
e.maxLeftOffset = allowedOffsets.left;
e.maxRightOffset = allowedOffsets.right
}
_dragUpdateHandler(e) {
var _this$_prevOffset, _this$_prevOffset2;
const targetOffset = {
top: e.offset.y - ((null === (_this$_prevOffset = this._prevOffset) || void 0 === _this$_prevOffset ? void 0 : _this$_prevOffset.y) ?? 0),
left: e.offset.x - ((null === (_this$_prevOffset2 = this._prevOffset) || void 0 === _this$_prevOffset2 ? void 0 : _this$_prevOffset2.x) ?? 0)
};
this._moveByOffset(targetOffset);
this._prevOffset = e.offset
}
_dragEndHandler(event) {
var _this$_positionContro, _this$_positionContro2;
null === (_this$_positionContro = this._positionController) || void 0 === _this$_positionContro || _this$_positionContro.dragHandled();
null === (_this$_positionContro2 = this._positionController) || void 0 === _this$_positionContro2 || _this$_positionContro2.detectVisualPositionChange(event)
}
_moveTo(top, left, e) {
if (!this._dragEnabled) {
return
}
e.preventDefault();
e.stopPropagation();
const offset = this._fitOffsetIntoAllowedRange(top, left);
this._moveByOffset(offset);
this._dragEndHandler(e)
}
_fitOffsetIntoAllowedRange(top, left) {
const allowedOffsets = this._getAllowedOffsets();
return {
top: fitIntoRange(top, -allowedOffsets.top, allowedOffsets.bottom),
left: fitIntoRange(left, -allowedOffsets.left, allowedOffsets.right)
}
}
_getContainerDimensions() {
var _this$_positionContro3;
const document = domAdapter.getDocument();
const container = null === (_this$_positionContro3 = this._positionController) || void 0 === _this$_positionContro3 || null === (_this$_positionContro3 = _this$_positionContro3.$dragResizeContainer) || void 0 === _this$_positionContro3 ? void 0 : _this$_positionContro3.get(0);
let containerWidth = getOuterWidth(container);
let containerHeight = getOuterHeight(container);
if (isWindow(container)) {
containerHeight = Math.max(document.body.clientHeight, containerHeight);
containerWidth = Math.max(document.body.clientWidth, containerWidth)
}
return {
width: containerWidth,
height: containerHeight
}
}
_getContainerPosition() {
var _this$_positionContro4;
const container = null === (_this$_positionContro4 = this._positionController) || void 0 === _this$_positionContro4 || null === (_this$_positionContro4 = _this$_positionContro4.$dragResizeContainer) || void 0 === _this$_positionContro4 ? void 0 : _this$_positionContro4.get(0);
return isWindow(container) ? {
top: 0,
left: 0
} : getOffset(container)
}
_getElementPosition() {
return getOffset(this._draggableElement)
}
_getInnerDelta() {
const containerDimensions = this._getContainerDimensions();
const elementDimensions = this._getElementDimensions();
return {
x: containerDimensions.width - (elementDimensions.width ?? 0),
y: containerDimensions.height - (elementDimensions.height ?? 0)
}
}
_getOuterDelta() {
const {
width: width = 0,
height: height = 0
} = this._getElementDimensions();
const {
outsideDragFactor: outsideDragFactor = 0
} = this._positionController ?? {};
return {
x: width * outsideDragFactor,
y: height * outsideDragFactor
}
}
_getFullDelta() {
const fullDelta = this._getInnerDelta();
const outerDelta = this._getOuterDelta();
return {
x: fullDelta.x + outerDelta.x,
y: fullDelta.y + outerDelta.y
}
}
_getElementDimensions() {
var _this$_draggableEleme, _this$_draggableEleme2;
return {
width: null === (_this$_draggableEleme = this._draggableElement) || void 0 === _this$_draggableEleme ? void 0 : _this$_draggableEleme.offsetWidth,
height: null === (_this$_draggableEleme2 = this._draggableElement) || void 0 === _this$_draggableEleme2 ? void 0 : _this$_draggableEleme2.offsetHeight
}
}
_getAllowedOffsets() {
const fullDelta = this._getFullDelta();
const isDragAllowed = fullDelta.y >= 0 && fullDelta.x >= 0;
if (!isDragAllowed) {
return {
top: 0,
bottom: 0,
left: 0,
right: 0
}
}
const elementPosition = this._getElementPosition();
const containerPosition = this._getContainerPosition();
const outerDelta = this._getOuterDelta();
return {
top: elementPosition.top - containerPosition.top + outerDelta.y,
bottom: -elementPosition.top + containerPosition.top + fullDelta.y,
left: elementPosition.left - containerPosition.left + outerDelta.x,
right: -elementPosition.left + containerPosition.left + fullDelta.x
}
}
_moveByOffset(offset) {
const currentPosition = locate(this._draggableElement);
const newPosition = {
left: currentPosition.left + offset.left,
top: currentPosition.top + offset.top
};
move(this._draggableElement, newPosition)
}
}