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