Number.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Ext.define("saas.override.form.field.Number", {
  2. override: "Ext.form.field.Number",
  3. hideTrigger: true, // 隐藏trigger
  4. mouseWheelEnabled: false, // 取消滚轮事件
  5. /**
  6. * @cfg {Boolean} thousandSeparator
  7. */
  8. thousandSeparator: false,
  9. /**
  10. * @inheritdoc
  11. */
  12. toRawNumber: function (value) {
  13. return String(value).replace(this.decimalSeparator, '.').replace(new RegExp(Ext.util.Format.thousandSeparator, "g"), '');
  14. },
  15. /**
  16. * @inheritdoc
  17. */
  18. getErrors: function (value) {
  19. if (!this.thousandSeparator)
  20. return this.callParent(arguments);
  21. var me = this,
  22. errors = Ext.form.field.Text.prototype.getErrors.apply(me, arguments),
  23. format = Ext.String.format,
  24. num;
  25. value = Ext.isDefined(value) ? value : this.processRawValue(this.getRawValue());
  26. if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
  27. return errors;
  28. }
  29. value = me.toRawNumber(value);
  30. if (isNaN(value.replace(Ext.util.Format.thousandSeparator, ''))) {
  31. errors.push(format(me.nanText, value));
  32. }
  33. num = me.parseValue(value);
  34. if (me.minValue === 0 && num < 0) {
  35. errors.push(this.negativeText);
  36. }
  37. else if (num < me.minValue) {
  38. errors.push(format(me.minText, me.minValue));
  39. }
  40. if (num > me.maxValue) {
  41. errors.push(format(me.maxText, me.maxValue));
  42. }
  43. return errors;
  44. },
  45. /**
  46. * @inheritdoc
  47. */
  48. valueToRaw: function (value) {
  49. if (!this.thousandSeparator)
  50. return this.callParent(arguments);
  51. var me = this;
  52. var format = "000,000";
  53. var l = (value+'').split('.')[1] ? (value+'').split('.')[1] : 0;
  54. for (var i = 0; i < l && i < me.decimalPrecision; i++) {
  55. if (i == 0)
  56. format += ".";
  57. format += "0";
  58. }
  59. value = me.parseValue(Ext.util.Format.number(value, format));
  60. value = me.fixPrecision(value);
  61. value = Ext.isNumber(value) ? value : parseFloat(me.toRawNumber(value));
  62. // var l = (value+'').split('.')[1] ? (value+'').split('.')[1] : 0;
  63. // format = format.split('.')[0] + new Array(l).map(function() {return '0'}).join('');
  64. value = isNaN(value) ? '' : String(Ext.util.Format.number(value, format)).replace('.', me.decimalSeparator);
  65. return value;
  66. },
  67. /**
  68. * @inheritdoc
  69. */
  70. getSubmitValue: function () {
  71. if (!this.thousandSeparator)
  72. return this.callParent(arguments);
  73. var me = this,
  74. value = me.callParent();
  75. if (!me.submitLocaleSeparator) {
  76. value = me.toRawNumber(value);
  77. }
  78. return value;
  79. },
  80. /**
  81. * @inheritdoc
  82. */
  83. setMinValue: function (value) {
  84. if (!this.thousandSeparator)
  85. return this.callParent(arguments);
  86. var me = this,
  87. allowed;
  88. me.minValue = Ext.Number.from(value, Number.NEGATIVE_INFINITY);
  89. me.toggleSpinners();
  90. // Build regexes for masking and stripping based on the configured options
  91. if (me.disableKeyFilter !== true) {
  92. allowed = me.baseChars + '';
  93. if (me.allowExponential) {
  94. allowed += me.decimalSeparator + 'e+-';
  95. }
  96. else {
  97. allowed += Ext.util.Format.thousandSeparator;
  98. if (me.allowDecimals) {
  99. allowed += me.decimalSeparator;
  100. }
  101. if (me.minValue < 0) {
  102. allowed += '-';
  103. }
  104. }
  105. allowed = Ext.String.escapeRegex(allowed);
  106. me.maskRe = new RegExp('[' + allowed + ']');
  107. if (me.autoStripChars) {
  108. me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
  109. }
  110. }
  111. },
  112. /**
  113. * @private
  114. */
  115. parseValue: function (value) {
  116. if (!this.thousandSeparator)
  117. return this.callParent(arguments);
  118. value = parseFloat(this.toRawNumber(value));
  119. return isNaN(value) ? null : value;
  120. }
  121. });