SHARE
TWEET

Untitled

a guest Oct 13th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fakeDrag = function(sourceNode, destinationNode) {
  2.     const DRAG_OVER = 'dragover';
  3.     const DRAG_END = 'dragend';
  4.     const DRAG_START = 'dragstart';
  5.     const DROP = 'drop';
  6.  
  7.     const createCustomEvent = (type) => {
  8.         const event = new CustomEvent('CustomEvent');
  9.         event.initCustomEvent(type, true, true, null);
  10.         event.dataTransfer = {
  11.             data: {},
  12.             setData (type, val) {
  13.                 this.data[type] = val;
  14.             },
  15.             getData (type) {
  16.                 return this.data[type];
  17.             },
  18.             // This ensures the directives 'dnd-moved=""' callback is fired
  19.             // on the 'dragend' event, removing the original dragged element..
  20.             dropEffect: 'move'
  21.  
  22.         };
  23.         return event;
  24.     };
  25.  
  26.     const dispatchEvent = (node, event) => {
  27.         if (node.dispatchEvent) {
  28.             return node.dispatchEvent(event);
  29.         }
  30.     };
  31.  
  32.     const addType = (sourceNode) => {
  33.         const defaultType = 'application/x-dnd';
  34.         const customType = sourceNode.getAttribute('dnd-type');
  35.  
  36.         if (customType) {
  37.             // Remove single quotes if any present.
  38.             // dnd-list converts any types provided by 'dnd-type="'xXxX'"' to lowercase, so match that here.
  39.             const formattedStr = customType.replace(/'/g,'');
  40.             return [`${defaultType}-${formattedStr.toLowerCase()}`];
  41.         }
  42.  
  43.         return [defaultType];
  44.     };
  45.  
  46.     const event = createCustomEvent(DRAG_START);
  47.     event.dataTransfer.types = addType(sourceNode, destinationNode);
  48.     dispatchEvent(sourceNode, event);
  49.  
  50.     const dragOverEvent = createCustomEvent(DRAG_OVER);
  51.     dragOverEvent.dataTransfer = event.dataTransfer;
  52.     dispatchEvent(destinationNode, dragOverEvent);
  53.  
  54.     const dropNode = document.querySelector('.dndPlaceholder');
  55.  
  56.     const dropEvent = createCustomEvent(DROP);
  57.     dropEvent.dataTransfer = event.dataTransfer;
  58.     dispatchEvent(dropNode, dropEvent);
  59.  
  60.     const dragEndEvent = createCustomEvent(DRAG_END);
  61.     dragEndEvent.dataTransfer = event.dataTransfer;
  62.     dispatchEvent(sourceNode, dragEndEvent);
  63.  
  64. };
RAW Paste Data
Top