Annotation of ECHO_content/js/js_lib.js, revision 1.1.1.1
1.1 casties 1: /* Copyright (C) 2003,2004 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: ROC 3.5.2004
18: first version by Christian Luginbuehl, 01.05.2003
19: Changed for digiLib in Zope by DW 24.03.2004
20: */
21:
22: // fixes for javascript < 1.2
23: if (! Array.prototype.push) {
24: Array.prototype.push = function(val) {
25: this[this.length] = val;
26: return this.length;
27: }
28: Array.prototype.pop = function() {
29: var val = this[this.length-1];
30: this.length -= 1;
31: return val;
32: }
33: }
34:
35: // auxiliary function to crop senseless precision
36: function cropFloat(x) {
37: return parseInt(10000*x)/10000;
38: }
39:
40:
41: /* **********************************************
42: * geometry classes
43: * ******************************************** */
44:
45: /*
46: * Size class
47: */
48: function Size(w, h) {
49: this.width = parseFloat(w);
50: this.height = parseFloat(h);
51: return this;
52: }
53:
54: /*
55: * Position class
56: */
57: function Position(x, y) {
58: this.x = parseFloat(x);
59: this.y = parseFloat(y);
60: return this;
61: }
62:
63: /*
64: * Rectangle class
65: */
66: function Rectangle(x, y, w, h) {
67: this.x = parseFloat(x);
68: this.y = parseFloat(y);
69: this.width = parseFloat(w);
70: this.height = parseFloat(h);
71: return this;
72: }
73: Rectangle.prototype.copy = function() {
74: // returns a copy of this Rectangle
75: return new Rectangle(this.x, this.y, this.width, this.height);
76: }
77: Rectangle.prototype.getPosition = function() {
78: // returns the position of this Rectangle
79: return new Position(this.x, this.y);
80: }
81: Rectangle.prototype.getSize = function() {
82: // returns the size of this Rectangle
83: return new Size(this.width, this.height);
84: }
85: Rectangle.prototype.getArea = function() {
86: // returns the area of this Rectangle
87: return (this.width * this.height);
88: }
89: Rectangle.prototype.containsPosition = function(pos) {
90: // returns if the given Position lies in this Rectangle
91: return ((pos.x >= this.x)&&(pos.y >= this.y)&&(pos.x <= this.x+this.width)&&(pos.y <= this.y+this.width));
92: }
93: Rectangle.prototype.intersect = function(rect) {
94: // returns the intersection of the given Rectangle and this one
95: var sec = rect.copy();
96: if (sec.x < this.x) {
97: sec.width = sec.width - (this.x - sec.x);
98: sec.x = this.x;
99: }
100: if (sec.y < this.y) {
101: sec.height = sec.height - (this.y - sec.y);
102: sec.y = this.y;
103: }
104: if (sec.x + sec.width > this.x + this.width) {
105: sec.width = (this.x + this.width) - sec.x;
106: }
107: if (sec.y + sec.height > this.y + this.height) {
108: sec.height = (this.y + this.height) - sec.y;
109: }
110: return sec;
111: }
112: Rectangle.prototype.fit = function(rect) {
113: // returns a Rectangle that fits into this one (by moving first)
114: var sec = rect.copy();
115: sec.x = Math.max(sec.x, this.x);
116: sec.x = Math.max(sec.x, this.x);
117: if (sec.x + sec.width > this.x + this.width) {
118: sec.x = this.x + this.width - sec.width;
119: }
120: if (sec.y + sec.height > this.y + this.height) {
121: sec.y = this.y + this.height - sec.height;
122: }
123: return sec.intersect(this);
124: }
125:
126: /*
127: * Transform class
128: *
129: * defines a class of affine transformations
130: */
131: function Transform() {
132: this.m00 = 1.0;
133: this.m01 = 0.0;
134: this.m02 = 0.0;
135: this.m10 = 0.0;
136: this.m11 = 1.0;
137: this.m12 = 0.0;
138: this.m20 = 0.0;
139: this.m21 = 0.0;
140: this.m22 = 1.0;
141: return this;
142: }
143: Transform.prototype.concat = function(traf) {
144: // add Transform traf to this Transform
145: for (var i = 0; i < 3; i++) {
146: for (var j = 0; j < 3; j++) {
147: var c = 0.0;
148: for (var k = 0; k < 3; k++) {
149: c += traf["m"+i+k] * this["m"+k+j];
150: }
151: this["m"+i+j] = c;
152: }
153: }
154: return this;
155: }
156: Transform.prototype.transform = function(rect) {
157: // returns transformed Rectangle or Position with this Transform applied
158: var x = this.m00 * rect.x + this.m01 * rect.y + this.m02;
159: var y = this.m10 * rect.x + this.m11 * rect.y + this.m12;
160: if (rect.width) {
161: var width = this.m00 * rect.width + this.m01 * rect.height;
162: var height = this.m10 * rect.width + this.m11 * rect.height;
163: return new Rectangle(x, y, width, height);
164: }
165: return new Position(x, y);
166: }
167: Transform.prototype.invtransform = function(pos) {
168: // returns transformed Position pos with the inverse of this Transform applied
169: var det = this.m00 * this.m11 - this.m01 * this.m10;
170: var x = (this.m11 * pos.x - this.m01 * pos.y - this.m11 * this.m02 + this.m01 * this.m12) / det;
171: var y = (- this.m10 * pos.x + this.m00 * pos.y + this.m10 * this.m02 - this.m00 * this.m12) / det;
172: return new Position(x, y);
173: }
174: function getRotation(angle, pos) {
175: // returns a Transform that is a rotation by angle degrees around [pos.x, pos.y]
176: var traf = new Transform();
177: if (angle != 0) {
178: var t = 2.0 * Math.PI * parseFloat(angle) / 360.0;
179: traf.m00 = Math.cos(t);
180: traf.m01 = - Math.sin(t);
181: traf.m10 = Math.sin(t);
182: traf.m11 = Math.cos(t);
183: traf.m02 = pos.x - pos.x * Math.cos(t) + pos.y * Math.sin(t);
184: traf.m12 = pos.y - pos.x * Math.sin(t) - pos.y * Math.cos(t);
185: }
186: return traf;
187: }
188: function getTranslation(pos) {
189: // returns a Transform that is a translation by [pos.x, pos,y]
190: var traf = new Transform();
191: traf.m02 = pos.x;
192: traf.m12 = pos.y;
193: return traf;
194: }
195: function getScale(size) {
196: // returns a Transform that is a scale by [size.width, size.height]
197: var traf = new Transform();
198: traf.m00 = size.width;
199: traf.m11 = size.height;
200: return traf;
201: }
202:
203:
204: /* **********************************************
205: * parameter routines
206: * ******************************************** */
207:
208: var dlParams = new Object();
209:
210: function newParameter(name, defaultValue, detail) {
211: // create a new parameter with a name and a default value
212: if (dlParams[name]) {
213: alert("Fatal: An object with name '" + name + "' already exists - cannot recreate!");
214: return false;
215: } else {
216: dlParams[name] = new Object();
217: dlParams[name].defaultValue = defaultValue;
218: dlParams[name].hasValue = false;
219: dlParams[name].value = defaultValue;
220: dlParams[name].detail = detail;
221: return dlParams[name];
222: }
223: }
224:
225: function getParameter(name) {
226: // returns the named parameter value or its default value
227: if (dlParams[name]) {
228: if (dlParams[name].hasValue) {
229: return dlParams[name].value;
230: } else {
231: return dlParams[name].defaultValue;
232: }
233: } else {
234: return undefined;
235: }
236: }
237:
238: function setParameter(name, value) {
239: // sets parameter value
240: if (dlParams[name]) {
241: dlParams[name].value = value;
242: dlParams[name].hasValue = true;
243: return true;
244: }
245: return false;
246: }
247:
248: function getAllParameters(detail) {
249: // returns a string of all parameters in query format
250: var params = new Array();
251: for ( param in dlParams ) {
252: if ((dlParams[param].detail <= detail)&&(dlParams[param].hasValue)) {
253: var val = getParameter(param);
254: if (val != "") {
255: params.push(param + "=" + val);
256: }
257: }
258: }
259: return params.join("&");
260: }
261:
262: function parseParameters(query) {
263: // gets parameter values from query format string
264: var params = query.split("&");
265: for (var i = 0; i < params.length; i++) {
266: var keyval = params[i].split("=");
267: if (keyval.length == 2) {
268: setParameter(keyval[0], keyval[1]);
269: }
270: }
271: }
272:
273:
274: /* **********************************************
275: * HTML/DOM routines
276: * ******************************************** */
277:
278: function getElement(tagid) {
279: // returns the element object with the id tagid
280: if (document.getElementById) {
281: return document.getElementById(tagid);
282: } else if (document.all) {
283: alert("document.all!");
284: return document.all[tagid];
285: } else {
286: var s = "";
287: for (var e in document.layers) {
288: s += "'" + e + "', ";
289: }
290: alert("no document.all! "+tagid+" = "+s);
291: return document[tagid];
292: }
293: }
294:
295: function getElementSize(tagid) {
296: // returns a Rectangle with the size and position of the named element
297: var x = 0;
298: var y = 0;
299: var width = 0;
300: var height = 0;
301: var elem = getElement(tagid);
302: if (elem.left) {
303: alert("elem.left!");
304: x = elem.left;
305: y = elem.top;
306: width = elem.width;
307: height = elem.height;
308: } else {
309: if (elem.clientLeft) {
310: // spass mit IE
311: x = elem.clientLeft;
312: y = elem.clientTop;
313: } else {
314: var e = elem;
315: while (e) {
316: x += e.offsetLeft;
317: y += e.offsetTop;
318: e = e.offsetParent;
319: }
320: }
321: width = elem.offsetWidth;
322: height = elem.offsetHeight;
323: }
324: return new Rectangle(x, y, width, height);
325: }
326:
327: function moveElement(tagid, rect) {
328: // moves and sizes the named element
329: var elem = getElement(tagid);
330: if (elem.style) {
331: if (rect.x) {
332: elem.style.left = Math.round(rect.x) + "px";
333: elem.style.top = Math.round(rect.y) + "px";
334: }
335: if (rect.width) {
336: elem.style.width = Math.round(rect.width) + "px";
337: elem.style.height = Math.round(rect.height) + "px";
338: }
339: } else {
340: alert("moveelement: no style property!");
341: if (rect.x) {
342: elem.left = Math.round(rect.x);
343: elem.top = Math.round(rect.y);
344: }
345: if (rect.width) {
346: elem.width = Math.round(rect.width);
347: elem.height = Math.round(rect.height);
348: }
349: }
350: return true;
351: }
352:
353: function showElement(tagid, show) {
354: // shows or hides the named element
355: var elem = getElement(tagid);
356: if (elem.style) {
357: if (show) {
358: elem.style.visibility = "visible";
359: } else {
360: elem.style.visibility = "hidden";
361: }
362: } else {
363: alert("showelement: no style property!");
364: }
365: return true;
366: }
367:
368: function evtPosition(evt) {
369: // returns the on-screen Position of the Event
370: var x;
371: var y;
372: if (document.all) {
373: x = parseInt(document.body.scrollLeft+event.clientX);
374: y = parseInt(document.body.scrollLeft+event.clientY);
375: } else {
376: x = parseInt(evt.pageX);
377: y = parseInt(evt.pageY);
378: }
379: return new Position(x, y);
380: }
381:
382: function registerMouseDown(tagid, handler) {
383: // register a mouse down event handler on the indicated element
384: if ( document.all ) {
385: document.all[tagid].onmousedown = handler;
386: } else if (document.getElementById) {
387: document.getElementById(tagid).addEventListener("mousedown", handler, true);
388: } else {
389: document[tagid].captureEvents(Event.MOUSEDOWN);
390: document[tagid].onmousedown = handler;
391: }
392: return true;
393: }
394:
395: function unregisterMouseDown(tagid, handler) {
396: // unregister the mouse down event handler
397: if ( document.all ) {
398: document.all[tagid].onmousedown = null;
399: } else if (document.getElementById) {
400: document.getElementById(tagid).removeEventListener("mousedown", handler, true);
401: } else {
402: document[tagid].releaseEvents(Event.MOUSEDOWN);
403: }
404: return true;
405: }
406:
407: function registerMouseMove(tagid, handler) {
408: // register a mouse move event handler on the indicated element
409: if ( document.all ) {
410: document.all[tagid].onmousemove = handler;
411: } else if (document.getElementById) {
412: document.getElementById(tagid).addEventListener("mousemove", handler, true);
413: } else {
414: document[tagid].captureEvents(Event.MOUSEMOVE);
415: document[tagid].onmousemove = handler;
416: }
417: return true;
418: }
419:
420: function unregisterMouseMove(tagid, handler) {
421: // unregister the mouse move event handler
422: if ( document.all ) {
423: document.all[tagid].onmousemove = null;
424: } else if (document.getElementById) {
425: document.getElementById(tagid).removeEventListener("mousemove", handler, true);
426: } else {
427: document[tagid].releaseEvents(Event.MOUSEMOVE);
428: }
429: return true;
430: }
431:
432: function registerKeyDown(handler) {
433: // register a key down handler
434: if ( document.all ) {
435: this.document.onkeypress = handler
436: } else if ( typeof(document.addEventListener) == "function" ) {
437: this.document.addEventListener('keypress', handler, true);
438: } else {
439: window.captureEvents(Event.KEYDOWN);
440: window.onkeydown = handler;
441: }
442: return true;
443: }
444:
445: function getWinSize() {
446: // returns a Size with the current window size (from www.quirksmode.org)
447: var wsize = new Size(100, 100);
448: if (self.innerHeight) {
449: // all except Explorer
450: wsize.width = self.innerWidth;
451: wsize.height = self.innerHeight;
452: } else if (document.documentElement && document.documentElement.clientHeight) {
453: // Explorer 6 Strict Mode
454: wsize.width = document.documentElement.clientWidth;
455: wsize.height = document.documentElement.clientHeight;
456: } else if (document.body) {
457: // other Explorers
458: wsize.width = document.body.clientWidth;
459: wsize.height = document.body.clientHeight;
460: }
461: return wsize;
462: }
463:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>