comparison client/digitallibrary/greyskin/baselib.js @ 433:35044254c5af

added Rectangle methods
author hertzhaft
date Mon, 09 Jan 2006 17:41:34 +0100
parents 7207a5549301
children 14557d3879d2
comparison
equal deleted inserted replaced
432:0e5aebfeb7d4 433:35044254c5af
21 Martin Raspe, 12.12.2005 (changes for Digilib NG) 21 Martin Raspe, 12.12.2005 (changes for Digilib NG)
22 22
23 */ 23 */
24 24
25 // was: function base_init() { 25 // was: function base_init() {
26 baseLibVersion = "2.0"; 26 baseLibVersion = "2.006";
27 browserType = getBrowserType(); 27 browserType = getBrowserType();
28 28
29 29
30 function getInt(n) { 30 function getInt(n) {
31 // returns always an integer 31 // returns always an integer
136 } 136 }
137 Rectangle.prototype.getPosition = function() { 137 Rectangle.prototype.getPosition = function() {
138 // returns the position of this Rectangle 138 // returns the position of this Rectangle
139 return new Position(this.x, this.y); 139 return new Position(this.x, this.y);
140 } 140 }
141 Rectangle.prototype.getPt1 = Rectangle.prototype.getPosition;
142
143 Rectangle.prototype.getPt2 = function() {
144 // returns the second point position of this Rectangle
145 return new Position(this.x + this.width, this.y + this.height);
146 }
147 Rectangle.prototype.setPt1 = function(pos) {
148 // sets the first point to position pos
149 this.x = pos.x;
150 this.y = pos.y;
151 return this;
152 }
153 Rectangle.prototype.setPt2 = function(pos) {
154 // sets the second point to position pos
155 this.width = pos.x - this.x;
156 this.height = pos.y - this.y;
157 return this;
158 }
159 Rectangle.prototype.getCenter = function() {
160 // returns the center position of this Rectangle
161 return new Position(this.x + this.width / 2, this.y + this.height / 2);
162 }
163 Rectangle.prototype.setCenter = function(pos) {
164 // moves this Rectangle's center to position pos
165 this.x = pos.x - this.width / 2;
166 this.y = pos.y - this.height / 2;
167 return this;
168 }
141 Rectangle.prototype.getSize = function() { 169 Rectangle.prototype.getSize = function() {
142 // returns the size of this Rectangle 170 // returns the size of this Rectangle
143 return new Size(this.width, this.height); 171 return new Size(this.width, this.height);
144 } 172 }
145 Rectangle.prototype.equals = function(other) { 173 Rectangle.prototype.equals = function(other) {
149 ); 177 );
150 } 178 }
151 Rectangle.prototype.getArea = function() { 179 Rectangle.prototype.getArea = function() {
152 // returns the area of this Rectangle 180 // returns the area of this Rectangle
153 return (this.width * this.height); 181 return (this.width * this.height);
182 }
183 Rectangle.prototype.normalize = function() {
184 // eliminates negative width and height
185 var p = this.getPt2();
186 this.x = Math.min(this.x, p.x);
187 this.y = Math.min(this.y, p.y);
188 this.width = Math.abs(this.width);
189 this.height = Math.abs(this.height);
190 return this;
154 } 191 }
155 Rectangle.prototype.containsPosition = function(pos) { 192 Rectangle.prototype.containsPosition = function(pos) {
156 // returns if Position "pos" lies inside of this rectangle 193 // returns if Position "pos" lies inside of this rectangle
157 return ((pos.x >= this.x) 194 return ((pos.x >= this.x)
158 && (pos.y >= this.y) 195 && (pos.y >= this.y)
168 rect.y + rect.height 205 rect.y + rect.height
169 ))); 206 )));
170 } 207 }
171 Rectangle.prototype.stayInside = function(rect) { 208 Rectangle.prototype.stayInside = function(rect) {
172 // changes this rectangle's x/y values so it stays inside of rectangle rect 209 // changes this rectangle's x/y values so it stays inside of rectangle rect
173 // but not its proportions 210 // keeping the proportions
174 if (this.x < rect.x) this.x = rect.x; 211 if (this.x < rect.x) this.x = rect.x;
175 if (this.y < rect.y) this.y = rect.y; 212 if (this.y < rect.y) this.y = rect.y;
176 if (this.x + this.width > rect.x + rect.width) 213 if (this.x + this.width > rect.x + rect.width)
177 this.x = rect.x + rect.width - this.width; 214 this.x = rect.x + rect.width - this.width;
178 if (this.y + this.height > rect.y + rect.height) 215 if (this.y + this.height > rect.y + rect.height)
179 this.y = rect.y + rect.height - this.height; 216 this.y = rect.y + rect.height - this.height;
217 return this;
218 }
219 Rectangle.prototype.clipTo = function(rect) {
220 // clips this rectangle so it stays inside of rectangle rect
221 p1 = rect.getPt1();
222 p2 = rect.getPt2();
223 this2 = this.getPt2();
224 this.setPt1(new Position(Math.max(this.x, p1.x), Math.max(this.y, p1.y)));
225 this.setPt2(new Position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y)));
180 return this; 226 return this;
181 } 227 }
182 Rectangle.prototype.intersect = function(rect) { 228 Rectangle.prototype.intersect = function(rect) {
183 // returns the intersection of the given Rectangle and this one 229 // returns the intersection of the given Rectangle and this one
184 // FIX ME: not really, it should return null if there is no overlap 230 // FIX ME: not really, it should return null if there is no overlap
478 var pos = getElementPosition(elem); 524 var pos = getElementPosition(elem);
479 var size = getElementSize(elem); 525 var size = getElementSize(elem);
480 return new Rectangle(pos.x, pos.y, size.width, size.height); 526 return new Rectangle(pos.x, pos.y, size.width, size.height);
481 } 527 }
482 528
483
484
485 function moveElement(elem, rect) { 529 function moveElement(elem, rect) {
486 // moves and sizes the element 530 // moves and sizes the element
487 if (elem.style) { 531 if (elem.style) {
488 if (defined(rect.x)) { 532 if (defined(rect.x)) {
489 elem.style.left = Math.round(rect.x) + "px"; 533 elem.style.left = Math.round(rect.x) + "px";
656 wsize.height = document.body.clientHeight; 700 wsize.height = document.body.clientHeight;
657 } 701 }
658 return wsize; 702 return wsize;
659 } 703 }
660 704
705 function getWinRect() {
706 var size = getWinSize();
707 return new Rectangle(0, 0, size.width, size.height);
708 }
709
661 function openWin(url, name, params) { 710 function openWin(url, name, params) {
662 // open browser window 711 // open browser window
663 var ow = window.open(url, name, params); 712 var ow = window.open(url, name, params);
664 ow.focus(); 713 ow.focus();
665 } 714 }
715
716 /* **********************************************
717 * cookie class
718 * ******************************************** */
719
720 function Cookie() {
721 return this.read();
722 }
723
724 Cookie.prototype.read = function() {
725 var s = document.cookie;
726 var lines = s.split("; ");
727 for (var i in lines) {
728 var line = lines[i];
729 var sep = line.indexOf("=");
730 if (sep != -1) this.add(
731 line.substr(0, sep),
732 line.substr(sep + 1)
733 );
734 }
735 return this;
736 }
737
738 Cookie.prototype.store = function() {
739 var lines = new Array();
740 for (var i in this) {
741 var item = this[i];
742 if (typeof(item) == typeof(lines)) // Array
743 lines.push(i + "=" + item.join(","));
744 else if (typeof(item) != "function") // single item
745 lines.push(i + "=" + item);
746 }
747 // var s = lines.join(";")
748 for (line in lines) document.cookie = lines[line];
749 return this;
750 }
751
752 Cookie.prototype.add = function(key, value) {
753 if (value.indexOf(",") == -1)
754 this[key] = value; // single value
755 else
756 this[key] = value.split(","); // list of values
757 return this[key];
758 }
759
760 Cookie.prototype.get = function(key) {
761 return this[key];
762 }
763
764 Cookie.prototype.addbool = function(key, value) {
765 this[key] = Boolean(value).toString();
766 return this[key];
767 }
768
769 Cookie.prototype.getbool = function(key) {
770 var val = this[key];
771 return (val > "") && (val != "0") && (val != "false");
772 }
773
774 Cookie.prototype.remove = function(key) {
775 delete this[key];
776 }
777
778 // :tabSize=4:indentSize=4:noTabs=true:
779