Merge des modifications du svn de Plinn r1524:1547.
[Plinn.git] / skins / ajax_scripts / DHTMLapi.js
1 /* ***********************************************************
2 Example 4-3 (DHTMLapi.js)
3 "Dynamic HTML:The Definitive Reference"
4 2nd Edition
5 by Danny Goodman
6 Published by O'Reilly & Associates ISBN 1-56592-494-0
7 http://www.oreilly.com
8 Copyright 2002 Danny Goodman. All Rights Reserved.
9 ************************************************************ */
10 // DHTMLapi.js custom API for cross-platform
11 // object positioning by Danny Goodman (http://www.dannyg.com).
12 // Release 2.0. Supports NN4, IE, and W3C DOMs.
13
14 // Global variables
15 var isCSS, isW3C, isIE4, isNN4;
16 // initialize upon load to let all browsers establish content objects
17 function initDHTMLAPI() {
18 if (document.images) {
19 isCSS = (document.body && document.body.style) ? true : false;
20 isW3C = (isCSS && document.getElementById) ? true : false;
21 isIE4 = (isCSS && document.all) ? true : false;
22 isNN4 = (document.layers) ? true : false;
23 isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
24 }
25 }
26 // set event handler to initialize API
27 //window.onload = initDHTMLAPI;
28 addListener(window, 'load', initDHTMLAPI);
29 //registerStartupFunction(initDHTMLAPI)
30
31 // Seek nested NN4 layer from string name
32 function seekLayer(doc, name) {
33 var theObj;
34 for (var i = 0; i < doc.layers.length; i++) {
35 if (doc.layers[i].name == name) {
36 theObj = doc.layers[i];
37 break;
38 }
39 // dive into nested layers if necessary
40 if (doc.layers[i].document.layers.length > 0) {
41 theObj = seekLayer(document.layers[i].document, name);
42 }
43 }
44 return theObj;
45 }
46
47 // Convert object name string or object reference
48 // into a valid element object reference
49 function getRawObject(obj) {
50 var theObj;
51 if (typeof obj == "string") {
52 if (isW3C) {
53 theObj = document.getElementById(obj);
54 } else if (isIE4) {
55 theObj = document.all(obj);
56 } else if (isNN4) {
57 theObj = seekLayer(document, obj);
58 }
59 } else {
60 // pass through object reference
61 theObj = obj;
62 }
63 return theObj;
64 }
65
66 // Convert object name string or object reference
67 // into a valid style (or NN4 layer) reference
68 function getObject(obj) {
69 var theObj = getRawObject(obj);
70 if (theObj && isCSS) {
71 theObj = theObj.style;
72 }
73 return theObj;
74 }
75
76 // Position an object at a specific pixel coordinate
77 function shiftTo(obj, x, y) {
78 var theObj = getObject(obj);
79 if (theObj) {
80 if (isCSS) {
81 // equalize incorrect numeric value type
82 var units = (typeof theObj.left == "string") ? "px" : 0
83 theObj.left = x + units;
84 theObj.top = y + units;
85 } else if (isNN4) {
86 theObj.moveTo(x,y)
87 }
88 }
89 }
90
91 // Move an object by x and/or y pixels
92 function shiftBy(obj, deltaX, deltaY) {
93 var theObj = getObject(obj);
94 if (theObj) {
95 if (isCSS) {
96 // equalize incorrect numeric value type
97 var units = (typeof theObj.left == "string") ? "px" : 0
98 theObj.left = getObjectLeft(obj) + deltaX + units;
99 theObj.top = getObjectTop(obj) + deltaY + units;
100 } else if (isNN4) {
101 theObj.moveBy(deltaX, deltaY);
102 }
103 }
104 }
105
106 // Set the z-order of an object
107 function setZIndex(obj, zOrder) {
108 var theObj = getObject(obj);
109 if (theObj) {
110 theObj.zIndex = zOrder;
111 }
112 }
113
114 // Set the background color of an object
115 function setBGColor(obj, color) {
116 var theObj = getObject(obj);
117 if (theObj) {
118 if (isNN4) {
119 theObj.bgColor = color;
120 } else if (isCSS) {
121 theObj.backgroundColor = color;
122 }
123 }
124 }
125
126 // Set the visibility of an object to visible
127 function show(obj) {
128 var theObj = getObject(obj);
129 if (theObj) {
130 theObj.visibility = "visible";
131 }
132 }
133
134 // Set the visibility of an object to hidden
135 function hide(obj) {
136 var theObj = getObject(obj);
137 if (theObj) {
138 theObj.visibility = "hidden";
139 }
140 }
141
142 // Retrieve the x coordinate of a positionable object
143 function getObjectLeft(obj) {
144 var elem = getRawObject(obj);
145 var result = 0;
146 if (document.defaultView) {
147 var style = document.defaultView;
148 var cssDecl = style.getComputedStyle(elem, "");
149 result = cssDecl.getPropertyValue("left");
150 } else if (elem.currentStyle) {
151 result = elem.currentStyle.left;
152 } else if (elem.style) {
153 result = elem.style.left;
154 } else if (isNN4) {
155 result = elem.left;
156 }
157 return parseInt(result);
158 }
159
160 // Retrieve the y coordinate of a positionable object
161 function getObjectTop(obj) {
162 var elem = getRawObject(obj);
163 var result = 0;
164 if (document.defaultView) {
165 var style = document.defaultView;
166 var cssDecl = style.getComputedStyle(elem, "");
167 result = cssDecl.getPropertyValue("top");
168 } else if (elem.currentStyle) {
169 result = elem.currentStyle.top;
170 } else if (elem.style) {
171 result = elem.style.top;
172 } else if (isNN4) {
173 result = elem.top;
174 }
175 return parseInt(result);
176 }
177
178 // Retrieve the rendered width of an element
179 function getObjectWidth(obj) {
180 var elem = getRawObject(obj);
181 var result = 0;
182 if (elem.offsetWidth) {
183 result = elem.offsetWidth;
184 } else if (elem.clip && elem.clip.width) {
185 result = elem.clip.width;
186 } else if (elem.style && elem.style.pixelWidth) {
187 result = elem.style.pixelWidth;
188 }
189 return parseInt(result);
190 }
191
192 // Retrieve the rendered height of an element
193 function getObjectHeight(obj) {
194 var elem = getRawObject(obj);
195 var result = 0;
196 if (elem.offsetHeight) {
197 result = elem.offsetHeight;
198 } else if (elem.clip && elem.clip.height) {
199 result = elem.clip.height;
200 } else if (elem.style && elem.style.pixelHeight) {
201 result = elem.style.pixelHeight;
202 }
203 return parseInt(result);
204 }
205
206
207 // Return the available content width space in browser window
208 function getInsideWindowWidth() {
209 if (window.innerWidth) {
210 return window.innerWidth;
211 } else if (isIE6CSS) {
212 // measure the html element's clientWidth
213 return document.body.parentElement.clientWidth
214 } else if (document.body && document.body.clientWidth) {
215 return document.body.clientWidth;
216 }
217 return 0;
218 }
219 // Return the available content height space in browser window
220 function getInsideWindowHeight() {
221 if (window.innerHeight) {
222 return window.innerHeight;
223 } else if (isIE6CSS) {
224 // measure the html element's clientHeight
225 return document.body.parentElement.clientHeight
226 } else if (document.body && document.body.clientHeight) {
227 return document.body.clientHeight;
228 }
229 return 0;
230 }
231