SHARE
TWEET

Untitled

a guest Mar 20th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. configureDragDrop(registerType) {
  2.     var imageThreshold = Math.max(120, window.innerHeight / 4),
  3.         sectionThreshold = Math.max(140, window.innerHeight / 4),
  4.         currentDY = 0,
  5.         frame;
  6.  
  7.     function makeScrollingHandler(threshold) {
  8.       function getScrollDY(clientY) {
  9.         var speed;
  10.         if (clientY < threshold) {
  11.           // -1 to 0 as we move from 0 to threshold
  12.           speed = -1 + clientY / threshold;
  13.         } else if (clientY > window.innerHeight - threshold) {
  14.           // 0 to 1 as we move from (innerHeight - threshold) to innerHeight
  15.           speed = 1 - (window.innerHeight - clientY) / threshold;
  16.         } else {
  17.           speed = 0;
  18.         }
  19.  
  20.         return Math.round(speed * 10);
  21.       }
  22.  
  23.       function tick() {
  24.         if (!currentDY) {
  25.           frame = null;
  26.           return;
  27.         }
  28.  
  29.         window.scrollTo(0, getScrollY() + currentDY);
  30.         frame = window.requestAnimationFrame(tick);
  31.       }
  32.  
  33.       function queueScroll(dy) {
  34.         currentDY = dy;
  35.  
  36.         if (!frame) {
  37.           frame = window.requestAnimationFrame(tick);
  38.         }
  39.       }
  40.  
  41.       function cancelScroll() {
  42.         window.cancelAnimationFrame(frame);
  43.         frame = null;
  44.         currentDY = 0;
  45.       }
  46.  
  47.       return {
  48.         dropTarget: {
  49.           enter: cancelScroll,
  50.           leave: cancelScroll,
  51.  
  52.           over(item, e) {
  53.             queueScroll(getScrollDY(e.clientY));
  54.           },
  55.  
  56.           acceptDrop() {
  57.             cancelScroll();
  58.             return false;
  59.           }
  60.         }
  61.       };
  62.     }
  63.  
  64.     registerType(DragItemTypes.SECTION, makeScrollingHandler(sectionThreshold));
  65.     registerType(DragItemTypes.IMAGE, makeScrollingHandler(imageThreshold));
  66.   },
RAW Paste Data
Top