Number.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. Ext.define("saas.override.form.field.Number", {
  2. override: "Ext.form.field.Number",
  3. /**
  4. * @cfg {Boolean} thousandSeparator
  5. */
  6. thousandSeparator: false,
  7. /**
  8. * @inheritdoc
  9. */
  10. toRawNumber: function (value) {
  11. return String(value).replace(this.decimalSeparator, '.').replace(new RegExp(Ext.util.Format.thousandSeparator, "g"), '');
  12. },
  13. /**
  14. * @inheritdoc
  15. */
  16. getErrors: function (value) {
  17. if (!this.thousandSeparator)
  18. return this.callParent(arguments);
  19. var me = this,
  20. errors = Ext.form.field.Text.prototype.getErrors.apply(me, arguments),
  21. format = Ext.String.format,
  22. num;
  23. value = Ext.isDefined(value) ? value : this.processRawValue(this.getRawValue());
  24. if (value.length < 1) { // if it's blank and textfield didn't flag it then it's valid
  25. return errors;
  26. }
  27. value = me.toRawNumber(value);
  28. if (isNaN(value.replace(Ext.util.Format.thousandSeparator, ''))) {
  29. errors.push(format(me.nanText, value));
  30. }
  31. num = me.parseValue(value);
  32. if (me.minValue === 0 && num < 0) {
  33. errors.push(this.negativeText);
  34. }
  35. else if (num < me.minValue) {
  36. errors.push(format(me.minText, me.minValue));
  37. }
  38. if (num > me.maxValue) {
  39. errors.push(format(me.maxText, me.maxValue));
  40. }
  41. return errors;
  42. },
  43. /**
  44. * @inheritdoc
  45. */
  46. valueToRaw: function (value) {
  47. if (!this.thousandSeparator)
  48. return this.callParent(arguments);
  49. var me = this;
  50. var format = "000,000";
  51. var l = (value+'').split('.')[1] ? (value+'').split('.')[1] : 0;
  52. for (var i = 0; i < l && i < me.decimalPrecision; i++) {
  53. if (i == 0)
  54. format += ".";
  55. format += "0";
  56. }
  57. value = me.parseValue(Ext.util.Format.number(value, format));
  58. value = me.fixPrecision(value);
  59. value = Ext.isNumber(value) ? value : parseFloat(me.toRawNumber(value));
  60. console.log('v1', format, value);
  61. // var l = (value+'').split('.')[1] ? (value+'').split('.')[1] : 0;
  62. // format = format.split('.')[0] + new Array(l).map(function() {return '0'}).join('');
  63. value = isNaN(value) ? '' : String(Ext.util.Format.number(value, format)).replace('.', me.decimalSeparator);
  64. console.log('v2', format, value);
  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. });