home *** CD-ROM | disk | FTP | other *** search
/ .net (Poland) 2000 September / Magazyn_Net_09_2000 (CDB).iso / offline / R1 / lib / dhtml / dynlayer.js
Encoding:
JavaScript  |  2000-10-12  |  6.8 KB  |  220 lines

  1. // Dynamic Layer Object - 19990604
  2. // Copyright (C) 1999 Dan Steinman
  3. // Distributed under the terms of the GNU Library General Public License
  4. // Available at http://www.dansteinman.com/dynduo/
  5.  
  6. function DynLayer(id,nestref,frame) {
  7.     if (!DynLayer.set && !frame) DynLayerInit()
  8.     this.frame = frame || self
  9.     if (is.ns) {
  10.         if (is.ns4) {
  11.             if (!frame) {
  12.                 if (!nestref) var nestref = DynLayer.nestRefArray[id]
  13.                 if (!DynLayerTest(id,nestref)) return
  14.                 this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
  15.             }
  16.             else this.css = (nestref)? eval("frame.document."+nestref+".document."+id) : frame.document.layers[id]
  17.             this.elm = this.event = this.css
  18.             this.doc = this.css.document
  19.         }
  20.         if (is.ns5) {
  21.             this.elm = document.getElementById(id)
  22.             this.css = this.elm.style
  23.             this.doc = document
  24.         }
  25.         this.x = this.css.left
  26.         this.y = this.css.top
  27.         this.w = this.css.clip.width
  28.         this.h = this.css.clip.height
  29.     }
  30.     else if (is.ie) {
  31.         this.elm = this.event = this.frame.document.all[id]
  32.         this.css = this.frame.document.all[id].style
  33.         this.doc = document
  34.         this.x = this.elm.offsetLeft
  35.         this.y = this.elm.offsetTop
  36.         this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
  37.         this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
  38.     }
  39.     this.id = id
  40.     this.nestref = nestref
  41.     this.obj = id + "DynLayer"
  42.     eval(this.obj + "=this")
  43. }
  44. function DynLayerMoveTo(x,y) {
  45.     if (x!=null) {
  46.         this.x = x
  47.         if (is.ns) this.css.left = this.x
  48.         else this.css.pixelLeft = this.x
  49.     }
  50.     if (y!=null) {
  51.         this.y = y
  52.         if (is.ns) this.css.top = this.y
  53.         else this.css.pixelTop = this.y
  54.     }
  55. }
  56. function DynLayerMoveBy(x,y) {
  57.     this.moveTo(this.x+x,this.y+y)
  58. }
  59. function DynLayerShow() {
  60.     this.css.visibility = (is.ns)? "show" : "visible"
  61. }
  62. function DynLayerHide() {
  63.     this.css.visibility = (is.ns)? "hide" : "hidden"
  64. }
  65. DynLayer.prototype.moveTo = DynLayerMoveTo
  66. DynLayer.prototype.moveBy = DynLayerMoveBy
  67. DynLayer.prototype.show = DynLayerShow
  68. DynLayer.prototype.hide = DynLayerHide
  69. DynLayerTest = new Function('return true')
  70.  
  71. function DynLayerInit(nestref) {
  72.     if (!DynLayer.set) DynLayer.set = true
  73.     if (is.ns) {
  74.         if (nestref) ref = eval('document.'+nestref+'.document')
  75.         else {nestref = ''; ref = document;}
  76.         for (var i=0; i<ref.layers.length; i++) {
  77.             var divname = ref.layers[i].name
  78.             DynLayer.nestRefArray[divname] = nestref
  79.             var index = divname.indexOf("Div")
  80.             if (index > 0) {
  81.                 eval(divname.substr(0,index)+' = new DynLayer("'+divname+'","'+nestref+'")')
  82.             }
  83.             if (ref.layers[i].document.layers.length > 0) {
  84.                 DynLayer.refArray[DynLayer.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
  85.             }
  86.         }
  87.         if (DynLayer.refArray.i < DynLayer.refArray.length) {
  88.             DynLayerInit(DynLayer.refArray[DynLayer.refArray.i++])
  89.         }
  90.     }
  91.     else if (is.ie) {
  92.         for (var i=0; i<document.all.tags("DIV").length; i++) {
  93.             var divname = document.all.tags("DIV")[i].id
  94.             var index = divname.indexOf("Div")
  95.             if (index > 0) {
  96.                 eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
  97.             }
  98.         }
  99.     }
  100.     return true
  101. }
  102. DynLayer.nestRefArray = new Array()
  103. DynLayer.refArray = new Array()
  104. DynLayer.refArray.i = 0
  105. DynLayer.set = false
  106.  
  107. function DynLayerSlideTo(endx,endy,inc,speed,fn) {
  108.     if (endx==null) endx = this.x
  109.     if (endy==null) endy = this.y
  110.     var distx = endx-this.x
  111.     var disty = endy-this.y
  112.     this.slideStart(endx,endy,distx,disty,inc,speed,fn)
  113. }
  114. function DynLayerSlideBy(distx,disty,inc,speed,fn) {
  115.     var endx = this.x + distx
  116.     var endy = this.y + disty
  117.     this.slideStart(endx,endy,distx,disty,inc,speed,fn)
  118. }
  119. function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
  120.     if (this.slideActive) return
  121.     if (!inc) inc = 10
  122.     if (!speed) speed = 20
  123.     var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
  124.     if (num==0) return
  125.     var dx = distx/num
  126.     var dy = disty/num
  127.     if (!fn) fn = null
  128.     this.slideActive = true
  129.     this.slide(dx,dy,endx,endy,num,1,speed,fn)
  130. }
  131. function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
  132.     if (!this.slideActive) return
  133.     if (i++ < num) {
  134.         this.moveBy(dx,dy)
  135.         this.onSlide()
  136.         if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
  137.         else this.onSlideEnd()
  138.     }
  139.     else {
  140.         this.slideActive = false
  141.         this.moveTo(endx,endy)
  142.         this.onSlide()
  143.         this.onSlideEnd()
  144.         eval(fn)
  145.     }
  146. }
  147. DynLayerSlideInit = new Function()
  148. DynLayer.prototype.slideInit = new Function()
  149. DynLayer.prototype.slideTo = DynLayerSlideTo
  150. DynLayer.prototype.slideBy = DynLayerSlideBy
  151. DynLayer.prototype.slideStart = DynLayerSlideStart
  152. DynLayer.prototype.slide = DynLayerSlide
  153. DynLayer.prototype.onSlide = new Function()
  154. DynLayer.prototype.onSlideEnd = new Function()
  155.  
  156. function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
  157.     if (is.ie) {
  158.         if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
  159.         else if (is.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,0)
  160.     }
  161. }
  162. function DynLayerClipTo(t,r,b,l) {
  163.     if (t==null) t = this.clipValues('t')
  164.     if (r==null) r = this.clipValues('r')
  165.     if (b==null) b = this.clipValues('b')
  166.     if (l==null) l = this.clipValues('l')
  167.     if (is.ns) {
  168.         this.css.clip.top = t
  169.         this.css.clip.right = r
  170.         this.css.clip.bottom = b
  171.         this.css.clip.left = l
  172.     }
  173.     else if (is.ie) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
  174. }
  175. function DynLayerClipBy(t,r,b,l) {
  176.     this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
  177. }
  178. function DynLayerClipValues(which) {
  179.     if (is.ie) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
  180.     if (which=="t") return (is.ns)? this.css.clip.top : Number(clipv[0])
  181.     if (which=="r") return (is.ns)? this.css.clip.right : Number(clipv[1])
  182.     if (which=="b") return (is.ns)? this.css.clip.bottom : Number(clipv[2])
  183.     if (which=="l") return (is.ns)? this.css.clip.left : Number(clipv[3])
  184. }
  185. DynLayer.prototype.clipInit = DynLayerClipInit
  186. DynLayer.prototype.clipTo = DynLayerClipTo
  187. DynLayer.prototype.clipBy = DynLayerClipBy
  188. DynLayer.prototype.clipValues = DynLayerClipValues
  189.  
  190. function BrowserCheck() {
  191.     var os = navigator.appVersion
  192.     if (os.indexOf('Macintosh')>=0) this.mac=true
  193.     else this.mac=false
  194.     var b = navigator.appName
  195.     if (b=="Netscape") this.b = "ns"
  196.     else if (b=="Microsoft Internet Explorer") this.b = "ie"
  197.     else this.b = b
  198.     this.version = navigator.appVersion
  199.     this.v = parseInt(this.version)
  200.     this.ns = (this.b=="ns" && this.v>=4)
  201.     this.ns4 = (this.b=="ns" && this.v==4)
  202.     this.ns5 = (this.b=="ns" && this.v==5)
  203.     this.ie = (this.b=="ie" && this.v>=4)
  204.     this.ie4 = (this.version.indexOf('MSIE 4')>0)
  205.     this.ie5 = (this.version.indexOf('MSIE 5')>0)
  206.     this.min = (this.ns||this.ie)
  207. }
  208. is = new BrowserCheck()
  209.  
  210. function preload(imgObj,imgSrc) {
  211.     if (document.images) {
  212.         eval(imgObj+' = new Image()')
  213.         eval(imgObj+'.src = "'+imgSrc+'"')
  214.     }
  215. }
  216.  
  217. function DynLayerImg(imgName,imgObj) {
  218.     this.doc.images[imgName].src = eval(imgObj+'.src')
  219. }
  220. DynLayer.prototype.chgImg = DynLayerImg