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 / SelectBox / SelectBox.js < prev    next >
Encoding:
JavaScript  |  1998-10-05  |  11.7 KB  |  345 lines

  1. /* ======================================================================
  2. OBJECT:        com_netobjects_SelectBox
  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 SelectBoxes with common built
  8.                 in behaviors.
  9. ====================================================================== */
  10. function com_netobjects_SelectBox(params) {
  11.  // Public Properties    
  12.     this.name                 = (params.name != null) ? params.name : "";
  13.     this.selectBoxName    = (params.selectBoxName != null) ? params.selectBoxName : "";
  14.     this.formName            = (params.formName != null) ? params.formName : "";
  15.     this.size                 = (params.size != null) ? params.size : "";
  16.     this.multiple             = (params.multiple == true) ? params.multiple : false;
  17.  
  18.     this.valuesListStr    = params.valuesListStr;
  19.     this.labelsListStr    = params.labelsListStr;
  20.     this.selectedStr         = params.selectedStr;
  21.  
  22.     this.validate             = ((params.validate != null) && (params.validate == true)) ? params.validate : "";
  23.     this.isRequired        = (params.isRequired     != null) ? params.isRequired : "";
  24.     this.state                 = (params.state != null) ? params.state : "";
  25.  
  26.     this.testMode            = (params.testMode != null) ? params.testMode : "none"
  27.     
  28.     // Private Properties
  29.     this.valuesList        = this.valuesListStr.split(";");//unEncodeList(this.valuesListStr);    
  30.     this.labelsList        = this.labelsListStr.split(";");//unEncodeList(this.labelsListStr);
  31.     this.selectedList        = this.selectedStr.split(";");//unEncodeList(this.selectedStr);
  32.  
  33.     this.isValid             = true;
  34.     this.errType             = null;
  35.     this.selectBoxIndex     = 0
  36.  
  37.     // Public Methods
  38.     this.validateValue     = _SelectBox_validateValue;
  39.     this.render             = _SelectBox_render;
  40.     this.getSelected         = _SelectBox_getSelected;
  41.     this.setSelected         = _SelectBox_setSelected
  42.  
  43.     // Private Methods
  44.     this.validateProperties = _SelectBox_validateProperties;
  45.     this.isElement         = _SelectBox_isElement;
  46.     this.ctlCnt             = _SelectBox_CtlCnt;
  47.  
  48.     // Public Events
  49.     this.onBlur              = _SelectBox_onBlur;
  50.     this.onChange             = _SelectBox_onChange;
  51.     this.onFocus           = _SelectBox_onFocus;
  52.     this.onClick             = _SelectBox_onClick;
  53.  
  54.     this.onValidate          = _SelectBox_onValidate;
  55.     this.onInvalid           = _SelectBox_onInvalid;
  56.     this.onValid           = _SelectBox_onValid;
  57.     this.onRender         = _SelectBox_onRender;
  58.  
  59.     // Private Events (internal event driven functionality)
  60.     this.eOnChange         = _SelectBox_eOnChange;
  61.  
  62.     // Validate object properties
  63.     this.validateProperties();
  64.  
  65.     // Public Methods ----------------------------------------
  66.     function _SelectBox_render() {
  67.         // Initialize Variables
  68.         var sSelectBoxName = "";
  69.         var svalue = "";
  70.         var ssize = "";
  71.         var smultiple = "";
  72.         var label, value;
  73.  
  74.         var sonBlur  = "";
  75.         var sonChange   = "";
  76.         var sonFocus    = "";
  77.         var sonSelect  = "";
  78.         var sonClick   = "";
  79.             
  80.         // Set Variables
  81.         sSelectBoxName = " Name = \"" + this.selectBoxName + "\""
  82.         if (this.size != "") {ssize = " Size = \"" + this.size + "\""}
  83.         if (this.multiple) {smultiple = " MULTIPLE "}
  84.         svalue     = " VALUE=\"" + this.selectedStr + "\"";
  85.  
  86.         // Build Nav & IE shared link events
  87.         sonBlur   = " onBlur=\"" + this.name + ".onBlur();\""
  88.         sonChange = " onchange=\"return " + this.name + ".eOnChange();\""
  89.         sonClick  = " onclick=\"" + this.name + ".onClick();\""
  90.         sonFocus  = " onFocus=\"" + this.name + ".onFocus();\""
  91.         eEvents   = sonBlur + sonChange + sonFocus + sonClick;
  92.  
  93.         // Write HTML
  94.         if (this.state == "hidden") {
  95.            document.write( "<Input Type=hidden "  + sSelectBoxName + svalue + eEvents + ">");
  96.         } else if (this.state == "read-only") {
  97.            document.write( this.selectedStr + "\n");
  98.         } else {
  99.            document.write( "<SELECT"  + sSelectBoxName + svalue + ssize + smultiple + eEvents + ">");
  100.             if (this.valuesList != null) {
  101.                 var max = Math.max(this.valuesList.length, this.labelsList.length);
  102.                 for (var i=0; i < max; i++)  {
  103.                     label = this.labelsList[i];
  104.                     value = this.valuesList[i];
  105.                     // If a label is null, set it to the value
  106.                     if (this.labelsList[i] == null && this.valuesList[i] != null)
  107.                         label = this.valuesList[i];
  108.                     // If a value is null, set it to the label
  109.                     else if (this.valuesList[i] == null && this.labelsList[i] != null)
  110.                         value = this.labelsList[i];
  111.                       document.write( "<OPTION VALUE=\"" + value +"\">" + label + "");
  112.                 }
  113.                 document.write( "</SELECT>\n");
  114.  
  115.                 this.setSelected(this.selectedStr);
  116.             }
  117.         }
  118.         
  119.         if (this.state != "read-only") {
  120.             // Set control index if multiple controls exist
  121.             this.selectBoxIndex = this.ctlCnt(this.selectBoxName)-1
  122.             if (isNaN(this.selectBoxIndex)) {this.selectBoxIndex=0}
  123.         }
  124.  
  125.         // Fire public events
  126.         this.onRender(); 
  127.     
  128.     } // END _SelectBox_render
  129.  
  130.  
  131.     /* ======================================================================
  132.     
  133.    METHOD: _SelectBox_validateProperties.  If the test mode is set,
  134.     generate error and/or warning messages if required or recommended properties
  135.     are set with invalid property values or are not set at all.
  136.  
  137.     ====================================================================== */
  138.     function _SelectBox_validateProperties() {
  139.         if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
  140.             var errors = new Array()
  141.           iIndex = 0;  
  142.          
  143.             // Check for errors
  144.             if (this.name == "") 
  145.                 errors[iIndex++] = "Name is a required property.";  
  146.             if (this.SelectBoxName == "") 
  147.                 errors[iIndex++] = "SelectBox name is a required property.";  
  148.             if (this.SelectBoxName == this.name) 
  149.                 errors[iIndex++] = "SelectBox name and object name may not be the same.";  
  150.          if (this.valuesList == null)
  151.                 errors[iIndex++] = "No values have been specified for the select box."; 
  152.          if (this.labelsList == null)
  153.                 errors[iIndex++] = "No labels have been specified for the select box."; 
  154.          if ((this.valuesList != null) && (this.labelsList != null) && (this.valuesList.length != this.labelsList.length))
  155.                 errors[iIndex++] = "The values list and label list do not have the same number of elements."; 
  156.  
  157.             // Display errors
  158.             for (var i=0; i<errors.length; i++)  {
  159.                 if (this.testMode.toLowerCase()    == "text") 
  160.                     document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_SelectBox, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n");
  161.                 else
  162.                       alert("TEST MODE, com_netobjects_SelectBox, '" + this.name + "' : " + errors[i]);
  163.             }            
  164.         } 
  165.     } // END _SelectBox_validateProperties
  166.  
  167.     /* ======================================================================
  168.     
  169.    METHOD: _SelectBox_validateValue.  
  170.  
  171.     ====================================================================== */
  172.     function _SelectBox_validateValue() {
  173.         this.isValid = true;
  174.         this.errType = ""
  175.  
  176.         // Check for required value
  177.         if ((this.isRequired) && ((this.getSelected() == null) || (this.getSelected() == ""))) {
  178.             this.errType = "NoValue"
  179.             this.isValid = false;
  180.             // exit function
  181.         }
  182.     
  183.         // Fire public events
  184.         if (this.isValid) {this.onValid()} else {this.onInvalid()};
  185.         this.onValidate()
  186.  
  187.         return this.isValid;
  188.  
  189.     } // END _SelectBox_validate
  190.  
  191.     /* ======================================================================
  192.     
  193.    METHOD: _SelectBox_getSelected
  194.  
  195.     ====================================================================== */
  196.     function _SelectBox_getSelected() {
  197.         var result = "";
  198.         var isSel;
  199.         var index = "";
  200.  
  201.         if (this.state != "read-only") {
  202.  
  203.             // Only attempt to index them by array if there's more than one with the same name.
  204.             // Otherwise, it will cause an error
  205.             if (this.ctlCnt(this.selectBoxName) > 1) {
  206.                 index = "[" + this.selectBoxIndex + "]"
  207.             }
  208.  
  209.             if (this.state == "editable") {
  210.                 var itmCnt = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options.length")
  211.     
  212.                 for (var i=0; i < itmCnt; i++) {
  213.                     isSel = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options["+ i +"].selected")
  214.                     if (isSel) {
  215.                         if (result.length > 0) { 
  216.                             result = result + ";"
  217.                         }
  218.                         result = result + eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options["+i+"].value")
  219.                     }
  220.                 } // end for loop
  221.             } else if (this.state == "hidden") 
  222.                 result = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".value")
  223.         } else 
  224.             result = this.selectedStr;
  225.  
  226.         return result;
  227.  
  228.     } // END _SelectBox_getSelected
  229.  
  230.  
  231.     /* ======================================================================
  232.     
  233.    METHOD: _SelectBox_setSelected
  234.  
  235.     ====================================================================== */
  236.     function _SelectBox_setSelected(newVal) {
  237.         var result = ""
  238.         var isSel
  239.         var index = ""
  240.  
  241.         if (this.state != "read-only") {
  242.             this.selectedStr = newVal
  243.             if (this.ctlCnt(this.selectBoxName) > 1) {
  244.                 index = "[" + this.selectBoxIndex + "]"
  245.             }
  246.               if (this.state == "editable") {
  247.             var itmCnt = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options.length")
  248.             var setVal = false;
  249.             for (var i=0; i < itmCnt; i++) {
  250.                     eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options["+i+"].selected = " + this.isElement(this.selectedStr, this.valuesList[i]))
  251.             }
  252.             } else if (this.state == "hidden")
  253.                 eval("window.document." + this.formName + "." + this.selectBoxName + index + ".value='" + newVal + "'");
  254.         } 
  255.         return result;
  256.  
  257.     } // END _SelectBox_setSelected
  258.  
  259.  
  260.     /* ======================================================================
  261.     
  262.    METHOD: _SelectBox_isElement, looks for string in ; delimited string
  263.  
  264.     ====================================================================== */
  265.     function _SelectBox_isElement(myStr, val) {
  266.         var result = false
  267.         var myArray = myStr.split(";");
  268.  
  269.         for (var i=0; i < myArray.length; i++) 
  270.             if (myArray[i] == val) { result = true}
  271.         return result;
  272.  
  273.     } // END _SelectBox_isElement
  274.     
  275.  
  276.     /* ======================================================================
  277.     
  278.    METHOD: _SelectBox_CtlCnt.  Counts number of elements with selected name. 
  279.  
  280.     ====================================================================== */
  281.     function _SelectBox_CtlCnt(ctlName) {
  282.         
  283.         var result = 0
  284.         var itmCnt =  eval("window.document." + this.formName + ".elements.length")
  285.  
  286.         for (var i=0; i < itmCnt; i++) {
  287.             if (ctlName == eval("window.document." + this.formName + ".elements["+ i +"].name")) {
  288.                 result = result + 1
  289.             }
  290.         }
  291.  
  292.         return result
  293.     
  294.     } // END _SelectBox_CtlCnt
  295.  
  296.  
  297.     // Private Event Handlers ----------------------------------------
  298.  
  299.     /* ======================================================================
  300.     
  301.    METHOD: _SelectBox_eOnChange.  For the selected state, display the 
  302.                 appropriate SelectBox if available.
  303.  
  304.     ====================================================================== */
  305.     function _SelectBox_eOnChange() {
  306.         this.selectedList = this.getSelected();
  307.         var result = true
  308.         if (this.validate) {
  309.             result = this.validateValue()
  310.         } 
  311.         this.onChange()
  312.  
  313.         return result
  314.  
  315.     } // END _SelectBox_eOnChange
  316.     
  317.  
  318.     // Public Event Handlers ----------------------------------------
  319.     function _SelectBox_onBlur() {
  320.     }
  321.     function _SelectBox_onChange() {
  322.     }
  323.     function _SelectBox_onFocus() {
  324.     }
  325.     function _SelectBox_onValidate() {
  326.     }
  327.     function _SelectBox_onInvalid() {
  328.             // Error messages are put here so that they may be overridden
  329.             if (this.errType == "NoValue") {
  330.                 alert("An entry in this field is REQUIRED.")    
  331.             } else {
  332.                 alert("Unknown Error.")    
  333.             } 
  334.     }
  335.     function _SelectBox_onValid() {
  336.     }
  337.     function _SelectBox_onRender() {
  338.     }
  339.     function _SelectBox_onClick() {
  340.     }
  341.  
  342. } // END CONSTRUCTOR com_netobjects_SelectBox
  343.  
  344.  
  345.