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 / Button / Button.js < prev    next >
Encoding:
JavaScript  |  1998-10-05  |  8.9 KB  |  261 lines

  1. /* ******************** Begin Button Object ******************** */
  2.  
  3. /* ======================================================================
  4. OBJECT:        com_netobjects_Button
  5.  
  6. PLATFORMS:    Netscape Navigator 4.01 or higher, 
  7.                 Microsoft Internet Explorer 4.02 or higher
  8.  
  9. DESC:            This object allows developers to create form buttons with common built
  10.                 in event handlers such as onClick, onMouseDown, and more.  The event
  11.                 handlers can be overridden to define custom responses to button events.
  12.                 The button property "type" indicates if the form button is of type "button",
  13.                 "submit" or "reset".  
  14. ====================================================================== */
  15. function com_netobjects_Button(params) {
  16.      // Public Properties    
  17.     this.name                 = (params.name != null) ? params.name : "";
  18.     this.buttonName        = (params.buttonName != null) ? params.buttonName : "";
  19.     this.formName            = (params.formName != null) ? params.formName : "";
  20.     this.label                = (params.label != null) ? params.label : "";
  21.     this.type                = (params.type != null) ? params.type : "";
  22.  
  23.     this.testMode            = (params.testMode != null) ? params.testMode : "off";
  24.  
  25.     // Public Methods
  26.     this.render             = _button_render;
  27.  
  28.     this.getName            = _button_getName;
  29.     this.setName            = _button_setName;
  30.  
  31.     // Private Methods
  32.     this.validateProperties = _button_validateProperties;
  33.  
  34.     // Public Events
  35.     this.onBlur                = _button_onBlur;
  36.     this.onClick             = _button_onClick;
  37.     this.onFocus           = _button_onFocus;
  38.     this.onMouseDown         = _button_onMouseDown;
  39.     this.onMouseUp          = _button_onMouseUp;
  40.     this.onRender         = _button_onRender;
  41.  
  42.     // Private Events (internal event driven functionality)
  43.  
  44.     // Validate object properties
  45.     this.validateProperties();
  46.  
  47.     // Public Methods ----------------------------------------
  48.  
  49.     /* ======================================================================
  50.     
  51.    METHOD: _button_render.  Renders the button in HTML.
  52.     
  53.     ====================================================================== */
  54.     function _button_render() {
  55.         // Initialize Variables
  56.         var sbuttonName = "";
  57.         var slabel = "";
  58.         var stype = "";
  59.         var sonBlur  = "";
  60.         var sonClick   = "";
  61.         var sonFocus    = "";
  62.         var sonMouseDown  = "";
  63.         var sonMouseUp  = "";
  64.  
  65.         var eEvents = "";
  66.     
  67.         // Set Variables
  68.         sbuttonName = " Name = \"" + this.buttonName + "\"";
  69.         slabel = " Value = \"" + this.label + "\"";
  70.         stype = " Type = \"" + this.type + "\"";
  71.  
  72.         // Build Nav & IE shared link events
  73.         sonBlur  = " onBlur=\"" + this.name + ".onBlur();\"";
  74.         sonClick   = " onClick=\"return " + this.name + ".onClick();\"";
  75.         sonFocus    = " onFocus=\"" + this.name + ".onFocus();\"";
  76.         sonMouseDown  = " onMouseDown=\"" + this.name + ".onMouseDown()\"";
  77.         sonMouseUp  = " onMouseUp=\"" + this.name + ".onMouseUp();\"";
  78.         eEvents  = sonBlur + sonClick + sonFocus + sonMouseDown + sonMouseUp;
  79.  
  80.         // Write HTML
  81.        document.write( "<Input" + sbuttonName + slabel + stype + eEvents + ">\n");
  82.         
  83.         // Fire public events
  84.         this.onRender(); 
  85.     } // END _button_render
  86.  
  87.  
  88.     // Property Getters 
  89.     /* ======================================================================
  90.     
  91.    METHOD: _button_getName.  Getter for the 'name' property.
  92.     
  93.     ====================================================================== */
  94.     function _button_getName() {    
  95.         return this.name;
  96.     } // END _button_getName
  97.  
  98.  
  99.     // Property Setters
  100.  
  101.     /* ======================================================================
  102.     
  103.    METHOD: _button_setName.  Setter for the 'name' property.  Returns
  104.     true if the property was set, false if it could not be set.
  105.     
  106.     ====================================================================== */
  107.     function _button_setName(thevalue) {    
  108.         var returnVal = true;
  109.  
  110.         // Don't allow users to set the name once a name has been set.  Errors
  111.         // will occur if the object's name is changed midstream.
  112.         if (this.name == null && !IsNum(thevalue.charAt(0)) && IsAlphaNumOrUnderscore(thevalue))
  113.             this.name = thevalue;
  114.         else
  115.             returnVal = false;
  116.  
  117.         return returnVal;
  118.     } // END _button_setName
  119.  
  120.  
  121.     /* ======================================================================
  122.     
  123.    METHOD: _button_validateProperties.  Used only in test mode.  Writes out
  124.     error and warning messages if some property values are set with bad
  125.     values.
  126.     
  127.     ====================================================================== */
  128.     function _button_validateProperties() {
  129.         if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
  130.             var errors = new Array();
  131.           iIndex = 0;  
  132.          
  133.             // Check for errors
  134.             if (this.name == "") 
  135.                 errors[iIndex++] = "Name is a required property.";  
  136.             if (this.buttonName == "") 
  137.                 errors[iIndex++] = "Button name is a required property.";  
  138.             if (this.name == this.buttonName) 
  139.                 errors[iIndex++] = "The name property and the buttonName property are currently equal. This can cause event handlers to fail and can cause other errors.";  
  140.  
  141.             // Display errors
  142.             for (var i=0; i<errors.length; i++)  {
  143.                 if (this.testMode.toLowerCase()    == "text") 
  144.                       document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_Button, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n");
  145.                 else
  146.                       alert("TEST MODE, com_netobjects_Button, '" + this.name + "' : " + errors[i]);
  147.             }            
  148.         } 
  149.     } // END _button_validateProperties
  150.     
  151.     // Public Event Handlers ----------------------------------------
  152.  
  153.     // Default handlers for each event, can be overridden in calling page.
  154.     function _button_onBlur() {
  155.     }
  156.     function _button_onClick() {
  157.     }
  158.     function _button_onFocus() {
  159.     }
  160.     function _button_onMouseDown() {
  161.     }
  162.     function _button_onMouseUp() {
  163.     }
  164.     function _button_onRender() {
  165.     }
  166. } // END CONSTRUCTOR com_netobjects_Button
  167.  
  168.  
  169. /* ======================================================================
  170. FUNCTION:      IsNum
  171.  
  172. INPUT:          numstr (string/number) - the string that will be tested to ensure 
  173.                                                that the value is a number (int or float)
  174.  
  175. RETURN:      true, if all characters represent a valid integer or float
  176.                  false, otherwise.
  177.  
  178. PLATFORMS:    Netscape Navigator 3.01 and higher,
  179.                   Microsoft Internet Explorer 3.02 and higher,
  180.                   Netscape Enterprise Server 3.0,
  181.                   Microsoft IIS/ASP 3.0.
  182. ====================================================================== */
  183. function IsNum( numstr ) {
  184.     // Return immediately if an invalid value was passed in
  185.     if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")    
  186.         return false;
  187.  
  188.     var isValid = true;
  189.     var decCount = 0;        // number of decimal points in the string
  190.  
  191.     // convert to a string for performing string comparisons.
  192.     numstr += "";    
  193.  
  194.     // Loop through string and test each character. If any
  195.     // character is not a number, return a false result.
  196.      // Include special cases for negative numbers (first char == '-')
  197.     // and a single decimal point (any one char in string == '.').   
  198.     for (i = 0; i < numstr.length; i++) {
  199.         // track number of decimal points
  200.         if (numstr.charAt(i) == ".")
  201.             decCount++;
  202.  
  203.         if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
  204.                 (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
  205.            isValid = false;
  206.            break;
  207.         } else if ((numstr.charAt(i) == "-" && i != 0) ||
  208.                 (numstr.charAt(i) == "." && numstr.length == 1) ||
  209.               (numstr.charAt(i) == "." && decCount > 1)) {
  210.            isValid = false;
  211.            break;
  212.       }                                 
  213.    } // END for   
  214.    
  215.        return isValid;
  216. }  // end IsNum
  217.  
  218.  
  219. /* ======================================================================
  220. FUNCTION:    IsAlphaNumOrUnderscore
  221.  
  222. INPUT:        str (string) - the string to be tested
  223.  
  224. RETURN:      true, if the string contains only alphanumeric characters or underscores.
  225.                 false, otherwise.
  226.  
  227. PLATFORMS:    Netscape Navigator 3.01 and higher,
  228.                   Microsoft Internet Explorer 3.02 and higher,
  229.                   Netscape Enterprise Server 3.0,
  230.                   Microsoft IIS/ASP 3.0.
  231. ====================================================================== */
  232. function IsAlphaNumOrUnderscore( str ) {
  233.     // Return immediately if an invalid value was passed in
  234.     if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  235.         return false;
  236.  
  237.     var isValid = true;
  238.  
  239.     str += "";    // convert to a string for performing string comparisons.
  240.     // Loop through string one character at a time. If non-alpha numeric
  241.     // is found then, break out of loop and return a false result
  242.  
  243.     for (i = 0; i < str.length; i++)
  244.        {
  245.         // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
  246.               if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
  247.                   ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
  248.                   ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
  249.                   (str.charAt(i) == "_") ) )
  250.               {
  251.                    isValid = false;
  252.                  break;
  253.               }
  254.  
  255.     } // END for   
  256.    
  257.     return isValid;
  258. }  // end IsAlphaNumOrUnderscore
  259.  
  260. /* ******************** END Button Object ******************** */
  261.