Subversion Repositories wimsdev

Rev

Rev 20 | Go to most recent revision | 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.  
  59.         //dlyr._dyndoc.addEventListener(DragEvent.docListener);
  60.  
  61.         dlyr.invokeEvent("dragstart",de);
  62. }
  63.  
  64. DragEvent.docListener = {
  65.         onmousemove : function(e) {
  66.                 //var x = e.getPageX();
  67.                 //var y = e.getPageY();
  68.                 //dynapi.debug.status('drag move '+e.x+' '+e.y);
  69.  
  70.                 var de = DragEvent.dragevent;
  71.                 if (de && de.isDragging) {
  72.                         var lyr = de.src;
  73.                         if (!lyr) return;
  74.  
  75.                         // DS: what is this?
  76.                         // Detect if we should start the drag
  77.                         /*if(DragEvent.dragPlay==0 || (Math.abs(de.pageX-e.getPageX())-DragEvent.dragPlay>0) || (Math.abs(de.pageY-e.getPageY())-DragEvent.dragPlay>0)) {
  78.                                 de.isDragging=true;
  79.                                 de.src.invokeEvent("dragstart",de);
  80.                                 e.setBubble(de.bubble);
  81.                         }
  82.                         */
  83.                         /*else if (!de.dragEnabled) {
  84.                                 // This allows 'cancelDrag' method to fire the mouseUp as if had been released by the user
  85.                                 lyr.invokeEvent("mouseup");
  86.                                 return;
  87.                         }*/
  88.  
  89.                         // Properties
  90.                         de.type="dragmove";
  91.                         de.pageX=e.getPageX();
  92.                         de.pageY=e.getPageY();
  93.  
  94.                         /*if (DragEvent.stopAtDocumentEdge) {
  95.                                 if (de.pageX<0) de.pageX = 0;
  96.                                 if (de.pageY<0) de.pageY = 0;
  97.                                 if (de.pageX>DynAPI.document.w) de.pageX = DynAPI.document.w;
  98.                                 if (de.pageY>DynAPI.document.h) de.pageY = DynAPI.document.h;
  99.                         }*/
  100.  
  101.                         var x=de.pageX-de.parentPageX-de.x;
  102.                         var y=de.pageY-de.parentPageY-de.y;
  103.  
  104.                         // Respect boundary, if any
  105.                         if (lyr._dragBoundary) {
  106.                                 var dB = lyr._dragBoundary;
  107.                                 var t = dB.top;
  108.                                 var r = dB.right;
  109.                                 var b = dB.bottom;
  110.                                 var l = dB.left
  111.                                 if (x<l) x = l;
  112.                                 else if (x>lyr.parent.w-lyr.w-r) x = lyr.parent.w-lyr.w-r;
  113.                                 if (y<t) y = t;
  114.                                 else if (y>lyr.parent.h-lyr.h-b) y = lyr.parent.h-lyr.h-b;
  115.                         }
  116.                         else if (lyr._dragBoundaryA) {
  117.                                 var dB = lyr._dragBoundaryA;
  118.                                 var b=dB[2];
  119.                                 var r=dB[1];
  120.                                 var l=dB[3];
  121.                                 var t=dB[0];
  122.                                 var w=lyr.w;
  123.                                 var h=lyr.h;
  124.                                 if (x<l) x=l;
  125.                                 else if (x+w>r) x=r-w;
  126.                                 if (y<t) y=t;
  127.                                 else if (y+h>b) y=b-h;
  128.                         }
  129.                         // Move dragged layer
  130.                         lyr.setLocation(x,y);
  131.                         lyr.invokeEvent("dragmove",de);
  132.  
  133.                         if (lyr._dragStealth==false && lyr.parent.DragOver) {
  134.                                 lyr.parent.DragOver(lyr);
  135.                         }
  136.  
  137.                         e.preventDefault();
  138.                         e.preventBubble();
  139.                 }
  140.         },
  141.         onmouseup : function(e) {
  142.                 // Get, if any, the currently drag in process and the layer. If none, return
  143.                 var de=DragEvent.dragevent;
  144.                 //de.bubble = true;
  145.                 if (!de) return;
  146.                 var lyr=de.src;
  147.                 if (!lyr) return;
  148.  
  149.                 if (!de.isDragging) {
  150.                 de.type="dragend";
  151.                 de.src=null;
  152.                 //e.setBubble(true);
  153.                         return;
  154.                 }
  155.                 if (dynapi.ua.ie) lyr.doc.body.onselectstart = null;
  156.  
  157.                 // Avoid click for the dragged layer ( with MouseEvent addition )
  158.                 if (dynapi.ua.def) dynapi.wasDragging=true;
  159.                 if (lyr.parent.DragDrop) lyr.parent.DragDrop(lyr);
  160.                 // Properties for the event
  161.                 de.type="dragend";
  162.                 de.isDragging=false;
  163.                 lyr.invokeEvent("dragend",de);
  164.  
  165.                 // Clean drag stuff
  166.                 de.src=null;
  167.                 //e.preventDefault();
  168.                 e.preventBubble();
  169.  
  170.                 //lyr._dyndoc.removeEventListener(DragEvent.docListener);
  171.         }
  172. };
  173. DragEvent.stopAtDocumentEdge = true;
  174. DragEvent.setDragBoundary=function(lyr,t,r,b,l) {
  175.         if (!lyr) {dynapi.debug.print("Error: no object passed to DragEvent.setDragBoundary()"); return;}
  176.         var a=arguments;
  177.         if (a.length==0) return;
  178.         if (a.length==1) {
  179.                 lyr._dragBoundary = {left:0,right:0,top:0,bottom:0};
  180.         }
  181.         if (a.length==2) {
  182.                 lyr._dragBoundary = arguments[1];
  183.         }
  184.         else if (a.length==5) lyr._dragBoundaryA = [t,r,b,l];
  185. };
  186. DragEvent.enableDragEvents=function() {
  187.         for (var i=0;i<arguments.length;i++) {
  188.                 var lyr=arguments[i];
  189.                 if (!lyr) {dynapi.debug.print("Error: no object passed to DragEvent.enableDragEvents()"); return;}
  190.                 if (lyr.isClass('DynLayer')) lyr.addEventListener(DragEvent.lyrListener);
  191.         }
  192.         dynapi.document.addEventListener(DragEvent.docListener);
  193.         dynapi.document.captureMouseEvents();
  194. };
  195. DragEvent.disableDragEvents=function() {
  196.         for (var i=0;i<arguments.length;i++) {
  197.                 var lyr=arguments[i];
  198.                 lyr.removeEventListener(DragEvent.lyrListener);
  199.         }
  200. };
  201.  
  202. DynLayer.setDragMode = function(b,boundry){
  203.         if(!self.DragEvent) return false;
  204.         if(boundry)DragEvent.setDragBoundary(this,boundry);
  205.         if (b) DragEvent.enableDragEvents(this);
  206.         else DragEvent.disableDragEvents(this);
  207.         return true;
  208. };
  209. DynLayer.prototype.setDragOverStealthMode = function(b){
  210.         this._dragStealth=(b)? true:false;
  211. };
  212. // Enable ondrop event
  213. DynElement.prototype.DragDrop=function(s){
  214.         if (!this.children.length>0) return false;
  215.         var ch,chX,sX,sY;
  216.         for (var i in this.children) {
  217.                 ch=this.children[i];
  218.                 if(ch._hasDragEvents){
  219.                         chX=ch.getPageX();
  220.                         chY=ch.getPageY();
  221.                         sX=s.getPageX();
  222.                         sY=s.getPageY();
  223.                         if (chX<sX && chX+ch.w>sX+s.w && chY<sY && chY+ch.h>sY+s.h) {
  224.                                 if (ch.DragDrop(s)) return true;
  225.                                 ch.invokeEvent("drop");
  226.                                 return true;
  227.                         }
  228.                 }
  229.         }
  230.         return false;
  231. };
  232.  
  233. // Enable ondragover event
  234. DynElement.prototype.DragOver=function(s){
  235.         if (!this.children.length>0) return false;
  236.         var ch,chX,sX,sY;
  237.         for (var i in this.children) {
  238.                 ch=this.children[i];
  239.                 if(ch._hasDragEvents){
  240.                         chX=ch.getPageX();
  241.                         chY=ch.getPageY();
  242.                         sX=s.getPageX();
  243.                         sY=s.getPageY();
  244.                         if (chX<sX && chX+ch.w>sX+s.w && chY<sY && chY+ch.h>sY+s.h) {
  245.                                 if (ch.DragDrop(s)) return true;
  246.                                 ch._isDragOver=true;
  247.                                 ch.invokeEvent("dragover");
  248.                                 return true;
  249.                         }else if (ch._isDragOver) {
  250.                                 ch._isDragOver=false;
  251.                                 ch.invokeEvent("dragout");
  252.                         }
  253.                 }
  254.         }
  255.         return false;
  256. };