Subversion Repositories wimsdev

Rev

Rev 11524 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.    DynAPI Distribution
  3.    DragEvent Class
  4.  
  5.    The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6. */
  7.  
  8. // DragEvent object
  9. function DragEvent(type,src) {
  10.         this.MouseEvent = MouseEvent;
  11.         this.MouseEvent();
  12.         this.DynEvent()
  13.         this.isDragging = false;
  14. }
  15. var protoDrag = dynapi.setPrototype('DragEvent','MouseEvent');
  16. protoDrag.getX=function() {return this.x};
  17. protoDrag.getY=function() {return this.y};
  18. protoDrag.getPageX=function() {return this.pageX};
  19. protoDrag.getPageY=function() {return this.pageY};
  20. protoDrag.cancelDrag=function() {this.isDragging=false};
  21.  
  22. //DragEvent.dragPlay=0;
  23.  
  24. DragEvent.dragevent = new DragEvent();
  25.  
  26. DragEvent.lyrListener = {
  27.         onmousedown : function(e) {
  28.                 DragEvent.startDrag(e);
  29.                 //e.preventDefault();
  30.         }
  31. };
  32.  
  33. DragEvent.startDrag = function(e,dlyr) {
  34.         var origdlyr = dlyr;
  35.         if (!dlyr) dlyr = e.getSource();
  36.  
  37.         if (dynapi.ua.dom) {
  38.                 dlyr.elm.ondragstart = function() { return false; }
  39.                 dlyr.elm.onselectstart = function() { return false; }
  40.         }
  41.  
  42.         // Initialize dragEvent object
  43.         var de=DragEvent.dragevent;
  44.         //de.bubble = true;
  45.         de.src = dlyr;
  46.         de.origin = (origdlyr)? e.origin : dlyr;
  47.         de.x = e.getPageX()-dlyr.getPageX();
  48.         de.y = e.getPageY()-dlyr.getPageY();
  49.         de.pageX = e.getPageX();
  50.         de.pageY = e.getPageY();
  51.         de.parentPageX = dlyr.parent.getPageX();
  52.         de.parentPageY = dlyr.parent.getPageY();
  53.  
  54.         de.isDragging = true;
  55.  
  56.         e.preventDefault();
  57.         e.preventBubble();
  58.         // prevent Safari allowing text selection when dragging :/
  59.         document.getElementById("wimsbodybox").classList.add('unselectable');
  60.  
  61.         //dlyr._dyndoc.addEventListener(DragEvent.docListener);
  62.  
  63.         dlyr.invokeEvent("dragstart",de);
  64. }
  65.  
  66. DragEvent.docListener = {
  67.         onmousemove : function(e) {
  68.                 //var x = e.getPageX();
  69.                 //var y = e.getPageY();
  70.                 //dynapi.debug.status('drag move '+e.x+' '+e.y);
  71.  
  72.                 var de = DragEvent.dragevent;
  73.                 if (de && de.isDragging) {
  74.                         var lyr = de.src;
  75.                         if (!lyr) return;
  76.  
  77.                         // DS: what is this?
  78.                         // Detect if we should start the drag
  79.                         /*if(DragEvent.dragPlay==0 || (Math.abs(de.pageX-e.getPageX())-DragEvent.dragPlay>0) || (Math.abs(de.pageY-e.getPageY())-DragEvent.dragPlay>0)) {
  80.                                 de.isDragging=true;
  81.                                 de.src.invokeEvent("dragstart",de);
  82.                                 e.setBubble(de.bubble);
  83.                         }
  84.                         */
  85.                         /*else if (!de.dragEnabled) {
  86.                                 // This allows 'cancelDrag' method to fire the mouseUp as if had been released by the user
  87.                                 lyr.invokeEvent("mouseup");
  88.                                 return;
  89.                         }*/
  90.  
  91.                         // Properties
  92.                         de.type="dragmove";
  93.                         de.pageX=e.getPageX();
  94.                         de.pageY=e.getPageY();
  95.  
  96.                         /*if (DragEvent.stopAtDocumentEdge) {
  97.                                 if (de.pageX<0) de.pageX = 0;
  98.                                 if (de.pageY<0) de.pageY = 0;
  99.                                 if (de.pageX>DynAPI.document.w) de.pageX = DynAPI.document.w;
  100.                                 if (de.pageY>DynAPI.document.h) de.pageY = DynAPI.document.h;
  101.                         }*/
  102.  
  103.                         var x=de.pageX-de.parentPageX-de.x;
  104.                         var y=de.pageY-de.parentPageY-de.y;
  105.  
  106.                         // Respect boundary, if any
  107.                         if (lyr._dragBoundary) {
  108.                                 var dB = lyr._dragBoundary;
  109.                                 var t = dB.top;
  110.                                 var r = dB.right;
  111.                                 var b = dB.bottom;
  112.                                 var l = dB.left
  113.                                 if (x<l) x = l;
  114.                                 else if (x>lyr.parent.w-lyr.w-r) x = lyr.parent.w-lyr.w-r;
  115.                                 if (y<t) y = t;
  116.                                 else if (y>lyr.parent.h-lyr.h-b) y = lyr.parent.h-lyr.h-b;
  117.                         }
  118.                         else if (lyr._dragBoundaryA) {
  119.                                 var dB = lyr._dragBoundaryA;
  120.                                 var b=dB[2];
  121.                                 var r=dB[1];
  122.                                 var l=dB[3];
  123.                                 var t=dB[0];
  124.                                 var w=lyr.w;
  125.                                 var h=lyr.h;
  126.                                 if (x<l) x=l;
  127.                                 else if (x+w>r) x=r-w;
  128.                                 if (y<t) y=t;
  129.                                 else if (y+h>b) y=b-h;
  130.                         }
  131.                         // Move dragged layer
  132.                         lyr.setLocation(x,y);
  133.                         lyr.invokeEvent("dragmove",de);
  134.  
  135.                         if (lyr._dragStealth==false && lyr.parent.DragOver) {
  136.                                 lyr.parent.DragOver(lyr);
  137.                         }
  138.  
  139.                         e.preventDefault();
  140.                         e.preventBubble();
  141.                 }
  142.         },
  143.         onmouseup : function(e) {
  144.                 // Get, if any, the currently drag in process and the layer. If none, return
  145.                 var de=DragEvent.dragevent;
  146.                 //de.bubble = true;
  147.                 if (!de) return;
  148.                 var lyr=de.src;
  149.                 if (!lyr) return;
  150.  
  151.                 if (!de.isDragging) {
  152.                 de.type="dragend";
  153.                 de.src=null;
  154.                 //e.setBubble(true);
  155.                         return;
  156.                 }
  157.                 if (dynapi.ua.ie) lyr.doc.body.onselectstart = null;
  158.  
  159.                 // Avoid click for the dragged layer ( with MouseEvent addition )
  160.                 if (dynapi.ua.def) dynapi.wasDragging=true;
  161.                 if (lyr.parent.DragDrop) lyr.parent.DragDrop(lyr);
  162.                 // Properties for the event
  163.                 de.type="dragend";
  164.                 de.isDragging=false;
  165.                 lyr.invokeEvent("dragend",de);
  166.  
  167.                 // Clean drag stuff
  168.                 de.src=null;
  169.                 //e.preventDefault();
  170.                 e.preventBubble();
  171.  
  172.                 document.getElementById("wimsbodybox").classList.remove('unselectable');
  173.  
  174.                 //lyr._dyndoc.removeEventListener(DragEvent.docListener);
  175.         }
  176. };
  177. DragEvent.stopAtDocumentEdge = true;
  178. DragEvent.setDragBoundary=function(lyr,t,r,b,l) {
  179.         if (!lyr) {dynapi.debug.print("Error: no object passed to DragEvent.setDragBoundary()"); return;}
  180.         var a=arguments;
  181.         if (a.length==0) return;
  182.         if (a.length==1) {
  183.                 lyr._dragBoundary = {left:0,right:0,top:0,bottom:0};
  184.         }
  185.         if (a.length==2) {
  186.                 lyr._dragBoundary = arguments[1];
  187.         }
  188.         else if (a.length==5) lyr._dragBoundaryA = [t,r,b,l];
  189. };
  190. DragEvent.enableDragEvents=function() {
  191.         for (var i=0;i<arguments.length;i++) {
  192.                 var lyr=arguments[i];
  193.                 if (!lyr) {dynapi.debug.print("Error: no object passed to DragEvent.enableDragEvents()"); return;}
  194.                 if (lyr.isClass('DynLayer')) lyr.addEventListener(DragEvent.lyrListener);
  195.         }
  196.         dynapi.document.addEventListener(DragEvent.docListener);
  197.         dynapi.document.captureMouseEvents();
  198. };
  199. DragEvent.disableDragEvents=function() {
  200.         for (var i=0;i<arguments.length;i++) {
  201.                 var lyr=arguments[i];
  202.                 lyr.removeEventListener(DragEvent.lyrListener);
  203.         }
  204. };
  205.  
  206. DynLayer.setDragMode = function(b,boundry){
  207.         if(!self.DragEvent) return false;
  208.         if(boundry)DragEvent.setDragBoundary(this,boundry);
  209.         if (b) DragEvent.enableDragEvents(this);
  210.         else DragEvent.disableDragEvents(this);
  211.         return true;
  212. };
  213. DynLayer.prototype.setDragOverStealthMode = function(b){
  214.         this._dragStealth=(b)? true:false;
  215. };
  216. // Enable ondrop event
  217. DynElement.prototype.DragDrop=function(s){
  218.         if (!this.children.length>0) return false;
  219.         var ch,chX,sX,sY;
  220.         for (var i in this.children) {
  221.                 ch=this.children[i];
  222.                 if(ch._hasDragEvents){
  223.                         chX=ch.getPageX();
  224.                         chY=ch.getPageY();
  225.                         sX=s.getPageX();
  226.                         sY=s.getPageY();
  227.                         if (chX<sX && chX+ch.w>sX+s.w && chY<sY && chY+ch.h>sY+s.h) {
  228.                                 if (ch.DragDrop(s)) return true;
  229.                                 ch.invokeEvent("drop");
  230.                                 return true;
  231.                         }
  232.                 }
  233.         }
  234.         return false;
  235. };
  236.  
  237. // Enable ondragover event
  238. DynElement.prototype.DragOver=function(s){
  239.         if (!this.children.length>0) return false;
  240.         var ch,chX,sX,sY;
  241.         for (var i in this.children) {
  242.                 ch=this.children[i];
  243.                 if(ch._hasDragEvents){
  244.                         chX=ch.getPageX();
  245.                         chY=ch.getPageY();
  246.                         sX=s.getPageX();
  247.                         sY=s.getPageY();
  248.                         if (chX<sX && chX+ch.w>sX+s.w && chY<sY && chY+ch.h>sY+s.h) {
  249.                                 if (ch.DragDrop(s)) return true;
  250.                                 ch._isDragOver=true;
  251.                                 ch.invokeEvent("dragover");
  252.                                 return true;
  253.                         }else if (ch._isDragOver) {
  254.                                 ch._isDragOver=false;
  255.                                 ch.invokeEvent("dragout");
  256.                         }
  257.                 }
  258.         }
  259.         return false;
  260. };