Forked from druska/native_js_drag_and_drop_helper.js
Created
December 1, 2020 15:17
-
-
Save dmk1111/ba449fd37d15eb9ade2431910c97246b to your computer and use it in GitHub Desktop.
Create the `simulateDragDrop` function which can be used to simulate clicking and dragging one DOM Node onto another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function simulateDragDrop(sourceNode, destinationNode) { | |
| var EVENT_TYPES = { | |
| DRAG_END: 'dragend', | |
| DRAG_START: 'dragstart', | |
| DROP: 'drop' | |
| } | |
| function createCustomEvent(type) { | |
| var event = new CustomEvent("CustomEvent") | |
| event.initCustomEvent(type, true, true, null) | |
| event.dataTransfer = { | |
| data: { | |
| }, | |
| setData: function(type, val) { | |
| this.data[type] = val | |
| }, | |
| getData: function(type) { | |
| return this.data[type] | |
| } | |
| } | |
| return event | |
| } | |
| function dispatchEvent(node, type, event) { | |
| if (node.dispatchEvent) { | |
| return node.dispatchEvent(event) | |
| } | |
| if (node.fireEvent) { | |
| return node.fireEvent("on" + type, event) | |
| } | |
| } | |
| var event = createCustomEvent(EVENT_TYPES.DRAG_START) | |
| dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event) | |
| var dropEvent = createCustomEvent(EVENT_TYPES.DROP) | |
| dropEvent.dataTransfer = event.dataTransfer | |
| dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent) | |
| var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END) | |
| dragEndEvent.dataTransfer = event.dataTransfer | |
| dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment