window.MXOBJ = MXOBJ = window.MXOBJ||{}; MXOBJ.attachEvent = MXOBJ.attachEvent || function(e,t,f) {if (e&&e.addEventListener)e.addEventListener(t, f, true); else e&&e.attachEvent('on'+t, f);}; if (!MXOBJ.stopAndPreventEvent) MXOBJ.stopAndPreventEvent = function(e) {e.stopPropagation();e.preventDefault();}; if (!MXOBJ.getComputedStyle) MXOBJ.getComputedStyle = function(e, name) { if (e.currentStyle) { name = name.toLowerCase().replace(/-(.)/g, function(match, group1) {return group1.toUpperCase();}); // camel case return e.currentStyle[name]; } else if (window.getComputedStyle) { return document.defaultView.getComputedStyle(e,null).getPropertyValue(name); } }; if (!MXOBJ.htmlTagMarginTop) { let htmlTag = document.getElementsByTagName('html')[0]; MXOBJ.htmlTagMarginTop = MXOBJ.getComputedStyle(htmlTag, 'margin-top'); MXOBJ.htmlTagMarginTop = parseInt(MXOBJ.htmlTagMarginTop.replace('px', '')); if (isNaN(MXOBJ.htmlTagMarginTop)) {MXOBJ.htmlTagMarginTop=0} } if (!MXOBJ.bodyTagMarginTop) { MXOBJ.bodyTagMarginTop = MXOBJ.getComputedStyle(document.body, 'margin-top'); MXOBJ.bodyTagMarginTop = parseInt(MXOBJ.bodyTagMarginTop.replace('px', '')); if (isNaN(MXOBJ.bodyTagMarginTop)) {MXOBJ.bodyTagMarginTop=0} } MXOBJ.DragAndDropElement = (function() { let actualDragging, widgetStartX, widgetStartY, eventStartX, eventStartY, myDevicePixelRatio; let downDisableTimer, downLongPressTimer; function absLeft(e, par) { return e.getBoundingClientRect().left - (par?par.getBoundingClientRect().left:0); /* return ret<0?e.getBoundingClientRect().left:ret; */ } function absTop(e, par) { return e.getBoundingClientRect().top - (par?par.getBoundingClientRect().top:0); /* return ret<0?e.getBoundingClientRect().top:ret; */ } /* function absLeft(e) {return e.getBoundingClientRect().left + window.scrollX} function absTop(e) {return e.getBoundingClientRect().top + window.scrollY} */ /* function absLeft(e) {return MXOBJ.gwt.getAbsoluteLeft(e);} function absTop(e) {return MXOBJ.gwt.getAbsoluteTop(e);} */ /* function absLeft(e) {return e.offsetLeft;} function absTop(e) {return e.offsetTop;} */ function touchX(evt) {return evt.clientX / myDevicePixelRatio;} function touchY(evt) {return evt.clientY / myDevicePixelRatio;} function resetDownLongPressTimer() {clearTimeout(downLongPressTimer); downLongPressTimer=null } function DragAndDropElement(child, parentToMoveOn, moveElem, mouseDownCallBack, mouseMoveCallBack, mouseUpCallBack, mouseDownCallBackInstant, mouseMoveCallBackInstant) { let _this = this; let isDragging = false; /* let mouseDownCallBack = mouseDownCallBack; let mouseMoveCallBack = mouseMoveCallBack; let mouseUpCallBack = mouseUpCallBack; let mouseDownCallBackInstant = mouseDownCallBackInstant; */ this.mouseMoveCallBackInstant = mouseMoveCallBackInstant; this.child = child; this.parentToMoveOn = parentToMoveOn; this.moveElem = moveElem||child; let CHANGE_POSITION_STYLE = true, UNSET_RIGHT_BOTTOM = true; let moveOnlyX = false; let moveOnlyY = false; let moveWithElems; this.downLongPressMili = 0; this.setMoveOnlyX = function(m) {moveOnlyX=m;}; this.setMoveOnlyY = function(m) {moveOnlyY=m;}; this.setMoveWithElems = function(arr) {moveWithElems=arr;}; this.setCHANGE_POSITION_STYLE = function(v) {CHANGE_POSITION_STYLE=v;}; this.setUNSET_RIGHT_BOTTOM = function(v) {UNSET_RIGHT_BOTTOM=v;}; this.onMouseDown = function (evt) { if (downDisableTimer!=null) {return;} downDisableTimer = setTimeout(function(){ downDisableTimer=null }, 500); evt=evt||window.event; let isTouch = MXOBJ.osdetection.isTouchSupported && (evt instanceof TouchEvent); if (isDragging || (evt.button!==0 && !isTouch)) {return;} MXOBJ.mouseEventCapturer_Elem = _this.child; if (!downLongPressTimer && _this.downLongPressMili>0) { downLongPressTimer = setTimeout(function(){ _this.onMouseDown(evt) }, _this.downLongPressMili); return; } resetDownLongPressTimer(); let touch = isTouch && evt.changedTouches && evt.changedTouches.length>0 ? evt.changedTouches.item(0) : null; myDevicePixelRatio = 1; //isTouch ? (window.outerWidth-8)/window.innerWidth : 0; eventStartX = touch ? touchX(touch) : evt.clientX; eventStartY = touch ? touchY(touch) : evt.clientY; /*console.log('evtjs : ' + eventStartX + ' x ' + eventStartY + ' isTouch: ' + isTouch + ' myDevicePixelRatio: ' + myDevicePixelRatio);*/ /*return;*/ if (mouseDownCallBackInstant) { if (!mouseDownCallBackInstant(_this.moveElem, evt, null, null, eventStartX, eventStartY, null, null, [_this])) {return;} } let par = _this.parentToMoveOn/*||_this.moveElem.parentElement*/; isDragging=true; actualDragging = this; if (!_this.parentToMoveOn || _this.parentToMoveOn===_this.moveElem.parentElement) { widgetStartX = _this.moveElem.hasAttribute('widgetStartX') ? parseInt(_this.moveElem.getAttribute('widgetStartX')) : _this.moveElem.offsetLeft; widgetStartY = _this.moveElem.hasAttribute('widgetStartY') ? parseInt(_this.moveElem.getAttribute('widgetStartY')) : _this.moveElem.offsetTop; } else { widgetStartX = absLeft(_this.moveElem, par) + (_this.moveElem.nodeName.toLowerCase()==='tr' ? 50 : 0); widgetStartY = absTop(_this.moveElem, par) - (_this.moveElem.nodeName.toLowerCase()==='tr' ? (_this.moveElem.offsetHeight/2.0) : 0) + (par?(MXOBJ.htmlTagMarginTop+MXOBJ.bodyTagMarginTop):0); } if (_this.moveElem.hasAttribute('corr_x')) {widgetStartX+=parseInt(_this.moveElem.getAttribute('corr_x'));} if (_this.moveElem.hasAttribute('corr_y')) {widgetStartY+=parseInt(_this.moveElem.getAttribute('corr_y'));} /* console.log('widgetStartX: ' + widgetStartX + ' eventStartX: ' + eventStartX + ' widgetStartY: ' + widgetStartY + ' eventStartY: ' + eventStartY + ' _this.moveElem.nodeName: ' + _this.moveElem.nodeName + ' par: ' + par + ' CHANGE_POSITION_STYLE: ' + CHANGE_POSITION_STYLE); */ let x = widgetStartX, y = widgetStartY; if (_this.parentToMoveOn) {_this.parentToMoveOn.appendChild(_this.moveElem);} if (CHANGE_POSITION_STYLE) { _this.moveElem.setAttribute("origposstyle", _this.moveElem.style.position); _this.moveElem.style.position = 'absolute'; } if (!moveOnlyY) {_this.moveElem.style.left = x + 'px';} if (!moveOnlyX) {_this.moveElem.style.top = y + 'px';} MXOBJ.stopAndPreventEvent(evt); if (mouseDownCallBack) {mouseDownCallBack(_this.moveElem, evt, null, null, eventStartX, eventStartY, null, null, [_this]);} if (UNSET_RIGHT_BOTTOM) { _this.moveElem.style.right = 'unset'; _this.moveElem.style.bottom = 'unset'; } }; this.onMouseMove = function (evt) { if (!isDragging) {return;} evt=evt||window.event; let i, isTouch = MXOBJ.osdetection.isTouchSupported && (evt instanceof TouchEvent); let touch = isTouch && evt.changedTouches && evt.changedTouches.length>0 ? evt.changedTouches.item(0) : null; let evtX = touch ? touchX(touch) : evt.clientX, evtY = touch ? touchY(touch) : evt.clientY; let x = widgetStartX + evtX - eventStartX, y = widgetStartY + evtY - eventStartY; if (_this.mouseMoveCallBackInstant) { if (!_this.mouseMoveCallBackInstant(_this.moveElem, evt, null, null, x, y-MXOBJ.htmlTagMarginTop-MXOBJ.bodyTagMarginTop)) {return;} } if (!moveOnlyY) { _this.moveElem.style.left = x + 'px'; if (moveWithElems) {for (i=0;i0 ? evt.changedTouches.item(0) : null; let evtX = touch ? touchX(touch) : evt.clientX, evtY = touch ? touchY(touch) : evt.clientY; isDragging=false; actualDragging = null; MXOBJ.mouseEventCapturer_Elem = null; MXOBJ.stopAndPreventEvent(evt); if (CHANGE_POSITION_STYLE) {_this.moveElem.style.position = _this.moveElem.getAttribute('origposstyle');} if (mouseUpCallBack) {mouseUpCallBack(_this.moveElem, evt, null, null, evtX, evtY-MXOBJ.htmlTagMarginTop-MXOBJ.bodyTagMarginTop, null, null, [_this]);} }; this.onMouseOut = function (evt) { resetDownLongPressTimer(); }; if (child) { child.ondragstart = function() { resetDownLongPressTimer(); return false; }; child.onmousedown = function() { return false; }; } MXOBJ.attachEvent(child, 'mousedown', this.onMouseDown); MXOBJ.attachEvent(child, 'mousemove', this.onMouseMove); MXOBJ.attachEvent(child, 'mouseup', this.onMouseUp); MXOBJ.attachEvent(child, 'mouseout', this.onMouseOut); MXOBJ.attachEvent(child, 'touchstart', this.onMouseDown); MXOBJ.attachEvent(child, 'touchmove', this.onMouseMove); MXOBJ.attachEvent(child, 'touchend', this.onMouseUp); } return DragAndDropElement; })();