Number.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // var l = (value+'').split('.')[1] ? (value+'').split('.')[1] : 0;
  61. // format = format.split('.')[0] + new Array(l).map(function() {return '0'}).join('');
  62. value = isNaN(value) ? '' : String(Ext.util.Format.number(value, format)).replace('.', me.decimalSeparator);
  63. return value;
  64. },
  65. /**
  66. * @inheritdoc
  67. */
  68. getSubmitValue: function () {
  69. if (!this.thousandSeparator)
  70. return this.callParent(arguments);
  71. var me = this,
  72. value = me.callParent();
  73. if (!me.submitLocaleSeparator) {
  74. value = me.toRawNumber(value);
  75. }
  76. return value;
  77. },
  78. /**
  79. * @inheritdoc
  80. */
  81. setMinValue: function (value) {
  82. if (!this.thousandSeparator)
  83. return this.callParent(arguments);
  84. var me = this,
  85. allowed;
  86. me.minValue = Ext.Number.from(value, Number.NEGATIVE_INFINITY);
  87. me.toggleSpinners();
  88. // Build regexes for masking and stripping based on the configured options
  89. if (me.disableKeyFilter !== true) {
  90. allowed = me.baseChars + '';
  91. if (me.allowExponential) {
  92. allowed += me.decimalSeparator + 'e+-';
  93. }
  94. else {
  95. allowed += Ext.util.Format.thousandSeparator;
  96. if (me.allowDecimals) {
  97. allowed += me.decimalSeparator;
  98. }
  99. if (me.minValue < 0) {
  100. allowed += '-';
  101. }
  102. }
  103. allowed = Ext.String.escapeRegex(allowed);
  104. me.maskRe = new RegExp('[' + allowed + ']');
  105. if (me.autoStripChars) {
  106. me.stripCharsRe = new RegExp('[^' + allowed + ']', 'gi');
  107. }
  108. }
  109. },
  110. /**
  111. * @private
  112. */
  113. parseValue: function (value) {
  114. if (!this.thousandSeparator)
  115. return this.callParent(arguments);
  116. value = parseFloat(this.toRawNumber(value));
  117. return isNaN(value) ? null : value;
  118. }
  119. });