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