CurrencyTextBox.js 3.7 KB

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