Issue #23853: More widget refactoring.
[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.$.fittableColumns,
74         classes: "xv-combobox-note"
75       });
76       // TODO: git rid of this
77       this.$.input.applyStyle("padding-top", "8px");
78       this.$.input.applyStyle("padding-left", "8px");
79     },
80     /**
81       Populate the note field
82
83       @param {String} value
84      */
85     setNote: function (value) {
86       this.$.comboboxNote.setContent(value);
87     }
88   });
89
90   // ..........................................................
91   // SHIP VIA
92   //
93
94   enyo.kind({
95     name: "XV.ShipViaCombobox",
96     kind: "XV.ComboboxWidget",
97     collection: "XM.shipVias",
98     label: "_shipVia".loc(),
99     keyAttribute: "code"
100   });
101
102   // ..........................................................
103   // STATE
104   //
105
106   /**
107     @class A combobox backed by the XM.states collection.
108     @name XV.StateCombobox
109     @extends XV.Combobox
110    */
111   enyo.kind({
112     name: "XV.StateCombobox",
113     kind: "XV.ComboboxWidget",
114     collection: "XM.states",
115     keyAttribute: "abbreviation",
116     published: {
117       country: null
118     },
119     /**
120     @todo Document the create method.
121     */
122     create: function () {
123       this.inherited(arguments);
124       var that = this,
125         filter = function (models) {
126           return _.filter(models, function (model) {
127             var country = model.get('country') || {};
128             return country.id === that._countryId;
129           });
130         };
131       this.setFilter(filter);
132     },
133     /**
134     @todo Document the countryChanged method.
135     */
136     countryChanged: function () {
137       var country = this.getCountry();
138       if (!country) {
139         this._countryId = undefined;
140       } else if (typeof country === 'string') {
141         country = _.find(XM.countries.models, function (model) {
142           return model.get('name') === country;
143         });
144         this._countryId = country ? country.id : undefined;
145       } else if (typeof country === 'object') {
146         this._countryId = country.id;
147       } else if (typeof country === 'number') {
148         this._countryId = country;
149       } else {
150         this._countryId = undefined;
151       }
152       this.buildList();
153     }
154   });
155
156   // ..........................................................
157   // UNIT
158   //
159
160   enyo.kind({
161     name: "XV.UnitCombobox",
162     kind: "XV.ComboboxWidget",
163     collection: "XM.units",
164     keyAttribute: "name",
165     showLabel: false,
166     setValue: function (value, options) {
167       if (value && value.id) {
168         this.inherited(arguments, [value.id, options]);
169       } else {
170         this.inherited(arguments);
171       }
172     }
173   });
174
175 }());