Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. Ext.define('Ext.ux.form.field.CurrencyNumber', {
  2. extend: 'Ext.form.field.Number',
  3. alias: 'widget.currencynumberfield',
  4. config: {
  5. /**
  6. * @cfg {String} currencySign
  7. * The currency sign that the currency formatter displays. Defaults to Ext.util.Format.currencySign
  8. */
  9. currencySign: null,
  10. /**
  11. * @cfg {Boolean} currencyAtEnd
  12. * This may be set to true to make the currency function append the currency sign to the formatted value.
  13. * Defaults to Ext.util.Format.currencyAtEnd
  14. */
  15. currencyAtEnd: null
  16. },
  17.  
  18. initComponent: function() {
  19. var me = this;
  20.  
  21. // Get currency format defaults from Ext.util.Format
  22.  
  23. if (me.currencySign == null) {
  24. me.currencySign = Ext.util.Format.currencySign;
  25. }
  26.  
  27. if (me.currencyAtEnd == null) {
  28. me.currencyAtEnd = Ext.util.Format.currencyAtEnd;
  29. }
  30.  
  31. if (me.decimalPrecision == null) {
  32. me.decimalPrecision = Ext.util.Format.currencyPrecision;
  33. }
  34.  
  35. me.callParent();
  36. },
  37.  
  38. /**
  39. * Converts a mixed-type value to a raw representation suitable for displaying in the field. This allows controlling
  40. * how value objects passed to {@link #setValue} are shown to the user.
  41. *
  42. * See {@link #rawToValue} for the opposite conversion.
  43. *
  44. * Formats value as currency using Ext.util.Format.currency.
  45. *
  46. * @param {Object} value The mixed-type value to convert to the raw representation.
  47. * @return {Object} The converted raw value.
  48. */
  49. valueToRaw: function(value) {
  50. // Only format if it's numeric, otherwise the invalid messaging is not displayed properly
  51. var val = String(value).replace(this.currencySign, '');
  52. if (Ext.isNumeric(val)) {
  53. return Ext.util.Format.currency(value, this.currencySign, this.decimalPrecision, this.currencyAtEnd);
  54. } else {
  55. return val;
  56. }
  57. },
  58.  
  59. /**
  60. * Converts a raw input field value into a mixed-type value that is suitable for this particular field type. This
  61. * allows controlling the normalization and conversion of user-entered values into field-type-appropriate values.
  62. *
  63. * Removes currency format and converts to numeric.
  64. *
  65. * See {@link #valueToRaw} for the opposite conversion.
  66. *
  67. * @param {Object} rawValue
  68. * @return {Object} The converted value.
  69. * @method
  70. */
  71. rawToValue: function(rawValue) {
  72. var me = this;
  73. var value = String(rawValue).replace(/[^0-9.]/g, '');
  74. return Ext.util.Format.round(value, me.decimalPrecision);
  75. },
  76.  
  77. /**
  78. * Removes Currency formatting and runs all of Number's validations and returns an array of any errors.
  79. * Note that this first runs Text's validations, so the returned array is an amalgamation of all field errors.
  80. * The additional validations run test that the value is a number, and that it is within the
  81. * configured min and max values.
  82. *
  83. * @param {Object} [value] The value to get errors for (defaults to the current field value)
  84. * @return {String[]} All validation errors for this field
  85. */
  86. getErrors: function(value) {
  87. value = arguments.length > 0 ? value : this.processRawValue(this.getRawValue());
  88.  
  89. var me = this,
  90. errors;
  91.  
  92. value = me.rawToValue(value);
  93. errors = me.callParent([value])
  94. return errors;
  95. },
  96.  
  97. /**
  98. * @method
  99. * Template method to do any pre-focus processing.
  100. * Removes currency formatting before focus.
  101. * @protected
  102. * @param {Ext.event.Event} e The event object
  103. */
  104. beforeFocus: function() {
  105. this.setRawValue(this.getValue());
  106. }
  107. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement