home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 October / tst.iso / multimed / IDN / web / tapdogs / JS / Button.js next >
Encoding:
Text File  |  1999-09-24  |  2.6 KB  |  97 lines

  1. // Lemon (HK) Ltd
  2. // last modified on 30 July 1999
  3.  
  4. /*
  5. new event handler
  6.  onButtonMouseOver
  7.  onButtonMouseOut
  8.  onClick
  9.  onPress
  10.  onRelease
  11. */
  12.  
  13. __registered_Button = new Array();
  14. __preload_images = new Array();
  15. __onmouseup=null;
  16. __onmousemove=null;
  17. __button=null;
  18.  
  19. function Button(src,alt,speed) {
  20.   this.lyr = SimpleLayer;
  21.   this.lyr(src);
  22.   __registered_Button[__registered_Button.length] = this;
  23.  
  24.   this.glyph = getObject(src+".glyph");
  25.   this.img_off = this.glyph ? this.glyph.src : null;
  26.   this.img_on = alt;
  27.   if (this.img_on) {
  28.       var img = new Image();
  29.       __preload_images[__preload_images.length] = img;
  30.     img.src = this.img_on;
  31.     this.onMouseOver(ButtonMouseOver);
  32.     this.onMouseOut(ButtonMouseOut);
  33.   } 
  34.  
  35.   this.activate = ButtonActivate;  
  36.   this.deactivate = ButtonDeactivate;  
  37.   this.onMouseDown(ButtonMouseDown);
  38.   this.onButtonMouseOver=null;
  39.   this.onButtonMouseOut=null;
  40.   this.speed = speed ? speed : 100;
  41.   this.onClick=null;
  42.   this.onPress=null;
  43.   this.onRelease=null; 
  44.   this.enable=true;
  45. }
  46. function ButtonActivate() {
  47.   this.enable=true;
  48. }
  49. function ButtonDeactivate() {
  50.   this.enable=false;
  51. }
  52. function ButtonMouseDown(e) {
  53.   if (__button) return true;
  54.   __button = findObject(this, __registered_Button);
  55.  
  56.   if (NS) document.captureEvents(Event.MOUSEUP);
  57.   __onmouseup=document.onmouseup;
  58.   document.onmouseup=ButtonMouseUp;
  59.   if (IE) { 
  60.     __onmousemove=document.onmousemove;
  61.     document.onmousemove = ButtonMouseMove;
  62.   }
  63.   if (__button.onClick!=null) __button.onClick();
  64.   if (__button.onPress!=null) doPress();
  65.   return false;
  66. }
  67. function ButtonMouseUp(e) {
  68.   if (!__button) return true;
  69.   if (this.onRelease!=null) this.onRelease();
  70.   
  71.   if (NS && !__onmouseup) document.releaseEvents(Event.MOUSEUP);
  72.   document.onmouseup=__onmouseup;
  73.   if (IE) document.onmousemove=__onmousemove;
  74.  
  75.   if (__button.timeout) clearTimeout(__button.timeout);
  76.   __button.timeout=null;
  77.   __button=null;
  78.   return false;
  79. }
  80. function ButtonMouseMove() { return false; }
  81. function doPress() {
  82.   __button.onPress();
  83.   __button.timeout=setTimeout("doPress()",__button.speed);
  84. }
  85. function ButtonMouseOver() {
  86.   var b = findObject(this, __registered_Button);
  87.   if (b != null && b.enable == false) return;
  88.   if (b != null && b.glyph != null) b.glyph.src = b.img_on;
  89.   if (b.onButtonMouseOver!=null) b.onButtonMouseOver();
  90. }
  91. function ButtonMouseOut() {
  92.   var b = findObject(this, __registered_Button);
  93.   if (b != null && b.enable == false) return;
  94.   if (b != null && b.glyph != null) b.glyph.src = b.img_off;
  95.   if (b.onButtonMouseOut!=null) b.onButtonMouseOut();
  96. }
  97.