71db45f57d243779d2dcab56c32cc571f6243b20
[xtuple] / enyo-client / application / source / widgets / checkbox.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 */
3 /*global XT:true, XM:true, enyo:true, _:true, XV:true */
4
5 (function () {
6
7   enyo.kind({
8     name: "XV.AccountRoleCheckboxWidget",
9     kind: "XV.CheckboxWidget",
10     published: {
11       linkEnabled: false
12     },
13     events: {
14       onSavePrompt: "",
15       onWorkspace: ""
16     },
17     handlers: {
18       ontap: "tapped"
19     },
20     clear: function () {
21       this.inherited(arguments);
22       this.$.input.setChecked(false);
23     },
24     inputChanged: function (inSender, inEvent) {
25       var isNotRelation = !this.isRelation(),
26         input = this.$.input.getValue(),
27         value = isNotRelation && input ? 1 : null;
28       this.setValue(value);
29     },
30     isRelation: function () {
31       if (this._isRelation !== undefined) { return this._isRelation; }
32       var model = this.getOwner().getValue(),
33         attr;
34
35       // Relation is true if it is not a number based attribute
36       if (model) {
37         attr = this.getAttr();
38         this._isRelation = _.isObject(model.getRelation(attr));
39       }
40       return this._isRelation;
41     },
42     setDisabled: function (isDisabled) {
43       this.$.input.setDisabled(isDisabled || this.getLinkEnabled());
44     },
45     setValue: function (value, options) {
46       this.inherited(arguments);
47       options = options || {};
48       var isRelation = this.isRelation(),
49         that = this,
50         color = "black",
51         enabled = false,
52         input = this.$.input.getValue(),
53         openWorkspace,
54         success,
55         callback,
56         model,
57         recordType,
58         relation,
59         documentKey,
60         nameAttribute,
61         attrs = {},
62         Klass,
63         map;
64
65       // Turn on label link if applicable
66       if (this.getValue() && isRelation) {
67         color = "blue";
68         enabled = true;
69       }
70       this.$.label.setStyle("color: " + color);
71       this.setLinkEnabled(enabled);
72       this.setDisabled(enabled);
73
74       // Automatically open a workspace to set up a record for this role if necessary
75       if (input && isRelation && !value && !options.silent) {
76         model = this.getOwner().getValue();
77         relation = model.getRelation(this.getAttr());
78         recordType = relation.relatedModel.prototype.recordType;
79         Klass = XT.getObjectByName(recordType);
80
81         // If it has an editable model it must be an XM.Info model
82         if (Klass.prototype.editableModel) {
83           Klass = XT.getObjectByName(Klass.prototype.editableModel);
84           documentKey = Klass.prototype.documentKey;
85           map = Klass.prototype.conversionMap;
86         } else {
87           documentKey = relation.relatedModel.prototype.documentKey;
88           map = relation.relatedModel.prototype.conversionMap;
89         }
90         // Most account docs will make this upper again, but needs to be lower for user account
91         attrs[documentKey] = model.get("number");
92         // Map other attribute candidates
93         _.each(map, function (value, key) {
94           attrs[value] = model.get(key);
95         });
96
97         // Init function for new workspace. Makes sure the workspace understands
98         // The account is already "converted".
99         success = function () {
100           this.getValue().checkConflicts = false;
101         };
102
103         // Callback to handle result of new role
104         callback = function (model) {
105           if (model) {
106             that.getOwner().requery();
107           } else {
108             that.$.input.setChecked(false);
109           }
110         };
111
112         // Only open the workspace if `valid` is true
113         openWorkspace = function (valid) {
114           if (valid) {
115             that.doWorkspace({
116               workspace: XV.getWorkspace(recordType),
117               attributes: attrs,
118               success: success,
119               callback: callback,
120               allowNew: false
121             });
122           } else {
123             that.$.input.setChecked(false);
124           }
125         };
126
127         // Must have committed all changes before proceeding
128         if (model.isDirty()) {
129           this.doSavePrompt({
130             callback: openWorkspace
131           });
132         } else {
133           openWorkspace(true);
134         }
135       }
136     },
137     tapped: function (inSender, inEvent) {
138       var value;
139       if (inEvent.originator.name === "label" &&
140           this.getLinkEnabled()) {
141         value = this.getValue();
142         this.doWorkspace({
143           workspace: XV.getWorkspace(value.recordType),
144           id: value.id
145         });
146       }
147     }
148   });
149
150 }());