Merge pull request #1 from shackbarth/keith1
[xtuple] / enyo-client / application / source / widgets / combobox.js
1 /*jshint node:true, indent:2, curly:true, eqeqeq:true, immed:true,
2 latedef:true, newcap:true, noarg:true,
3 regexp:true, undef:true, trailing:true, white:true */
4 /*global XT:true, XV:true, XM:true, Globalize:true, enyo:true, _:true */
5
6 (function () {
7
8   // ..........................................................
9   // CREDIT CARD GATEWAY
10   //
11
12   enyo.kind(
13     /** @lends XV.CreditCardGatewayCombobox# */{
14     name: "XV.CreditCardGatewayCombobox",
15     kind: "XV.ComboboxWidget",
16     events: {
17       onNotify: ""
18     },
19     collection: "XM.creditCardGateways",
20     controlValueChanged: function (inSender, inEvent) {
21       if (!XM.creditCardGateways.find(function (model) { return model.id === inEvent.value; })) {
22         this.doNotify({message: "_unsupportedGateway".loc()});
23       }
24       return this.inherited(arguments);
25     }
26   });
27
28   // ..........................................................
29   // COUNTRY
30   //
31
32   /**
33     @class A combobox backed by the XM.countries collection.
34     @name XV.CountryCombobox
35     @extends XV.Combobox
36    */
37   enyo.kind(
38     /** @lends XV.CountryCombobox# */{
39     name: "XV.CountryCombobox",
40     kind: "XV.ComboboxWidget",
41     collection: "XM.countries"
42   });
43
44   // ..........................................................
45   // HONORIFIC
46   //
47
48   /**
49     @class A combobox backed by the XM.honorifics collection.
50     @name XV.HonorificCombobox
51     @extends XV.Combobox
52    */
53   enyo.kind(/** @lends XV.HonorificCombobox# */{
54     name: "XV.HonorificCombobox",
55     kind: "XV.ComboboxWidget",
56     keyAttribute: "code",
57     label: "_honorific".loc(),
58     collection: "XM.honorifics"
59   });
60
61   // ..........................................................
62   // QUOTE LINE CHARACTERISTIC
63   //
64
65   enyo.kind({
66     name: "XV.QuoteLineCharacteristicCombobox",
67     kind: "XV.ComboboxWidget",
68     keyAttribute: "value",
69     create: function () {
70       this.inherited(arguments);
71       this.createComponent({
72         name: "comboboxNote",
73         container: this.$.container,
74         classes: "xv-combobox-note"
75       });
76     },
77     /**
78       Populate the note field
79
80       @param {String} value
81      */
82     setNote: function (value) {
83       this.$.comboboxNote.setContent(value);
84     }
85   });
86
87   // ..........................................................
88   // SHIP VIA
89   //
90
91   enyo.kind({
92     name: "XV.ShipViaCombobox",
93     kind: "XV.ComboboxWidget",
94     collection: "XM.shipVias",
95     label: "_shipVia".loc(),
96     keyAttribute: "code"
97   });
98
99   // ..........................................................
100   // STATE
101   //
102
103   /**
104     @class A combobox backed by the XM.states collection.
105     @name XV.StateCombobox
106     @extends XV.Combobox
107    */
108   enyo.kind({
109     name: "XV.StateCombobox",
110     kind: "XV.ComboboxWidget",
111     collection: "XM.states",
112     keyAttribute: "abbreviation",
113     published: {
114       country: null
115     },
116     /**
117     @todo Document the create method.
118     */
119     create: function () {
120       this.inherited(arguments);
121       var that = this,
122         filter = function (models) {
123           return _.filter(models, function (model) {
124             var country = model.get('country') || {};
125             return country.id === that._countryId;
126           });
127         };
128       this.setFilter(filter);
129     },
130     /**
131     @todo Document the countryChanged method.
132     */
133     countryChanged: function () {
134       var country = this.getCountry();
135       if (!country) {
136         this._countryId = undefined;
137       } else if (typeof country === 'string') {
138         country = _.find(XM.countries.models, function (model) {
139           return model.get('name') === country;
140         });
141         this._countryId = country ? country.id : undefined;
142       } else if (typeof country === 'object') {
143         this._countryId = country.id;
144       } else if (typeof country === 'number') {
145         this._countryId = country;
146       } else {
147         this._countryId = undefined;
148       }
149       this.buildList();
150     }
151   });
152
153   // ..........................................................
154   // UNIT
155   //
156
157   enyo.kind({
158     name: "XV.UnitCombobox",
159     kind: "XV.ComboboxWidget",
160     collection: "XM.units",
161     keyAttribute: "name",
162     showLabel: false,
163     setValue: function (value, options) {
164       if (value && value.id) {
165         this.inherited(arguments, [value.id, options]);
166       } else {
167         this.inherited(arguments);
168       }
169     }
170   });
171
172 }());