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 / TextArea / TextArea.js < prev    next >
Encoding:
JavaScript  |  1998-10-05  |  15.2 KB  |  445 lines

  1. /* ******************** BEGIN TextArea Object ******************** */
  2.  
  3. /* ======================================================================
  4. OBJECT:        com_netobjects_TextArea
  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 TextArea fields with common built
  10.                 in behaviors such as onClick events and field validation.
  11.  
  12. NOTES:        The onKeyPress, onKeyDown and onKeyUp events are disabled for Navigator 4.0 only
  13.                 due to a bug in Netscape Navigator 4.0.
  14. ====================================================================== */
  15. function com_netobjects_TextArea(params) {
  16.      // Public Properties    
  17.     this.name                 = (params.name != null) ? params.name : "";
  18.     this.textAreaName        = (params.textAreaName != null) ? params.textAreaName : "";
  19.     this.formName            = (params.formName != null) ? params.formName : "";
  20.     this.defaultValue        = (params.defaultValue != null) ? params.defaultValue : "";
  21.     this.cols                = (params.cols != null) ? params.cols : "";
  22.     this.rows                = (params.rows != null) ? params.rows : "";
  23.     this.wrap                = (params.wrap != null) ? params.wrap : "";
  24.  
  25.     this.validate             = (params.validate != null && params.validate == true) ? params.validate : false;
  26.     this.isRequired        = (params.isRequired != null && params.isRequired == true) ? params.isRequired : false;
  27.     this.maxChars            = (params.maxChars != null) ? params.maxChars : 0;  // 0 indicates no maximum limit
  28.     this.state                 = (params.state != null) ? params.state : "";
  29.  
  30.     this.testMode            = (params.testMode != null) ? params.testMode : "off";
  31.     
  32.     // Private Properties
  33.     this.isValid             = true;
  34.     this.errType             = null;
  35.     this.textAreaIndex     = null
  36.  
  37.     // Public Methods
  38.     this.render             = _textarea_render;
  39.     this.validateValue     = _textarea_validateValue;
  40.  
  41.     this.getValue            = _textarea_getValue;
  42.     this.setValue            = _textarea_setValue;
  43.  
  44.     // Private Methods
  45.     this.validateProperties = _textarea_validateProperties;
  46.     this.gethdlElement        = _textarea_gethdlElement;    // gets the handle to the form element object on the page
  47.  
  48.     // Public Events
  49.     this.onBlur         = _textarea_onBlur;
  50.     this.onChange         = _textarea_onChange;
  51.     this.onFocus       = _textarea_onFocus;
  52.     this.onKeyDown        = _textarea_onKeyDown;
  53.     this.onKeyPress    = _textarea_onKeyPress;
  54.     this.onKeyUp        = _textarea_onKeyUp;
  55.     this.onSelect         = _textarea_onSelect;
  56.  
  57.     this.onValidate      = _textarea_onValidate;
  58.     this.onInvalid       = _textarea_onInvalid;
  59.     this.onValid       = _textarea_onValid;
  60.     this.onRender     = _textarea_onRender;
  61.  
  62.     // Private Events (internal event driven functionality)
  63.     this.eOnChange    = _textarea_eOnChange;
  64.  
  65.     // Validate object properties
  66.     this.validateProperties();
  67.  
  68.     // Public Methods ----------------------------------------
  69.  
  70.     /* ======================================================================
  71.     
  72.    METHOD: _textarea_render.  Renders the element in HTML.
  73.     
  74.     ====================================================================== */
  75.     function _textarea_render() {
  76.         // Initialize Variables
  77.         var stextAreaName = "";
  78.         var svalue = "";
  79.         var srows = "";
  80.         var scols = "";
  81.         var swrap = "";
  82.         var sonKeyDown = "";
  83.         var sonKeyPress = "";
  84.         var sonKeyUp = "";
  85.  
  86.         var sonBlur      = "";
  87.         var sonChange  = "";
  88.         var sonFocus   = "";
  89.         var sonSelect  = "";
  90.     
  91.         // Set Variables
  92.         stextAreaName = ' NAME="' + this.textAreaName + '"';
  93.         if (this.rows != "")
  94.             srows = ' ROWS="' + this.rows + '"';
  95.         if (this.cols != "")    
  96.             scols = ' COLS="' + this.cols + '"';
  97.         if (this.wrap != "")
  98.             swrap = ' WRAP="' + this.wrap + '"';
  99.  
  100.         svalue = this.defaultValue;
  101.  
  102.         // Build Nav & IE shared link events
  103.         sonBlur      = ' onBlur="' + this.name + '.onBlur();"';
  104.         sonChange   = ' onChange="' + this.name + '.eOnChange();"';
  105.         sonFocus    = ' onFocus="' + this.name + '.onFocus();"';
  106.  
  107.         // Workaround for a Nav 4.0 bug - these events don't work well with onChange.  
  108.         // So this component will provide onChange for Nav 4.0, but not the following three events:
  109.         if (DetermineCurrentBrowser() != "Netscape 4.0") {
  110.             sonKeyDown     = ' onKeyDown="' + this.name + '.onKeyDown();"';
  111.             sonKeyPress    = ' onKeyPress="' + this.name + '.onKeyPress();"';
  112.             sonKeyUp      = ' onKeyUp="' + this.name + '.onKeyUp();"';
  113.         }
  114.         sonSelect      = ' onSelect="' + this.name + '.onSelect();"';
  115.         eEvents      = sonBlur + sonChange + sonFocus + sonKeyDown + sonKeyPress + sonKeyUp + sonSelect;
  116.  
  117.         // Write HTML
  118.         if (this.state == "hidden") {
  119.            document.write( "<Input Type=hidden" + stextAreaName + ' value="' + svalue + '"' + eEvents + ">\n");
  120.         } else if (this.state == "read-only") {
  121.            document.write( this.defaultValue + "\n");
  122.         } else {
  123.            document.write("<TEXTAREA " + 
  124.                             stextAreaName + 
  125.                             srows + 
  126.                             scols + 
  127.                             swrap +
  128.                             eEvents + ">");
  129.             document.write(svalue);
  130.             document.writeln("</TEXTAREA>");
  131.         }
  132.  
  133.         if (this.state != "read-only") { 
  134.             // Set control index:
  135.             // The control index is used by the object to identify its respective form element
  136.             // in the event that there are more than one form element with the same name.
  137.             this.textAreaIndex = eval("window.document." + this.formName + "." + this.textAreaName + ".length")-1
  138.             if (isNaN(this.textAreaIndex)) {this.textAreaIndex=0}
  139.         }
  140.  
  141.         // Write HTML
  142.         
  143.         // Fire public events
  144.         this.onRender(); 
  145.     } // END _textarea_render
  146.  
  147.  
  148.     /* ======================================================================
  149.     
  150.    METHOD: _textarea_validateValue.  Validates the current value of the 
  151.     textarea field.  Checks isRequired (not blank) and length vs. maxChars.
  152.     
  153.     ====================================================================== */
  154.     function _textarea_validateValue() {
  155.         var value = (this.gethdlElement() != null) ? (this.gethdlElement()).value : this.defaultValue;
  156.         this.isValid = true;
  157.         this.errType = "";
  158.  
  159.         // Check for required value
  160.         if ((this.isRequired) && ((value == null) || (value == ""))) {
  161.             this.errType = "NoValue";
  162.             this.isValid = false;
  163.         }
  164.         if (value != null && this.maxChars > 0 && value.length > this.maxChars) {
  165.             this.errType = "OverMaxChars";
  166.             this.isValid = false;
  167.         }
  168.  
  169.         // Fire public events
  170.         if (this.isValid) 
  171.             this.onValid();
  172.         else 
  173.             this.onInvalid();
  174.         this.onValidate();
  175.  
  176.         return this.isValid;
  177.     } // END _textarea_validateValue
  178.  
  179.  
  180.     /* ======================================================================
  181.     
  182.    METHOD: _textarea_getValue.  Gets the value of the form element on the page.
  183.     
  184.     ====================================================================== */
  185.     function _textarea_getValue() {
  186.         var value = "";
  187.         
  188.         if (this.gethdlElement() != null)
  189.             value = (this.gethdlElement()).value
  190.         else
  191.             value = this.defaultValue;
  192.  
  193.         return value;
  194.     } // END _textarea_getValue
  195.  
  196.  
  197.     /* ======================================================================
  198.     
  199.    METHOD: _textarea_setValue.  Sets the value of the form element on the page.
  200.     
  201.     ====================================================================== */
  202.     function _textarea_setValue(value) {
  203.         var success = false;
  204.         
  205.         if (this.gethdlElement() != null && value != null) {
  206.             (this.gethdlElement()).value = value;
  207.             success = true;
  208.         }
  209.  
  210.         return success;
  211.     } // END _textarea_setValue
  212.  
  213.  
  214.     // Private Methods ----------------------------------------
  215.  
  216.     //
  217.     // Validate current properties
  218.     //
  219.     function _textarea_validateProperties() {
  220.         if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
  221.             var errors = new Array()
  222.           iIndex = 0;  
  223.          
  224.             // Check for errors
  225.             if (this.name == "") 
  226.                 errors[iIndex++] = "Name is a required property.";  
  227.             if (this.textAreaName == "") 
  228.                 errors[iIndex++] = "Text area name is a required property.";  
  229.             if (this.textAreaName == this.name) 
  230.                 errors[iIndex++] = "Text area name and object name may not be the same.";  
  231.             if (this.formName == "") 
  232.                 errors[iIndex++] = "Form name is a required property.";  
  233.             if ((this.defaultValue == "") && (this.isRequired) && (this.validate))
  234.                 errors[iIndex++] = "WARNING: A value is required but no default is given.  Invalid values are only automatically trapped upon change from their original value.";  
  235.  
  236.             // Display errors
  237.             for (var i=0; i<errors.length; i++)  {
  238.                 if (this.testMode.toLowerCase()    == "text") 
  239.                     document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_Button, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n");
  240.                 else
  241.                       alert("TEST MODE, com_netobjects_TextBox, '" + this.name + "' : " + errors[i]);
  242.  
  243.             }            
  244.         } 
  245.     } // END _textarea_validateProperties
  246.  
  247.  
  248.  
  249.     /* ======================================================================
  250.     
  251.    METHOD: _textarea_gethdlElement.  Gets handle of associated element. 
  252.     
  253.     ====================================================================== */
  254.     function _textarea_gethdlElement() {
  255.         var result = null
  256.         if (this.textAreaIndex != null) { // if null hasn't rendered a hidden or default element
  257.             var index = "";
  258.             
  259.                 if (!isNaN(eval("window.document." + this.formName + "." + this.textAreaName + ".length"))) {
  260.                 index = "[" + this.textAreaIndex + "]"
  261.             }
  262.             result = eval("window.document." + this.formName + "." + this.textAreaName + index )
  263.         }
  264.         return result
  265.     } // END _textarea_gethdlElement
  266.  
  267.  
  268.     // Private Event Handlers ----------------------------------------
  269.  
  270.     /* ======================================================================
  271.     
  272.    EVENT: _textarea_eOnChange.  Internal event handler that validates the
  273.     value when the value of the textarea field is changed.
  274.     
  275.     ====================================================================== */
  276.     function _textarea_eOnChange() {
  277.         // Validate the newly changed value
  278.         if (this.validate) 
  279.             this.validateValue();
  280.  
  281.         // Fire public events
  282.         this.onChange();
  283.  
  284.         return this.isValid;
  285.     } // END _textarea_eOnChange
  286.     
  287.     // Public Event Handlers ----------------------------------------
  288.  
  289.     // Default handlers for each event, overridden in calling page.
  290.     function _textarea_onBlur() {
  291.     }
  292.     function _textarea_onChange() {
  293.     }
  294.     function _textarea_onFocus() {
  295.     }
  296.     function _textarea_onKeyDown() {
  297.     }
  298.     function _textarea_onKeyPress() {
  299.     }
  300.     function _textarea_onKeyUp() {
  301.     }
  302.     function _textarea_onSelect() {
  303.     }
  304.     function _textarea_onValidate() {
  305.     }
  306.     function _textarea_onInvalid() {
  307.         if (this.errType == "NoValue") 
  308.             alert("An entry in this field is REQUIRED.")    ;
  309.         else if (this.errType == "OverMaxChars") 
  310.             alert("You have exceeded " + this.maxChars + " characters, the maximum allowed.");
  311.     } // END _textarea_onInvalid
  312.     function _textarea_onValid() {
  313.     }
  314.     function _textarea_onRender() {
  315.     }
  316. } // END CONSTRUCTOR com_netobjects_TextArea 
  317.  
  318.  
  319. // Shared functions ----------------------------------------
  320.  
  321. /* ======================================================================
  322. FUNCTION:      IsNum
  323.  
  324. INPUT:          numstr (string/number) - the string that will be tested to ensure 
  325.                                                that the value is a number (int or float)
  326.  
  327. RETURN:      true, if all characters represent a valid integer or float
  328.                  false, otherwise.
  329.  
  330. PLATFORMS:    Netscape Navigator 3.01 and higher,
  331.                   Microsoft Internet Explorer 3.02 and higher,
  332.                   Netscape Enterprise Server 3.0,
  333.                   Microsoft IIS/ASP 3.0.
  334. ====================================================================== */
  335. function IsNum( numstr ) {
  336.     // Return immediately if an invalid value was passed in
  337.     if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")    
  338.         return false;
  339.  
  340.     var isValid = true;
  341.     var decCount = 0;        // number of decimal points in the string
  342.  
  343.     // convert to a string for performing string comparisons.
  344.     numstr += "";    
  345.  
  346.     // Loop through string and test each character. If any
  347.     // character is not a number, return a false result.
  348.      // Include special cases for negative numbers (first char == '-')
  349.     // and a single decimal point (any one char in string == '.').   
  350.     for (i = 0; i < numstr.length; i++) {
  351.         // track number of decimal points
  352.         if (numstr.charAt(i) == ".")
  353.             decCount++;
  354.  
  355.         if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
  356.                 (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
  357.            isValid = false;
  358.            break;
  359.         } else if ((numstr.charAt(i) == "-" && i != 0) ||
  360.                 (numstr.charAt(i) == "." && numstr.length == 1) ||
  361.               (numstr.charAt(i) == "." && decCount > 1)) {
  362.            isValid = false;
  363.            break;
  364.       }                                 
  365.    } // END for   
  366.    
  367.        return isValid;
  368. }  // end IsNum
  369.  
  370.  
  371. /* ======================================================================
  372. FUNCTION:    IsAlphaNumOrUnderscore
  373.  
  374. INPUT:        str (string) - the string to be tested
  375.  
  376. RETURN:      true, if the string contains only alphanumeric characters or underscores.
  377.                 false, otherwise.
  378.  
  379. PLATFORMS:    Netscape Navigator 3.01 and higher,
  380.                   Microsoft Internet Explorer 3.02 and higher,
  381.                   Netscape Enterprise Server 3.0,
  382.                   Microsoft IIS/ASP 3.0.
  383. ====================================================================== */
  384. function IsAlphaNumOrUnderscore( str ) {
  385.     // Return immediately if an invalid value was passed in
  386.     if (str+"" == "undefined" || str+"" == "null" || str+"" == "")    
  387.         return false;
  388.  
  389.     var isValid = true;
  390.  
  391.     str += "";    // convert to a string for performing string comparisons.
  392.     // Loop through string one character at a time. If non-alpha numeric
  393.     // is found then, break out of loop and return a false result
  394.  
  395.     for (i = 0; i < str.length; i++)
  396.        {
  397.         // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
  398.               if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
  399.                   ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
  400.                   ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
  401.                   (str.charAt(i) == "_") ) )
  402.               {
  403.                    isValid = false;
  404.                  break;
  405.               }
  406.  
  407.     } // END for   
  408.    
  409.     return isValid;
  410. }  // end IsAlphaNumOrUnderscore
  411.  
  412.  
  413. /* ======================================================================
  414. FUNCTION:    DetermineCurrentBrowser 
  415.  
  416. INPUT:         none.
  417.  
  418. RETURN:        a string indicating the current browser
  419.  
  420. DESC:            This function is used with client-side JavaScript to detect
  421.                 the brand and version of the user's browser.  Detects only
  422.                 Netscape and Microsoft browsers.  Returns "Other" if a different
  423.                 brand is detected.
  424.  
  425. PLATFORMS:    Netscape Navigator 3.01 and higher,
  426.                   Microsoft Internet Explorer 3.02 and higher,
  427. ====================================================================== */
  428. function DetermineCurrentBrowser() {
  429.     var current_browser = ""; 
  430.     var bwr = navigator.appName; 
  431.     var ver = parseInt(navigator.appVersion, 10); 
  432.     
  433.     if ( bwr == "Netscape" && ver == 4 ) current_browser = "Netscape 4.0"; 
  434.     else if ( bwr == "Netscape" && ver == 3 ) current_browser = "Netscape 3.0";  
  435.     else if ( bwr == "Netscape" && ver == 2 ) current_browser = "Netscape 2.0"; 
  436.     else if ( bwr == "Microsoft Internet Explorer" && ver == 3 ) current_browser = "MSIE 3.0"; 
  437.     else if ( bwr == "Microsoft Internet Explorer" && ver == 4 ) current_browser = "MSIE 4.0"; 
  438.     else current_browser = "Other";
  439.     
  440.     return current_browser;
  441. } // end DetermineCurrentBrowser
  442.  
  443.  
  444. /* ******************** END TextArea Object ******************** */
  445.