| [ Index ] |
krapohl.info |
[Summary view] [Print] [Text view]
1 // =================================================================== 2 // Author: Matt Kruse <matt@mattkruse.com> 3 // WWW: http://www.mattkruse.com/ 4 // 5 // NOTICE: You may use this code for any purpose, commercial or 6 // private, without any further permission from the author. You may 7 // remove this notice from your final code if you wish, however it is 8 // appreciated by the author if at least my web site address is kept. 9 // 10 // You may *NOT* re-distribute this code in any way except through its 11 // use. That means, you can include it in your product, or your web 12 // site, or any other form where the code is actually being used. You 13 // may not put the plain javascript up on your site for download or 14 // include it in your javascript libraries for download. Instead, 15 // please just point to my URL to ensure the most up-to-date versions 16 // of the files. Thanks. 17 // =================================================================== 18 19 20 /* 21 PopupWindow.js 22 Author: Matt Kruse 23 Last modified: 3/21/02 24 25 DESCRIPTION: This object allows you to easily and quickly popup a window 26 in a certain place. The window can either be a DIV or a separate browser 27 window. 28 29 COMPATABILITY: Works with Netscape 4.x, 6.x, IE 5.x on Windows. Some small 30 positioning errors - usually with Window positioning - occur on the 31 Macintosh platform. Due to bugs in Netscape 4.x, populating the popup 32 window with <STYLE> tags may cause errors. 33 34 USAGE: 35 // Create an object for a WINDOW popup 36 var win = new PopupWindow(); 37 38 // Create an object for a DIV window using the DIV named 'mydiv' 39 var win = new PopupWindow('mydiv'); 40 41 // Set the window to automatically hide itself when the user clicks 42 // anywhere else on the page except the popup 43 win.autoHide(); 44 45 // Show the window relative to the anchor name passed in 46 win.showPopup(anchorname); 47 48 // Hide the popup 49 win.hidePopup(); 50 51 // Set the size of the popup window (only applies to WINDOW popups 52 win.setSize(width,height); 53 54 // Populate the contents of the popup window that will be shown. If you 55 // change the contents while it is displayed, you will need to refresh() 56 win.populate(string); 57 58 // Refresh the contents of the popup 59 win.refresh(); 60 61 // Specify how many pixels to the right of the anchor the popup will appear 62 win.offsetX = 50; 63 64 // Specify how many pixels below the anchor the popup will appear 65 win.offsetY = 100; 66 67 NOTES: 68 1) Requires the functions in AnchorPosition.js 69 70 2) Your anchor tag MUST contain both NAME and ID attributes which are the 71 same. For example: 72 <A NAME="test" ID="test"> </A> 73 74 3) There must be at least a space between <A> </A> for IE5.5 to see the 75 anchor tag correctly. Do not do <A></A> with no space. 76 77 4) When a PopupWindow object is created, a handler for 'onmouseup' is 78 attached to any event handler you may have already defined. Do NOT define 79 an event handler for 'onmouseup' after you define a PopupWindow object or 80 the autoHide() will not work correctly. 81 */ 82 83 // Set the position of the popup window based on the anchor 84 function PopupWindow_getXYPosition(anchorname) { 85 var coordinates; 86 if (this.type == "WINDOW") { 87 coordinates = getAnchorWindowPosition(anchorname); 88 } 89 else { 90 coordinates = getAnchorPosition(anchorname); 91 } 92 this.x = coordinates.x; 93 this.y = coordinates.y; 94 } 95 // Set width/height of DIV/popup window 96 function PopupWindow_setSize(width,height) { 97 this.width = width; 98 this.height = height; 99 } 100 // Fill the window with contents 101 function PopupWindow_populate(contents) { 102 this.contents = contents; 103 this.populated = false; 104 } 105 // Refresh the displayed contents of the popup 106 function PopupWindow_refresh() { 107 if (this.divName != null) { 108 // refresh the DIV object 109 if (this.use_gebi) { 110 document.getElementById(this.divName).innerHTML = this.contents; 111 } 112 else if (this.use_css) { 113 document.all[this.divName].innerHTML = this.contents; 114 } 115 else if (this.use_layers) { 116 var d = document.layers[this.divName]; 117 d.document.open(); 118 d.document.writeln(this.contents); 119 d.document.close(); 120 } 121 } 122 else { 123 if (this.popupWindow != null && !this.popupWindow.closed) { 124 this.popupWindow.document.open(); 125 this.popupWindow.document.writeln(this.contents); 126 this.popupWindow.document.close(); 127 this.popupWindow.focus(); 128 } 129 } 130 } 131 // Position and show the popup, relative to an anchor object 132 function PopupWindow_showPopup(anchorname) { 133 this.getXYPosition(anchorname); 134 this.x += this.offsetX; 135 this.y += this.offsetY; 136 if (!this.populated && (this.contents != "")) { 137 this.populated = true; 138 this.refresh(); 139 } 140 if (this.divName != null) { 141 // Show the DIV object 142 if (this.use_gebi) { 143 document.getElementById(this.divName).style.left = this.x; 144 document.getElementById(this.divName).style.top = this.y; 145 document.getElementById(this.divName).style.visibility = "visible"; 146 } 147 else if (this.use_css) { 148 document.all[this.divName].style.left = this.x; 149 document.all[this.divName].style.top = this.y; 150 document.all[this.divName].style.visibility = "visible"; 151 } 152 else if (this.use_layers) { 153 document.layers[this.divName].left = this.x; 154 document.layers[this.divName].top = this.y; 155 document.layers[this.divName].visibility = "visible"; 156 } 157 } 158 else { 159 if (this.popupWindow == null || this.popupWindow.closed) { 160 // If the popup window will go off-screen, move it so it doesn't 161 if (screen && screen.availHeight) { 162 if ((this.y + this.height) > screen.availHeight) { 163 this.y = screen.availHeight - this.height; 164 } 165 } 166 if (screen && screen.availWidth) { 167 if ((this.x + this.width) > screen.availWidth) { 168 this.x = screen.availWidth - this.width; 169 } 170 } 171 this.popupWindow = window.open("about:blank","window_"+anchorname,"toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,alwaysRaised,dependent,titlebar=no,width="+this.width+",height="+this.height+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+""); 172 } 173 this.refresh(); 174 } 175 } 176 // Hide the popup 177 function PopupWindow_hidePopup() { 178 if (this.divName != null) { 179 if (this.use_gebi) { 180 document.getElementById(this.divName).style.visibility = "hidden"; 181 } 182 else if (this.use_css) { 183 document.all[this.divName].style.visibility = "hidden"; 184 } 185 else if (this.use_layers) { 186 document.layers[this.divName].visibility = "hidden"; 187 } 188 } 189 else { 190 if (this.popupWindow && !this.popupWindow.closed) { 191 this.popupWindow.close(); 192 this.popupWindow = null; 193 } 194 } 195 } 196 // Pass an event and return whether or not it was the popup DIV that was clicked 197 function PopupWindow_isClicked(e) { 198 if (this.divName != null) { 199 if (this.use_layers) { 200 var clickX = e.pageX; 201 var clickY = e.pageY; 202 var t = document.layers[this.divName]; 203 if ((clickX > t.left) && (clickX < t.left+t.clip.width) && (clickY > t.top) && (clickY < t.top+t.clip.height)) { 204 return true; 205 } 206 else { return false; } 207 } 208 else if (document.all) { // Need to hard-code this to trap IE for error-handling 209 var t = window.event.srcElement; 210 while (t.parentElement != null) { 211 if (t.id==this.divName) { 212 return true; 213 } 214 t = t.parentElement; 215 } 216 return false; 217 } 218 else if (this.use_gebi) { 219 var t = e.originalTarget; 220 while (t.parentNode != null) { 221 if (t.id==this.divName) { 222 return true; 223 } 224 t = t.parentNode; 225 } 226 return false; 227 } 228 return false; 229 } 230 return false; 231 } 232 233 // Check an onMouseDown event to see if we should hide 234 function PopupWindow_hideIfNotClicked(e) { 235 if (this.autoHideEnabled && !this.isClicked(e)) { 236 this.hidePopup(); 237 } 238 } 239 // Call this to make the DIV disable automatically when mouse is clicked outside it 240 function PopupWindow_autoHide() { 241 this.autoHideEnabled = true; 242 } 243 // This global function checks all PopupWindow objects onmouseup to see if they should be hidden 244 function PopupWindow_hidePopupWindows(e) { 245 for (var i=0; i<popupWindowObjects.length; i++) { 246 if (popupWindowObjects[i] != null) { 247 var p = popupWindowObjects[i]; 248 p.hideIfNotClicked(e); 249 } 250 } 251 } 252 // Run this immediately to attach the event listener 253 function PopupWindow_attachListener() { 254 if (document.layers) { 255 document.captureEvents(Event.MOUSEUP); 256 } 257 window.popupWindowOldEventListener = document.onmouseup; 258 if (window.popupWindowOldEventListener != null) { 259 document.onmouseup = new Function("window.popupWindowOldEventListener(); PopupWindow_hidePopupWindows();"); 260 } 261 else { 262 document.onmouseup = PopupWindow_hidePopupWindows; 263 } 264 } 265 // CONSTRUCTOR for the PopupWindow object 266 // Pass it a DIV name to use a DHTML popup, otherwise will default to window popup 267 function PopupWindow() { 268 if (!window.popupWindowIndex) { window.popupWindowIndex = 0; } 269 if (!window.popupWindowObjects) { window.popupWindowObjects = new Array(); } 270 if (!window.listenerAttached) { 271 window.listenerAttached = true; 272 PopupWindow_attachListener(); 273 } 274 this.index = popupWindowIndex++; 275 popupWindowObjects[this.index] = this; 276 this.divName = null; 277 this.popupWindow = null; 278 this.width=0; 279 this.height=0; 280 this.populated = false; 281 this.visible = false; 282 this.autoHideEnabled = false; 283 284 this.contents = ""; 285 if (arguments.length>0) { 286 this.type="DIV"; 287 this.divName = arguments[0]; 288 } 289 else { 290 this.type="WINDOW"; 291 } 292 this.use_gebi = false; 293 this.use_css = false; 294 this.use_layers = false; 295 if (document.getElementById) { this.use_gebi = true; } 296 else if (document.all) { this.use_css = true; } 297 else if (document.layers) { this.use_layers = true; } 298 else { this.type = "WINDOW"; } 299 this.offsetX = 0; 300 this.offsetY = 0; 301 // Method mappings 302 this.getXYPosition = PopupWindow_getXYPosition; 303 this.populate = PopupWindow_populate; 304 this.refresh = PopupWindow_refresh; 305 this.showPopup = PopupWindow_showPopup; 306 this.hidePopup = PopupWindow_hidePopup; 307 this.setSize = PopupWindow_setSize; 308 this.isClicked = PopupWindow_isClicked; 309 this.autoHide = PopupWindow_autoHide; 310 this.hideIfNotClicked = PopupWindow_hideIfNotClicked; 311 } 312
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Feb 16 22:40:07 2005 | Cross-referenced by PHPXref 0.6 |