Last active
January 1, 2016 07:29
-
-
Save bazilio91/8112135 to your computer and use it in GitHub Desktop.
Creates (or emulates) drag & drop event
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
/** | |
* Creates (or emulates) drag & drop event | |
* @param type event type | |
* @param options event options | |
* @param transferData data attached to event transfer | |
* @returns {*} | |
* | |
* Usage: | |
* var event = createDragEvent('drop'); // or dragend, or dragstart, etc. | |
* // feel free to use event.dataTransfer.setData('key', value); | |
* view.onDragStart(event); | |
* $container.find('.b-drop-place')[0].dispatchEvent(event); | |
*/ | |
function createDragEvent(type, options, transferData) { | |
var event = null; | |
options = options || {}; | |
transferData = transferData || {}; | |
options = $.extend({ | |
bubbles: true, | |
cancelable: (type !== "mousemove"), | |
view: window, | |
detail: 0, | |
screenX: 0, | |
screenY: 0, | |
clientX: 1, | |
clientY: 1, | |
ctrlKey: false, | |
altKey: false, | |
shiftKey: false, | |
metaKey: false, | |
button: 0, | |
relatedTarget: undefined, | |
dataTransfer: {} | |
}, options); | |
if ('undefined' !== typeof CustomEvent) { | |
event = new CustomEvent('MouseEvent'); | |
} else { | |
// PhantomJS | |
event = document.createEvent('Event'); | |
} | |
event.initEvent(type, options.bubbles, options.cancelable, | |
options.view, options.detail, | |
options.screenX, options.screenY, options.clientX, options.clientY, | |
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, | |
options.button, options.relatedTarget || document.body.parentNode); | |
event.dataTransfer = { | |
getData: function (key) { | |
return event.dataTransfer[key]; | |
}, | |
setData: function (key, value) { | |
event.dataTransfer[key] = value; | |
} | |
}; | |
_.each(transferData, function (value, key) { | |
event.dataTransfer.setData(key, value); | |
}); | |
return event; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment