home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 February / Chip_2003-02_cd1.bin / firmy / acomw / js / dynlayer.js < prev   
Text File  |  2002-11-29  |  8KB  |  249 lines

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