PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/subModal.js

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