Merge pull request #1 from shackbarth/keith1
[xtuple] / enyo-client / application / source / widgets / characteristics.js
1 /*jshint indent:2, curly:true, eqeqeq:true, immed:true, latedef:true,
2 newcap:true, noarg:true, regexp:true, undef:true, trailing:true,
3 white:true, strict:false*/
4 /*global enyo:true, XT:true, XV:true, Globalize:true, XM:true */
5
6 (function () {
7
8   // ..........................................................
9   // ACCOUNT
10   //
11
12   enyo.kind({
13     name: "XV.AccountCharacteristicsWidget",
14     kind: "XV.CharacteristicsWidget",
15     model: "XM.AccountCharacteristic",
16     which: "isAccounts"
17   });
18
19   // ..........................................................
20   // CONTACT
21   //
22
23   enyo.kind({
24     name: "XV.ContactCharacteristicsWidget",
25     kind: "XV.CharacteristicsWidget",
26     model: "XM.ContactCharacteristic",
27     which: "isContacts"
28   });
29
30   // ..........................................................
31   // CUSTOMER
32   //
33
34   enyo.kind({
35     name: "XV.CustomerCharacteristicsWidget",
36     kind: "XV.CharacteristicsWidget",
37     model: "XM.CustomerCharacteristic",
38     which: "isCustomers"
39   });
40
41   // ..........................................................
42   // EMPLOYEE
43   //
44
45   enyo.kind({
46     name: "XV.EmployeeCharacteristicsWidget",
47     kind: "XV.CharacteristicsWidget",
48     model: "XM.EmployeeCharacteristic",
49     which: "isEmployees"
50   });
51
52   // ..........................................................
53   // INCIDENT
54   //
55
56   enyo.kind({
57     name: "XV.IncidentCharacteristicsWidget",
58     kind: "XV.CharacteristicsWidget",
59     model: "XM.IncidentCharacteristic",
60     which: "isIncidents"
61   });
62
63   // ..........................................................
64   // ITEM
65   //
66
67   enyo.kind({
68     name: "XV.ItemCharacteristicsWidget",
69     kind: "XV.CharacteristicsWidget",
70     model: "XM.ItemCharacteristic",
71     which: "isItems"
72   });
73
74   // ..........................................................
75   // OPPORTUNITY
76   //
77
78   enyo.kind({
79     name: "XV.OpportunityCharacteristicsWidget",
80     kind: "XV.CharacteristicsWidget",
81     model: "XM.OpportunityCharacteristic",
82     which: "isOpportunities"
83   });
84
85   // ..........................................................
86   // ORDER (ABSTRACT)
87   //
88
89   enyo.kind({
90     name: "XV.OrderCharacteristicItem",
91     kind: "XV.CharacteristicItem",
92     components: [
93       {kind: "XV.ComboboxWidget", name: "combobox", attr: "value"}
94     ],
95     disabledChanged: function (oldValue) {
96       this.$.combobox.setDisabled(this.disabled);
97     },
98     /**
99       Look at the characteristic of this model, and look at the characteristics of the item
100       to pass to the combobox not just the value that has been changed but a dropdown
101       collection that represents all of the item's characteristic values possible for this
102       characteristic.
103      */
104     valueChanged: function (inSender, inEvent) {
105       var model = this.getValue(),
106         characteristic = model.getValue("characteristic"),
107         characteristicId = characteristic.id,
108         characteristicName = characteristic.get("name"),
109         line = model.collection[this.parent.parent.parent.getParentKey()],
110         allItemCharacteristics = line.getValue("item.characteristics"),
111         // filter for only the models of the appropriate characteristic
112         // allItemCharacteristics may be "", in which case we want an empty array
113         // we can use this underscore method in backbone but it unfortunately
114         // returns an array of models instead of a collection
115         relevantArray = allItemCharacteristics ? allItemCharacteristics.filter(function (model) {
116           return model.getValue("characteristic.id") === characteristicId;
117         }) : [],
118         // so we have to make it a collection here
119         relevantItemCharacteristics = new XM.CharacteristicCollection(relevantArray);
120
121       // pass the backing collection to the combobox
122       this.$.combobox.setCollection(relevantItemCharacteristics);
123
124       // for this type of characteristic, the label is just a label and not a picker
125       this.$.combobox.setLabel(characteristicName);
126
127       // set the selected value of the combobox
128       this.$.combobox.setValue(model.get("value"), {silent: true});
129     }
130   });
131
132   enyo.kind({
133     name: "XV.OrderCharacteristicsWidget",
134     kind: "XV.CharacteristicsWidget",
135     model: "XM.PurchaseOrderLineCharacteristic",
136     published: {
137       parentKey: null
138     },
139     components: [
140       {kind: "onyx.GroupboxHeader", content: "_characteristics".loc()},
141       {kind: "Repeater", count: 0, onSetupItem: "setupItem", components: [
142         {kind: "XV.OrderCharacteristicItem", name: "characteristicItem"}
143       ]},
144     ],
145     /**
146       Show even though we don't have a this.getWhich()
147      */
148     create: function () {
149       this.inherited(arguments);
150       // just undo the super-class function.
151       this.show();
152     },
153     disabledChanged: function () {
154       // Over-ride: there is no "new" button here.
155     }
156   });
157
158   // ..........................................................
159   // QUOTE LINE
160   //
161
162   enyo.kind({
163     name: "XV.QuoteLineCharacteristicItem",
164     kind: "XV.CharacteristicItem",
165     components: [
166       {kind: "XV.QuoteLineCharacteristicCombobox", name: "combobox", attr: "value", showLabel: false},
167       {name: "price"}
168     ],
169     disabledChanged: function (oldValue) {
170       this.$.combobox.setDisabled(this.disabled);
171     },
172     /**
173       The price display is bound to the price attribute of the model, although we only want
174       to show it if the quote line's item isSold
175      */
176     priceChanged: function () {
177       var model = this.getValue(),
178         line = model.collection.quoteLine ? model.collection.quoteLine : model.collection.salesOrderLine,
179         itemIsSold = line.getValue("item.isSold"),
180         price = model.get("price") !== undefined ? model.get("price") : "",
181         note = itemIsSold ? Globalize.format(price, "c") : "";
182
183       this.$.combobox.setNote(note);
184     },
185     /**
186       Remove bindings
187      */
188     destroy: function () {
189       this.getValue().off("change:price", this.priceChanged, this);
190       this.inherited(arguments);
191     },
192     /**
193       Look at the characteristic of this model, and look at the characteristics of the item
194       to pass to the combobox not just the value that has been changed but a dropdown
195       collection that represents all of the item's characteristic values possible for this
196       characteristic.
197      */
198     valueChanged: function (inSender, inEvent) {
199       var model = this.getValue(),
200         characteristic = model.getValue('characteristic'),
201         characteristicId = characteristic.id,
202         characteristicName = characteristic.get('name'),
203         // this could be quoteLine or a salesOrderLine
204         line = model.collection.quoteLine ? model.collection.quoteLine : model.collection.salesOrderLine,
205         allItemCharacteristics = line.getValue("item.characteristics"),
206         // filter for only the models of the appropriate characteristic
207         // allItemCharacteristics may be "", in which case we want an empty array
208         // we can use this underscore method in backbone but it unfortunately
209         // returns an array of models instead of a collection
210         relevantArray = allItemCharacteristics ? allItemCharacteristics.filter(function (model) {
211           return model.getValue("characteristic.id") === characteristicId;
212         }) : [],
213         // so we have to make it a collection here
214         relevantItemCharacteristics = new XM.CharacteristicCollection(relevantArray),
215         itemIsSold = line.getValue("item.isSold");
216
217       // pass the backing collection to the combobox
218       this.$.combobox.setCollection(relevantItemCharacteristics);
219
220       // for this type of characteristic, the label is just a label and not a picker
221       this.$.combobox.setLabel(characteristicName);
222
223       // set the selected value of the combobox
224       this.$.combobox.setValue(model.get('value'), {silent: true});
225
226       // display the price if we already have it
227       this.priceChanged();
228
229       // bind the price label to the price attribute on the model
230       this.getValue().on("change:price", this.priceChanged, this);
231     }
232   });
233
234   enyo.kind({
235     name: "XV.QuoteLineCharacteristicsWidget",
236     kind: "XV.CharacteristicsWidget",
237     model: "XM.QuoteLineCharacteristic",
238     components: [
239       {kind: "onyx.GroupboxHeader", content: "_characteristics".loc()},
240       {kind: "Repeater", count: 0, onSetupItem: "setupItem", components: [
241         {kind: "XV.QuoteLineCharacteristicItem", name: "characteristicItem"}
242       ]},
243     ],
244     /**
245       Show even though we don't have a this.getWhich()
246      */
247     create: function () {
248       this.inherited(arguments);
249       // just undo the super-class function.
250       this.show();
251     },
252     disabledChanged: function () {
253       // Over-ride: there is no "new" button here.
254     },
255     setValue: function (value) {
256       this.inherited(arguments);
257
258       // TODO: hide it if there are no characteristics to show
259       // this doesn't quite work yet
260       //if (value
261       //    && value.quoteLine
262       //    && value.quoteLine.getValue("itemSite.item.characteristics")
263       //    && value.quoteLine.getValue("itemSite.item.characteristics").length) {
264       //  this.show();
265       //} else {
266       //  this.hide();
267       //}
268     }
269   });
270
271   // ..........................................................
272   // SALE TYPE
273   //
274
275   enyo.kind({
276     name: "XV.SaleTypeCharacteristicsWidget",
277     kind: "XV.CharacteristicsWidget",
278     model: "XM.SaleTypeCharacteristic",
279     which: "isSalesOrders" // not a bug
280   });
281
282   // ..........................................................
283   // SALES ORDER
284   //
285
286   enyo.kind({
287     name: "XV.SalesOrderCharacteristicsWidget",
288     kind: "XV.CharacteristicsWidget",
289     model: "XM.SalesOrderCharacteristic",
290     which: "isSalesOrders"
291   });
292
293 }());