home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / document.cab / gp.chm / popups.js < prev    next >
Text File  |  1999-12-10  |  7KB  |  217 lines

  1. //POPUPS SCRIPT - HTML Help only
  2. //Version 2  (2.00.0001)
  3. //For use with HTML Help and IE 4 and 5 only
  4. //December 16, 1998
  5. //Original Developer: Mark Jewett
  6. //Version 2 revised by Peter Costantini
  7.  
  8. //***********************************************************************
  9. //*** CUSTOMIZABLE OPTIONS **********************************************
  10.  
  11. //Author-definable options
  12. var popBackColor = "#ffffee";                /*Background color of Popup*/
  13. var popBordColor = "#aaaaaa";                /*Border color of Popup*/
  14. var popWidth = 300;                        /*Width of Popup*/
  15. /* Next two variables for use in sizing popups proportionately. */
  16. var popWidthIncrement = 50                /* Amount to add to popup width for string longer than popStringLimit */
  17. var popStringLimit = 400                /* String length beyond which to add popWidthIncrement to popup width */
  18.  
  19. //***********************************************************************
  20. //DO NOT CHANGE CODE FROM HERE FOWARD UNLESS YOU KNOW WHAT YOU'RE DOING
  21.  
  22. //***********************************************************************
  23. //GLOBAL VARIABLES
  24.  
  25. var posX, posY;                        //coordinates of popup
  26. var popOpen, stayOpen;                //state of popups
  27.  
  28. //***********************************************************************
  29. //ADD HTML FORMATTING TO HOST PAGE
  30.  
  31. //dynamically write in DIV to hold popup
  32.  
  33. popupDIV = "<DIV ID='popup' STYLE='position:absolute; top:200; left:200; width:" + popWidth + "; padding:10; visibility:hidden; background:" + popBackColor + "; border:solid 2 " + popBordColor + "; z-index:3;'></DIV>";
  34.  
  35. document.write (popupDIV);
  36.  
  37. //***********************************************************************
  38. //EVENT HANDLING
  39.  
  40. document.onclick = doCLICK;
  41. document.onmouseup = doSELECT;
  42. document.onkeypress = doKEY;
  43.  
  44. //***********************************************************************
  45. //MAIN FUNCTION
  46. // * startPopups() *
  47. // Accepts text variable from defs.js passed by calling file as parameter. Checks that definition file is done loading and another popup is not open. The defs_loaded flag is set in defs.js.
  48.  
  49. function startPopups(x)
  50. {
  51.     if (defs_loaded != true) window.setTimeout("startPopups("+x+")", 50);
  52.  
  53.     if (!popOpen)
  54.     {
  55.         doPopup(x);
  56.         return;
  57.     }
  58. }
  59.  
  60. //***********************************************************************
  61. // * doCLICK() *
  62. //Sets position of popup DIV based on mouse click, or leaves it in place if a nested popup is clicked.  Nesting is indicated in variables in defs.js.
  63.  
  64. function doCLICK()
  65. {
  66.     srcElement = window.event.srcElement;
  67.     if (((srcElement.parentElement.tagName.charAt(0).toLowerCase() == "a") && (srcElement.parentElement.href.substring(0,22) == "javascript:startPopups")) ||
  68.         ((srcElement.tagName.charAt(0).toLowerCase() == "a") && (srcElement.href.substring(0,22) == "javascript:startPopups")))
  69.     {
  70.         if ((popOpen) && (window.event.srcElement.name == "nest"))
  71.         {
  72.             posX = document.all.popup.style.posLeft;
  73.             posY = document.all.popup.style.posTop;
  74.         }
  75.         else
  76.         {
  77.             posX = window.event.clientX;
  78.             posY = window.event.clientY + document.body.scrollTop;        
  79.         }
  80.     }
  81.     if (popOpen) closePopup();
  82. }
  83.  
  84. //***********************************************************************
  85. // * doKEY() *
  86. //If user clicks ESC, closes popup.
  87.  
  88. function doKEY()
  89. {
  90.     if (window.event.keyCode == 27) closePopup();
  91. }
  92.  
  93. //***********************************************************************
  94. // * doSELECT() *
  95. //If user selects text inside of popup, does not close popup.
  96.  
  97. function doSELECT()
  98. {
  99.     stayOpen = false;
  100.     if (!popOpen) return true;
  101.     if (popOpen)
  102.     {
  103.         if (document.selection.type != "Text") return true;
  104.         else if (document.selection.createRange().text == "") return true;
  105.     }
  106.     if (window.event.srcElement.tagName.charAt(0).toLowerCase() == "a") return true;
  107.     stayOpen = true;
  108.     return false;
  109. }
  110.  
  111. //***********************************************************************
  112. // * doPopup(def) *
  113. //Creates popup and insert HTML string from defs.js into it.
  114.  
  115. function doPopup(def)
  116. {
  117.     //get definition from "defs.js"
  118.     popString = eval(def);
  119.     
  120.     //fill contents with popup def
  121.     document.all.popup.innerHTML = popString;
  122.     
  123.     /* Expand width of popup DIV proportionately for long definitions. */
  124.     var popupWidth = document.all.popup.offsetWidth;
  125.     if (popString.length < popStringLimit)
  126.     {
  127.         popupWidth = popWidth;
  128.         document.all.popup.style.width = popWidth;
  129.     }
  130.     else
  131.     {
  132.         if (popString.length < (2 * popStringLimit))
  133.         {
  134.             popupWidth = popWidth + popWidthIncrement;
  135.             document.all.popup.style.width = popWidth + popWidthIncrement;
  136.         }
  137.         else
  138.         {
  139.                 popupWidth = popWidth + (2 * popWidthIncrement);
  140.                 document.all.popup.style.width = popWidth + (2 * popWidthIncrement);
  141.         }
  142.     }
  143.  
  144.     //determine if popup will be offscreen to right
  145.      var rightlimit = posX + popupWidth;
  146.     if (rightlimit >= document.body.clientWidth)
  147.     {
  148.         posX -= (rightlimit - document.body.clientWidth);
  149.     }
  150.  
  151.     /* If left side of popup would go off left side of visible window, position it at the left edge of the window. */
  152.     
  153.     if (posX < 0)
  154.     {
  155.         posX = 0;
  156.     }
  157.     
  158.     //this Timeout necessary to allow contents of popup to fill, before getting the height
  159.  
  160.     window.setTimeout ("doPopHeight()", 0);
  161.  
  162.     //position popup
  163.     document.all.popup.style.top = posY;
  164.     document.all.popup.style.left = posX;
  165.  
  166.     /* Once popup is positioned, if it's too wide for the window, reduce its width to the width of the window. */
  167.     
  168.     if (document.all.popup.style.width > document.body.clientWidth)
  169.     {
  170.         document.all.popup.style.width = document.body.clientWidth;
  171.     }
  172.  
  173.     //Make popup visible.
  174.     document.all.popup.style.visibility = "visible";
  175.  
  176.     //Track state of popup.
  177.     popOpen = true;
  178.  
  179.     //Do not allow event to bubble.
  180.     return false;
  181. }
  182.  
  183. //***********************************************************************
  184. // * doPopHeight() *
  185. //Gets height of popup (based on newly added contents) and determines if it will extend off bottom of visible page.  If it does, move it up, but not past top of visible page.
  186.  
  187. function doPopHeight()
  188. {
  189.     var pageBottom = document.body.scrollTop + document.body.clientHeight;
  190.     var popHeight = document.all.popup.offsetHeight;
  191.     if (popHeight + posY >= pageBottom)
  192.     {
  193.         if (popHeight <= document.body.clientHeight)
  194.         {
  195.             document.all.popup.style.top = pageBottom - popHeight;
  196.         }
  197.         else
  198.     /* Position top of popup at top of page if it is longer than visible page. */
  199.         {
  200.             document.all.popup.style.top = document.body.scrollTop;
  201.         }
  202.     }
  203. }
  204.  
  205. //***********************************************************************
  206. // * closePopup() *
  207. //Close popup unless stayOpen flag is set.
  208.  
  209. function closePopup()
  210. {
  211.     if (stayOpen == true) return;
  212.     document.all.popup.style.visibility = "hidden";
  213.     document.all.popup.innerHTML = "";
  214.     popOpen = false;
  215.     return;
  216. }
  217.