1 if(!dojo._hasResource['openils.widget.AutoFieldWidget']) {
2 dojo.provide('openils.widget.AutoFieldWidget');
3 dojo.require('openils.Util');
4 dojo.require('openils.User');
5 dojo.require('fieldmapper.IDL');
7 dojo.declare('openils.widget.AutoFieldWidget', null, {
14 * idlField -- Field description object from fieldmapper.IDL.fmclasses
15 * fmObject -- If available, the object being edited. This will be used
16 * to set the value of the widget.
17 * fmClass -- Class name (not required if idlField or fmObject is set)
18 * fmField -- Field name (not required if idlField)
19 * parentNode -- If defined, the widget will be appended to this DOM node
20 * dijitArgs -- Optional parameters object, passed directly to the dojo widget
21 * orgLimitPerms -- If this field defines a set of org units and an orgLimitPerms
22 * is defined, the code will limit the org units in the set to those
23 * allowed by the permission
25 constructor : function(args) {
29 // find the field description in the IDL if not provided
31 this.fmClass = this.fmObject.classname;
32 this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
35 this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
36 var fields = this.fmIDL.fields;
38 if(fields[f].name == this.fmField)
39 this.idlField = fields[f];
44 * Turn the widget-stored value into a value oils understands
46 getFormattedValue : function() {
47 var value = this.baseWidgetValue();
48 switch(this.idlField.datatype) {
50 return (value) ? 't' : 'f'
52 return dojo.date.stamp.toISOString(value);
54 return (value == '') ? null : value;
58 baseWidgetValue : function(value) {
59 var attr = (this.readOnly) ? 'content' : 'value';
60 if(arguments.length) this.widget.attr(attr, value);
61 return this.widget.attr(attr);
65 * Turn the widget-stored value into something visually suitable
67 getDisplayString : function() {
68 var value = this.widgetValue;
69 switch(this.idlField.datatype) {
71 return (value) ? 'True' : 'False'; // XXX i18n!
73 dojo.require('dojo.date.locale');
74 dojo.require('dojo.date.stamp');
75 var date = dojo.date.stamp.fromISOString(value);
76 return dojo.date.locale.format(date, {formatLength:'short'});
78 return fieldmapper.aou.findOrgUnit(value).shortname();
84 build : function(onload) {
87 // core widget provided for us, attach and move on
88 if(this.parentNode) // may already be in the "right" place
89 this.parentNode.appendChild(this.widget.domNode);
94 if(this.widgetValue == null)
95 this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null;
98 dojo.require('dijit.layout.ContentPane');
99 this.widget = new dijit.layout.ContentPane(this.dijitArgs, this.parentNode);
101 } else if(this.widgetClass) {
102 dojo.require(this.widgetClass);
103 eval('this.widget = new ' + this.widgetClass + '(this.dijitArgs, this.parentNode);');
107 switch(this.idlField.datatype) {
110 dojo.require('dijit.form.TextBox');
111 this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
112 this.widget.attr('disabled', true); // never allow editing of IDs
116 this._buildOrgSelector();
120 dojo.require('dijit.form.CurrencyTextBox');
121 this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode);
125 dojo.require('dijit.form.NumberTextBox');
126 this.dijitArgs = dojo.mixin(this.dijitArgs || {}, {constraints:{places:0}});
127 this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
131 dojo.require('dijit.form.NumberTextBox');
132 this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
136 dojo.require('dijit.form.DateTextBox');
137 dojo.require('dojo.date.stamp');
138 this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode);
139 if(this.widgetValue != null)
140 this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue);
144 dojo.require('dijit.form.CheckBox');
145 this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode);
146 this.widgetValue = openils.Util.isTrue(this.widgetValue);
150 if(this._buildLinkSelector()) break;
153 dojo.require('dijit.form.TextBox');
154 this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
158 if(!this.async) this._widgetLoaded();
162 _buildLinkSelector : function() {
164 /* verify we can and should grab the related class */
165 var linkClass = this.idlField['class'];
166 if(this.idlField.reltype != 'has_a') return false;
167 if(!fieldmapper.IDL.fmclasses[linkClass].permacrud) return false;
168 if(!fieldmapper.IDL.fmclasses[linkClass].permacrud.retrieve) return false;
170 dojo.require('openils.PermaCrud');
171 dojo.require('dojo.data.ItemFileReadStore');
172 dojo.require('dijit.form.FilteringSelect');
176 var rclassIdl = fieldmapper.IDL.fmclasses[linkClass];
178 if(linkClass == 'pgt')
179 return self._buildPermGrpSelector();
182 this.widget = new dijit.form.FilteringSelect(this.dijitArgs, this.parentNode);
184 for(var f in rclassIdl.fields) {
185 if(self.idlField.key == rclassIdl.fields[f].name) {
186 vfield = rclassIdl.fields[f];
192 throw new Error("'" + linkClass + "' has no '" + self.idlField.key + "' field!");
194 this.widget.searchAttr = this.widget.labelAttr = vfield.selector || vfield.name;
195 this.widget.valueAttr = vfield.name;
197 new openils.PermaCrud().retrieveAll(linkClass, {
199 oncomplete : function(r) {
200 var list = openils.Util.readResponse(r, false, true);
203 new dojo.data.ItemFileReadStore({data:fieldmapper[linkClass].toStoreData(list)});
205 self.widget.startup();
206 self._widgetLoaded();
214 * For widgets that run asynchronously, provide a callback for finishing up
216 _widgetLoaded : function(value) {
218 this.baseWidgetValue(this.getDisplayString());
220 this.baseWidgetValue(this.widgetValue);
221 if(this.idlField.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
222 this.widget.attr('disabled', true);
225 this.onload(this.widget, self);
228 _buildOrgSelector : function() {
229 dojo.require('fieldmapper.OrgUtils');
230 dojo.require('openils.widget.FilteringTreeSelect');
231 this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
232 this.widget.searchAttr = 'shortname';
233 this.widget.labelAttr = 'shortname';
234 this.widget.parentField = 'parent_ou';
236 // if we have a limit perm, find the relevent orgs (async)
237 if(this.orgLimitPerms && this.orgLimitPerms.length > 0) {
239 var user = new openils.User();
241 user.getPermOrgList(this.orgLimitPerms,
243 self.widget.tree = orgList;
244 self.widget.startup();
245 self._widgetLoaded();
250 this.widget.tree = fieldmapper.aou.globalOrgTree;
251 this.widget.startup();
255 _buildPermGrpSelector : function() {
256 dojo.require('openils.widget.FilteringTreeSelect');
257 this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
258 this.widget.searchAttr = 'name';
260 if(this.cache.permGrpTree) {
261 this.widget.tree = this.cache.permGrpTree;
262 this.widget.startup();
268 new openils.PermaCrud().retrieveAll('pgt', {
270 oncomplete : function(r) {
271 var list = openils.Util.readResponse(r, false, true);
276 map[list[l].id()] = list[l];
279 var pnode = map[node.parent()];
280 if(!pnode) {root = node; continue;}
281 if(!pnode.children()) pnode.children([]);
282 pnode.children().push(node);
284 self.widget.tree = self.cache.permGrpTree = root;
285 self.widget.startup();
286 self._widgetLoaded();