]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoFieldWidget.js
added support for displaying the value from the 'selector' field for read-only widgets
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / AutoFieldWidget.js
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');
6
7     dojo.declare('openils.widget.AutoFieldWidget', null, {
8
9         async : false,
10         cache : {},
11         cacheSingle : {},
12
13         /**
14          * args:
15          *  idlField -- Field description object from fieldmapper.IDL.fmclasses
16          *  fmObject -- If available, the object being edited.  This will be used 
17          *      to set the value of the widget.
18          *  fmClass -- Class name (not required if idlField or fmObject is set)
19          *  fmField -- Field name (not required if idlField)
20          *  parentNode -- If defined, the widget will be appended to this DOM node
21          *  dijitArgs -- Optional parameters object, passed directly to the dojo widget
22          *  orgLimitPerms -- If this field defines a set of org units and an orgLimitPerms 
23          *      is defined, the code will limit the org units in the set to those
24          *      allowed by the permission
25          */
26         constructor : function(args) {
27             for(var k in args)
28                 this[k] = args[k];
29
30             // find the field description in the IDL if not provided
31             if(this.fmObject) 
32                 this.fmClass = this.fmObject.classname;
33             this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
34
35             if(!this.idlField) {
36                 this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
37                 var fields = this.fmIDL.fields;
38                 for(var f in fields) 
39                     if(fields[f].name == this.fmField)
40                         this.idlField = fields[f];
41             }
42
43             if(!this.idlField) 
44                 throw new Error("AutoFieldWidget could not determine which field to render.  We need more information.");
45         },
46
47         /**
48          * Turn the widget-stored value into a value oils understands
49          */
50         getFormattedValue : function() {
51             var value = this.baseWidgetValue();
52             switch(this.idlField.datatype) {
53                 case 'bool':
54                     return (value) ? 't' : 'f'
55                 case 'timestamp':
56                     return dojo.date.stamp.toISOString(value);
57                 case 'int':
58                 case 'float':
59                     if(isNaN(value)) value = 0;
60                 default:
61                     return (value === '') ? null : value;
62             }
63         },
64
65         baseWidgetValue : function(value) {
66             var attr = (this.readOnly) ? 'content' : 'value';
67             if(arguments.length) this.widget.attr(attr, value);
68             return this.widget.attr(attr);
69         },
70         
71         /**
72          * Turn the widget-stored value into something visually suitable
73          */
74         getDisplayString : function() {
75             var value = this.widgetValue;
76             switch(this.idlField.datatype) {
77                 case 'bool':
78                     return (value) ? 'True' : 'False'; // XXX i18n!
79                 case 'timestamp':
80                     dojo.require('dojo.date.locale');
81                     dojo.require('dojo.date.stamp');
82                     var date = dojo.date.stamp.fromISOString(value);
83                     return dojo.date.locale.format(date, {formatLength:'short'});
84                 case 'org_unit':
85                     if(value === null || value === undefined) return '';
86                     return fieldmapper.aou.findOrgUnit(value).shortname();
87                 case 'int':
88                 case 'float':
89                     if(isNaN(value)) value = 0;
90                 default:
91                     if(value === undefined || value === null)
92                         value = '';
93                     return value+'';
94             }
95         },
96
97         build : function(onload) {
98
99             if(this.widget) {
100                 // core widget provided for us, attach and move on
101                 if(this.parentNode) // may already be in the "right" place
102                     this.parentNode.appendChild(this.widget.domNode);
103                 return;
104             }
105
106             this.onload = onload;
107             if(this.widgetValue == null)
108                 this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null;
109
110             if(this.readOnly) {
111                 dojo.require('dijit.layout.ContentPane');
112                 this.widget = new dijit.layout.ContentPane(this.dijitArgs, this.parentNode);
113                 this._tryLinkedDisplayField();
114
115             } else if(this.widgetClass) {
116                 dojo.require(this.widgetClass);
117                 eval('this.widget = new ' + this.widgetClass + '(this.dijitArgs, this.parentNode);');
118
119             } else {
120
121                 switch(this.idlField.datatype) {
122                     
123                     case 'id':
124                         dojo.require('dijit.form.TextBox');
125                         this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
126                         this.widget.attr('disabled', true); // never allow editing of IDs
127                         break;
128
129                     case 'org_unit':
130                         this._buildOrgSelector();
131                         break;
132
133                     case 'money':
134                         dojo.require('dijit.form.CurrencyTextBox');
135                         this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode);
136                         break;
137
138                     case 'int':
139                         dojo.require('dijit.form.NumberTextBox');
140                         this.dijitArgs = dojo.mixin(this.dijitArgs || {}, {constraints:{places:0}});
141                         this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
142                         break;
143
144                     case 'float':
145                         dojo.require('dijit.form.NumberTextBox');
146                         this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
147                         break;
148
149                     case 'timestamp':
150                         dojo.require('dijit.form.DateTextBox');
151                         dojo.require('dojo.date.stamp');
152                         this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode);
153                         if(this.widgetValue != null) 
154                             this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue);
155                         break;
156
157                     case 'bool':
158                         dojo.require('dijit.form.CheckBox');
159                         this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode);
160                         this.widgetValue = openils.Util.isTrue(this.widgetValue);
161                         break;
162
163                     case 'link':
164                         if(this._buildLinkSelector()) break;
165
166                     default:
167                         dojo.require('dijit.form.TextBox');
168                         this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
169                 }
170             }
171
172             if(!this.async) this._widgetLoaded();
173             return this.widget;
174         },
175
176         // we want to display the value for our widget.  However, instead of displaying
177         // an ID, for exmaple, display the value for the 'selector' field on the object
178         // the ID points to
179         _tryLinkedDisplayField : function() {
180
181             if(this.idlField.datatype == 'org_unit')
182                 return false; // we already handle org_units, no need to re-fetch
183
184             var linkInfo = this._getLinkSelector();
185             if(!linkInfo || !linkInfo.vfield) return false;
186             var lclass = linkInfo.linkClass;
187
188             // first try the object list cache
189             if(this.cache[lclass]) {
190                 for(var k in this.cache[lclass]) {
191                     var cc = this.cache[lclass][k];
192                     if(cc[linkInfo.vfield.name]() == this.widgetValue) {
193                         console.log("serving " + this.idlField.name + " from list cache");
194                         this.widgetValue = cc[linkInfo.vfield.selector]();
195                         return;
196                     }
197                 }
198             }
199
200             // then try the single object cache
201             if(this.cacheSingle[lclass] && this.cacheSingle[lclass][this.widgetValue]) {
202                 console.log("serving " + this.idlField.name + " from cacheSingle");
203                 this.widgetValue = this.cacheSingle[lclass][this.widgetValue];
204                 return;
205             }
206
207             // if those fail, fetch the linked object
208             dojo.require('openils.PermaCrud');
209             this.async = true;
210             var self = this;
211             new openils.PermaCrud().retrieve(lclass, this.widgetValue, {   
212                 async : true,
213                 oncomplete : function(r) {
214                     var item = openils.Util.readResponse(r);
215                     if(!self.cacheSingle[lclass])
216                         self.cacheSingle[lclass] = {};
217                     self.widgetValue = item[linkInfo.vfield.selector]();
218                     self.cacheSingle[lclass][self.widgetValue] = item;
219                     self.widget.startup();
220                     self._widgetLoaded();
221                 }
222             });
223         },
224
225         _getLinkSelector : function() {
226             var linkClass = this.idlField['class'];
227             if(this.idlField.reltype != 'has_a')  return false;
228             if(!fieldmapper.IDL.fmclasses[linkClass].permacrud) return false;
229             if(!fieldmapper.IDL.fmclasses[linkClass].permacrud.retrieve) return false;
230
231             var vfield;
232             var rclassIdl = fieldmapper.IDL.fmclasses[linkClass];
233
234             for(var f in rclassIdl.fields) {
235                 if(this.idlField.key == rclassIdl.fields[f].name) {
236                     vfield = rclassIdl.fields[f];
237                     break;
238                 }
239             }
240
241             if(!vfield) 
242                 throw new Error("'" + linkClass + "' has no '" + this.idlField.key + "' field!");
243
244             return {
245                 linkClass : linkClass,
246                 vfield : vfield
247             };
248         },
249
250         _buildLinkSelector : function() {
251             var selectorInfo = this._getLinkSelector();
252             if(!selectorInfo) return false;
253             var linkClass = selectorInfo.linkClass;
254             var vfield = selectorInfo.vfield;
255
256             this.async = true;
257
258             if(linkClass == 'pgt')
259                 return this._buildPermGrpSelector();
260
261             this.widget = new dijit.form.FilteringSelect(this.dijitArgs, this.parentNode);
262             this.widget.searchAttr = this.widget.labelAttr = vfield.selector || vfield.name;
263             this.widget.valueAttr = vfield.name;
264
265             dojo.require('openils.PermaCrud');
266             dojo.require('dojo.data.ItemFileReadStore');
267             dojo.require('dijit.form.FilteringSelect');
268
269             var self = this;
270             var oncomplete = function(list) {
271                 if(list) {
272                     self.widget.store = 
273                         new dojo.data.ItemFileReadStore({data:fieldmapper[linkClass].toStoreData(list)});
274                     self.cache[linkClass] = list;
275                 }
276                 self.widget.startup();
277                 self._widgetLoaded();
278             };
279
280             if(this.cache[linkClass]) {
281                 oncomplete(this.cache[linkClass]);
282
283             } else {
284                 new openils.PermaCrud().retrieveAll(linkClass, {   
285                     async : true,
286                     oncomplete : function(r) {
287                         var list = openils.Util.readResponse(r, false, true);
288                         oncomplete(list);
289                     }
290                 });
291             }
292
293             return true;
294         },
295
296         /**
297          * For widgets that run asynchronously, provide a callback for finishing up
298          */
299         _widgetLoaded : function(value) {
300             if(this.readOnly) {
301                 this.baseWidgetValue(this.getDisplayString());
302             } else {
303                 this.baseWidgetValue(this.widgetValue);
304                 if(this.idlField.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
305                     this.widget.attr('disabled', true); 
306             }
307             if(this.onload)
308                 this.onload(this.widget, self);
309         },
310
311         _buildOrgSelector : function() {
312             dojo.require('fieldmapper.OrgUtils');
313             dojo.require('openils.widget.FilteringTreeSelect');
314             this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
315             this.widget.searchAttr = 'shortname';
316             this.widget.labelAttr = 'shortname';
317             this.widget.parentField = 'parent_ou';
318             var user = new openils.User();
319             if(this.widgetValue == null) 
320                 this.widgetValue = user.user.ws_ou();
321             
322             // if we have a limit perm, find the relevent orgs (async)
323             if(this.orgLimitPerms && this.orgLimitPerms.length > 0) {
324                 this.async = true;
325                 var self = this;
326                 user.getPermOrgList(this.orgLimitPerms, 
327                     function(orgList) {
328                         self.widget.tree = orgList;
329                         self.widget.startup();
330                         self._widgetLoaded();
331                     }
332                 );
333
334             } else {
335                 this.widget.tree = fieldmapper.aou.globalOrgTree;
336                 this.widget.startup();
337             }
338         },
339
340         _buildPermGrpSelector : function() {
341             dojo.require('openils.widget.FilteringTreeSelect');
342             this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
343             this.widget.searchAttr = 'name';
344
345             if(this.cache.permGrpTree) {
346                 this.widget.tree = this.cache.permGrpTree;
347                 this.widget.startup();
348                 return;
349             } 
350
351             var self = this;
352             this.async = true;
353             new openils.PermaCrud().retrieveAll('pgt', {
354                 async : true,
355                 oncomplete : function(r) {
356                     var list = openils.Util.readResponse(r, false, true);
357                     if(!list) return;
358                     var map = {};
359                     var root = null;
360                     for(var l in list)
361                         map[list[l].id()] = list[l];
362                     for(var l in list) {
363                         var node = list[l];
364                         var pnode = map[node.parent()];
365                         if(!pnode) {root = node; continue;}
366                         if(!pnode.children()) pnode.children([]);
367                         pnode.children().push(node);
368                     }
369                     self.widget.tree = self.cache.permGrpTree = root;
370                     self.widget.startup();
371                     self._widgetLoaded();
372                 }
373             });
374
375             return true;
376         }
377     });
378 }
379