CurrencyTextBox.js.uncompressed.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. define("dijit/form/CurrencyTextBox", [
  2. "dojo/currency", // currency._mixInDefaults currency.format currency.parse currency.regexp
  3. "dojo/_base/declare", // declare
  4. "dojo/_base/lang", // lang.hitch
  5. "./NumberTextBox"
  6. ], function(currency, declare, lang, NumberTextBox){
  7. // module:
  8. // dijit/form/CurrencyTextBox
  9. /*=====
  10. var __Constraints = declare([NumberTextBox.__Constraints, currency.__FormatOptions, currency.__ParseOptions], {
  11. // summary:
  12. // Specifies both the rules on valid/invalid values (minimum, maximum,
  13. // number of required decimal places), and also formatting options for
  14. // displaying the value when the field is not focused (currency symbol,
  15. // etc.)
  16. // description:
  17. // Follows the pattern of `dijit/form/NumberTextBox.__Constraints`.
  18. // In general developers won't need to set this parameter
  19. // example:
  20. // To ensure that the user types in the cents (for example, 1.00 instead of just 1):
  21. // | {fractional:true}
  22. });
  23. =====*/
  24. return declare("dijit.form.CurrencyTextBox", NumberTextBox, {
  25. // summary:
  26. // A validating currency textbox
  27. // description:
  28. // CurrencyTextBox is similar to `dijit/form/NumberTextBox` but has a few
  29. // extra features related to currency:
  30. //
  31. // 1. After specifying the currency type (american dollars, euros, etc.) it automatically
  32. // sets parse/format options such as how many decimal places to show.
  33. // 2. The currency mark (dollar sign, euro mark, etc.) is displayed when the field is blurred
  34. // but erased during editing, so that the user can just enter a plain number.
  35. // currency: [const] String
  36. // the [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD"
  37. currency: "",
  38. /*=====
  39. // constraints: __Constraints
  40. // Despite the name, this parameter specifies both constraints on the input
  41. // (including minimum/maximum allowed values) as well as
  42. // formatting options.
  43. constraints: {},
  44. ======*/
  45. baseClass: "dijitTextBox dijitCurrencyTextBox",
  46. // Override pattern ValidationTextBox.pattern.... we use a reg-ex generating function rather
  47. // than a straight regexp to deal with locale (plus formatting options too?)
  48. pattern: function(constraints){
  49. // if focused, accept either currency data or NumberTextBox format
  50. return '(' + (this.focused ? this.inherited(arguments, [ lang.mixin({}, constraints, this.editOptions) ]) + '|' : '')
  51. + currency.regexp(constraints) + ')';
  52. },
  53. // Override NumberTextBox._formatter to deal with currencies, ex: converts "123.45" to "$123.45"
  54. _formatter: currency.format,
  55. _parser: currency.parse,
  56. parse: function(/*String*/ value, /*Object*/ constraints){
  57. // summary:
  58. // Parses string value as a Currency, according to the constraints object
  59. // tags:
  60. // protected extension
  61. var v = this.inherited(arguments);
  62. if(isNaN(v) && /\d+/.test(value)){ // currency parse failed, but it could be because they are using NumberTextBox format so try its parse
  63. v = lang.hitch(lang.mixin({}, this, { _parser: NumberTextBox.prototype._parser }), "inherited")(arguments);
  64. }
  65. return v;
  66. },
  67. _setConstraintsAttr: function(/*Object*/ constraints){
  68. if(!constraints.currency && this.currency){
  69. constraints.currency = this.currency;
  70. }
  71. this.inherited(arguments, [ currency._mixInDefaults(lang.mixin(constraints, { exponent: false })) ]); // get places
  72. }
  73. });
  74. });