home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 May / comcd0502.iso / homepage / special / javascript / 01_01 / Java / pinwheel / xLayer.js < prev    next >
Encoding:
JavaScript  |  2000-11-12  |  10.3 KB  |  300 lines

  1. /******************************************************************* 
  2. * File    : xLayer.js 
  3. * Created : 2000/06/08 
  4. * Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com 
  5. * Purpose : To create a cross browser dynamic layers. This 
  6. *        library is based on the library defined in the 
  7. *        excellent book. "JavasScript - The Definitive guide" 
  8. *        by David Flanagan. Published by O'Reilly. 
  9. *        ISBN 1-56592-392-8 
  10. * History 
  11. * Date         Version        Description 
  12. * 2000-06-08    1.0        Initial version 
  13. * 2000-06-17    1.1        Changed function name to setzIndex()
  14. * 2000-06-17    1.2        Changed function name to getzIndex()
  15. * 2000-08-07    1.3        Added the event handling functionality
  16. *                    from the book.
  17. * 2000-08-15    1.4        Finally! The NS functions are now prototypes.
  18. * 2000-10-14    1.5        Attempting to add NS6 (W3C Standard) functionality
  19. * 2000-11-04    1.6        Added NS6 Event handling
  20. * 2000-11-06    1.7        Added xLayerFromObj - Allows pre existing 
  21. *                    layers/divs to gain the functionality of xLayer.
  22. ***********************************************************************/ 
  23. var xLayerNo=0; 
  24. function xLayer(newLayer, x, y, w) 
  25. {
  26.     if(x==null)x=0; 
  27.     if(y==null)y=0; 
  28.     if(w==null)w=100; 
  29.      if(document.layers) 
  30.     {
  31.         if(typeof newLayer == "string")
  32.         {
  33.             this.layer=new Layer(w); 
  34.             this.layer.document.open(); 
  35.             this.layer.document.write(newLayer); 
  36.             this.layer.document.close(); 
  37.         }
  38.         else
  39.             this.layer=newLayer;
  40.  
  41.         this.layer.moveTo(x,y); 
  42.         this.images=this.layer.document.images; 
  43.      } 
  44.     else 
  45.     if(document.all) 
  46.     {
  47.         var Xname;
  48.         if(typeof newLayer == "string")
  49.         {
  50.             xName="xLayer" + xLayerNo++; 
  51.              var txt =   "<DIV ID='" + xName 
  52.                 + "' STYLE=\"position:absolute;" 
  53.                 + "left:"  + x + ";" 
  54.                 + "top:"   + y + ";" 
  55.                 + "width:" + w + ";" 
  56.                 + "visibility:hidden\">" 
  57.                 + newLayer 
  58.                  + "</DIV>"; 
  59.  
  60.             document.body.insertAdjacentHTML("BeforeEnd",txt); 
  61.         }
  62.         else
  63.             xName=newLayer.id;
  64.  
  65.         this.content = document.all[xName]; 
  66.         this.layer   = document.all[xName].style; 
  67.         this.images  = document.images; 
  68.     } 
  69.     else 
  70.     if (document.getElementById)
  71.     {
  72.         /*** Add Netscape 6.0 support (NOTE: This may work in I.E. 5+. Will have to test***/
  73.  
  74.         var newDiv;
  75.  
  76.         if(typeof newLayer == "string")
  77.         {
  78.             var xName="xLayer" + xLayerNo++;
  79.  
  80.             var txt = ""
  81.                 + "position:absolute;"
  82.                 + "left:"  + x + "px;"
  83.                 + "top:"   + y + "px;"
  84.                 + "width:" + w + "px;"
  85.                 + "visibility:hidden";
  86.  
  87.             var newRange   = document.createRange();
  88.  
  89.             newDiv = document.createElement("DIV");
  90.             newDiv.setAttribute("style",txt);
  91.             newDiv.setAttribute("id", xName);
  92.  
  93.             document.body.appendChild(newDiv);
  94.  
  95.             newRange.setStartBefore(newDiv);
  96.             strFrag = newRange.createContextualFragment(newLayer);    
  97.             newDiv.appendChild(strFrag);
  98.         }
  99.         else
  100.             newDiv = newLayer;
  101.  
  102.         this.content = newDiv;    
  103.         this.layer   = newDiv.style;
  104.         this.images  = document.images;
  105.     }
  106.  
  107.     return(this); 
  108.  
  109. function xLayerFromObj(theObj)
  110. {
  111.     if(document.layers)
  112.         return(new xLayer(document.layers[theObj]));
  113.     else 
  114.     if(document.all)
  115.         return(new xLayer(document.all[theObj]));
  116.     else 
  117.     if(document.getElementById)
  118.         return(new xLayer(document.getElementById(theObj)));
  119. }
  120.  
  121. if(navigator.appName.indexOf("Netscape") != -1 && !document.getElementById)
  122. {
  123. var eventmasks = {
  124.       onabort:Event.ABORT, onblur:Event.BLUR, onchange:Event.CHANGE,
  125.       onclick:Event.CLICK, ondblclick:Event.DBLCLICK, 
  126.       ondragdrop:Event.DRAGDROP, onerror:Event.ERROR, 
  127.       onfocus:Event.FOCUS, onkeydown:Event.KEYDOWN,
  128.       onkeypress:Event.KEYPRESS, onkeyup:Event.KEYUP, onload:Event.LOAD,
  129.       onmousedown:Event.MOUSEDOWN, onmousemove:Event.MOUSEMOVE, 
  130.       onmouseout:Event.MOUSEOUT, onmouseover:Event.MOUSEOVER, 
  131.       onmouseup:Event.MOUSEUP, onmove:Event.MOVE, onreset:Event.RESET,
  132.       onresize:Event.RESIZE, onselect:Event.SELECT, onsubmit:Event.SUBMIT,
  133.       onunload:Event.UNLOAD
  134. };
  135.  
  136. /**** START prototypes for NS ***/ 
  137. xLayer.prototype.moveTo     = function(x,y)     { this.layer.moveTo(x,y); }
  138. xLayer.prototype.moveBy     = function(x,y)     { this.layer.moveBy(x,y); }
  139. xLayer.prototype.show        = function()     { this.layer.visibility = "show"; }
  140. xLayer.prototype.hide         = function()     { this.layer.visibility = "hide"; }
  141. xLayer.prototype.setzIndex    = function(z)    { this.layer.zIndex = z; }
  142. xLayer.prototype.setBgColor     = function(color) { this.layer.bgColor = color; }
  143. xLayer.prototype.setBgImage     = function(image) { this.layer.background.src = image; }
  144. xLayer.prototype.getX         = function()     { return this.layer.left; }
  145. xLayer.prototype.getY         = function()     { return this.layer.top; }
  146. xLayer.prototype.getWidth     = function()     { return this.layer.width; }
  147. xLayer.prototype.getHeight     = function()     { return this.layer.height; }
  148. xLayer.prototype.getzIndex    = function()    { return this.layer.zIndex; }
  149. xLayer.prototype.isVisible     = function()     { return this.layer.visibility == "show"; }
  150. xLayer.prototype.setContent   = function(xHtml)
  151. {
  152.     this.layer.document.open();
  153.     this.layer.document.write(xHtml);
  154.     this.layer.document.close();
  155. }
  156.  
  157. xLayer.prototype.clip = function(x1,y1, x2,y2)
  158. {
  159.     this.layer.clip.top    =y1;
  160.     this.layer.clip.left    =x1;
  161.     this.layer.clip.bottom    =y2;
  162.     this.layer.clip.right    =x2;
  163. }
  164. xLayer.prototype.addEventHandler = function(eventname, handler) 
  165. {
  166.         this.layer.captureEvents(eventmasks[eventname]);
  167.         var xl = this;
  168.         this.layer[eventname] = function(event) { 
  169.             return handler(xl, event.type, event.x, event.y, 
  170.                            event.which, event.which,
  171.                            ((event.modifiers & Event.SHIFT_MASK) != 0),
  172.                            ((event.modifiers & Event.CTRL_MASK) != 0),
  173.                            ((event.modifiers & Event.ALT_MASK) != 0));
  174.         }
  175. }
  176.  
  177. /*** END NS ***/ 
  178. else if(document.all) 
  179. /*** START prototypes for IE ***/ 
  180. xLayer.prototype.moveTo = function(x,y) 
  181.     this.layer.pixelLeft = x; 
  182.     this.layer.pixelTop = y; 
  183. xLayer.prototype.moveBy = function(x,y) 
  184.     this.layer.pixelLeft += x; 
  185.     this.layer.pixelTop += y; 
  186. xLayer.prototype.show        = function()     { this.layer.visibility = "visible"; } 
  187. xLayer.prototype.hide        = function()     { this.layer.visibility = "hidden"; } 
  188. xLayer.prototype.setzIndex    = function(z)    { this.layer.zIndex = z; } 
  189. xLayer.prototype.setBgColor    = function(color) { this.layer.backgroundColor = color; } 
  190. xLayer.prototype.setBgImage    = function(image) { this.layer.backgroundImage = image; } 
  191. xLayer.prototype.setContent   = function(xHtml)    { this.content.innerHTML=xHtml; } 
  192. xLayer.prototype.getX        = function()     { return this.layer.pixelLeft; } 
  193. xLayer.prototype.getY        = function()     { return this.layer.pixelTop; } 
  194. xLayer.prototype.getWidth    = function()     { return this.layer.pixelWidth; } 
  195. xLayer.prototype.getHeight    = function()     { return this.layer.pixelHeight; } 
  196. xLayer.prototype.getzIndex    = function()    { return this.layer.zIndex; } 
  197. xLayer.prototype.isVisible    = function()    { return this.layer.visibility == "visible"; } 
  198. xLayer.prototype.clip        = function(x1,y1, x2,y2) 
  199.     this.layer.clip="rect("+y1+" "+x2+" "+y2+" "+x1+")"; 
  200.     this.layer.pixelWidth=x2; 
  201.     this.layer.pixelHeight=y2; 
  202.     this.layer.overflow="hidden"; 
  203. }
  204. xLayer.prototype.addEventHandler = function(eventName, handler) 
  205. {
  206.     var xl = this;
  207.     this.content[eventName] = function() 
  208.     { 
  209.             var e = window.event;
  210.             e.cancelBubble = true;
  211.             return handler(xl, e.type, e.x, e.y, 
  212.                            e.button, e.keyCode, 
  213.                            e.shiftKey, e.ctrlKey, e.altKey); 
  214.         }
  215. }
  216.  /*** END IE ***/ 
  217. else if (document.getElementById) 
  218. {
  219. /*** W3C (NS 6) ***/ 
  220. xLayer.prototype.moveTo = function(x,y)
  221. {
  222.     this.layer.left = x+"px";
  223.     this.layer.top = y+"px";
  224. }
  225. xLayer.prototype.moveBy     = function(x,y)     { this.moveTo(this.getX()+x, this.getY()+y); } 
  226. xLayer.prototype.show        = function()     { this.layer.visibility = "visible"; }
  227. xLayer.prototype.hide        = function()     { this.layer.visibility = "hidden"; }
  228. xLayer.prototype.setzIndex    = function(z)    { this.layer.zIndex = z; }
  229. xLayer.prototype.setBgColor    = function(color) { this.layer.backgroundColor = color; }
  230. xLayer.prototype.setBgImage    = function(image) { this.layer.backgroundImage = image; }
  231. xLayer.prototype.getX        = function()     { return parseInt(this.layer.left); }
  232. xLayer.prototype.getY        = function()     { return parseInt(this.layer.top); }
  233. xLayer.prototype.getWidth    = function()     { return parseInt(this.layer.width); }
  234. xLayer.prototype.getHeight    = function()     { return parseInt(this.layer.height); }
  235. xLayer.prototype.getzIndex    = function()    { return this.layer.zIndex; }
  236. xLayer.prototype.isVisible    = function()    { return this.layer.visibility == "visible"; }
  237. xLayer.prototype.clip        = function(x1,y1, x2,y2)
  238. {
  239.     this.layer.clip="rect("+y1+" "+x2+" "+y2+" "+x1+")";
  240.     this.layer.width=x2 + "px";
  241.     this.layer.height=y2+ "px";
  242.     this.layer.overflow="hidden";
  243. }
  244. xLayer.prototype.addEventHandler = function(eventName, handler) 
  245. {
  246.     var xl = this;
  247.     this.content[eventName] = function(e) 
  248.     { 
  249.             e.cancelBubble = true;
  250.             return handler(xl, e.type, e.pageX, e.pageY, 
  251.                            e.button, e.keyCode, 
  252.                            e.shiftKey, e.ctrlKey, e.altKey); 
  253.     }
  254. }
  255. xLayer.prototype.setContent   = function(xHtml)
  256. {
  257.     var newRange   = document.createRange();
  258.     newRange.setStartBefore(this.content);
  259.  
  260.     while (this.content.hasChildNodes())
  261.         this.content.removeChild(this.content.lastChild);
  262.  
  263.     var strFrag    = newRange.createContextualFragment(xHtml);    
  264.     this.content.appendChild(strFrag);
  265. }
  266.  
  267. } else
  268. {
  269. xLayer.prototype.moveTo     = function(x,y)     {  }
  270. xLayer.prototype.moveBy     = function(x,y)     {  }
  271. xLayer.prototype.show         = function()     {  }
  272. xLayer.prototype.hide         = function()     {  }
  273. xLayer.prototype.setzIndex    = function(z) {  }
  274. xLayer.prototype.setBgColor     = function(color) {  }
  275. xLayer.prototype.setBgImage     = function(image) {  }
  276. xLayer.prototype.getX         = function()     { return 0; }
  277. xLayer.prototype.getY         = function()     { return 0; }
  278. xLayer.prototype.getWidth     = function()     { return 0; }
  279. xLayer.prototype.getHeight     = function()     { return 0; }
  280. xLayer.prototype.getzIndex    = function()    { return 0; }
  281. xLayer.prototype.isVisible     = function()     { return false; }
  282. xlayer.prototype.setContent   = function(xHtml) { }
  283. }
  284.  
  285. /*** End  - xLayer - a cross browser layer object by www.Roy.Whittle.com ***/