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