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, {
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
26 constructor : function(args) {
30 // find the field description in the IDL if not provided
32 this.fmClass = this.fmObject.classname;
33 this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
36 this.fmIDL = fieldmapper.IDL.fmclasses[this.fmClass];
37 var fields = this.fmIDL.fields;
39 if(fields[f].name == this.fmField)
40 this.idlField = fields[f];
44 throw new Error("AutoFieldWidget could not determine which field to render. We need more information. fmClass=" +
45 this.fmClass + ' fmField=' + this.fmField + ' fmObject=' + js2JSON(this.fmObject));
47 this.auth = openils.User.authtoken;
48 if(!this.cache[this.auth]) {
49 this.cache[this.auth] = {};
54 * Turn the widget-stored value into a value oils understands
56 getFormattedValue : function() {
57 var value = this.baseWidgetValue();
58 switch(this.idlField.datatype) {
60 return (value) ? 't' : 'f'
62 return dojo.date.stamp.toISOString(value);
65 if(isNaN(value)) value = 0;
67 return (value === '') ? null : value;
71 baseWidgetValue : function(value) {
72 var attr = (this.readOnly) ? 'content' : 'value';
73 if(arguments.length) this.widget.attr(attr, value);
74 return this.widget.attr(attr);
78 * Turn the widget-stored value into something visually suitable
80 getDisplayString : function() {
81 var value = this.widgetValue;
82 switch(this.idlField.datatype) {
84 return (value) ? 'True' : 'False'; // XXX i18n!
86 dojo.require('dojo.date.locale');
87 dojo.require('dojo.date.stamp');
88 var date = dojo.date.stamp.fromISOString(value);
89 return dojo.date.locale.format(date, {formatLength:'short'});
91 if(value === null || value === undefined) return '';
92 return fieldmapper.aou.findOrgUnit(value).shortname();
95 if(isNaN(value)) value = 0;
97 if(value === undefined || value === null)
103 build : function(onload) {
106 // core widget provided for us, attach and move on
107 if(this.parentNode) // may already be in the "right" place
108 this.parentNode.appendChild(this.widget.domNode);
112 if(!this.parentNode) // give it somewhere to live so that dojo won't complain
113 this.parentNode = document.createElement('div');
115 this.onload = onload;
116 if(this.widgetValue == null)
117 this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null;
120 dojo.require('dijit.layout.ContentPane');
121 this.widget = new dijit.layout.ContentPane(this.dijitArgs, this.parentNode);
122 if(this.widgetValue !== null)
123 this._tryLinkedDisplayField();
125 } else if(this.widgetClass) {
126 dojo.require(this.widgetClass);
127 eval('this.widget = new ' + this.widgetClass + '(this.dijitArgs, this.parentNode);');
131 switch(this.idlField.datatype) {
134 dojo.require('dijit.form.TextBox');
135 this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
139 this._buildOrgSelector();
143 dojo.require('dijit.form.CurrencyTextBox');
144 this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode);
148 dojo.require('dijit.form.NumberTextBox');
149 this.dijitArgs = dojo.mixin(this.dijitArgs || {}, {constraints:{places:0}});
150 this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
154 dojo.require('dijit.form.NumberTextBox');
155 this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
159 dojo.require('dijit.form.DateTextBox');
160 dojo.require('dojo.date.stamp');
161 this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode);
162 if(this.widgetValue != null)
163 this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue);
167 dojo.require('dijit.form.CheckBox');
168 this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode);
169 this.widgetValue = openils.Util.isTrue(this.widgetValue);
173 if(this._buildLinkSelector()) break;
176 dojo.require('dijit.form.TextBox');
177 this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
181 if(!this.async) this._widgetLoaded();
185 // we want to display the value for our widget. However, instead of displaying
186 // an ID, for exmaple, display the value for the 'selector' field on the object
188 _tryLinkedDisplayField : function(noAsync) {
190 if(this.idlField.datatype == 'org_unit')
191 return false; // we already handle org_units, no need to re-fetch
193 var linkInfo = this._getLinkSelector();
194 if(!(linkInfo && linkInfo.vfield && linkInfo.vfield.selector))
196 var lclass = linkInfo.linkClass;
198 if(lclass == 'aou') {
199 this.widgetValue = fieldmapper.aou.findOrgUnit(this.widgetValue).shortname();
203 // first try the store cache
205 if(this.cache[this.auth][lclass]) {
206 var store = this.cache[this.auth][lclass];
208 query[linkInfo.vfield.name] = ''+this.widgetValue;
209 store.fetch({query:query, onComplete:
211 self.widgetValue = store.getValue(list[0], linkInfo.vfield.selector);
217 // then try the single object cache
218 if(this.cacheSingle[lclass] && this.cacheSingle[lclass][this.widgetValue]) {
219 this.widgetValue = this.cacheSingle[lclass][this.widgetValue];
223 // if those fail, fetch the linked object
224 dojo.require('openils.PermaCrud');
227 new openils.PermaCrud().retrieve(lclass, this.widgetValue, {
228 async : !this.forceSync,
229 oncomplete : function(r) {
230 var item = openils.Util.readResponse(r);
231 if(!self.cacheSingle[lclass])
232 self.cacheSingle[lclass] = {};
233 self.widgetValue = item[linkInfo.vfield.selector]();
234 self.cacheSingle[lclass][self.widgetValue] = item;
235 self.widget.startup();
236 self._widgetLoaded();
241 _getLinkSelector : function() {
242 var linkClass = this.idlField['class'];
243 if(this.idlField.reltype != 'has_a') return false;
244 if(!fieldmapper.IDL.fmclasses[linkClass].permacrud) return false;
245 if(!fieldmapper.IDL.fmclasses[linkClass].permacrud.retrieve) return false;
248 var rclassIdl = fieldmapper.IDL.fmclasses[linkClass];
250 for(var f in rclassIdl.fields) {
251 if(this.idlField.key == rclassIdl.fields[f].name) {
252 vfield = rclassIdl.fields[f];
258 throw new Error("'" + linkClass + "' has no '" + this.idlField.key + "' field!");
261 linkClass : linkClass,
266 _buildLinkSelector : function() {
267 var selectorInfo = this._getLinkSelector();
268 if(!selectorInfo) return false;
270 var linkClass = selectorInfo.linkClass;
271 var vfield = selectorInfo.vfield;
275 if(linkClass == 'pgt')
276 return this._buildPermGrpSelector();
277 if(linkClass == 'aou')
278 return this._buildOrgSelector();
279 if(linkClass == 'acpl')
280 return this._buildCopyLocSelector();
283 dojo.require('openils.PermaCrud');
284 dojo.require('dojo.data.ItemFileReadStore');
285 dojo.require('dijit.form.FilteringSelect');
287 this.widget = new dijit.form.FilteringSelect(this.dijitArgs, this.parentNode);
288 this.widget.searchAttr = this.widget.labelAttr = vfield.selector || vfield.name;
289 this.widget.valueAttr = vfield.name;
292 var oncomplete = function(list) {
295 new dojo.data.ItemFileReadStore({data:fieldmapper[linkClass].toStoreData(list)});
296 self.cache[self.auth][linkClass] = self.widget.store;
298 self.widget.store = self.cache[self.auth][linkClass];
300 self.widget.startup();
301 self._widgetLoaded();
304 if(this.cache[self.auth][linkClass]) {
308 new openils.PermaCrud().retrieveAll(linkClass, {
309 async : !this.forceSync,
310 oncomplete : function(r) {
311 var list = openils.Util.readResponse(r, false, true);
321 * For widgets that run asynchronously, provide a callback for finishing up
323 _widgetLoaded : function(value) {
325 this.baseWidgetValue(this.getDisplayString());
327 this.baseWidgetValue(this.widgetValue);
328 if(this.idlField.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
329 this.widget.attr('disabled', true);
330 if(this.disableWidgetTest && this.disableWidgetTest(this.idlField.name, this.fmObject))
331 this.widget.attr('disabled', true);
334 this.onload(this.widget, this);
337 _buildOrgSelector : function() {
338 dojo.require('fieldmapper.OrgUtils');
339 dojo.require('openils.widget.FilteringTreeSelect');
340 this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
341 this.widget.searchAttr = 'shortname';
342 this.widget.labelAttr = 'shortname';
343 this.widget.parentField = 'parent_ou';
344 var user = new openils.User();
346 if(this.widgetValue == null)
347 this.widgetValue = user.user.ws_ou();
349 // if we have a limit perm, find the relevent orgs (async)
350 if(this.orgLimitPerms && this.orgLimitPerms.length > 0) {
353 user.getPermOrgList(this.orgLimitPerms,
355 self.widget.tree = orgList;
356 self.widget.startup();
357 self._widgetLoaded();
362 this.widget.tree = fieldmapper.aou.globalOrgTree;
363 this.widget.startup();
367 _buildPermGrpSelector : function() {
368 dojo.require('openils.widget.FilteringTreeSelect');
369 this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
370 this.widget.searchAttr = 'name';
372 if(this.cache.permGrpTree) {
373 this.widget.tree = this.cache.permGrpTree;
374 this.widget.startup();
380 new openils.PermaCrud().retrieveAll('pgt', {
381 async : !this.forceSync,
382 oncomplete : function(r) {
383 var list = openils.Util.readResponse(r, false, true);
388 map[list[l].id()] = list[l];
391 var pnode = map[node.parent()];
392 if(!pnode) {root = node; continue;}
393 if(!pnode.children()) pnode.children([]);
394 pnode.children().push(node);
396 self.widget.tree = self.cache.permGrpTree = root;
397 self.widget.startup();
398 self._widgetLoaded();
405 _buildCopyLocSelector : function() {
406 dojo.require('dijit.form.FilteringSelect');
407 this.widget = new dijit.form.FilteringSelect(this.dijitArgs, this.parentNode);
408 this.widget.searchAttr = this.widget.labalAttr = 'name';
409 this.widget.valueAttr = 'id';
411 if(this.cache.copyLocStore) {
412 this.widget.store = this.cache.copyLocStore;
413 this.widget.startup();
419 var ws_ou = openils.User.user.ws_ou();
420 var orgs = fieldmapper.aou.findOrgUnit(ws_ou).orgNodeTrail().map(function (i) { return i.id() });
421 orgs = orgs.concat(fieldmapper.aou.descendantNodeList(ws_ou).map(function (i) { return i.id() }));
424 new openils.PermaCrud().search('acpl', {owning_lib : orgs}, {
425 async : !this.forceSync,
426 oncomplete : function(r) {
427 var list = openils.Util.readResponse(r, false, true);
430 new dojo.data.ItemFileReadStore({data:fieldmapper.acpl.toStoreData(list)});
431 self.cache.copyLocStore = self.widget.store;
432 self.widget.startup();
433 self._widgetLoaded();