comparison client/digitallibrary/greyskin/baselib.js @ 394:82e86b6e4fda

simplyfications
author hertzhaft
date Thu, 08 Dec 2005 13:16:43 +0100
parents
children 8b9b4513cd64
comparison
equal deleted inserted replaced
393:c577e67c81e7 394:82e86b6e4fda
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
21
22 */
23
24 function base_init() {
25 // init function
26 baseScriptVersion = "1.2";
27 dlParams = new Object();
28 browserType = getBrowserType();
29 }
30
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
46 return parseInt(10000*x)/10000;
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 }
102
103
104 /*
105 * Position class
106 */
107 function Position(x, y) {
108 this.x = parseFloat(x);
109 this.y = parseFloat(y);
110 return this;
111 }
112 Position.prototype.toString = function() {
113 return this.x + "," + this.y;
114 }
115
116 /*
117 * Rectangle class
118 */
119 function Rectangle(x, y, w, h) {
120 this.x = parseFloat(x);
121 this.y = parseFloat(y);
122 this.width = parseFloat(w);
123 this.height = parseFloat(h);
124 return this;
125 }
126 Rectangle.prototype.toString = function() {
127 return this.width+"x"+this.height+"@"+this.x+","+this.y;
128 }
129 Rectangle.prototype.copy = function() {
130 // returns a copy of this Rectangle
131 return new Rectangle(this.x, this.y, this.width, this.height);
132 }
133 Rectangle.prototype.getPosition = function() {
134 // returns the position of this Rectangle
135 return new Position(this.x, this.y);
136 }
137 Rectangle.prototype.getSize = function() {
138 // returns the size of this Rectangle
139 return new Size(this.width, this.height);
140 }
141 Rectangle.prototype.getArea = function() {
142 // returns the area of this Rectangle
143 return (this.width * this.height);
144 }
145 Rectangle.prototype.containsPosition = function(pos) {
146 // returns if the given Position lies in this Rectangle
147 return ((pos.x >= this.x)&&(pos.y >= this.y)&&(pos.x <= this.x+this.width)&&(pos.y <= this.y+this.width));
148 }
149 Rectangle.prototype.intersect = function(rect) {
150 // returns the intersection of the given Rectangle and this one
151 var sec = rect.copy();
152 if (sec.x < this.x) {
153 sec.width = sec.width - (this.x - sec.x);
154 sec.x = this.x;
155 }
156 if (sec.y < this.y) {
157 sec.height = sec.height - (this.y - sec.y);
158 sec.y = this.y;
159 }
160 if (sec.x + sec.width > this.x + this.width) {
161 sec.width = (this.x + this.width) - sec.x;
162 }
163 if (sec.y + sec.height > this.y + this.height) {
164 sec.height = (this.y + this.height) - sec.y;
165 }
166 return sec;
167 }
168 Rectangle.prototype.fit = function(rect) {
169 // returns a Rectangle that fits into this one (by moving first)
170 var sec = rect.copy();
171 sec.x = Math.max(sec.x, this.x);
172 sec.x = Math.max(sec.x, this.x);
173 if (sec.x + sec.width > this.x + this.width) {
174 sec.x = this.x + this.width - sec.width;
175 }
176 if (sec.y + sec.height > this.y + this.height) {
177 sec.y = this.y + this.height - sec.height;
178 }
179 return sec.intersect(this);
180 }
181
182 /*
183 * Transform class
184 *
185 * defines a class of affine transformations
186 */
187 function Transform() {
188 this.m00 = 1.0;
189 this.m01 = 0.0;
190 this.m02 = 0.0;
191 this.m10 = 0.0;
192 this.m11 = 1.0;
193 this.m12 = 0.0;
194 this.m20 = 0.0;
195 this.m21 = 0.0;
196 this.m22 = 1.0;
197 return this;
198 }
199 Transform.prototype.concat = function(traf) {
200 // add Transform traf to this Transform
201 for (var i = 0; i < 3; i++) {
202 for (var j = 0; j < 3; j++) {
203 var c = 0.0;
204 for (var k = 0; k < 3; k++) {
205 c += traf["m"+i+k] * this["m"+k+j];
206 }
207 this["m"+i+j] = c;
208 }
209 }
210 return this;
211 }
212 Transform.prototype.transform = function(rect) {
213 // returns transformed Rectangle or Position with this Transform applied
214 var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
215 var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
216 if (rect.width) {
217 var width = this.m00 * rect.width + this.m01 * rect.height;
218 var height = this.m10 * rect.width + this.m11 * rect.height;
219 return new Rectangle(x, y, width, height);
220 }
221 return new Position(x, y);
222 }
223 Transform.prototype.invtransform = function(pos) {
224 // returns transformed Position pos with the inverse of this Transform applied
225 var det = this.m00 * this.m11 - this.m01 * this.m10;
226 var x = (this.m11 * pos.x - this.m01 * pos.y - this.m11 * this.m02 + this.m01 * this.m12) / det;
227 var y = (- this.m10 * pos.x + this.m00 * pos.y + this.m10 * this.m02 - this.m00 * this.m12) / det;
228 return new Position(x, y);
229 }
230 function getRotation(angle, pos) {
231 // returns a Transform that is a rotation by angle degrees around [pos.x, pos.y]
232 var traf = new Transform();
233 if (angle != 0) {
234 var t = 2.0 * Math.PI * parseFloat(angle) / 360.0;
235 traf.m00 = Math.cos(t);
236 traf.m01 = - Math.sin(t);
237 traf.m10 = Math.sin(t);
238 traf.m11 = Math.cos(t);
239 traf.m02 = pos.x - pos.x * Math.cos(t) + pos.y * Math.sin(t);
240 traf.m12 = pos.y - pos.x * Math.sin(t) - pos.y * Math.cos(t);
241 }
242 return traf;
243 }
244 function getTranslation(pos) {
245 // returns a Transform that is a translation by [pos.x, pos,y]
246 var traf = new Transform();
247 traf.m02 = pos.x;
248 traf.m12 = pos.y;
249 return traf;
250 }
251 function getScale(size) {
252 // returns a Transform that is a scale by [size.width, size.height]
253 var traf = new Transform();
254 traf.m00 = size.width;
255 traf.m11 = size.height;
256 return traf;
257 }
258
259
260 /* **********************************************
261 * parameter routines
262 * ******************************************** */
263
264 function newParameter(name, defaultValue, detail) {
265 // create a new parameter with a name and a default value
266 if (!defined(dlParams[name])) dlParams[name] = new Object(); // no error condition
267 //alert("Fatal: An object with name '" + name + "' already exists - cannot recreate!");
268 //return false;
269 dlParams[name].defaultValue = defaultValue;
270 dlParams[name].hasValue = false;
271 dlParams[name].value = defaultValue;
272 dlParams[name].detail = detail;
273 return dlParams[name];
274 }
275
276 function getParameter(name) {
277 // returns the named parameter value or its default value
278 if (defined(dlParams[name])) {
279 if (dlParams[name].hasValue) {
280 return dlParams[name].value;
281 } else {
282 return dlParams[name].defaultValue;
283 }
284 } else {
285 return null;
286 }
287 }
288
289 function setParameter(name, value, relative) {
290 // sets parameter value (relative values with +/- unless literal)
291 if (defined(dlParams[name])) {
292 if ((relative)&&(value.slice)) {
293 var sign = value.slice(0,1);
294 if (sign == '+') {
295 dlParams[name].value = parseFloat(dlParams[name].value) + parseFloat(value.slice(1));
296 } else if (sign == '-') {
297 dlParams[name].value = parseFloat(dlParams[name].value) - parseFloat(value.slice(1));
298 } else {
299 dlParams[name].value = value;
300 }
301 } else {
302 dlParams[name].value = value;
303 }
304 dlParams[name].hasValue = true;
305 return true;
306 }
307 return false;
308 }
309
310 function hasParameter(name) {
311 // returns if the parameter's value has been set
312 if (defined(dlParams[name])) {
313 return dlParams[name].hasValue;
314 }
315 return false;
316 }
317
318 function getAllParameters(detail) {
319 // returns a string of all parameters in query format
320 if (! detail) {
321 detail = 255;
322 }
323 var params = new Array();
324 for (param in dlParams) {
325 if (((dlParams[param].detail & detail) > 0)&&(dlParams[param].hasValue)) {
326 var val = getParameter(param);
327 if (val != "") {
328 params.push(param + "=" + val);
329 }
330 }
331 }
332 return params.join("&");
333 }
334
335 getQueryString = getAllParameters;
336
337 function parseParameters(query) {
338 // gets parameter values from query format string
339 var params = query.split("&");
340 for (var i = 0; i < params.length; i++) {
341 var keyval = params[i].split("=");
342 if (keyval.length == 2) {
343 setParameter(keyval[0], keyval[1]);
344 }
345 }
346 }
347
348
349 /* **********************************************
350 * HTML/DOM routines
351 * ******************************************** */
352
353 function getElement(tagid, quiet) {
354 // returns the element object with the id tagid
355 var e;
356 if (document.getElementById) {
357 e = document.getElementById(tagid);
358 } else if (document.all) {
359 alert("document.all!");
360 e = document.all[tagid];
361 } else if (document.layers) {
362 e = document.layers[tagid];
363 }
364 if (e) {
365 return e;
366 } else {
367 if (! quiet) {
368 alert("unable to find element: "+tagid);
369 }
370 return null;
371 }
372 }
373
374 function getElementPosition(elem) {
375 // returns a Position with the position of the element
376 var x = 0;
377 var y = 0;
378 if (defined(elem.offsetLeft)) {
379 var e = elem;
380 while (e) {
381 if (defined(e.clientLeft)) {
382 // special for IE
383 if (browserType.isMac) {
384 if (e.offsetParent.tagName == "BODY") {
385 // IE for Mac extraspecial
386 x += e.clientLeft;
387 y += e.clientTop;
388 break;
389 }
390 } else {
391 if ((e.tagName != "TABLE") && (e.tagName != "BODY")) {
392 x += e.clientLeft;
393 y += e.clientTop;
394 }
395 }
396 }
397 x += e.offsetLeft;
398 y += e.offsetTop;
399 e = e.offsetParent;
400 }
401 } else if (defined(elem.x)) {
402 x = elem.x;
403 y = elem.y;
404 } else if (defined(elem.pageX)) {
405 x = elem.pageX;
406 y = elem.pageY;
407 } else {
408 alert("unable to get position of "+elem+" (id:"+elem.id+")");
409 }
410 return new Position(getInt(x), getInt(y));
411 }
412
413 function getElementSize(elem) {
414 // returns a Rectangle with the size of the element
415 var width = 0;
416 var height = 0;
417 if (defined(elem.offsetWidth)) {
418 width = elem.offsetWidth;
419 height = elem.offsetHeight;
420 } else if (defined(elem.width)) {
421 width = elem.width;
422 height = elem.height;
423 } else if (defined(elem.clip.width)) {
424 width = elem.clip.width;
425 height = elem.clip.height;
426 } else {
427 alert("unable to get size of "+elem+" (id:"+elem.id+")");
428 }
429 return new Size(getInt(width), getInt(height));
430 }
431
432 function getElementRect(elem) {
433 // returns a Rectangle with the size and position of the element
434 var pos = getElementPosition(elem);
435 var size = getElementSize(elem);
436 return new Rectangle(pos.x, pos.y, size.width, size.height);
437 }
438
439
440
441 function moveElement(elem, rect) {
442 // moves and sizes the element
443 if (elem.style) {
444 if (defined(rect.x)) {
445 elem.style.left = Math.round(rect.x) + "px";
446 elem.style.top = Math.round(rect.y) + "px";
447 }
448 if (defined(rect.width)) {
449 elem.style.width = Math.round(rect.width) + "px";
450 elem.style.height = Math.round(rect.height) + "px";
451 }
452 } else if (document.layers) {
453 if (defined(rect.x)) {
454 elem.pageX = getInt(rect.x);
455 elem.pageY = getInt(rect.y);
456 }
457 if (defined(rect.width)) {
458 elem.clip.width = getInt(rect.width);
459 elem.clip.height = getInt(rect.height);
460 }
461 } else {
462 alert("moveelement: no style nor layer property!");
463 return false;
464 }
465 return true;
466 }
467
468 function showElement(elem, show) {
469 // shows or hides the element
470 if (elem.style) {
471 if (show) {
472 elem.style.visibility = "visible";
473 } else {
474 elem.style.visibility = "hidden";
475 }
476 } else if (defined(elem.visibility)) {
477 if (show) {
478 elem.visibility = "show";
479 } else {
480 elem.visibility = "hide";
481 }
482 } else {
483 alert("showelement: no style nor layer property!");
484 }
485 return true;
486 }
487
488 function evtPosition(evt) {
489 // returns the on-screen Position of the Event
490 var x;
491 var y;
492 evt = (evt) ? evt : window.event;
493 if (!evt) {
494 alert("no event found! "+evt);
495 return;
496 }
497 if (defined(evt.pageX)) {
498 x = parseInt(evt.pageX);
499 y = parseInt(evt.pageY);
500 } else if (defined(evt.clientX)) {
501 x = parseInt(document.body.scrollLeft+evt.clientX);
502 y = parseInt(document.body.scrollTop+evt.clientY);
503 } else {
504 alert("evtPosition: don't know how to deal with "+evt);
505 }
506 return new Position(x, y);
507 }
508
509 function registerEvent(type, elem, handler) {
510 // register the given event handler on the indicated element
511 if (elem.addEventListener) {
512 elem.addEventListener(type, handler, false);
513 } else {
514 if (type == "mousedown") {
515 if (elem.captureEvents) {
516 elem.captureEvents(Event.MOUSEDOWN);
517 }
518 elem.onmousedown = handler;
519 } else if (type == "mouseup") {
520 if (elem.captureEvents) {
521 elem.captureEvents(Event.MOUSEUP);
522 }
523 elem.onmouseup = handler;
524 } else if (type == "mousemove") {
525 if (elem.captureEvents) {
526 elem.captureEvents(Event.MOUSEMOVE);
527 }
528 elem.onmousemove = handler;
529 } else if (type == "keypress") {
530 if (elem.captureEvents) {
531 elem.captureEvents(Event.KEYPRESS);
532 }
533 elem.onkeypress = handler;
534 } else {
535 alert("registerEvent: unknown event type "+type);
536 return false;
537 }
538 }
539 return true;
540 }
541
542 function unregisterEvent(type, elem, handler) {
543 // unregister the given event handler from the indicated element
544 if (elem.removeEventListener) {
545 elem.removeEventListener(type, handler, false);
546 } else {
547 if (type == "mousedown") {
548 if (elem.releaseEvents) {
549 elem.releaseEvents(Event.MOUSEDOWN);
550 }
551 elem.onmousedown = null;
552 } else if (type == "mouseup") {
553 if (elem.releaseEvents) {
554 elem.releaseEvents(Event.MOUSEUP);
555 }
556 elem.onmouseup = null;
557 } else if (type == "mousemove") {
558 if (elem.releaseEvents) {
559 elem.releaseEvents(Event.MOUSEMOVE);
560 }
561 elem.onmousemove = null;
562 } else if (type == "keypress") {
563 if (elem.releaseEvents) {
564 elem.releaseEvents(Event.KEYPRESS);
565 }
566 elem.onkeypress = null;
567 } else {
568 alert("unregisterEvent: unknown event type "+type);
569 return false;
570 }
571 }
572 return true;
573 }
574
575
576 // old registerXXYY API for compatibility
577 function registerMouseDown(elem, handler) {
578 return registerEvent("mousedown", elem, handler);
579 }
580 function unregisterMouseDown(elem, handler) {
581 return unregisterEvent("mousedown", elem, handler);
582 }
583 function registerMouseMove(elem, handler) {
584 return registerEvent("mousemove", elem, handler);
585 }
586 function unregisterMouseMove(elem, handler) {
587 return unregisterEvent("mousemove", elem, handler);
588 }
589 function registerKeyDown(handler) {
590 return registerEvent("keypress", elem, handler);
591 }
592
593
594 function getWinSize() {
595 // returns a Size with the current window size (mostly from www.quirksmode.org)
596 var wsize = new Size(100, 100);
597 if (defined(self.innerHeight)) {
598 // all except Explorer
599 if ((self.innerWidth == 0)||(self.innerHeight == 0)) {
600 // Safari 1.2 bug
601 if (parent) {
602 parent.innerHeight;
603 parent.innerWidth;
604 }
605 }
606 wsize.width = self.innerWidth;
607 wsize.height = self.innerHeight;
608 } else if (document.documentElement && document.documentElement.clientHeight) {
609 // Explorer 6 Strict Mode
610 wsize.width = document.documentElement.clientWidth;
611 wsize.height = document.documentElement.clientHeight;
612 } else if (document.body) {
613 // other Explorers
614 wsize.width = document.body.clientWidth;
615 wsize.height = document.body.clientHeight;
616 }
617 return wsize;
618 }
619
620 function openWin(url, name, params) {
621 // open browser window
622 var ow = window.open(url, name, params);
623 ow.focus();
624 }