You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
8.2 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-form-NumberField-method-constructor'><span id='Ext-form-NumberField'>/**
  19. </span></span> * @class Ext.form.NumberField
  20. * @extends Ext.form.TextField
  21. * Numeric text field that provides automatic keystroke filtering and numeric validation.
  22. * @constructor
  23. * Creates a new NumberField
  24. * @param {Object} config Configuration options
  25. * @xtype numberfield
  26. */
  27. Ext.form.NumberField = Ext.extend(Ext.form.TextField, {
  28. <span id='Ext-form-NumberField-cfg-stripCharsRe'> /**
  29. </span> * @cfg {RegExp} stripCharsRe @hide
  30. */
  31. <span id='Ext-form-NumberField-cfg-maskRe'> /**
  32. </span> * @cfg {RegExp} maskRe @hide
  33. */
  34. <span id='Ext-form-NumberField-cfg-fieldClass'> /**
  35. </span> * @cfg {String} fieldClass The default CSS class for the field (defaults to &quot;x-form-field x-form-num-field&quot;)
  36. */
  37. fieldClass: &quot;x-form-field x-form-num-field&quot;,
  38. <span id='Ext-form-NumberField-cfg-allowDecimals'> /**
  39. </span> * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
  40. */
  41. allowDecimals : true,
  42. <span id='Ext-form-NumberField-cfg-decimalSeparator'> /**
  43. </span> * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
  44. */
  45. decimalSeparator : &quot;.&quot;,
  46. <span id='Ext-form-NumberField-cfg-decimalPrecision'> /**
  47. </span> * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
  48. */
  49. decimalPrecision : 2,
  50. <span id='Ext-form-NumberField-cfg-allowNegative'> /**
  51. </span> * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
  52. */
  53. allowNegative : true,
  54. <span id='Ext-form-NumberField-cfg-minValue'> /**
  55. </span> * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
  56. */
  57. minValue : Number.NEGATIVE_INFINITY,
  58. <span id='Ext-form-NumberField-cfg-maxValue'> /**
  59. </span> * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
  60. */
  61. maxValue : Number.MAX_VALUE,
  62. <span id='Ext-form-NumberField-cfg-minText'> /**
  63. </span> * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to &quot;The minimum value for this field is {minValue}&quot;)
  64. */
  65. minText : &quot;The minimum value for this field is {0}&quot;,
  66. <span id='Ext-form-NumberField-cfg-maxText'> /**
  67. </span> * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to &quot;The maximum value for this field is {maxValue}&quot;)
  68. */
  69. maxText : &quot;The maximum value for this field is {0}&quot;,
  70. <span id='Ext-form-NumberField-cfg-nanText'> /**
  71. </span> * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
  72. * if a valid character like '.' or '-' is left in the field with no number (defaults to &quot;{value} is not a valid number&quot;)
  73. */
  74. nanText : &quot;{0} is not a valid number&quot;,
  75. <span id='Ext-form-NumberField-cfg-baseChars'> /**
  76. </span> * @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
  77. */
  78. baseChars : &quot;0123456789&quot;,
  79. <span id='Ext-form-NumberField-cfg-autoStripChars'> /**
  80. </span> * @cfg {Boolean} autoStripChars True to automatically strip not allowed characters from the field. Defaults to &lt;tt&gt;false&lt;/tt&gt;
  81. */
  82. autoStripChars: false,
  83. <span id='Ext-form-NumberField-method-initEvents'> // private
  84. </span> initEvents : function() {
  85. var allowed = this.baseChars + '';
  86. if (this.allowDecimals) {
  87. allowed += this.decimalSeparator;
  88. }
  89. if (this.allowNegative) {
  90. allowed += '-';
  91. }
  92. allowed = Ext.escapeRe(allowed);
  93. this.maskRe = new RegExp('[' + allowed + ']');
  94. if (this.autoStripChars) {
  95. this.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
  96. }
  97. Ext.form.NumberField.superclass.initEvents.call(this);
  98. },
  99. <span id='Ext-form-NumberField-method-getErrors'> /**
  100. </span> * Runs all of NumberFields validations and returns an array of any errors. Note that this first
  101. * runs TextField's validations, so the returned array is an amalgamation of all field errors.
  102. * The additional validations run test that the value is a number, and that it is within the
  103. * configured min and max values.
  104. * @param {Mixed} value The value to get errors for (defaults to the current field value)
  105. * @return {Array} All validation errors for this field
  106. */
  107. getErrors: function(value) {
  108. var errors = Ext.form.NumberField.superclass.getErrors.apply(this, arguments);
  109. value = Ext.isDefined(value) ? value : this.processValue(this.getRawValue());
  110. if (value.length &lt; 1) { // if it's blank and textfield didn't flag it then it's valid
  111. return errors;
  112. }
  113. value = String(value).replace(this.decimalSeparator, &quot;.&quot;);
  114. if(isNaN(value)){
  115. errors.push(String.format(this.nanText, value));
  116. }
  117. var num = this.parseValue(value);
  118. if (num &lt; this.minValue) {
  119. errors.push(String.format(this.minText, this.minValue));
  120. }
  121. if (num &gt; this.maxValue) {
  122. errors.push(String.format(this.maxText, this.maxValue));
  123. }
  124. return errors;
  125. },
  126. <span id='Ext-form-NumberField-method-getValue'> getValue : function() {
  127. </span> return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
  128. },
  129. <span id='Ext-form-NumberField-method-setValue'> setValue : function(v) {
  130. </span> v = Ext.isNumber(v) ? v : parseFloat(String(v).replace(this.decimalSeparator, &quot;.&quot;));
  131. v = this.fixPrecision(v);
  132. v = isNaN(v) ? '' : String(v).replace(&quot;.&quot;, this.decimalSeparator);
  133. return Ext.form.NumberField.superclass.setValue.call(this, v);
  134. },
  135. <span id='Ext-form-NumberField-method-setMinValue'> /**
  136. </span> * Replaces any existing {@link #minValue} with the new value.
  137. * @param {Number} value The minimum value
  138. */
  139. setMinValue : function(value) {
  140. this.minValue = Ext.num(value, Number.NEGATIVE_INFINITY);
  141. },
  142. <span id='Ext-form-NumberField-method-setMaxValue'> /**
  143. </span> * Replaces any existing {@link #maxValue} with the new value.
  144. * @param {Number} value The maximum value
  145. */
  146. setMaxValue : function(value) {
  147. this.maxValue = Ext.num(value, Number.MAX_VALUE);
  148. },
  149. <span id='Ext-form-NumberField-method-parseValue'> // private
  150. </span> parseValue : function(value) {
  151. value = parseFloat(String(value).replace(this.decimalSeparator, &quot;.&quot;));
  152. return isNaN(value) ? '' : value;
  153. },
  154. <span id='Ext-form-NumberField-method-fixPrecision'> /**
  155. </span> * @private
  156. *
  157. */
  158. fixPrecision : function(value) {
  159. var nan = isNaN(value);
  160. if (!this.allowDecimals || this.decimalPrecision == -1 || nan || !value) {
  161. return nan ? '' : value;
  162. }
  163. return parseFloat(parseFloat(value).toFixed(this.decimalPrecision));
  164. },
  165. <span id='Ext-form-NumberField-method-beforeBlur'> beforeBlur : function() {
  166. </span> var v = this.parseValue(this.getRawValue());
  167. if (!Ext.isEmpty(v)) {
  168. this.setValue(v);
  169. }
  170. }
  171. });
  172. Ext.reg('numberfield', Ext.form.NumberField);
  173. </pre>
  174. </body>
  175. </html>