PageRenderTime 60ms CodeModel.GetById 13ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/subModal.js

http://submodal.googlecode.com/
JavaScript | 305 lines | 187 code | 39 blank | 79 comment | 50 complexity | 65dde835686fd9a31bba54b087cb14d0 MD5 | raw file
  1/**
  2 * SUBMODAL v1.6
  3 * Used for displaying DHTML only popups instead of using buggy modal windows.
  4 *
  5 * By Subimage LLC
  6 * http://www.subimage.com
  7 *
  8 * Contributions by:
  9 * 	Eric Angel - tab index code
 10 * 	Scott - hiding/showing selects for IE users
 11 *	Todd Huss - inserting modal dynamically and anchor classes
 12 *
 13 * Up to date code can be found at http://submodal.googlecode.com
 14 */
 15
 16// Popup code
 17var gPopupMask = null;
 18var gPopupContainer = null;
 19var gPopFrame = null;
 20var gReturnFunc;
 21var gPopupIsShown = false;
 22var gDefaultPage = "/loading.html";
 23var gHideSelects = false;
 24var gReturnVal = null;
 25
 26var gTabIndexes = new Array();
 27// Pre-defined list of tags we want to disable/enable tabbing into
 28var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	
 29
 30// If using Mozilla or Firefox, use Tab-key trap.
 31if (!document.all) {
 32	document.onkeypress = keyDownHandler;
 33}
 34
 35/**
 36 * Initializes popup code on load.	
 37 */
 38function initPopUp() {
 39	// Add the HTML to the body
 40	theBody = document.getElementsByTagName('BODY')[0];
 41	popmask = document.createElement('div');
 42	popmask.id = 'popupMask';
 43	popcont = document.createElement('div');
 44	popcont.id = 'popupContainer';
 45	popcont.innerHTML = '' +
 46		'<div id="popupInner">' +
 47			'<div id="popupTitleBar">' +
 48				'<div id="popupTitle"></div>' +
 49				'<div id="popupControls">' +
 50					'<img src="close.gif" onclick="hidePopWin(false);" id="popCloseBox" />' +
 51				'</div>' +
 52			'</div>' +
 53			'<iframe src="'+ gDefaultPage +'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
 54		'</div>';
 55	theBody.appendChild(popmask);
 56	theBody.appendChild(popcont);
 57	
 58	gPopupMask = document.getElementById("popupMask");
 59	gPopupContainer = document.getElementById("popupContainer");
 60	gPopFrame = document.getElementById("popupFrame");	
 61	
 62	// check to see if this is IE version 6 or lower. hide select boxes if so
 63	// maybe they'll fix this in version 7?
 64	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
 65	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
 66		gHideSelects = true;
 67	}
 68	
 69	// Add onclick handlers to 'a' elements of class submodal or submodal-width-height
 70	var elms = document.getElementsByTagName('a');
 71	for (i = 0; i < elms.length; i++) {
 72		if (elms[i].className.indexOf("submodal") == 0) { 
 73			// var onclick = 'function (){showPopWin(\''+elms[i].href+'\','+width+', '+height+', null);return false;};';
 74			// elms[i].onclick = eval(onclick);
 75			elms[i].onclick = function(){
 76				// default width and height
 77				var width = 400;
 78				var height = 200;
 79				// Parse out optional width and height from className
 80				params = this.className.split('-');
 81				if (params.length == 3) {
 82					width = parseInt(params[1]);
 83					height = parseInt(params[2]);
 84				}
 85				showPopWin(this.href,width,height,null); return false;
 86			}
 87		}
 88	}
 89}
 90addEvent(window, "load", initPopUp);
 91
 92 /**
 93	* @argument width - int in pixels
 94	* @argument height - int in pixels
 95	* @argument url - url to display
 96	* @argument returnFunc - function to call when returning true from the window.
 97	* @argument showCloseBox - show the close box - default true
 98	*/
 99function showPopWin(url, width, height, returnFunc, showCloseBox) {
100	// show or hide the window close widget
101	if (showCloseBox == null || showCloseBox == true) {
102		document.getElementById("popCloseBox").style.display = "block";
103	} else {
104		document.getElementById("popCloseBox").style.display = "none";
105	}
106	gPopupIsShown = true;
107	disableTabIndexes();
108	gPopupMask.style.display = "block";
109	gPopupContainer.style.display = "block";
110	// calculate where to place the window on screen
111	centerPopWin(width, height);
112	
113	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
114
115
116	gPopupContainer.style.width = width + "px";
117	gPopupContainer.style.height = (height+titleBarHeight) + "px";
118	
119	setMaskSize();
120
121	// need to set the width of the iframe to the title bar width because of the dropshadow
122	// some oddness was occuring and causing the frame to poke outside the border in IE6
123	gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
124	gPopFrame.style.height = (height) + "px";
125	
126	// set the url
127	gPopFrame.src = url;
128	
129	gReturnFunc = returnFunc;
130	// for IE
131	if (gHideSelects == true) {
132		hideSelectBoxes();
133	}
134	
135	window.setTimeout("setPopTitle();", 600);
136}
137
138//
139var gi = 0;
140function centerPopWin(width, height) {
141	if (gPopupIsShown == true) {
142		if (width == null || isNaN(width)) {
143			width = gPopupContainer.offsetWidth;
144		}
145		if (height == null) {
146			height = gPopupContainer.offsetHeight;
147		}
148		
149		//var theBody = document.documentElement;
150		var theBody = document.getElementsByTagName("BODY")[0];
151		//theBody.style.overflow = "hidden";
152		var scTop = parseInt(getScrollTop(),10);
153		var scLeft = parseInt(theBody.scrollLeft,10);
154	
155		setMaskSize();
156		
157		//window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
158		
159		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
160		
161		var fullHeight = getViewportHeight();
162		var fullWidth = getViewportWidth();
163		
164		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
165		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
166		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
167	}
168}
169addEvent(window, "resize", centerPopWin);
170addEvent(window, "scroll", centerPopWin);
171window.onscroll = centerPopWin;
172
173
174/**
175 * Sets the size of the popup mask.
176 *
177 */
178function setMaskSize() {
179	var theBody = document.getElementsByTagName("BODY")[0];
180			
181	var fullHeight = getViewportHeight();
182	var fullWidth = getViewportWidth();
183	
184	// Determine what's bigger, scrollHeight or fullHeight / width
185	if (fullHeight > theBody.scrollHeight) {
186		popHeight = fullHeight;
187	} else {
188		popHeight = theBody.scrollHeight;
189	}
190	
191	if (fullWidth > theBody.scrollWidth) {
192		popWidth = fullWidth;
193	} else {
194		popWidth = theBody.scrollWidth;
195	}
196	
197	gPopupMask.style.height = popHeight + "px";
198	gPopupMask.style.width = popWidth + "px";
199}
200
201/**
202 * @argument callReturnFunc - bool - determines if we call the return function specified
203 * @argument returnVal - anything - return value 
204 */
205function hidePopWin(callReturnFunc) {
206	gPopupIsShown = false;
207	var theBody = document.getElementsByTagName("BODY")[0];
208	theBody.style.overflow = "";
209	restoreTabIndexes();
210	if (gPopupMask == null) {
211		return;
212	}
213	gPopupMask.style.display = "none";
214	gPopupContainer.style.display = "none";
215	if (callReturnFunc == true && gReturnFunc != null) {
216		// Set the return code to run in a timeout.
217		// Was having issues using with an Ajax.Request();
218		gReturnVal = window.frames["popupFrame"].returnVal;
219		window.setTimeout('gReturnFunc(gReturnVal);', 1);
220	}
221	gPopFrame.src = gDefaultPage;
222	// display all select boxes
223	if (gHideSelects == true) {
224		displaySelectBoxes();
225	}
226}
227
228/**
229 * Sets the popup title based on the title of the html document it contains.
230 * Uses a timeout to keep checking until the title is valid.
231 */
232function setPopTitle() {
233	return;
234	if (window.frames["popupFrame"].document.title == null) {
235		window.setTimeout("setPopTitle();", 10);
236	} else {
237		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
238	}
239}
240
241// Tab key trap. iff popup is shown and key was [TAB], suppress it.
242// @argument e - event - keyboard event that caused this function to be called.
243function keyDownHandler(e) {
244    if (gPopupIsShown && e.keyCode == 9)  return false;
245}
246
247// For IE.  Go through predefined tags and disable tabbing into them.
248function disableTabIndexes() {
249	if (document.all) {
250		var i = 0;
251		for (var j = 0; j < gTabbableTags.length; j++) {
252			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
253			for (var k = 0 ; k < tagElements.length; k++) {
254				gTabIndexes[i] = tagElements[k].tabIndex;
255				tagElements[k].tabIndex="-1";
256				i++;
257			}
258		}
259	}
260}
261
262// For IE. Restore tab-indexes.
263function restoreTabIndexes() {
264	if (document.all) {
265		var i = 0;
266		for (var j = 0; j < gTabbableTags.length; j++) {
267			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
268			for (var k = 0 ; k < tagElements.length; k++) {
269				tagElements[k].tabIndex = gTabIndexes[i];
270				tagElements[k].tabEnabled = true;
271				i++;
272			}
273		}
274	}
275}
276
277
278/**
279 * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
280 * IE has a problem with wanted select form tags to always be the topmost z-index or layer
281 *
282 * Thanks for the code Scott!
283 */
284function hideSelectBoxes() {
285  var x = document.getElementsByTagName("SELECT");
286
287  for (i=0;x && i < x.length; i++) {
288    x[i].style.visibility = "hidden";
289  }
290}
291
292/**
293 * Makes all drop down form select boxes on the screen visible so they do not 
294 * reappear after the dialog is closed.
295 * 
296 * IE has a problem with wanting select form tags to always be the 
297 * topmost z-index or layer.
298 */
299function displaySelectBoxes() {
300  var x = document.getElementsByTagName("SELECT");
301
302  for (i=0;x && i < x.length; i++){
303    x[i].style.visibility = "visible";
304  }
305}