UNPKG

react-dnd-mouse-backend

Version:

A lightweight attempt for solving the whole HTML5 DnD - svg tags compability issue

2 lines 5.73 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ReactDnDMouseMoveBackend=t():e.ReactDnDMouseMoveBackend=t()}(this,function(){return function(e){function t(n){if(o[n])return o[n].exports;var i=o[n]={exports:{},id:n,loaded:!1};return e[n].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var o={};return t.m=e,t.c=o,t.p="",t(0)}([function(e,t,o){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0});var i=o(1),r=n(i),s=function(e){return new r.default(e)};t.default=s},function(e,t){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e){return{x:e.clientX,y:e.clientY}}function i(e){var t=e.nodeType===u?e:e.parentElement;if(!t)return null;var o=t.getBoundingClientRect(),n=o.top,i=o.left;return{x:i,y:n}}function r(e){return"which"in e?3===e.which:"button"in e&&2===e.button}Object.defineProperty(t,"__esModule",{value:!0});var s=function(){function e(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,o,n){return o&&e(t.prototype,o),n&&e(t,n),t}}(),u=1,d=function(){function e(t){o(this,e),this.actions=t.getActions(),this.monitor=t.getMonitor(),this.registry=t.getRegistry(),this.sourceNodes={},this.sourceNodesOptions={},this.sourcePreviewNodes={},this.sourcePreviewNodesOptions={},this.targetNodes={},this.targetNodeOptions={},this.mouseClientOffset={},this.getSourceClientOffset=this.getSourceClientOffset.bind(this),this.handleWindowMoveStart=this.handleWindowMoveStart.bind(this),this.handleWindowMoveStartCapture=this.handleWindowMoveStartCapture.bind(this),this.handleWindowMoveCapture=this.handleWindowMoveCapture.bind(this),this.handleWindowMoveEndCapture=this.handleWindowMoveEndCapture.bind(this)}return s(e,[{key:"setup",value:function(){if("undefined"!=typeof window){if(this.constructor.isSetUp)throw new Error("Cannot have two DnD Mouse backend at the same time");this.constructor.isSetUp=!0,window.addEventListener("mousedown",this.handleWindowMoveStartCapture,!0),window.addEventListener("mousedown",this.handleWindowMoveStart),window.addEventListener("mousemove",this.handleWindowMoveCapture,!0),window.addEventListener("mouseup",this.handleWindowMoveEndCapture,!0)}}},{key:"getSourceClientOffset",value:function(e){return i(this.sourceNodes[e])}},{key:"teardown",value:function(){"undefined"!=typeof window&&(this.constructor.isSetUp=!1,this.mouseClientOffset={},window.removeEventListener("mousedown",this.handleWindowMoveStartCapture,!0),window.removeEventListener("mousedown",this.handleWindowMoveStart),window.removeEventListener("mousemove",this.handleWindowMoveCapture,!0),window.removeEventListener("mouseup",this.handleWindowMoveEndCapture,!0))}},{key:"connectDragSource",value:function(e,t){var o=this;this.sourceNodes[e]=t;var n=this.handleMoveStart.bind(this,e);return t.addEventListener("mousedown",n),function(){delete o.sourceNodes[e],t.removeEventListener("mousedown",n)}}},{key:"connectDragPreview",value:function(e,t,o){var n=this;return this.sourcePreviewNodesOptions[e]=o,this.sourcePreviewNodes[e]=t,function(){delete n.sourcePreviewNodes[e],delete n.sourcePreviewNodesOptions[e]}}},{key:"connectDropTarget",value:function(e,t){var o=this;return this.targetNodes[e]=t,function(){delete o.targetNodes[e]}}},{key:"handleWindowMoveStartCapture",value:function(){this.moveStartSourceIds=[]}},{key:"handleMoveStart",value:function(e,t){r(t)||this.moveStartSourceIds.unshift(e)}},{key:"handleWindowMoveStart",value:function(e){var t=n(e);t&&(this.mouseClientOffset=t)}},{key:"handleWindowMoveCapture",value:function(e){var t=this,o=this.moveStartSourceIds,i=n(e);if(i&&(this.monitor.isDragging()||!this.mouseClientOffset.hasOwnProperty("x")||!o||this.mouseClientOffset.x===i.x&&this.mouseClientOffset.y===i.y||(this.moveStartSourceIds=null,this.actions.beginDrag(o,{clientOffset:this.mouseClientOffset,getSourceClientOffset:this.getSourceClientOffset,publishSource:!1})),this.monitor.isDragging())){var r=this.sourceNodes[this.monitor.getSourceId()];this.installSourceNodeRemovalObserver(r),this.actions.publishDragSource(),e.preventDefault();var s=Object.keys(this.targetNodes).filter(function(e){var o=t.targetNodes[e].getBoundingClientRect();return i.x>=o.left&&i.x<=o.right&&i.y>=o.top&&i.y<=o.bottom});this.actions.hover(s,{clientOffset:i})}}},{key:"handleWindowMoveEndCapture",value:function(e){return!this.monitor.isDragging()||this.monitor.didDrop()?void(this.moveStartSourceIds=null):(e.preventDefault(),this.mouseClientOffset={},this.uninstallSourceNodeRemovalObserver(),this.actions.drop(),void this.actions.endDrag())}},{key:"installSourceNodeRemovalObserver",value:function(e){var t=this;this.uninstallSourceNodeRemovalObserver(),this.draggedSourceNode=e,this.draggedSourceNodeRemovalObserver=new window.MutationObserver(function(){e.parentElement||(t.resurrectSourceNode(),t.uninstallSourceNodeRemovalObserver())}),e&&e.parentElement&&this.draggedSourceNodeRemovalObserver.observe(e.parentElement,{childList:!0})}},{key:"resurrectSourceNode",value:function(){this.draggedSourceNode.style.display="none",this.draggedSourceNode.removeAttribute("data-reactid"),document.body.appendChild(this.draggedSourceNode)}},{key:"uninstallSourceNodeRemovalObserver",value:function(){this.draggedSourceNodeRemovalObserver&&this.draggedSourceNodeRemovalObserver.disconnect(),this.draggedSourceNodeRemovalObserver=null,this.draggedSourceNode=null}}]),e}();t.default=d}])}); //# sourceMappingURL=ReactDnDMouseMoveBackend.min.js.map