Subversion Repositories wimsdev

Rev

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

  1. function Position(x, y)
  2. {
  3.   this.X = x;
  4.   this.Y = y;
  5.  
  6.   this.Add = function(val)
  7.   {
  8.     var newPos = new Position(this.X, this.Y);
  9.     if(val !== null)
  10.     {
  11.       if(!isNaN(val.X))
  12.         newPos.X += val.X;
  13.       if(!isNaN(val.Y))
  14.         newPos.Y += val.Y;
  15.     }
  16.     return newPos;
  17.   };
  18.  
  19.   this.Subtract = function(val)
  20.   {
  21.     var newPos = new Position(this.X, this.Y);
  22.     if(val !== null)
  23.     {
  24.       if(!isNaN(val.X))
  25.         newPos.X -= val.X;
  26.       if(!isNaN(val.Y))
  27.         newPos.Y -= val.Y;
  28.     }
  29.     return newPos;
  30.   };
  31.  
  32.   this.Min = function(val)
  33.   {
  34.     var newPos = new Position(this.X, this.Y);
  35.     if(val === null)
  36.       return newPos;
  37.  
  38.     if(!isNaN(val.X) && this.X > val.X)
  39.       newPos.X = val.X;
  40.     if(!isNaN(val.Y) && this.Y > val.Y)
  41.       newPos.Y = val.Y;
  42.  
  43.     return newPos;
  44.   };
  45.  
  46.   this.Max = function(val)
  47.   {
  48.     var newPos = new Position(this.X, this.Y);
  49.     if(val === null)
  50.       return newPos;
  51.  
  52.     if(!isNaN(val.X) && this.X < val.X)
  53.       newPos.X = val.X;
  54.     if(!isNaN(val.Y) && this.Y < val.Y)
  55.       newPos.Y = val.Y;
  56.  
  57.     return newPos;
  58.   };
  59.  
  60.   this.Bound = function(lower, upper)
  61.   {
  62.     var newPos = this.Max(lower);
  63.     return newPos.Min(upper);
  64.   };
  65.  
  66.   this.Check = function()
  67.   {
  68.     var newPos = new Position(this.X, this.Y);
  69.     if(isNaN(newPos.X))
  70.       newPos.X = 0;
  71.     if(isNaN(newPos.Y))
  72.       newPos.Y = 0;
  73.     return newPos;
  74.   };
  75.  
  76.   this.Apply = function(element)
  77.   {
  78.     if(typeof(element) == "string")
  79.       element = document.getElementById(element);
  80.     if(element === null)
  81.       return;
  82.     if(!isNaN(this.X))
  83.       element.style.left = this.X + 'px';
  84.     if(!isNaN(this.Y))
  85.       element.style.top = this.Y + 'px';
  86.   };
  87. }
  88.  
  89. function hookEvent(element, eventName, callback)
  90. {
  91.   if(typeof(element) == "string")
  92.     element = document.getElementById(element);
  93.   if(element === null)
  94.     return;
  95.   if(element.addEventListener)
  96.   {
  97.     element.addEventListener(eventName, callback, false);
  98.   }
  99.   else if(element.attachEvent)
  100.     element.attachEvent("on" + eventName, callback);
  101. }
  102.  
  103. function unhookEvent(element, eventName, callback)
  104. {
  105.   if(typeof(element) == "string")
  106.     element = document.getElementById(element);
  107.   if(element === null)
  108.     return;
  109.   if(element.removeEventListener)
  110.     element.removeEventListener(eventName, callback, false);
  111.   else if(element.detachEvent)
  112.     element.detachEvent("on" + eventName, callback);
  113. }
  114.  
  115. function cancelEvent(e)
  116. {
  117.   e = e ? e : window.event;
  118.   if(e.stopPropagation)
  119.     e.stopPropagation();
  120.   if(e.preventDefault)
  121.     e.preventDefault();
  122.   e.cancelBubble = true;
  123.   e.cancel = true;
  124.   e.returnValue = false;
  125.   return false;
  126. }
  127.  
  128. function getMousePos(eventObj)
  129. {
  130.   eventObj = eventObj ? eventObj : window.event;
  131.   var pos;
  132.   if(isNaN(eventObj.layerX))
  133.     pos = new Position(eventObj.offsetX, eventObj.offsetY);
  134.   else
  135.     pos = new Position(eventObj.layerX, eventObj.layerY);
  136.   return correctOffset(pos, pointerOffset, true);
  137. }
  138.  
  139. function getEventTarget(e)
  140. {
  141.   e = e ? e : window.event;
  142.   return e.target ? e.target : e.srcElement;
  143. }
  144.  
  145. function absoluteCursorPostion(eventObj)
  146. {
  147.   eventObj = eventObj ? eventObj : window.event;
  148.  
  149.   if(isNaN(window.scrollX))
  150.     return new Position(eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft,
  151.       eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
  152.   else
  153.     return new Position(eventObj.clientX + window.scrollX, eventObj.clientY + window.scrollY);
  154. }
  155.  
  156. function dragObject(element, attachElement, lowerBound, upperBound, startCallback, moveCallback, endCallback, attachLater)
  157. {
  158.   if(typeof(element) == "string")
  159.     element = document.getElementById(element);
  160.   if(element === null)
  161.       return;
  162.  
  163.   if(lowerBound !== null && upperBound !== null)
  164.   {
  165.     var temp = lowerBound.Min(upperBound);
  166.     upperBound = lowerBound.Max(upperBound);
  167.     lowerBound = temp;
  168.   }
  169.  
  170.   var cursorStartPos = null;
  171.   var elementStartPos = null;
  172.   var dragging = false;
  173.   var listening = false;
  174.   var disposed = false;
  175.  
  176.   function dragStart(eventObj)
  177.   {
  178.     if(dragging || !listening || disposed) return;
  179.     dragging = true;
  180.  
  181.     if(startCallback !== null)
  182.       startCallback(eventObj, element);
  183.  
  184.     cursorStartPos = absoluteCursorPostion(eventObj);
  185.  
  186.     elementStartPos = new Position(parseInt(element.style.left,10), parseInt(element.style.top,10));
  187.  
  188.     elementStartPos = elementStartPos.Check();
  189.  
  190.     hookEvent(document, "mousemove", dragGo);
  191.     hookEvent(document, "mouseup", dragStopHook);
  192.  
  193.     return cancelEvent(eventObj);
  194.   }
  195.  
  196.   function dragGo(eventObj)
  197.   {
  198.     if(!dragging || disposed) return;
  199.  
  200.     var newPos = absoluteCursorPostion(eventObj);
  201.     newPos = newPos.Add(elementStartPos).Subtract(cursorStartPos);
  202.     newPos = newPos.Bound(lowerBound, upperBound);
  203.     newPos.Apply(element);
  204.     if(moveCallback !== null)
  205.       moveCallback(newPos, element);
  206.  
  207.     return cancelEvent(eventObj);
  208.   }
  209.  
  210.   function dragStopHook(eventObj)
  211.   {
  212.     dragStop();
  213.     return cancelEvent(eventObj);
  214.   }
  215.  
  216.   function dragStop()
  217.   {
  218.     if(!dragging || disposed) return;
  219.     unhookEvent(document, "mousemove", dragGo);
  220.     unhookEvent(document, "mouseup", dragStopHook);
  221.     cursorStartPos = null;
  222.     elementStartPos = null;
  223.     if(endCallback !== null)
  224.       endCallback(element);
  225.     dragging = false;
  226.   }
  227.  
  228.   this.Dispose = function()
  229.   {
  230.     if(disposed) return;
  231.     this.StopListening(true);
  232.     element = null;
  233.     attachElement = null;
  234.     lowerBound = null;
  235.     upperBound = null;
  236.     startCallback = null;
  237.     moveCallback = null;
  238.     endCallback = null;
  239.     disposed = true;
  240.   };
  241.  
  242.   this.StartListening = function()
  243.   {
  244.     if(listening || disposed) return;
  245.     listening = true;
  246.     hookEvent(attachElement, "mousedown", dragStart);
  247.   };
  248.  
  249.   this.StopListening = function(stopCurrentDragging)
  250.   {
  251.     if(!listening || disposed) return;
  252.     unhookEvent(attachElement, "mousedown", dragStart);
  253.     listening = false;
  254.  
  255.     if(stopCurrentDragging && dragging)
  256.       dragStop();
  257.   };
  258.  
  259.   this.IsDragging = function(){ return dragging; };
  260.   this.IsListening = function() { return listening; };
  261.   this.IsDisposed = function() { return disposed; };
  262.  
  263.   if(typeof(attachElement) == "string")
  264.     attachElement = document.getElementById(attachElement);
  265.   if(attachElement === null)
  266.     attachElement = element;
  267.  
  268.   if(!attachLater)
  269.     this.StartListening();
  270. }
  271.