K_Reverter的网页开发记录

要么不做,要么就当作艺术品来做!

导航

Google Maps API 2.0解析(8-GDragPanel dragPoint用来支持拖动的类)


  
//本类是为那些需要通过鼠标拖动的层提供支持的,包括地图、缩放等级条等
  //参数分别为:
  // a 要支持拖动的层
  // b,c 要移动到的位置
  // 限制在某层的范围内拖动
  function GDragPanel(a,b,c,d)
  {
   
this.instance=a;
   
this.container=d;
   
this.disabled=false;
   
this.dragPoint=new GPoint(0,0);
   
this.dragging=false;
   
this.clickStartPos=new GPoint(0,0);
   setPositionAbsolute(a);
   
this.moveTo(isNumber(b)?b:a.offsetLeft,isNumber(c)?c:a.offsetTop);
   
this.onMouseDownAdapter=createAdapter(this,this.onMouseDown);
   
this.onMouseMoveAdapter=createAdapter(this,this.onMouseMove);
   
this.onMouseUpAdapter=createAdapter(this,this.onMouseUp);
   
if(browser.isFirefox())
   {
    bindDom(window,GEvent_mouseout,
this,this.onMouseOut)
   }
   
this.eventSrc=a.setCapture?a:window;
   addDomListener(a,GEvent_mousedown,
this.onMouseDownAdapter);
   bindDom(a,GEvent_mouseup,
this,this.onMouseUp);
   bindDom(a,GEvent_click,
this,this.onClick);
   bindDom(a,GEvent_dblclick,
this,this.onDblClick)
  }
  
//将该层移动到指定的像素位置
  GDragPanel.prototype.moveTo=function(a,b)
  {
   a
=getMathRound(a);
   b
=getMathRound(b);
   
if(this.left!=a||this.top!=b)
   {
    
this.left=a;
    
this.top=b;
    
var c=this.instance.style;
    c.left
=GetPixelValue(a);
    c.top
=GetPixelValue(b);
    trigger(
this,GEvent_move)
   }
  };
  
//触发双击事件
  GDragPanel.prototype.onDblClick=function(a)
  {
   trigger(
this,GEvent_dblclick,a)
  };
  
//触发单击事件,如果该层允许拖动,则不触发
  GDragPanel.prototype.onClick=function(a)
  {
   
if(this.disabled)
   {
    trigger(
this,GEvent_click,a)
   }
  };
  
//在层不允许拖动时,触发MouseUp事件
  GDragPanel.prototype.onMouseUp=function(a)
  {
   
if(this.disabled)
   {
    trigger(
this,GEvent_mouseup,a)
   }
  };
  GDragPanel.prototype.onMouseDown
=function(a)
  {
   trigger(
this,GEvent_mousedown,a);
   
if(a.cancelDrag)//这个我也不懂,网上也没有找到关于这个的信息
   {
    
return
   }
   
var b=a.button==0||a.button==1//中键单击部开始拖动进程
   if(this.disabled||!b)
   {
    PreventEventDefault(a);
    
return false
   }
   
this.dragPoint.x=a.clientX;
   
this.dragPoint.y=a.clientY;
   
this.dragging=true;
   
this.mouseMoveListener=addDomListener(this.eventSrc,GEvent_mousemove,this.onMouseMoveAdapter);
   
this.mouseUpListener=addDomListener(this.eventSrc,GEvent_mouseup,this.onMouseUpAdapter);
   
if(this.instance.setCapture)
   {
    
this.instance.setCapture()
   }
   
this.clickStartTime=(new Date()).getTime();//记录当前时间,用来做是否双击的判断
   this.clickStartPos.x=a.clientX;
   
this.clickStartPos.y=a.clientY;
   trigger(
this,GEvent_dragstart,a);
   
this.originalCursor=this.instance.style.cursor;
   setCursor(
this.instance,"move");
   PreventEventDefault(a)
  };
  
//鼠标拖动时的操作,请注意,Google为了提高拖动的性能,设置为每隔30毫秒执行一次操作
  //也就是说,假如30毫秒之中出现了多次鼠标移动事件,也只会执行一次移动的操作
  GDragPanel.prototype.onMouseMove=function(a)
  {
   
if(browser.os==0)
   {
    
if(a==null)
    {
     
return
    }
    
if(this.dragDisabled)//是否拖动操作被暂停
    {
     
this.savedMove=new Object();
     
this.savedMove.clientX=a.clientX;
     
this.savedMove.clientY=a.clientY;
     
return
    }
    setWindowTimeOut(
this,function(){this.dragDisabled=false;this.onMouseMove

(
this.savedMove)},30);//每隔30毫秒进行一次拖动操作
    this.dragDisabled=true;
    
this.savedMove=null
   }
   
var b=this.left+(a.clientX-this.dragPoint.x);
   
var c=this.top+(a.clientY-this.dragPoint.y);
   
var d=0;
   
var e=0;
   
var f=this.container;//假如设置了移动范围层,则将移动范围限制在其中
   if(f)
   {
    
var g=this.instance;
    
var h=GetMathMax(0,GetMathMin(b,f.offsetWidth-g.offsetWidth));
    d
=h-b;
    b
=h;
    
var i=GetMathMax(0,GetMathMin(c,f.offsetHeight-g.offsetHeight));
    e
=i-c;
    c
=i
   }
   
this.moveTo(b,c);
   
this.dragPoint.x=a.clientX+d;
   
this.dragPoint.y=a.clientY+e;
   trigger(
this,GEvent_drag,a)
  };
  
//终止拖动进程
  GDragPanel.prototype.onMouseUp=function(a)
  {
   trigger(
this,GEvent_mouseup,a);
   removeListener(
this.mouseMoveListener);
   removeListener(
this.mouseUpListener);
   
this.dragging=false;
   setCursor(
this.instance,this.originalCursor);//还原鼠标样式
   if(document.releaseCapture)
   {
    document.releaseCapture()
   }
   trigger(
this,GEvent_dragend,a);
   
//判断鼠标按下和释放的时间间隔和位置间隔,如果小于0.5秒并且距离在2以内,则认为是双击,触发双击事件
   var b=(new Date()).getTime();
   
if(b-this.clickStartTime<=500&&getMathAbs(this.clickStartPos.x-a.clientX)<=2&&getMathAbs

(
this.clickStartPos.y-a.clientY)<=2)
   {
    trigger(
this,GEvent_click,a)
   }
  };
  
//本方法是对FireFox进行支持
  GDragPanel.prototype.onMouseOut=function(a)
  {
   
if(!a.relatedTarget&&this.dragging)
   {
    
this.onMouseUp(a)
   }
  };
  GDragPanel.prototype.disable
=function()
  {
   
this.disabled=true
  };
  GDragPanel.prototype.enable
=function()
  {
   
this.disabled=false
  };
  GDragPanel.prototype.enabled
=function()
  {
   
return!this.disabled
  };

        这个类和上一个版本没有太大的区别

posted on 2006-05-11 20:15  K_Reverter  阅读(704)  评论(0编辑  收藏  举报