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