Merge pull request #1 from shackbarth/keith1
[xtuple] / enyo-client / application / source / widgets / relation.js
1 /*jshint node:true, indent:2, curly:true, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
2 regexp:true, undef:true, trailing:true, white:true, strict:false */
3 /*global XT:true, XV:true, XM:true, Backbone:true, window:true, enyo:true, _:true */
4
5 (function () {
6
7   // ..........................................................
8   // ACCOUNT
9   //
10
11   enyo.kind({
12     name: "XV.AccountWidget",
13     kind: "XV.RelationWidget",
14     collection: "XM.AccountRelationCollection",
15     list: "XV.AccountList"
16   });
17
18   // ..........................................................
19   // CONTACT
20   //
21
22   enyo.kind({
23     name: "XV.ContactWidget",
24     kind: "XV.RelationWidget",
25     label: "_contact".loc(),
26     keyAttribute: "name",
27     nameAttribute: "jobTitle",
28     descripAttribute: "phone",
29     collection: "XM.ContactRelationCollection",
30     list: "XV.ContactList",
31     published: {
32       showAddress: false
33     },
34     descriptionComponents: [
35       {name: "jobTitleRow", controlClasses: "enyo-inline", showing: false, components: [
36         {classes: 'xv-description', name: "name"}
37       ]},
38       {name: "phoneRow", controlClasses: "enyo-inline", showing: false, components: [
39         {classes: "xv-description hyperlink", target: '_blank', name: "description"}
40       ]},
41       {name: "alternateRow", controlClasses: "enyo-inline", showing: false, components: [
42         {classes: "xv-description hyperlink", target: "_blank", name: "alternate"}
43       ]},
44       {name: "faxRow", controlClasses: "enyo-inline", showing: false, components: [
45         {classes: "xv-description hyperlink", target: "_blank", name: "fax"}
46       ]},
47       {name: "emailRow", controlClasses: "enyo-inline", showing: false, components: [
48         {classes: 'xv-description hyperlink', target: "_blank", name: "email"}
49       ]},
50       {name: "webAddressRow", controlClasses: "enyo-inline", showing: false, components: [
51         {classes: 'xv-description hyperlink', target: "_blank", name: "webAddress"}
52       ]},
53       {name: "addressRow", controlClasses: "enyo-inline", showing: false, components: [
54         {classes: "xv-description", name: "address", allowHtml: true}
55       ]}
56     ],
57     setValue: function (value, options) {
58       this.inherited(arguments);
59
60       if (value && !value.get) {
61         // the value of the widget is still being fetched asyncronously.
62         // when the value is fetched, this function will be run again,
63         // so for now we can just stop here.
64         return;
65       }
66
67       // The rows are here because sometimes the values needs labels
68       // to go with the values.
69       var jobTitle = value ? value.get("jobTitle") : "",
70         phone = value ? value.get("phone") : "",
71         alternate = value ? value.get("alternate") : "",
72         fax = value ? value.get("fax") : "",
73         primaryEmail = value ? value.get("primaryEmail") : "",
74         webAddress = value ? value.get("webAddress") : "",
75         address = value ? XM.Address.format(value.get("address")) : "",
76         showAddress = this.getShowAddress();
77
78       this.$.jobTitleRow.setShowing(!!jobTitle);
79       this.$.name.setContent(jobTitle);
80
81       this.$.phoneRow.setShowing(!!phone);
82       this.$.description.setContent(phone);
83       this.$.description.setAttribute('href', 'tel://' + phone);
84
85       this.$.alternateRow.setShowing(!!alternate);
86       this.$.alternate.setContent(alternate);
87       this.$.alternate.setAttribute('href', 'tel://' + alternate);
88
89       this.$.faxRow.setShowing(!!fax);
90       this.$.fax.setContent(fax);
91       this.$.fax.setAttribute('href', 'tel://' + fax);
92
93       this.$.emailRow.setShowing(!!primaryEmail);
94       this.$.email.setContent(primaryEmail);
95       this.$.email.setAttribute('href', 'mailto:' + primaryEmail);
96
97       this.$.webAddressRow.setShowing(!!webAddress);
98       this.$.webAddress.setContent(webAddress);
99       this.$.webAddress.setAttribute('href', '//' + alternate);
100
101       this.$.addressRow.setShowing(address && showAddress);
102       this.$.address.setContent(address);
103     },
104     openWindow: function () {
105       var address = this.value ? this.value.get("webAddress") : null;
106       if (address) { window.open("http://" + address); }
107       return true;
108     },
109     callPhone: function () {
110       var phoneNumber = this.value ? this.value.get("phone") : null,
111         win;
112       if (phoneNumber) {
113         win = window.open("tel://" + phoneNumber);
114         win.close();
115       }
116       return true;
117     },
118     sendMail: function () {
119       var email = this.value ? this.value.get("primaryEmail") : null,
120         win;
121       if (email) {
122         win = window.open("mailto:" + email);
123         win.close();
124       }
125       return true;
126     }
127   });
128
129   // ..........................................................
130   // CUSTOMER
131   //
132
133   enyo.kind({
134     name: "XV.CustomerWidget",
135     kind: "XV.RelationWidget",
136     collection: "XM.CustomerRelationCollection",
137     list: "XV.CustomerList"
138   });
139
140   enyo.kind({
141     name: "XV.BillingCustomerWidget",
142     kind: "XV.RelationWidget",
143     collection: "XM.BillingCustomerCollection",
144     query: { parameters: [{attribute: "isActive", value: true}]},
145     list: "XV.CustomerList"
146   });
147
148   enyo.kind({
149     name: "XV.SalesCustomerWidget",
150     kind: "XV.RelationWidget",
151     collection: "XM.SalesCustomerCollection",
152     list: "XV.CustomerList"
153   });
154
155   // ..........................................................
156   // CUSTOMER GROUP
157   //
158   enyo.kind({
159     name: "XV.CustomerGroupWidget",
160     kind: "XV.RelationWidget",
161     collection: "XM.CustomerGroupCollection",
162     keyAttribute: "name",
163     list: "XV.CustomerGroupList"
164   });
165
166   // ..........................................................
167   // CUSTOMER PROSPECT
168   //
169
170   enyo.kind({
171     name: "XV.CustomerProspectWidget",
172     kind: "XV.RelationWidget",
173     collection: "XM.CustomerProspectRelationCollection",
174     list: "XV.CustomerProspectList",
175     create: function () {
176       var ret = this.inherited(arguments);
177       this.createComponent({
178         kind: "onyx.Popup",
179         name: "customerOrProspectPopup",
180         centered: true,
181         modal: true,
182         floating: true,
183         scrim: true,
184         onShow: "popupShown",
185         onHide: "popupHidden",
186         components: [
187           {content: "_customerOrProspect".loc()},
188           {tag: "br"},
189           {kind: "onyx.Button", name: "customerButton", content: "_customer".loc(), ontap: "popupTapped",
190             classes: "onyx-blue xv-popup-button"},
191           {kind: "onyx.Button", name: "prospectButton", content: "_prospect".loc(), ontap: "popupTapped",
192             classes: "onyx-blue xv-popup-button"}
193         ]
194       });
195       this.$.newItem.setDisabled(false);
196       return ret;
197     },
198     /**
199      @menuItemSelected
200      this overrides the menuItemSelected function of RelationWidget to
201      account for the different types of models presented by the widget.
202      */
203     menuItemSelected: function (inSender, inEvent) {
204       var that = this,
205         menuItem = inEvent.originator,
206         list = this.getList(),
207         model = this.getValue(),
208         K, status, id, workspace,
209         callback;
210
211       switch (menuItem.name)
212       {
213       case "searchItem":
214         callback = function (value) {
215           that.setValue(value);
216         };
217         this.doSearch({
218           list: list,
219           searchText: this.$.input.getValue(),
220           callback: callback
221         });
222         break;
223       case "openItem":
224         K = model.getClass();
225         status = model.get("status");
226         id = model ? model.id : null;
227         workspace = status === K.PROSPECT_STATUS ? "XV.ProspectWorkspace" : "XV.CustomerWorkspace";
228
229         this.doWorkspace({
230           workspace: workspace,
231           id: id,
232           allowNew: false
233         });
234         break;
235       case "newItem":
236         this.$.customerOrProspectPopup.show();
237       }
238     },
239     popupTapped: function (inSender, inEvent) {
240       var that = this,
241         callback = function (model) {
242           if (!model) { return; }
243           var Model = that._collection.model,
244             attrs = {},
245             value,
246             options = {};
247           options.success = function () {
248             that.setValue(value);
249           };
250           attrs[Model.prototype.idAttribute] = model.id;
251           value = Model.findOrCreate(attrs);
252           value.fetch(options);
253         };
254
255       this.$.customerOrProspectPopup.hide();
256       this.doWorkspace({
257         callback: callback,
258         workspace: inEvent.originator.name === "customerButton" ?
259           "XV.CustomerWorkspace" : "XV.ProspectWorkspace",
260         allowNew: false
261       });
262     },
263   });
264
265   // ..........................................................
266   // CUSTOMER SHIPTO
267   //
268
269   enyo.kind({
270     name: "XV.CustomerShiptoWidget",
271     kind: "XV.RelationWidget",
272     collection: "XM.CustomerShiptoRelationCollection",
273     list: "XV.CustomerShiptoList"
274   });
275
276   // ..........................................................
277   // DEPARTMENT
278   //
279
280   enyo.kind({
281     name: "XV.DepartmentWidget",
282     kind: "XV.RelationWidget",
283     collection: "XM.DepartmentCollection",
284     list: "XV.DepartmentList"
285   });
286
287   // ..........................................................
288   // EMPLOYEE
289   //
290
291   enyo.kind({
292     name: "XV.EmployeeWidget",
293     kind: "XV.RelationWidget",
294     collection: "XM.EmployeeRelationCollection",
295     list: "XV.EmployeeList",
296     keyAttribute: "code"
297   });
298
299   // ..........................................................
300   // EXPENSE CATEGORY
301   //
302
303   enyo.kind({
304     name: "XV.ExpenseCategoryWidget",
305     kind: "XV.RelationWidget",
306     collection: "XM.ExpenseCategoryCollection",
307     list: "XV.ExpenseCategoryList",
308     keyAttribute: "code"
309   });
310
311   // ..........................................................
312   // INCIDENT
313   //
314
315   enyo.kind({
316     name: "XV.IncidentWidget",
317     kind: "XV.RelationWidget",
318     collection: "XM.IncidentRelationCollection",
319     list: "XV.IncidentList",
320     nameAttribute: "description"
321   });
322
323   // ..........................................................
324   // ITEM
325   //
326
327   enyo.kind({
328     name: "XV.ItemWidget",
329     kind: "XV.RelationWidget",
330     collection: "XM.ItemRelationCollection",
331     list: "XV.ItemList",
332     nameAttribute: "description1",
333     descripAttribute: "description2"
334   });
335
336   // ..........................................................
337   // LEDGER ACCOUNT
338   //
339
340   enyo.kind({
341     name: "XV.LedgerAccountWidget",
342     kind: "XV.RelationWidget",
343     collection: "XM.LedgerAccountRelationCollection",
344     list: "XV.LedgerAccountList",
345     keyAttribute: "name",
346     nameAttribute: "description"
347   });
348
349   // ..........................................................
350   // OPPORTUNITY
351   //
352
353   enyo.kind({
354     name: "XV.OpportunityWidget",
355     kind: "XV.RelationWidget",
356     collection: "XM.OpportunityRelationCollection",
357     keyAttribute: "name",
358     list: "XV.OpportunityList"
359   });
360
361   // ..........................................................
362   // PROJECT
363   //
364
365   enyo.kind({
366     name: "XV.ProjectWidget",
367     kind: "XV.RelationWidget",
368     collection: "XM.ProjectRelationCollection",
369     list: "XV.ProjectList",
370     create: function () {
371       this.inherited(arguments);
372       this.setShowing(XT.session.settings.get("UseProjects"));
373     },
374     setShowing: function (showing) {
375       if (!showing || showing && XT.session.settings.get("UseProjects")) {
376         this.inherited(arguments);
377       }
378     }
379   });
380
381   // ..........................................................
382   // PURCHASE ORDER
383   //
384
385   enyo.kind({
386     name: "XV.PurchaseOrderWidget",
387     kind: "XV.RelationWidget",
388     collection: "XM.PurchaseOrderRelationCollection",
389     keyAttribute: "number",
390     list: "XV.PurchaseOrderList"
391   });
392
393   // ..........................................................
394   // SALES ORDER
395   //
396
397   enyo.kind({
398     name: "XV.SalesOrderWidget",
399     kind: "XV.RelationWidget",
400     collection: "XM.SalesOrderRelationCollection",
401     keyAttribute: "number",
402     list: "XV.SalesOrderList"
403   });
404
405   // ..........................................................
406   // SHIFT
407   //
408
409   enyo.kind({
410     name: "XV.ShiftWidget",
411     kind: "XV.RelationWidget",
412     collection: "XM.ShiftCollection",
413     list: "XV.ShiftList"
414   });
415
416   // ..........................................................
417   // USER ACCOUNT
418   //
419
420   enyo.kind({
421     name: "XV.UserAccountWidget",
422     classes: "xv-useraccount-widget",
423     kind: "XV.RelationWidget",
424     collection: "XM.UserAccountRelationCollection",
425     list: "XV.UserAccountList",
426     keyAttribute: "username",
427     nameAttribute: "properName"
428   });
429
430   // ..........................................................
431   // VENDOR
432   //
433
434   enyo.kind({
435     name: "XV.VendorWidget",
436     kind: "XV.RelationWidget",
437     collection: "XM.VendorRelationCollection",
438     keyAttribute: "number",
439     list: "XV.VendorList"
440   });
441
442 }());