450
|
1 /* Copyright (C) 2003-2006 IT-Group MPIWG, WTWG Uni Bern and others
|
398
|
2
|
|
3 This program is free software; you can redistribute it and/or
|
|
4 modify it under the terms of the GNU General Public License
|
|
5 as published by the Free Software Foundation; either version 2
|
|
6 of the License, or (at your option) any later version.
|
|
7
|
|
8 This program is distributed in the hope that it will be useful,
|
|
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 GNU General Public License for more details.
|
|
12
|
|
13 You should have received a copy of the GNU General Public License
|
|
14 along with this program; if not, write to the Free Software
|
|
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
16
|
|
17 Authors:
|
|
18 Christian Luginbuehl, 01.05.2003 (first version)
|
|
19 DW 24.03.2004 (Changed for digiLib in Zope)
|
450
|
20 Robert Casties, 2.11.2004 (almost complete rewrite)
|
407
|
21 Martin Raspe, 12.12.2005 (changes for Digilib NG)
|
398
|
22
|
|
23 */
|
|
24
|
407
|
25 // was: function base_init() {
|
450
|
26 baseLibVersion = "2.010";
|
407
|
27 browserType = getBrowserType();
|
398
|
28
|
444
|
29 sliders = {};
|
|
30 activeSlider = null;
|
398
|
31
|
|
32 function getInt(n) {
|
|
33 // returns always an integer
|
|
34 n = parseInt(n);
|
450
|
35 return (isNaN(n)) ? 0 : n;
|
398
|
36 }
|
|
37
|
|
38 function defined(x) {
|
|
39 // returns if x is defined
|
|
40 return (typeof arguments[0] != "undefined");
|
|
41 }
|
|
42
|
|
43 function cropFloat(x) {
|
|
44 // auxiliary function to crop senseless precision
|
407
|
45 return parseInt(10000 * x) / 10000;
|
398
|
46 }
|
|
47
|
|
48 function getBrowserType() {
|
|
49 // browser sniffer
|
|
50 var bt = Object();
|
|
51 bt.doDHTML = false;
|
|
52 bt.versIE = 0;
|
|
53
|
|
54 if ((! document.cssonly && document.layers) || document.all || document.getElementById) {
|
|
55 var vers = navigator.appVersion.split('MSIE ');
|
|
56 vers = vers[vers.length - 1];
|
|
57 bt.versIE = getInt(vers);
|
|
58 bt.isIE = navigator.userAgent.indexOf('MSIE') >= 0;
|
|
59 bt.isMac = navigator.platform.indexOf('Mac') >= 0;
|
|
60 bt.isWin = navigator.platform.indexOf('Win') >= 0;
|
|
61 bt.isN4 = (navigator.userAgent.indexOf('Mozilla/4.') >= 0) && ! bt.isIE;
|
|
62 bt.isIEWin = bt.versIE > 0 && bt.isWin;
|
|
63 if (navigator.appVersion.indexOf('MSIE') < 0 || ! bt.isMac || bt.versIE >= 5) {
|
|
64 bt.doDHTML = true;
|
|
65 bt.isOpera = navigator.userAgent.indexOf(' Opera ') >= 0;
|
|
66 bt.isKonq = navigator.userAgent.indexOf(' Konqueror') >= 0;
|
|
67 }
|
|
68 }
|
|
69 return bt;
|
|
70 }
|
|
71
|
|
72 // fixes for javascript < 1.2
|
|
73 if (! Array.prototype.push) {
|
|
74 Array.prototype.push = function(val) {
|
|
75 this[this.length] = val;
|
|
76 return this.length;
|
|
77 }
|
|
78 Array.prototype.pop = function() {
|
|
79 var val = this[this.length-1];
|
|
80 this.length -= 1;
|
|
81 return val;
|
|
82 }
|
|
83 }
|
|
84
|
|
85
|
|
86 /* **********************************************
|
|
87 * geometry classes
|
|
88 * ******************************************** */
|
|
89
|
|
90 /*
|
|
91 * Size class
|
|
92 */
|
|
93 function Size(w, h) {
|
|
94 this.width = parseFloat(w);
|
|
95 this.height = parseFloat(h);
|
|
96 return this;
|
|
97 }
|
|
98 Size.prototype.toString = function() {
|
|
99 return this.width + "x" + this.height;
|
|
100 }
|
426
|
101 Size.prototype.equals = function(other) {
|
|
102 return (this.width == other.width
|
|
103 && this.height == other.height)
|
|
104 }
|
398
|
105
|
|
106 /*
|
|
107 * Position class
|
|
108 */
|
|
109 function Position(x, y) {
|
|
110 this.x = parseFloat(x);
|
|
111 this.y = parseFloat(y);
|
|
112 return this;
|
|
113 }
|
|
114 Position.prototype.toString = function() {
|
|
115 return this.x + "," + this.y;
|
|
116 }
|
426
|
117 Position.prototype.equals = function(other) {
|
|
118 return (this.x == other.x
|
|
119 && this.y == other.y)
|
450
|
120 }
|
398
|
121 /*
|
|
122 * Rectangle class
|
|
123 */
|
|
124 function Rectangle(x, y, w, h) {
|
450
|
125 if (typeof x == "object") {
|
|
126 // assume x and y are Position
|
|
127 this.x = x.x;
|
|
128 this.y = x.y;
|
|
129 this.width = y.x - x.x;
|
|
130 this.height = y.y - x.y;
|
|
131 return this;
|
|
132 }
|
398
|
133 this.x = parseFloat(x);
|
|
134 this.y = parseFloat(y);
|
|
135 this.width = parseFloat(w);
|
|
136 this.height = parseFloat(h);
|
|
137 return this;
|
|
138 }
|
|
139 Rectangle.prototype.toString = function() {
|
|
140 return this.width+"x"+this.height+"@"+this.x+","+this.y;
|
|
141 }
|
|
142 Rectangle.prototype.copy = function() {
|
|
143 // returns a copy of this Rectangle
|
|
144 return new Rectangle(this.x, this.y, this.width, this.height);
|
|
145 }
|
|
146 Rectangle.prototype.getPosition = function() {
|
|
147 // returns the position of this Rectangle
|
|
148 return new Position(this.x, this.y);
|
|
149 }
|
433
|
150 Rectangle.prototype.getPt1 = Rectangle.prototype.getPosition;
|
450
|
151 // returns the upper left corner position
|
433
|
152
|
|
153 Rectangle.prototype.getPt2 = function() {
|
450
|
154 // returns the lower right corner position of this Rectangle
|
433
|
155 return new Position(this.x + this.width, this.y + this.height);
|
|
156 }
|
|
157 Rectangle.prototype.setPt1 = function(pos) {
|
450
|
158 // sets the upper left corner to position pos
|
433
|
159 this.x = pos.x;
|
|
160 this.y = pos.y;
|
|
161 return this;
|
|
162 }
|
|
163 Rectangle.prototype.setPt2 = function(pos) {
|
450
|
164 // sets the lower right corner to position pos
|
433
|
165 this.width = pos.x - this.x;
|
|
166 this.height = pos.y - this.y;
|
|
167 return this;
|
|
168 }
|
|
169 Rectangle.prototype.getCenter = function() {
|
|
170 // returns the center position of this Rectangle
|
|
171 return new Position(this.x + this.width / 2, this.y + this.height / 2);
|
|
172 }
|
|
173 Rectangle.prototype.setCenter = function(pos) {
|
|
174 // moves this Rectangle's center to position pos
|
|
175 this.x = pos.x - this.width / 2;
|
|
176 this.y = pos.y - this.height / 2;
|
|
177 return this;
|
|
178 }
|
398
|
179 Rectangle.prototype.getSize = function() {
|
|
180 // returns the size of this Rectangle
|
|
181 return new Size(this.width, this.height);
|
|
182 }
|
426
|
183 Rectangle.prototype.equals = function(other) {
|
|
184 // equal props
|
|
185 return (this.getPosition().equals(other.getPosition())
|
|
186 && this.getSize().equals(other.getSize())
|
|
187 );
|
|
188 }
|
398
|
189 Rectangle.prototype.getArea = function() {
|
|
190 // returns the area of this Rectangle
|
|
191 return (this.width * this.height);
|
|
192 }
|
433
|
193 Rectangle.prototype.normalize = function() {
|
|
194 // eliminates negative width and height
|
|
195 var p = this.getPt2();
|
|
196 this.x = Math.min(this.x, p.x);
|
|
197 this.y = Math.min(this.y, p.y);
|
|
198 this.width = Math.abs(this.width);
|
|
199 this.height = Math.abs(this.height);
|
|
200 return this;
|
|
201 }
|
398
|
202 Rectangle.prototype.containsPosition = function(pos) {
|
412
|
203 // returns if Position "pos" lies inside of this rectangle
|
|
204 return ((pos.x >= this.x)
|
|
205 && (pos.y >= this.y)
|
|
206 && (pos.x <= this.x + this.width)
|
|
207 && (pos.y <= this.y + this.width)
|
|
208 );
|
|
209 }
|
|
210 Rectangle.prototype.containsRect = function(rect) {
|
|
211 // returns if rectangle "rect" is contained in this rectangle
|
450
|
212 return (this.containsPosition(rect.getPt1())
|
|
213 && this.containsPosition(rect.getPt2()));
|
412
|
214 }
|
|
215 Rectangle.prototype.stayInside = function(rect) {
|
|
216 // changes this rectangle's x/y values so it stays inside of rectangle rect
|
433
|
217 // keeping the proportions
|
412
|
218 if (this.x < rect.x) this.x = rect.x;
|
|
219 if (this.y < rect.y) this.y = rect.y;
|
|
220 if (this.x + this.width > rect.x + rect.width)
|
|
221 this.x = rect.x + rect.width - this.width;
|
|
222 if (this.y + this.height > rect.y + rect.height)
|
|
223 this.y = rect.y + rect.height - this.height;
|
|
224 return this;
|
398
|
225 }
|
433
|
226 Rectangle.prototype.clipTo = function(rect) {
|
|
227 // clips this rectangle so it stays inside of rectangle rect
|
450
|
228 var p1 = rect.getPt1();
|
|
229 var p2 = rect.getPt2();
|
|
230 var this2 = this.getPt2();
|
433
|
231 this.setPt1(new Position(Math.max(this.x, p1.x), Math.max(this.y, p1.y)));
|
|
232 this.setPt2(new Position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y)));
|
|
233 return this;
|
|
234 }
|
398
|
235 Rectangle.prototype.intersect = function(rect) {
|
|
236 // returns the intersection of the given Rectangle and this one
|
412
|
237 // FIX ME: not really, it should return null if there is no overlap
|
398
|
238 var sec = rect.copy();
|
|
239 if (sec.x < this.x) {
|
|
240 sec.width = sec.width - (this.x - sec.x);
|
|
241 sec.x = this.x;
|
|
242 }
|
|
243 if (sec.y < this.y) {
|
|
244 sec.height = sec.height - (this.y - sec.y);
|
|
245 sec.y = this.y;
|
|
246 }
|
|
247 if (sec.x + sec.width > this.x + this.width) {
|
|
248 sec.width = (this.x + this.width) - sec.x;
|
|
249 }
|
|
250 if (sec.y + sec.height > this.y + this.height) {
|
|
251 sec.height = (this.y + this.height) - sec.y;
|
|
252 }
|
|
253 return sec;
|
|
254 }
|
|
255 Rectangle.prototype.fit = function(rect) {
|
|
256 // returns a Rectangle that fits into this one (by moving first)
|
|
257 var sec = rect.copy();
|
|
258 sec.x = Math.max(sec.x, this.x);
|
426
|
259 sec.y = Math.max(sec.y, this.x);
|
398
|
260 if (sec.x + sec.width > this.x + this.width) {
|
|
261 sec.x = this.x + this.width - sec.width;
|
|
262 }
|
|
263 if (sec.y + sec.height > this.y + this.height) {
|
|
264 sec.y = this.y + this.height - sec.height;
|
|
265 }
|
|
266 return sec.intersect(this);
|
|
267 }
|
|
268
|
|
269 /*
|
|
270 * Transform class
|
|
271 *
|
|
272 * defines a class of affine transformations
|
|
273 */
|
|
274 function Transform() {
|
|
275 this.m00 = 1.0;
|
|
276 this.m01 = 0.0;
|
|
277 this.m02 = 0.0;
|
|
278 this.m10 = 0.0;
|
|
279 this.m11 = 1.0;
|
|
280 this.m12 = 0.0;
|
|
281 this.m20 = 0.0;
|
|
282 this.m21 = 0.0;
|
|
283 this.m22 = 1.0;
|
|
284 return this;
|
|
285 }
|
|
286 Transform.prototype.concat = function(traf) {
|
|
287 // add Transform traf to this Transform
|
|
288 for (var i = 0; i < 3; i++) {
|
|
289 for (var j = 0; j < 3; j++) {
|
|
290 var c = 0.0;
|
|
291 for (var k = 0; k < 3; k++) {
|
|
292 c += traf["m"+i+k] * this["m"+k+j];
|
|
293 }
|
|
294 this["m"+i+j] = c;
|
|
295 }
|
|
296 }
|
|
297 return this;
|
|
298 }
|
|
299 Transform.prototype.transform = function(rect) {
|
|
300 // returns transformed Rectangle or Position with this Transform applied
|
|
301 var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
|
|
302 var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
|
|
303 if (rect.width) {
|
|
304 var width = this.m00 * rect.width + this.m01 * rect.height;
|
|
305 var height = this.m10 * rect.width + this.m11 * rect.height;
|
|
306 return new Rectangle(x, y, width, height);
|
|
307 }
|
|
308 return new Position(x, y);
|
|
309 }
|
|
310 Transform.prototype.invtransform = function(pos) {
|
|
311 // returns transformed Position pos with the inverse of this Transform applied
|
|
312 var det = this.m00 * this.m11 - this.m01 * this.m10;
|
|
313 var x = (this.m11 * pos.x - this.m01 * pos.y - this.m11 * this.m02 + this.m01 * this.m12) / det;
|
|
314 var y = (- this.m10 * pos.x + this.m00 * pos.y + this.m10 * this.m02 - this.m00 * this.m12) / det;
|
450
|
315 if (pos.width) {
|
|
316 var width = (this.m11 * pos.width - this.m01 * pos.height - this.m11 * this.m02 + this.m01 * this.m12) / det;
|
|
317 var height = (- this.m10 * pos.width + this.m00 * pos.height + this.m10 * this.m02 - this.m00 * this.m12) / det;
|
|
318 return new Rectangle(x, y, width, height);
|
|
319 }
|
398
|
320 return new Position(x, y);
|
|
321 }
|
|
322 function getRotation(angle, pos) {
|
|
323 // returns a Transform that is a rotation by angle degrees around [pos.x, pos.y]
|
|
324 var traf = new Transform();
|
|
325 if (angle != 0) {
|
|
326 var t = 2.0 * Math.PI * parseFloat(angle) / 360.0;
|
|
327 traf.m00 = Math.cos(t);
|
|
328 traf.m01 = - Math.sin(t);
|
|
329 traf.m10 = Math.sin(t);
|
|
330 traf.m11 = Math.cos(t);
|
|
331 traf.m02 = pos.x - pos.x * Math.cos(t) + pos.y * Math.sin(t);
|
|
332 traf.m12 = pos.y - pos.x * Math.sin(t) - pos.y * Math.cos(t);
|
|
333 }
|
|
334 return traf;
|
|
335 }
|
|
336 function getTranslation(pos) {
|
|
337 // returns a Transform that is a translation by [pos.x, pos,y]
|
|
338 var traf = new Transform();
|
|
339 traf.m02 = pos.x;
|
|
340 traf.m12 = pos.y;
|
|
341 return traf;
|
|
342 }
|
|
343 function getScale(size) {
|
|
344 // returns a Transform that is a scale by [size.width, size.height]
|
|
345 var traf = new Transform();
|
|
346 traf.m00 = size.width;
|
|
347 traf.m11 = size.height;
|
|
348 return traf;
|
|
349 }
|
|
350
|
|
351
|
450
|
352 /*
|
|
353 * parameters class
|
|
354 */
|
398
|
355
|
450
|
356 function Parameters() {
|
|
357 this.params = new Object();
|
452
|
358 this.PARAM_ALL = 65535;
|
450
|
359 return this;
|
|
360 }
|
|
361 Parameters.prototype.define = function(name, defaultValue, detail) {
|
398
|
362 // create a new parameter with a name and a default value
|
450
|
363 if (!this.params[name]) this.params[name] = new Object();
|
|
364 this.params[name].defaultValue = defaultValue;
|
|
365 this.params[name].hasValue = false;
|
|
366 this.params[name].value = defaultValue;
|
|
367 this.params[name].detail = detail;
|
|
368 return this.params[name];
|
|
369 }
|
|
370 Parameters.prototype.reset = function(name) {
|
412
|
371 // resets the given parameter to its default value
|
450
|
372 if (!this.params[name]) {
|
|
373 alert("Could not reset non-existing parameter '" + name + "'");
|
|
374 return false;
|
412
|
375 }
|
450
|
376 this.params[name].hasValue = false;
|
|
377 this.params[name].value = this.params[name].defaultValue;
|
|
378 return this.params[name];
|
|
379 }
|
|
380 Parameters.prototype.resetAll = function() {
|
|
381 // resets all parameters to their default values
|
|
382 for (var p in this.params) {
|
|
383 this.reset(p);
|
|
384 }
|
|
385 return true;
|
|
386 }
|
|
387 Parameters.prototype.remove = function(name) {
|
|
388 if (!defined(this.params[name])) return false;
|
|
389 delete this.params[name];
|
|
390 return true;
|
|
391 }
|
|
392 Parameters.prototype.get = function(name) {
|
398
|
393 // returns the named parameter value or its default value
|
450
|
394 if (!defined(this.params[name])) return null;
|
|
395 return this.params[name].hasValue ? this.params[name].value : this.params[name].defaultValue;
|
|
396 }
|
|
397 Parameters.prototype.set = function(name, value, relative) {
|
|
398 // sets parameter value (relative values with +/- if relative=true)
|
|
399 if (!defined(this.params[name])) return null;
|
|
400 var p = this.params[name];
|
412
|
401 if (relative && value.slice) {
|
450
|
402 // value is a string -- check if it starts with +/-
|
412
|
403 var sign = value.slice(0, 1);
|
|
404 if (sign == '+') {
|
|
405 p.value = parseFloat(p.value) + parseFloat(value.slice(1));
|
|
406 } else if (sign == '-') {
|
|
407 p.value = parseFloat(p.value) - parseFloat(value.slice(1));
|
|
408 } else {
|
|
409 p.value = value;
|
|
410 }
|
450
|
411 } else {
|
|
412 p.value = value;
|
|
413 }
|
412
|
414 p.hasValue = true;
|
|
415 return p.value;
|
450
|
416 }
|
|
417 Parameters.prototype.isSet = function(name) {
|
412
|
418 // returns if the parameter's value has been set
|
450
|
419 if (!defined(this.params[name])) return null;
|
|
420 return this.params[name].hasValue;
|
|
421 }
|
|
422 Parameters.prototype.getAll = function(detail) {
|
398
|
423 // returns a string of all parameters in query format
|
452
|
424 if (!detail) detail = this.PARAM_ALL;
|
450
|
425 var pa = new Array();
|
|
426 for (p in this.params) {
|
|
427 if (((this.params[p].detail & detail) > 0)
|
|
428 && (this.params[p].hasValue)) {
|
|
429 var val = this.params[p].value;
|
398
|
430 if (val != "") {
|
450
|
431 pa.push(p + "=" + val);
|
398
|
432 }
|
|
433 }
|
|
434 }
|
450
|
435 return pa.join("&");
|
398
|
436 }
|
450
|
437 Parameters.prototype.parse = function(query) {
|
398
|
438 // gets parameter values from query format string
|
450
|
439 var pa = query.split("&");
|
|
440 for (var i = 0; i < pa.length; i++) {
|
|
441 var keyval = pa[i].split("=");
|
398
|
442 if (keyval.length == 2) {
|
450
|
443 this.set(keyval[0], keyval[1]);
|
398
|
444 }
|
|
445 }
|
|
446 }
|
|
447
|
450
|
448 /*
|
|
449 * Flags class
|
|
450 *
|
|
451 * Flags are (hash-) collections of unique strings.
|
|
452 */
|
|
453 function Flags() {
|
|
454 this.flags = new Object();
|
|
455 return this;
|
|
456 }
|
|
457 Flags.prototype.define = function(name, detail) {
|
|
458 // create a new flag with a name and detail level
|
|
459 this.flags[name] = new Object();
|
|
460 this.flags[name].set = false;
|
|
461 this.flags[name].detail = detail;
|
|
462 return this.flags[name];
|
|
463 }
|
|
464 Flags.prototype.get = function(name) {
|
|
465 return (this.flags[name]) ? this.flags[name].set : false;
|
|
466 }
|
|
467 Flags.prototype.set = function(name, value) {
|
|
468 if (!defined(value)) value = true;
|
|
469 if (!this.flags[name]) this.flags[name] = new Object;
|
|
470 this.flags[name].set = value;
|
|
471 }
|
|
472 Flags.prototype.reset = function(name) {
|
|
473 if (!this.flags[name]) this.flags[name] = new Object;
|
|
474 this.flags[name].set = false;
|
|
475 }
|
|
476 Flags.prototype.toggle = function(name) {
|
|
477 if (!this.flags[name]) this.flags[name] = new Object;
|
|
478 this.flags[name].set = !this.flags[name].set;
|
|
479 }
|
|
480 Flags.prototype.resetAll = function() {
|
|
481 for (var f in this.flags) {
|
|
482 this.flags[f].set = false;
|
|
483 }
|
|
484 }
|
|
485 Flags.prototype.parse = function(query, sep) {
|
|
486 // sets the flags from the string query
|
|
487 if (!sep) sep = ",";
|
|
488 var fa = query.split(sep);
|
|
489 for (var i = 0; i < fa.length ; i++) {
|
|
490 var f = fa[i];
|
|
491 if (f != "") {
|
|
492 this.set(f);
|
|
493 }
|
|
494 }
|
|
495 }
|
|
496 Flags.prototype.getAll = function(detail, sep) {
|
|
497 // returns a string of all flags in query format
|
|
498 if (!detail) detail = 255;
|
|
499 if (!sep) sep = ",";
|
|
500 var fa = new Array();
|
|
501 for (f in this.flags) {
|
|
502 if (this.flags[f].set) {
|
|
503 // if the flag has a detail level it must match
|
|
504 // otherwise we assume detail=128
|
|
505 if (this.flags[f].detail) {
|
|
506 if ((this.flags[f].detail & detail) > 0) {
|
|
507 fa.push(f);
|
|
508 }
|
|
509 } else {
|
|
510 if ((detail & 128) > 0) {
|
|
511 fa.push(f);
|
|
512 }
|
|
513 }
|
|
514 }
|
|
515 }
|
|
516 return fa.join(sep);
|
|
517 }
|
|
518
|
398
|
519
|
|
520 /* **********************************************
|
|
521 * HTML/DOM routines
|
|
522 * ******************************************** */
|
|
523
|
|
524 function getElement(tagid, quiet) {
|
|
525 // returns the element object with the id tagid
|
|
526 var e;
|
|
527 if (document.getElementById) {
|
|
528 e = document.getElementById(tagid);
|
|
529 } else if (document.all) {
|
|
530 alert("document.all!");
|
|
531 e = document.all[tagid];
|
|
532 } else if (document.layers) {
|
|
533 e = document.layers[tagid];
|
|
534 }
|
|
535 if (e) {
|
|
536 return e;
|
|
537 } else {
|
|
538 if (! quiet) {
|
|
539 alert("unable to find element: "+tagid);
|
|
540 }
|
|
541 return null;
|
|
542 }
|
|
543 }
|
|
544
|
|
545 function getElementPosition(elem) {
|
|
546 // returns a Position with the position of the element
|
|
547 var x = 0;
|
|
548 var y = 0;
|
|
549 if (defined(elem.offsetLeft)) {
|
|
550 var e = elem;
|
|
551 while (e) {
|
|
552 if (defined(e.clientLeft)) {
|
|
553 // special for IE
|
|
554 if (browserType.isMac) {
|
|
555 if (e.offsetParent.tagName == "BODY") {
|
|
556 // IE for Mac extraspecial
|
|
557 x += e.clientLeft;
|
|
558 y += e.clientTop;
|
|
559 break;
|
|
560 }
|
|
561 } else {
|
|
562 if ((e.tagName != "TABLE") && (e.tagName != "BODY")) {
|
|
563 x += e.clientLeft;
|
|
564 y += e.clientTop;
|
|
565 }
|
|
566 }
|
|
567 }
|
|
568 x += e.offsetLeft;
|
|
569 y += e.offsetTop;
|
|
570 e = e.offsetParent;
|
|
571 }
|
|
572 } else if (defined(elem.x)) {
|
|
573 x = elem.x;
|
|
574 y = elem.y;
|
|
575 } else if (defined(elem.pageX)) {
|
|
576 x = elem.pageX;
|
|
577 y = elem.pageY;
|
|
578 } else {
|
438
|
579 alert("unable to get position of " + elem + " (id:" + elem.id + ")");
|
398
|
580 }
|
|
581 return new Position(getInt(x), getInt(y));
|
|
582 }
|
|
583
|
|
584 function getElementSize(elem) {
|
|
585 // returns a Rectangle with the size of the element
|
|
586 var width = 0;
|
|
587 var height = 0;
|
|
588 if (defined(elem.offsetWidth)) {
|
|
589 width = elem.offsetWidth;
|
|
590 height = elem.offsetHeight;
|
|
591 } else if (defined(elem.width)) {
|
|
592 width = elem.width;
|
|
593 height = elem.height;
|
|
594 } else if (defined(elem.clip.width)) {
|
|
595 width = elem.clip.width;
|
|
596 height = elem.clip.height;
|
|
597 } else {
|
438
|
598 alert("unable to get size of " + elem + " (id:" + elem.id + ")");
|
398
|
599 }
|
|
600 return new Size(getInt(width), getInt(height));
|
|
601 }
|
|
602
|
|
603 function getElementRect(elem) {
|
|
604 // returns a Rectangle with the size and position of the element
|
412
|
605 // FIX ME: what about borders?
|
398
|
606 var pos = getElementPosition(elem);
|
|
607 var size = getElementSize(elem);
|
|
608 return new Rectangle(pos.x, pos.y, size.width, size.height);
|
|
609 }
|
|
610
|
|
611 function moveElement(elem, rect) {
|
|
612 // moves and sizes the element
|
|
613 if (elem.style) {
|
|
614 if (defined(rect.x)) {
|
|
615 elem.style.left = Math.round(rect.x) + "px";
|
|
616 elem.style.top = Math.round(rect.y) + "px";
|
|
617 }
|
|
618 if (defined(rect.width)) {
|
|
619 elem.style.width = Math.round(rect.width) + "px";
|
|
620 elem.style.height = Math.round(rect.height) + "px";
|
|
621 }
|
|
622 } else if (document.layers) {
|
|
623 if (defined(rect.x)) {
|
|
624 elem.pageX = getInt(rect.x);
|
|
625 elem.pageY = getInt(rect.y);
|
|
626 }
|
|
627 if (defined(rect.width)) {
|
|
628 elem.clip.width = getInt(rect.width);
|
|
629 elem.clip.height = getInt(rect.height);
|
|
630 }
|
|
631 } else {
|
438
|
632 alert("moveElement(): element has no style or layer property!");
|
398
|
633 return false;
|
|
634 }
|
|
635 return true;
|
|
636 }
|
|
637
|
|
638 function showElement(elem, show) {
|
|
639 // shows or hides the element
|
435
|
640 if (elem.style)
|
|
641 elem.style.visibility = show ? "visible" : "hidden";
|
438
|
642 else if (defined(elem.visibility))
|
435
|
643 elem.visibility = show ? "show" : "hide";
|
438
|
644 else
|
435
|
645 alert("showElement(): element has no style or layer property!");
|
398
|
646 return true;
|
|
647 }
|
|
648
|
|
649 function evtPosition(evt) {
|
|
650 // returns the on-screen Position of the Event
|
|
651 var x;
|
|
652 var y;
|
|
653 evt = (evt) ? evt : window.event;
|
|
654 if (!evt) {
|
435
|
655 alert("no event found! " + evt);
|
398
|
656 return;
|
435
|
657 }
|
398
|
658 if (defined(evt.pageX)) {
|
|
659 x = parseInt(evt.pageX);
|
|
660 y = parseInt(evt.pageY);
|
|
661 } else if (defined(evt.clientX)) {
|
435
|
662 x = parseInt(document.body.scrollLeft + evt.clientX);
|
|
663 y = parseInt(document.body.scrollTop + evt.clientY);
|
398
|
664 } else {
|
435
|
665 alert("evtPosition(): don't know how to deal with " + evt);
|
|
666 }
|
398
|
667 return new Position(x, y);
|
|
668 }
|
|
669
|
|
670 function registerEvent(type, elem, handler) {
|
412
|
671 // register the given event handler on the indicated element
|
|
672 if (elem.addEventListener) {
|
|
673 elem.addEventListener(type, handler, false); // bubble
|
|
674 }
|
|
675 else if (elem.attachEvent) {
|
|
676 elem.attachEvent("on" + type, handler);
|
|
677 }
|
|
678 else if (elem.captureEvents) {
|
|
679 if (Event) {
|
|
680 t = type.toUpperCase();
|
|
681 elem.captureEvents(Event[t]);
|
438
|
682 elem[ "on" + type ] = handler;
|
412
|
683 }
|
|
684 }
|
|
685 else {
|
|
686 alert("Could not register event of type " + type);
|
|
687 return false;
|
|
688 }
|
|
689 return true;
|
|
690 }
|
|
691
|
407
|
692 function unregisterEvent(type, elem, handler) {
|
412
|
693 // unregister the given event handler from the indicated element
|
|
694 if (elem.removeEventListener) {
|
|
695 elem.removeEventListener(type, handler, false);
|
|
696 }
|
|
697 else if (elem.detachEvent) {
|
438
|
698 elem.detachEvent("on" + type, handler);
|
412
|
699 }
|
|
700 else if (elem.releaseEvents) {
|
|
701 if (Event) {
|
|
702 t = type.toUpperCase();
|
|
703 elem.releaseEvents(Event[t]);
|
438
|
704 elem[ "on" + type ] = null;
|
412
|
705 }
|
|
706 }
|
|
707 else {
|
|
708 alert("Could not register event of type " + type);
|
|
709 return false;
|
|
710 }
|
|
711 return true;
|
398
|
712 }
|
|
713
|
407
|
714 function registerEventById(type, id, handler) {
|
442
|
715 registerEvent(type, getElement(id), handler);
|
412
|
716 }
|
407
|
717
|
|
718 function unregisterEventById(type, id, handler) {
|
442
|
719 unregisterEvent(type, getElement(id), handler);
|
412
|
720 }
|
407
|
721
|
|
722 function stopEvent(e) {
|
412
|
723 if (!e) var e = window.event;
|
|
724 e.cancelBubble = true;
|
|
725 if (e.stopPropagation) e.stopPropagation();
|
|
726 return false;
|
398
|
727 }
|
|
728
|
407
|
729 function getEventSrc(e) {
|
412
|
730 if (e.target) return e.target;
|
|
731 if (e.srcElement) return e.srcElement;
|
407
|
732 }
|
398
|
733
|
|
734 // old registerXXYY API for compatibility
|
|
735 function registerMouseDown(elem, handler) {
|
|
736 return registerEvent("mousedown", elem, handler);
|
|
737 }
|
|
738 function unregisterMouseDown(elem, handler) {
|
|
739 return unregisterEvent("mousedown", elem, handler);
|
|
740 }
|
|
741 function registerMouseMove(elem, handler) {
|
|
742 return registerEvent("mousemove", elem, handler);
|
|
743 }
|
|
744 function unregisterMouseMove(elem, handler) {
|
|
745 return unregisterEvent("mousemove", elem, handler);
|
|
746 }
|
|
747 function registerKeyDown(handler) {
|
|
748 return registerEvent("keypress", elem, handler);
|
|
749 }
|
|
750
|
|
751
|
|
752 function getWinSize() {
|
|
753 // returns a Size with the current window size (mostly from www.quirksmode.org)
|
|
754 var wsize = new Size(100, 100);
|
|
755 if (defined(self.innerHeight)) {
|
|
756 // all except Explorer
|
|
757 if ((self.innerWidth == 0)||(self.innerHeight == 0)) {
|
|
758 // Safari 1.2 bug
|
|
759 if (parent) {
|
|
760 parent.innerHeight;
|
|
761 parent.innerWidth;
|
|
762 }
|
|
763 }
|
|
764 wsize.width = self.innerWidth;
|
|
765 wsize.height = self.innerHeight;
|
|
766 } else if (document.documentElement && document.documentElement.clientHeight) {
|
|
767 // Explorer 6 Strict Mode
|
|
768 wsize.width = document.documentElement.clientWidth;
|
|
769 wsize.height = document.documentElement.clientHeight;
|
|
770 } else if (document.body) {
|
|
771 // other Explorers
|
|
772 wsize.width = document.body.clientWidth;
|
|
773 wsize.height = document.body.clientHeight;
|
|
774 }
|
|
775 return wsize;
|
|
776 }
|
|
777
|
433
|
778 function getWinRect() {
|
|
779 var size = getWinSize();
|
|
780 return new Rectangle(0, 0, size.width, size.height);
|
|
781 }
|
|
782
|
398
|
783 function openWin(url, name, params) {
|
|
784 // open browser window
|
|
785 var ow = window.open(url, name, params);
|
|
786 ow.focus();
|
|
787 }
|
433
|
788
|
|
789 /* **********************************************
|
|
790 * cookie class
|
|
791 * ******************************************** */
|
|
792
|
|
793 function Cookie() {
|
|
794 return this.read();
|
|
795 }
|
|
796
|
|
797 Cookie.prototype.read = function() {
|
|
798 var s = document.cookie;
|
435
|
799 var lines = s.split("; "); // semicolon and space for all browsers?
|
433
|
800 for (var i in lines) {
|
|
801 var line = lines[i];
|
|
802 var sep = line.indexOf("=");
|
|
803 if (sep != -1) this.add(
|
|
804 line.substr(0, sep),
|
|
805 line.substr(sep + 1)
|
|
806 );
|
|
807 }
|
|
808 return this;
|
|
809 }
|
|
810
|
|
811 Cookie.prototype.store = function() {
|
|
812 var lines = new Array();
|
|
813 for (var i in this) {
|
|
814 var item = this[i];
|
|
815 if (typeof(item) == typeof(lines)) // Array
|
|
816 lines.push(i + "=" + item.join(","));
|
|
817 else if (typeof(item) != "function") // single item
|
|
818 lines.push(i + "=" + item);
|
|
819 }
|
|
820 // var s = lines.join(";")
|
|
821 for (line in lines) document.cookie = lines[line];
|
|
822 return this;
|
|
823 }
|
|
824
|
|
825 Cookie.prototype.add = function(key, value) {
|
438
|
826 value = value.toString();
|
433
|
827 if (value.indexOf(",") == -1)
|
|
828 this[key] = value; // single value
|
|
829 else
|
|
830 this[key] = value.split(","); // list of values
|
|
831 return this[key];
|
|
832 }
|
|
833
|
|
834 Cookie.prototype.get = function(key) {
|
|
835 return this[key];
|
|
836 }
|
|
837
|
|
838 Cookie.prototype.addbool = function(key, value) {
|
|
839 this[key] = Boolean(value).toString();
|
|
840 return this[key];
|
|
841 }
|
|
842
|
|
843 Cookie.prototype.getbool = function(key) {
|
|
844 var val = this[key];
|
|
845 return (val > "") && (val != "0") && (val != "false");
|
|
846 }
|
|
847
|
|
848 Cookie.prototype.remove = function(key) {
|
|
849 delete this[key];
|
|
850 }
|
|
851
|
442
|
852 function Slider(id, valMin, valMax, valStart, stepSize, onChange) {
|
|
853 // a (horizontal) slider widget
|
|
854 this.id = id;
|
|
855 this.elem = getElement(id);
|
444
|
856 this.slider = getElement(id + "-slider"); // the slider handle
|
|
857 this.input = getElement(id + "-input", 1); // optional input field
|
|
858 this.bar = getElement(id + "-bar"); // the slider bar
|
442
|
859 this.barRect = getElementRect(this.bar);
|
444
|
860 this.sliderRect = getElementRect(this.slider);
|
|
861 this.xMin = this.barRect.x;
|
|
862 this.xMax = this.xMin + this.barRect.width;
|
|
863 this.xDiff = this.xMax - this.xMin;
|
|
864 this.Y = this.barRect.getCenter().y; // middle axis of bar
|
442
|
865 this.valMin = valMin;
|
|
866 this.valMax = valMax;
|
444
|
867 this.valDiff = Math.abs(valMax - valMin);
|
442
|
868 this.valStart = valStart;
|
|
869 this.value = valStart;
|
|
870 this.stepSize = stepSize;
|
444
|
871 this.valueLabel = getElement(id + "-value", 1);
|
|
872 this.valMinLabel = getElement(id + "-valmin", 1);
|
|
873 this.valMaxLabel = getElement(id + "-valmax", 1);
|
442
|
874 this.onChange = onChange ? onChange : function() {};
|
444
|
875 this.update();
|
|
876 this.activate();
|
|
877 sliders[id + '-slider'] = this; // make a handle to the object
|
442
|
878 return this;
|
|
879 }
|
|
880
|
|
881 Slider.prototype.show = function(show) {
|
|
882 showElement(this.elem, show);
|
444
|
883 this.activate();
|
442
|
884 }
|
|
885
|
|
886 Slider.prototype.activate = function() {
|
444
|
887 this.setupEvents();
|
442
|
888 }
|
|
889
|
444
|
890 Slider.prototype.deactivate = function() {
|
|
891 unregisterEvent("mousedown", this.slider, this.onDragStart);
|
442
|
892 }
|
|
893
|
|
894 Slider.prototype.reset = function() {
|
|
895 this.setValue(this.startVal);
|
|
896 }
|
|
897
|
|
898 Slider.prototype.setValue = function(newVal) {
|
|
899 // sets slider to new value and updates
|
|
900 this.value = newVal;
|
444
|
901 this.update();
|
|
902 }
|
|
903
|
|
904 Slider.prototype.calcValue = function() {
|
|
905 // calculates value from slider position
|
|
906 var xSlider = this.sliderRect.getCenter().x - this.xMin;
|
|
907 this.value = xSlider * this.valDiff / this.xDiff;
|
|
908 return this.value;
|
442
|
909 }
|
|
910
|
|
911 Slider.prototype.update = function() {
|
|
912 // updates slider position to new value
|
444
|
913 var xSlider = this.value * this.xDiff / this.valDiff;
|
|
914 moveElement(this.slider, this.sliderRect.setCenter(
|
|
915 new Position(xSlider + this.xMin, this.Y)));
|
|
916 var strVal = this.value.toString();
|
|
917 if (this.valueLabel) this.valueLabel.innerHTML = strVal;
|
|
918 if (this.input) this.input.value = strVal;
|
442
|
919 }
|
|
920
|
|
921 Slider.prototype.setupEvents = function() {
|
|
922 // installs all event callbacks
|
|
923 registerEvent("mousedown", this.slider, this.onDragStart);
|
|
924 }
|
|
925
|
|
926 Slider.prototype.onDragStart = function(evt) {
|
444
|
927 var slider = sliders[this.id];
|
|
928 activeSlider = slider;
|
|
929 unregisterEvent("mousedown", slider.slider, slider.onDragStart);
|
|
930 registerEvent("mousemove", document, slider.onDrag);
|
|
931 registerEvent("mouseup", document, slider.onDragEnd);
|
|
932 slider.startPos = evtPosition(evt);
|
|
933 slider.startX = slider.sliderRect.getCenter().x;
|
442
|
934 return stopEvent(evt);
|
|
935 }
|
|
936
|
|
937 Slider.prototype.onDrag = function(evt) {
|
444
|
938 var slider = activeSlider;
|
442
|
939 var pos = evtPosition(evt);
|
444
|
940 var currX = slider.slider
|
|
941 var newX = pos.x - slider.startPos + slider.startX;
|
|
942 if (newX < slider.xMin) newX = slider.xMin;
|
|
943 if (newX > slider.xMax) newX = slider.xMax;
|
|
944 moveElement(slider.slider, slider.sliderRect.setCenter(
|
|
945 new Position(newX, slider.Y)));
|
442
|
946 return stopEvent(evt);
|
|
947 }
|
|
948
|
|
949 Slider.prototype.onDragEnd = function(evt) {
|
444
|
950 var slider = activeSlider;
|
|
951 unregisterEvent("mousemove", document, slider.onDrag);
|
|
952 unregisterEvent("mouseup", document, slider.onDragEnd);
|
|
953 slider.onChange(slider.calcValue());
|
|
954 activeSlider = null;
|
442
|
955 return stopEvent(evt);
|
|
956 }
|
|
957
|
|
958 Slider.prototype.onInputChange = function() {
|
444
|
959 var slider = activeSlider;
|
|
960 slider.onChange(s.value);
|
442
|
961 }
|
|
962
|
444
|
963 // :tabSize=4:indentSize=4:noTabs=true:
|
|
964
|