home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / ScriptBuilder / NOSB30_TRIAL.exe / data1.cab / Program_Files / CompLib / PopUpWindow / PopUpWindow.js < prev    next >
Encoding:
JavaScript  |  1998-10-05  |  10.6 KB  |  308 lines

  1. /* ======================================================================
  2. OBJECT:             com_netobjects_PopUpWindow
  3.  
  4. DESCRIPTION:    The PopUpWindow object opens and closes a new browser window.  
  5.                     Browser window properties can be set, such as height, width, etc.
  6.                     Also configurable is whether or not certain browser elements are 
  7.                     present, such as the status bar, toolbar, etc.
  8.  
  9. TESTED
  10. PLATFORMS:    Netscape Navigator 4.05, 
  11.                 Microsoft Internet Explorer 4.01
  12. ====================================================================== */
  13. function com_netobjects_PopUpWindow(params) {
  14.  
  15.    // Public Properties    
  16.     this.name             = (params.name != null) ? params.name : "";
  17.     this.testMode         = (params.testMode != null) ? params.testMode : "off";
  18.  
  19.     this.URL             = (params.URL != null) ? params.URL : "";
  20.     this.toolbar         = (params.toolbar != null) ? params.toolbar : true;
  21.     this.location         = (params.location != null) ? params.location : true;
  22.     this.directories     = (params.directories != null) ? params.directories : true;
  23.     this.status         = (params.status != null) ? params.status : true;
  24.     this.menubar         = (params.menubar != null) ? params.menubar : true;
  25.     this.scrollbars     = (params.scrollbars != null) ? params.scrollbars : true;
  26.     this.resizable     = (params.resizable != null) ? params.resizable : true;
  27.     this.width             = (params.width != null) ? params.width : true;
  28.     this.height         = (params.height != null) ? params.height : true;
  29.     this.top             = (params.top != null) ? params.top : true;
  30.     this.left             = (params.left != null) ? params.left : true;
  31.  
  32.     // Private Properties
  33.     this.hdlWindow        = null;    // handle to the opened window
  34.  
  35.     // Public Methods
  36.     this.open        = _popupwindow_open;
  37.     this.close        = _popupwindow_close;
  38.     this.getName    = _popupwindow_getName;
  39.     this.setName    = _popupwindow_setName;
  40.  
  41.     // Private Methods
  42.     this.getFeatures    = _popupwindow_getFeatures;
  43.     this.validateProperties = _popupwindow_validateProperties;
  44.  
  45.     // Public Events
  46.     this.onClose    = _popupwindow_onClose;
  47.     this.onOpen        = _popupwindow_onOpen;
  48.  
  49.     // Private Events (internal event driven functionality)
  50.     this.eOnClose    = _popupwindow_eOnClose;
  51.  
  52.     // If test mode is set, check for invalid property values
  53.     this.validateProperties();
  54.  
  55.     // Public Methods ----------------------------------------
  56.  
  57.     /* ======================================================================
  58.     
  59.    METHOD: _popupwindow_open.  Opens the pop-up window.
  60.     
  61.     ====================================================================== */
  62.     function _popupwindow_open() {    
  63.         this.hdlWindow = window.open(this.URL, this.name, this.getFeatures());
  64.         
  65.         // Fire public event
  66.         this.onOpen(); 
  67.     } // END _popupwindow_open
  68.  
  69.  
  70.     /* ======================================================================
  71.     
  72.    METHOD: _popupwindow_close.  Closes the pop-up window if it's open.
  73.     
  74.     ====================================================================== */
  75.     function _popupwindow_close() {    
  76.         err = "";
  77.  
  78.         if (this.hdlWindow != null && !this.hdlWindow.closed) 
  79.             this.hdlWindow.close();
  80.         
  81.         // Fire private event
  82.         this.eOnClose(); 
  83.         // Fire public event
  84.     } // END _popupwindow_close
  85.  
  86.  
  87.     // Property Getters 
  88.     /* ======================================================================
  89.     
  90.    METHOD: _popupwindow_getName.  Getter for the 'name' property.
  91.     
  92.     ====================================================================== */
  93.     function _popupwindow_getName() {    
  94.         return this.name;
  95.     } // END _popupwindow_getName
  96.  
  97.  
  98.     // Property Setters
  99.     /* ======================================================================
  100.     
  101.    METHOD: _popupwindow_setName.  Setter for the 'name' property.  Returns
  102.     true if the property was set, false if it could not be set.
  103.     
  104.     ====================================================================== */
  105.     function _popupwindow_setName(thevalue) {    
  106.         var returnVal = true;
  107.  
  108.         // Don't allow users to set the name once a name has been set.  Errors
  109.         // will occur if the object's name is changed midstream.
  110.         if (this.name == null && !IsNum(thevalue.charAt(0)) && IsAlphaNumOrUnderscore(thevalue))
  111.             this.name = thevalue;
  112.         else
  113.             returnVal = false;
  114.  
  115.         return returnVal;
  116.     } // END _popupwindow_setName
  117.  
  118.  
  119.     // Private Methods ----------------------------------------
  120.  
  121.     /* ======================================================================
  122.     
  123.    METHOD: _popupwindow_getFeatures.  Generates a string that combines all 
  124.     all of the yes/no propeties into a comma-delimited list of name/value
  125.    pairs.  This string corresponds to the required syntax of the JavaScript
  126.     window.open() method's feature list argument.
  127.  
  128.     E.g.  statusbar=yes,toolbar=yes,height=200,width=100
  129.     
  130.     ====================================================================== */
  131.     function _popupwindow_getFeatures() {
  132.         sFeatures = "";
  133.         
  134.         if (this.toolbar)
  135.             sFeatures += "toolbar=yes";
  136.         if (this.location) 
  137.             sFeatures += ",location=yes";
  138.         if (this.directories) 
  139.             sFeatures += ",directories=yes";
  140.         if (this.status) 
  141.             sFeatures += ",status=yes";
  142.         if (this.menubar) 
  143.             sFeatures += ",menubar=yes";
  144.         if (this.scrollbars) 
  145.             sFeatures += ",scrollbars=yes";
  146.         if (this.resizable) 
  147.             sFeatures += ",resizable=yes";
  148.         if (this.width != null && this.width != "") 
  149.             sFeatures += ",width=" + this.width;
  150.         if (this.height != null && this.height != "") 
  151.             sFeatures += ",height=" + this.height;
  152.         if (this.top != null && this.left != "") 
  153.             sFeatures += ",top=" + this.top;
  154.         if (this.left != null && this.left != "") 
  155.             sFeatures += ",left=" + this.left;
  156.         
  157.         // Strip off the unnecessary starting comma, if one exists
  158.         if (sFeatures.charAt(0) == ',')
  159.             sFeatures = sFeatures.substring(1, sFeatures.length);
  160.  
  161.         return sFeatures;
  162.     } // END    _popupwindow_getFeatures()
  163.  
  164.     /* ======================================================================
  165.     
  166.    METHOD: _popupwindow_validateProperties.  Used only in test mode.  Writes out
  167.     error and warning messages if some property values are set with bad
  168.     values.
  169.     
  170.     ====================================================================== */
  171.     function _popupwindow_validateProperties() {
  172.         if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
  173.             var errors = new Array();
  174.           iIndex = 0;  
  175.          
  176.             // Check for errors
  177.             if (this.name == "") 
  178.                 errors[iIndex++] = "Name is a required property.";  
  179.  
  180.             // Display errors
  181.             for (var i=0; i<errors.length; i++)  {
  182.                 if (this.testMode.toLowerCase()    == "text") 
  183.                       document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_PopUpWindow, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n");
  184.                 else
  185.                       alert("TEST MODE, com_netobjects_PopUpWindow, '" + this.name + "' : " + errors[i]);
  186.             }            
  187.         } 
  188.     } // END _popupwindow_validateProperties
  189.     
  190.  
  191.     // Private Event Handlers ----------------------------------------
  192.  
  193.     /* ======================================================================
  194.     
  195.    EVENT: _popupwindow_eOnClose.  Internal event handler.  Resets window handle
  196.      to null then fires the public onClose event.
  197.     
  198.     ====================================================================== */
  199.     function _popupwindow_eOnClose() {
  200.         this.hdlWindow = null;    
  201.  
  202.         // Fire public event
  203.         this.onClose();
  204.     } // end _popupwindow_eOnClose()
  205.  
  206.     
  207.     // Public Event Handlers ----------------------------------------
  208.  
  209.     function _popupwindow_onClose() {
  210.     }
  211.     function _popupwindow_onOpen() {
  212.     }
  213.  
  214. } // END CONSTRUCTOR com_netobjects_PopUpWindow
  215.  
  216. // Shared code - reused in several components
  217.  
  218. /* ======================================================================
  219. FUNCTION:    IsAlphaNumOrUnderscore
  220.  
  221. INPUT:        str (string) - the string to be tested
  222.  
  223. RETURN:      true, if the string contains only alphanumeric characters or underscores.
  224.                 false, otherwise.
  225.  
  226. PLATFORMS:    Netscape Navigator 3.01 and higher,
  227.                   Microsoft Internet Explorer 3.02 and higher,
  228.                   Netscape Enterprise Server 3.0,
  229.                   Microsoft IIS/ASP 3.0.
  230. ====================================================================== */
  231. function IsAlphaNumOrUnderscore( str ) {
  232.     // Return immediately if an invalid value was passed in
  233.     if (str == null || str+"" == "")    
  234.         return false;
  235.  
  236.     var isValid = true;
  237.  
  238.     str += "";    // convert to a string for performing string comparisons.
  239.     // Loop through string one character at a time. If non-alpha numeric
  240.     // is found then, break out of loop and return a false result
  241.  
  242.     for (i = 0; i < str.length; i++)
  243.        {
  244.         // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
  245.               if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
  246.                   ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
  247.                   ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
  248.                   (str.charAt(i) == "_") ) )
  249.               {
  250.                    isValid = false;
  251.                  break;
  252.               }
  253.  
  254.     } // END for   
  255.    
  256.     return isValid;
  257.  
  258. }  // end IsAlphaNumOrUnderscore
  259.  
  260. /* ======================================================================
  261. FUNCTION:      IsNum
  262.  
  263. INPUT:          numstr (string/number) - the string that will be tested to ensure 
  264.                                                that the value is a number (int or float)
  265.  
  266. RETURN:      true, if all characters represent a valid integer or float
  267.                  false, otherwise.
  268.  
  269. PLATFORMS:    Netscape Navigator 3.01 and higher,
  270.                   Microsoft Internet Explorer 3.02 and higher,
  271.                   Netscape Enterprise Server 3.0,
  272.                   Microsoft IIS/ASP 3.0.
  273. ====================================================================== */
  274. function IsNum( numstr ) {
  275.     // Return immediately if an invalid value was passed in
  276.     if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")    
  277.         return false;
  278.  
  279.     var isValid = true;
  280.     var decCount = 0;        // number of decimal points in the string
  281.  
  282.     // convert to a string for performing string comparisons.
  283.     numstr += "";    
  284.  
  285.     // Loop through string and test each character. If any
  286.     // character is not a number, return a false result.
  287.      // Include special cases for negative numbers (first char == '-')
  288.     // and a single decimal point (any one char in string == '.').   
  289.     for (i = 0; i < numstr.length; i++) {
  290.         // track number of decimal points
  291.         if (numstr.charAt(i) == ".")
  292.             decCount++;
  293.  
  294.         if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
  295.                 (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
  296.            isValid = false;
  297.            break;
  298.         } else if ((numstr.charAt(i) == "-" && i != 0) ||
  299.                 (numstr.charAt(i) == "." && numstr.length == 1) ||
  300.               (numstr.charAt(i) == "." && decCount > 1)) {
  301.            isValid = false;
  302.            break;
  303.       }                                 
  304.    } // END for   
  305.    
  306.        return isValid;
  307. }  // end IsNum
  308.