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 / RadioButton / RadioButton.js < prev    next >
Encoding:
JavaScript  |  1998-10-05  |  6.5 KB  |  181 lines

  1. /* ======================================================================
  2. OBJECT:        com_netobjects_RadioButton
  3.  
  4. PLATFORMS:    Netscape Navigator 4.01 or higher, 
  5.                 Microsoft Internet Explorer 4.02 or higher
  6.  
  7. DESC:            This object allows developers to create RadioButtons with common built
  8.                 in behaviors.
  9. ====================================================================== */
  10. function com_netobjects_RadioButton(params) {
  11.  // Public Properties    
  12.     this.name                 = (params.name != null) ? params.name : "";
  13.     this.radioButtonName    = (params.radioButtonName != null) ? params.radioButtonName : "";
  14.     this.formName            = (params.formName != null) ? params.formName : "";
  15.     this.defaultselected    = (params.defaultselected == true) ? params.defaultselected : false;
  16.     this.selectedVal        = (params.selectedVal != null) ? params.selectedVal : "";
  17.     this.state                 = (params.state != null) ? params.state : "";
  18.     this.showLabel            = (params.showLabel == true) ? params.showLabel : false;
  19.  
  20.     this.testMode            = (params.testMode != null) ? params.testMode : "none"
  21.     
  22.     // Private Properties
  23.     this.radioButtonIndex = null
  24.  
  25.     // Public Methods
  26.     this.render             = _RadioButton_render;
  27.     this.getSelected         = _RadioButton_getSelected;
  28.     this.setSelected         = _RadioButton_setSelected;
  29.  
  30.     // Private Methods
  31.     this.validateProperties = _RadioButton_validateProperties;
  32.  
  33.     // Public Events
  34.     this.onBlur              = _RadioButton_onBlur;
  35.     this.onClick             = _RadioButton_onClick;
  36.     this.onFocus           = _RadioButton_onFocus;
  37.     this.onRender         = _RadioButton_onRender;
  38.  
  39.     // Private Events (internal event driven functionality)
  40.  
  41.     // Validate object properties
  42.     this.validateProperties();
  43.  
  44.     // Public Methods ----------------------------------------
  45.     function _RadioButton_render() {
  46.         // Initialize Variables
  47.         var sRadioButtonName = "";
  48.         var svalue = "";
  49.         var sselected = "";
  50.         var slabel = "";
  51.  
  52.         var sonBlur  = "";
  53.         var sonClick = "";
  54.         var sonFocus = "";
  55.     
  56.         // Set Variables
  57.         sRadioButtonName = " Name = '" + this.radioButtonName + "'"
  58.  
  59.         // Build Nav & IE shared link events
  60.         sonBlur  = " onBlur=\"" + this.name + ".onBlur();\""
  61.         sonClick   = " onclick=\"return " + this.name + ".onClick();\""
  62.         sonFocus    = " onFocus=\"" + this.name + ".onFocus();\""
  63.         eEvents  = sonBlur + sonClick + sonFocus;
  64.  
  65.         // Write HTML
  66.         if (this.state == "hidden") {
  67.             if (this.defaultselected) {  // Only print if selected
  68.                 document.write( "<Input Type=hidden " + sRadioButtonName + " VALUE = \""+  this.selectedVal  + "\"" + eEvents + ">\n");
  69.             }
  70.         } else if (this.state == "read-only") {
  71.             sselected = (this.defaultselected) ? this.selectedVal : ""
  72.            document.write( sselected + "\n");
  73.         } else {
  74.             if (this.showLabel) slabel = "   " + this.selectedVal + " ";
  75.             
  76.             svalue = " Value = \"" + this.selectedVal     + "\""
  77.             sselected = (this.defaultselected) ? " CHECKED " : ""
  78.            document.write( slabel + "<Input Type=Radio "  + sRadioButtonName + svalue + sselected + eEvents + ">\n");
  79.         }
  80.  
  81.         if ((this.state != "read-only") && !((this.state == "hidden") && (!this.defaultselected))) {
  82.             // Set control index if multiple controls exist
  83.             this.radioButtonIndex = eval("window.document." + this.formName + "." + this.radioButtonName + ".length")-1
  84.             if (isNaN(this.radioButtonIndex)) {this.radioButtonIndex=0}
  85.         }
  86.  
  87.         // Fire public events
  88.         this.onRender(); 
  89.     
  90.     } // END _RadioButton_render
  91.  
  92.  
  93.     /* ======================================================================
  94.     
  95.    METHOD: _RadioButton_validateProperties.  If the test mode is set,
  96.     generate error and/or warning messages if required or recommended properties
  97.     are set with invalid property values or are not set at all.
  98.  
  99.     ====================================================================== */
  100.     function _RadioButton_validateProperties() {
  101.         if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
  102.             var errors = new Array()
  103.           iIndex = 0;  
  104.          
  105.             // Check for errors
  106.             if (this.name == "") 
  107.                 errors[iIndex++] = "Name is a required property.";  
  108.             if (this.radioButtonName == "") 
  109.                 errors[iIndex++] = "RadioButton name is a required property.";  
  110.             if (this.radioButtonName == this.name) 
  111.                 errors[iIndex++] = "RadioButton name and object name may not be the same.";  
  112.             if (this.formName == "") 
  113.                 errors[iIndex++] = "Form name is a required property.";  
  114.  
  115.             // Display errors
  116.             for (var i=0; i<errors.length; i++)  {
  117.                 if (this.testMode.toLowerCase()    == "text") 
  118.                     document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_Button, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n");
  119.                 else
  120.                       alert("TEST MODE, com_netobjects_RadioButton, '" + this.name + "' : " + errors[i]);
  121.  
  122.             }            
  123.         } 
  124.     } // END _RadioButton_validateProperties
  125.  
  126.     /* ======================================================================
  127.     
  128.    METHOD: _RadioButton_getSelected.  Gets whether radio button is currently selected.
  129.  
  130.     ====================================================================== */
  131.     function _RadioButton_getSelected() {
  132.         var result = this.defaultselected
  133.         if (this.radioButtonIndex != null) {
  134.             var index = ""
  135.             if (!isNaN(eval("window.document." + this.formName + "." + this.radioButtonName + ".length"))) {
  136.                 index = "[" + this.radioButtonIndex + "]"
  137.             }
  138.             result = eval("window.document." + this.formName + "." + this.radioButtonName + index + ".checked")        
  139.         }
  140.  
  141.         return result
  142.     } // END _RadioButton_getSelected
  143.  
  144.     /* ======================================================================
  145.     
  146.    METHOD: _RadioButton_setSelected.  Sets whether radio button is currently selected.
  147.  
  148.     ====================================================================== */
  149.     function _RadioButton_setSelected(newVal) {
  150.         if (newVal == null) newVal = true;
  151.         var result = false
  152.         if (this.radioButtonIndex != null) {
  153.             var index = ""
  154.             if (!isNaN(eval("window.document." + this.formName + "." + this.radioButtonName + ".length"))) {
  155.                 index = "[" + this.radioButtonIndex + "]"
  156.             }
  157.  
  158.             eval("window.document." + this.formName + "." + this.radioButtonName + index + ".checked=" + newVal)    
  159.             result = true
  160.         }
  161.     
  162.         return result
  163.     } // END _RadioButton_setSelected
  164.  
  165.  
  166.     // Public Event Handlers ----------------------------------------
  167.     function _RadioButton_onBlur() {
  168.     }
  169.     function _RadioButton_onClick() {
  170.     }
  171.     function _RadioButton_onFocus() {
  172.     }
  173.     function _RadioButton_onRender() {
  174.     }
  175.     // Private Event Handlers ----------------------------------------
  176.     
  177.  
  178.  
  179. } // END CONSTRUCTOR com_netobjects_RadioButton
  180.  
  181.