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