@deephaven/golden-layout
Version:
A multi-screen javascript Layout manager
127 lines (125 loc) • 7.13 kB
JavaScript
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import $ from 'jquery';
import EventEmitter from "./EventEmitter.js";
class DragListener extends EventEmitter {
constructor(eElement, root) {
var _this$_eElement;
var destroyAfterMouseUp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
super();
_defineProperty(this, "_eElement", void 0);
_defineProperty(this, "_oDocument", void 0);
_defineProperty(this, "_eBody", void 0);
_defineProperty(this, "_root", void 0);
_defineProperty(this, "_destroyAfterMouseUp", void 0);
/**
* The delay after which to start the drag in milliseconds
*/
_defineProperty(this, "_nDelay", 400);
_defineProperty(this, "_timeout", void 0);
_defineProperty(this, "timeout", void 0);
/**
* The distance the mouse needs to be moved to qualify as a drag
*/
_defineProperty(this, "_nDistance", 10);
//TODO - works better with delay only
_defineProperty(this, "_nX", 0);
_defineProperty(this, "_nY", 0);
_defineProperty(this, "_nOriginalX", 0);
_defineProperty(this, "_nOriginalY", 0);
_defineProperty(this, "_bDragging", false);
this._eElement = eElement;
this._oDocument = $(document);
this._eBody = $(document.body);
this._root = root;
// used by drag sources, to destroy listener at the right time
this._destroyAfterMouseUp = destroyAfterMouseUp;
this.onMouseMove = this.onMouseMove.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);
this.onMouseDown = this.onMouseDown.bind(this);
this._startDrag = this._startDrag.bind(this);
// https://github.com/microsoft/TypeScript/issues/48546
(_this$_eElement = this._eElement) === null || _this$_eElement === void 0 || _this$_eElement.on('mousedown', this.onMouseDown);
}
destroy() {
var _this$_eElement2, _this$_oDocument;
(_this$_eElement2 = this._eElement) === null || _this$_eElement2 === void 0 || _this$_eElement2.unbind('mousedown', this.onMouseDown);
(_this$_oDocument = this._oDocument) === null || _this$_oDocument === void 0 || _this$_oDocument.unbind('mouseup', this.onMouseUp);
this._eElement = undefined;
this._oDocument = undefined;
this._eBody = undefined;
clearTimeout(this._timeout);
this._timeout = undefined;
}
onMouseDown(oEvent) {
oEvent.preventDefault();
if (oEvent.button === 0) {
var _coordinates$x, _coordinates$y, _this$_oDocument2, _this$_oDocument3;
var coordinates = this._getCoordinates(oEvent);
this._nOriginalX = (_coordinates$x = coordinates.x) !== null && _coordinates$x !== void 0 ? _coordinates$x : 0;
this._nOriginalY = (_coordinates$y = coordinates.y) !== null && _coordinates$y !== void 0 ? _coordinates$y : 0;
(_this$_oDocument2 = this._oDocument) === null || _this$_oDocument2 === void 0 || _this$_oDocument2.on('mousemove', this.onMouseMove);
(_this$_oDocument3 = this._oDocument) === null || _this$_oDocument3 === void 0 || _this$_oDocument3.on('mouseup', this.onMouseUp);
this._timeout = window.setTimeout(this._startDrag, this._nDelay);
}
}
onMouseMove(oEvent) {
if (this._timeout != null) {
var _coordinates$x2, _coordinates$y2;
oEvent.preventDefault();
var coordinates = this._getCoordinates(oEvent);
this._nX = ((_coordinates$x2 = coordinates.x) !== null && _coordinates$x2 !== void 0 ? _coordinates$x2 : 0) - this._nOriginalX;
this._nY = ((_coordinates$y2 = coordinates.y) !== null && _coordinates$y2 !== void 0 ? _coordinates$y2 : 0) - this._nOriginalY;
if (this._bDragging === false) {
if (Math.abs(this._nX) > this._nDistance || Math.abs(this._nY) > this._nDistance) {
this._startDrag();
}
}
if (this._bDragging) {
this.emit('drag', this._nX, this._nY, oEvent);
}
}
}
onMouseUp(oEvent) {
if (this._timeout != null) {
var _this$_oDocument4, _this$_oDocument5, _this$_oDocument6, _this$_eBody, _this$_root;
clearTimeout(this._timeout);
(_this$_oDocument4 = this._oDocument) === null || _this$_oDocument4 === void 0 || _this$_oDocument4.unbind('mousemove', this.onMouseMove);
(_this$_oDocument5 = this._oDocument) === null || _this$_oDocument5 === void 0 || _this$_oDocument5.unbind('mouseup', this.onMouseUp);
(_this$_oDocument6 = this._oDocument) === null || _this$_oDocument6 === void 0 || (_this$_oDocument6 = _this$_oDocument6.find('iframe')) === null || _this$_oDocument6 === void 0 || _this$_oDocument6.css('pointer-events', '');
if (this._bDragging === true) {
this._bDragging = false;
this.emit('dragStop', oEvent, this._nOriginalX + this._nX);
}
// after dragStop, so that .lm_dragging is removed after size is processed
// and any overflow: hidden remains applied during the calculations
(_this$_eBody = this._eBody) === null || _this$_eBody === void 0 || _this$_eBody.removeClass('lm_dragging');
(_this$_root = this._root) === null || _this$_root === void 0 || _this$_root.childElementContainer.removeClass('lm_dragging');
if (!(this._eElement instanceof Window)) {
var _this$_eElement3;
(_this$_eElement3 = this._eElement) === null || _this$_eElement3 === void 0 || _this$_eElement3.removeClass('lm_dragging');
}
if (this._destroyAfterMouseUp) this.destroy();
}
}
_startDrag() {
var _this$_eBody2, _this$_eElement4, _this$_root2, _this$_oDocument7;
window.clearTimeout(this._timeout);
this._bDragging = true;
(_this$_eBody2 = this._eBody) === null || _this$_eBody2 === void 0 || _this$_eBody2.addClass('lm_dragging');
(_this$_eElement4 = this._eElement) === null || _this$_eElement4 === void 0 || _this$_eElement4.addClass('lm_dragging');
(_this$_root2 = this._root) === null || _this$_root2 === void 0 || _this$_root2.childElementContainer.addClass('lm_dragging');
(_this$_oDocument7 = this._oDocument) === null || _this$_oDocument7 === void 0 || (_this$_oDocument7 = _this$_oDocument7.find('iframe')) === null || _this$_oDocument7 === void 0 || _this$_oDocument7.css('pointer-events', 'none');
this.emit('dragStart', this._nOriginalX, this._nOriginalY);
}
_getCoordinates(event) {
var _event$pageX, _event$pageY;
return {
x: (_event$pageX = event.pageX) !== null && _event$pageX !== void 0 ? _event$pageX : 0,
y: (_event$pageY = event.pageY) !== null && _event$pageY !== void 0 ? _event$pageY : 0
};
}
}
export default DragListener;
//# sourceMappingURL=DragListener.js.map