NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/js/dynlayer.js

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
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  *
10  * @project     CWC2
11  * @revision    $Id: dynlayer.js,v 1.9 2004/04/21 17:56:58 pspencer Exp $
12  * @purpose     Used with PanMap widget
13  * @author      DM Solutions Group (assefa@dmsolutions.ca)
14  * @copyright
15  * <b>Copyright (c) 2002, DM Solutions Group Inc.</b>
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be included
24  * in all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
29  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32  * DEALINGS IN THE SOFTWARE.
33  */
34 function DynLayer(id,nestref,frame) {
35         //alert("DynaLayer");
36         if (!is.ns5 && !DynLayer.set && !frame) DynLayerInit()
37     this.frame = frame || self
38     if (is.ns) {
39         if (is.ns4) {
40             if (!frame) {
41                 if (!nestref) var nestref = DynLayer.nestRefArray[id]
42                 if (!DynLayerTest(id,nestref)) return
43                 this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
44             }
45             else
46                        {
47                             this.css = (nestref)? eval("frame.document."+nestref+".document."+id) : frame.document.layers[id];
48                        }
49             this.elm = this.event = this.css
50             this.doc = this.css.document
51                         this.x = this.css.left;
52                 this.y = this.css.top;
53                 this.w = this.css.clip.width;
54                 this.h = this.css.clip.height;
55         }
56         else if (is.ns5) {
57             this.elm = document.getElementById(id)
58             this.css = this.elm.style
59             this.doc = document
60        
61                 //this.x = this.css.left
62                 //this.y = this.css.top
63                 //this.w = this.css.clip.width
64                 //this.h = this.css.clip.height;
65                         this.x = CWCDHTML_FindObjectPosX( document.getElementById('mapFrameTL')) + 1;
66                 this.y = CWCDHTML_FindObjectPosY( document. getElementById('mapFrameTL')) + 1;
67                 this.w = gMapWiWidth;
68                 this.h = gMapWiHeight;
69                 }
70        
71     }
72     else if (is.ie) {
73         this.elm = this.event = this.frame.document.all[id]
74         this.css = this.frame.document.all[id].style
75         this.doc = document
76         this.x = this.elm.offsetLeft
77         this.y = this.elm.offsetTop
78         this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
79         this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
80     }
81     this.id = id
82     this.nestref = nestref
83     this.obj = id + "DynLayer"
84     eval(this.obj + "=this")
85 }
86 function DynLayerMoveTo(x,y) {
87     if (x!=null) {
88         this.x = x
89         if (is.ns) this.css.left = this.x
90         else this.css.pixelLeft = this.x
91     }
92     if (y!=null) {
93         this.y = y
94         if (is.ns) this.css.top = this.y
95         else this.css.pixelTop = this.y
96     }
97 }
98 function DynLayerMoveBy(x,y) {
99     this.moveTo(this.x+x,this.y+y)
100 }
101 function DynLayerShow() {
102     this.css.visibility = (is.ns4)? "show" : "visible"
103 }
104 function DynLayerHide() {
105     this.css.visibility = (is.ns4)? "hide" : "hidden"
106 }
107 DynLayer.prototype.moveTo = DynLayerMoveTo
108 DynLayer.prototype.moveBy = DynLayerMoveBy
109 DynLayer.prototype.show = DynLayerShow
110 DynLayer.prototype.hide = DynLayerHide
111 DynLayerTest = new Function('return true')
112
113 // DynLayerInit Function
114 function DynLayerInit(nestref) {
115         //alert("DynLayerInit");
116        
117     if (!DynLayer.set) DynLayer.set = true
118     if (is.ns) {
119         if (nestref) ref = eval('document.'+nestref+'.document')
120         else {nestref = ''; ref = document;}
121         for (var i=0; i<ref.layers.length; i++) {
122             var divname = ref.layers[i].name
123             DynLayer.nestRefArray[divname] = nestref
124             var index = divname.indexOf("Div")
125             if (index > 0) {
126        
127                 eval(divname.substr(0,index)+' = new DynLayer("'+divname+'","'+nestref+'")')
128             }
129             if (ref.layers[i].document.layers.length > 0) {
130                 DynLayer.refArray[DynLayer.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
131             }
132         }
133         if (DynLayer.refArray.i < DynLayer.refArray.length) {
134             DynLayerInit(DynLayer.refArray[DynLayer.refArray.i++])
135         }
136     }
137     else if (is.ie) {
138         for (var i=0; i<document.all.tags("DIV").length; i++) {
139             var divname = document.all.tags("DIV")[i].id
140             var index = divname.indexOf("Div")
141             if (index > 0) {
142                 eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
143             }
144         }
145     }
146     return true
147 }
148 DynLayer.nestRefArray = new Array()
149 DynLayer.refArray = new Array()
150 DynLayer.refArray.i = 0
151 DynLayer.set = false
152
153 // Slide Methods
154 function DynLayerSlideTo(endx,endy,inc,speed,fn) {
155     if (endx==null) endx = this.x
156     if (endy==null) endy = this.y
157     var distx = endx-this.x
158     var disty = endy-this.y
159     this.slideStart(endx,endy,distx,disty,inc,speed,fn)
160 }
161 function DynLayerSlideBy(distx,disty,inc,speed,fn) {
162     var endx = this.x + distx
163     var endy = this.y + disty
164     this.slideStart(endx,endy,distx,disty,inc,speed,fn)
165 }
166 function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
167     if (this.slideActive) return
168     if (!inc) inc = 10
169     if (!speed) speed = 20
170     var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
171     if (num==0) return
172     var dx = distx/num
173     var dy = disty/num
174     if (!fn) fn = null
175     this.slideActive = true
176     this.slide(dx,dy,endx,endy,num,1,speed,fn)
177 }
178 function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
179     if (!this.slideActive) return
180     if (i++ < num) {
181         this.moveBy(dx,dy)
182         this.onSlide()
183         if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
184         else this.onSlideEnd()
185     }
186     else {
187         this.slideActive = false
188         this.moveTo(endx,endy)
189         this.onSlide()
190         this.onSlideEnd()
191         eval(fn)
192     }
193 }
194 function DynLayerSlideInit() {}
195 DynLayer.prototype.slideInit = DynLayerSlideInit
196 DynLayer.prototype.slideTo = DynLayerSlideTo
197 DynLayer.prototype.slideBy = DynLayerSlideBy
198 DynLayer.prototype.slideStart = DynLayerSlideStart
199 DynLayer.prototype.slide = DynLayerSlide
200 DynLayer.prototype.onSlide = new Function()
201 DynLayer.prototype.onSlideEnd = new Function()
202
203 // Clip Methods
204 function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
205     if (is.ie) {
206         if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
207         else if (is.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,0)
208     }
209 }
210 function DynLayerClipTo(t,r,b,l) {
211     if (t==null) t = this.clipValues('t')
212     if (r==null) r = this.clipValues('r')
213     if (b==null) b = this.clipValues('b')
214     if (l==null) l = this.clipValues('l')
215     if (is.ns && !is.ns5) {
216         this.css.clip.top = t
217         this.css.clip.right = r
218         this.css.clip.bottom = b
219         this.css.clip.left = l
220     }
221     else if (is.ie) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
222         else if (is.ns5)
223         {
224              //CWCDHTML_ClipLayer( 'MapLayerDiv', l, t, r, b );
225              var layer = CWCDHTML_GetLayer('MapLayerDiv');
226              layer.clip = "rect("+t+"px,"+r+"px,"+b+"px,"+l+"px)";
227              //window.status = "rect("+t+"px,"+r+"px,"+b+"px,"+l+"px)";
228         }
229 }
230 function DynLayerClipBy(t,r,b,l) {
231     //window.status = "DynLayerClipBy";
232     this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
233 }
234 function DynLayerClipValues(which) {
235     if (is.ie || is.ns5)
236     {
237         var re1 = /,/gi;
238         var re2 = / /gi;
239         var l1 = this.css.clip.replace( re1, "" );
240         l1 = l1.replace( re2, "" );
241         var clipv = l1.split("rect(")[1].split(")")[0].split("px");
242     }
243    
244     if (which=="t") return (is.ns && !is.ns5)? this.css.clip.top : Number(clipv[0])
245     if (which=="r") return (is.ns && !is.ns5)? this.css.clip.right : Number(clipv[1])
246     if (which=="b") return (is.ns && !is.ns5)? this.css.clip.bottom : Number(clipv[2])
247     if (which=="l") return (is.ns && !is.ns5)? this.css.clip.left : Number(clipv[3])
248 }
249 DynLayer.prototype.clipInit = DynLayerClipInit
250 DynLayer.prototype.clipTo = DynLayerClipTo
251 DynLayer.prototype.clipBy = DynLayerClipBy
252 DynLayer.prototype.clipValues = DynLayerClipValues
253
254 // Write Method
255 function DynLayerWrite(html) {
256     if (is.ns) {
257         this.doc.open()
258         this.doc.write(html)
259         this.doc.close()
260     }
261     else if (is.ie) {
262         this.event.innerHTML = html
263     }
264 }
265 DynLayer.prototype.write = DynLayerWrite
266
267 // BrowserCheck Object
268 function BrowserCheck() {
269     var b = navigator.appName
270     if (b=="Netscape") this.b = "ns"
271     else if (b=="Microsoft Internet Explorer") this.b = "ie"
272     else this.b = b
273     this.version = navigator.appVersion
274         this.v = parseInt(this.version)
275     this.ns = (this.b=="ns" && this.v>=4)
276     this.ns4 = (this.b=="ns" && this.v==4)
277     this.ns5 = (this.b=="ns" && this.v==5)
278     this.ie = (this.b=="ie" && this.v>=4)
279     this.ie4 = (this.version.indexOf('MSIE 4')>0)
280     this.ie5 = (this.version.indexOf('MSIE 5')>0)
281     this.min = (this.ns||this.ie)
282 }
283 is = new BrowserCheck()
284
285 // CSS Function
286 function css(id,left,top,width,height,color,vis,z,other) {
287     if (id=="START") return '<STYLE TYPE="text/css">\n'
288     else if (id=="END") return '</STYLE>'
289     var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;'
290     if (arguments.length>=4 && width!=null) str += ' width:'+width+'px;'
291     if (arguments.length>=5 && height!=null) {
292         str += ' height:'+height+'px;'
293         if (arguments.length<9 || other.indexOf('clip')==-1) str += ' clip:rect(0px '+width+'px '+height+'px 0px);'
294     }
295     if (arguments.length>=6 && color!=null) str += (is.ns)? ' layer-background-color:'+color+';' : ' background-color:'+color+';'
296     if (arguments.length>=7 && vis!=null) str += ' visibility:'+vis+';'
297     if (arguments.length>=8 && z!=null) str += ' z-index:'+z+';'
298     if (arguments.length==9 && other!=null) str += ' '+other
299     str += '}\n'
300     return str
301 }
302 function writeCSS(str,showAlert) {
303     str = css('START')+str+css('END')
304     document.write(str)
305     if (showAlert) alert(str)
306 }
Note: See TracBrowser for help on using the browser.