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.");
46 this.auth = openils.User.authtoken;
47 if(!this.cache[this.auth]) {
48 this.cache[this.auth] = {};
53 * Turn the widget-stored value into a value oils understands
55 getFormattedValue : function() {
56 var value = this.baseWidgetValue();
57 switch(this.idlField.datatype) {
59 return (value) ? 't' : 'f'
61 return dojo.date.stamp.toISOString(value);
64 if(isNaN(value)) value = 0;
66 return (value === '') ? null : value;
70 baseWidgetValue : function(value) {
71 var attr = (this.readOnly) ? 'content' : 'value';
72 if(arguments.length) this.widget.attr(attr, value);
73 return this.widget.attr(attr);
77 * Turn the widget-stored value into something visually suitable
79 getDisplayString : function() {
80 var value = this.widgetValue;
81 switch(this.idlField.datatype) {
83 return (value) ? 'True' : 'False'; // XXX i18n!
85 dojo.require('dojo.date.locale');
86 dojo.require('dojo.date.stamp');
87 var date = dojo.date.stamp.fromISOString(value);
88 return dojo.date.locale.format(date, {formatLength:'short'});
90 if(value === null || value === undefined) return '';
91 return fieldmapper.aou.findOrgUnit(value).shortname();
94 if(isNaN(value)) value = 0;
96 if(value === undefined || value === null)
102 build : function(onload) {
105 // core widget provided for us, attach and move on
106 if(this.parentNode) // may already be in the "right" place
107 this.parentNode.appendChild(this.widget.domNode);
111 if(!this.parentNode) // give it somewhere to live so that dojo won't complain
112 this.parentNode = document.createElement('div');
114 this.onload = onload;
115 if(this.widgetValue == null)
116 this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null;
119 dojo.require('dijit.layout.ContentPane');
120 this.widget = new dijit.layout.ContentPane(this.dijitArgs, this.parentNode);
121 if(this.widgetValue !== null)
122 this._tryLinkedDisplayField();
124 } else if(this.widgetClass) {
125 dojo.require(this.widgetClass);
126 eval('this.widget = new ' + this.widgetClass + '(this.dijitArgs, this.parentNode);');
130 switch(this.idlField.datatype) {
133 dojo.require('dijit.form.TextBox');
134 this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
138 this._buildOrgSelector();
142 dojo.require('dijit.form.CurrencyTextBox');
143 this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode);
147 dojo.require('dijit.form.NumberTextBox');
148 this.dijitArgs = dojo.mixin(this.dijitArgs || {}, {constraints:{places:0}});
149 this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
153 dojo.require('dijit.form.NumberTextBox');
154 this.widget = new dijit.form.NumberTextBox(this.dijitArgs, this.parentNode);
158 dojo.require('dijit.form.DateTextBox');
159 dojo.require('dojo.date.stamp');
160 this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode);
161 if(this.widgetValue != null)
162 this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue);
166 dojo.require('dijit.form.CheckBox');
167 this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode);
168 this.widgetValue = openils.Util.isTrue(this.widgetValue);
172 if(this._buildLinkSelector()) break;
175 dojo.require('dijit.form.TextBox');
176 this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
180 if(!this.async) this._widgetLoaded();
184 // we want to display the value for our widget. However, instead of displaying
185 // an ID, for exmaple, display the value for the 'selector' field on the object
187 _tryLinkedDisplayField : function(noAsync) {
189 if(this.idlField.datatype == 'org_unit')
190 return false; // we already handle org_units, no need to re-fetch
192 var linkInfo = this._getLinkSelector();
193 if(!(linkInfo && linkInfo.vfield && linkInfo.vfield.selector))
195 var lclass = linkInfo.linkClass;
197 if(lclass == 'aou') {
198 this.widgetValue = fieldmapper.aou.findOrgUnit(this.widgetValue).shortname();
202 // first try the store cache
204 if(this.cache[this.auth][lclass]) {
205 var store = this.cache[this.auth][lclass];
207 query[linkInfo.vfield.name] = ''+this.widgetValue;
208 store.fetch({query:query, onComplete:
210 self.widgetValue = store.getValue(list[0], linkInfo.vfield.selector);
216 // then try the single object cache
217 if(this.cacheSingle[lclass] && this.cacheSingle[lclass][this.widgetValue]) {
218 this.widgetValue = this.cacheSingle[lclass][this.widgetValue];
222 // if those fail, fetch the linked object
223 dojo.require('openils.PermaCrud');
226 new openils.PermaCrud().retrieve(lclass, this.widgetValue, {
227 async : !this.forceSync,
228 oncomplete : function(r) {
229 var item = openils.Util.readResponse(r);
230 if(!self.cacheSingle[lclass])
231 self.cacheSingle[lclass] = {};
232 self.widgetValue = item[linkInfo.vfield.selector]();
233 self.cacheSingle[lclass][self.widgetValue] = item;
234 self.widget.startup();
235 self._widgetLoaded();
240 _getLinkSelector : function() {
241 var linkClass = this.idlField['class'];
242 if(this.idlField.reltype != 'has_a') return false;
243 if(!fieldmapper.IDL.fmclasses[linkClass].permacrud) return false;
244 if(!fieldmapper.IDL.fmclasses[linkClass].permacrud.retrieve) return false;
247 var rclassIdl = fieldmapper.IDL.fmclasses[linkClass];
249 for(var f in rclassIdl.fields) {
250 if(this.idlField.key == rclassIdl.fields[f].name) {
251 vfield = rclassIdl.fields[f];
257 throw new Error("'" + linkClass + "' has no '" + this.idlField.key + "' field!");
260 linkClass : linkClass,
265 _buildLinkSelector : function() {
266 var selectorInfo = this._getLinkSelector();
267 if(!selectorInfo) return false;
269 var linkClass = selectorInfo.linkClass;
270 var vfield = selectorInfo.vfield;
274 if(linkClass == 'pgt')
275 return this._buildPermGrpSelector();
276 if(linkClass == 'aou')
277 return this._buildOrgSelector();
278 if(linkClass == 'acpl')
279 return this._buildCopyLocSelector();
282 dojo.require('openils.PermaCrud');
283 dojo.require('dojo.data.ItemFileReadStore');
284 dojo.require('dijit.form.FilteringSelect');
286 this.widget = new dijit.form.FilteringSelect(this.dijitArgs, this.parentNode);
287 this.widget.searchAttr = this.widget.labelAttr = vfield.selector || vfield.name;
288 this.widget.valueAttr = vfield.name;
291 var oncomplete = function(list) {
294 new dojo.data.ItemFileReadStore({data:fieldmapper[linkClass].toStoreData(list)});
295 self.cache[self.auth][linkClass] = self.widget.store;
297 self.widget.store = self.cache[self.auth][linkClass];
299 self.widget.startup();
300 self._widgetLoaded();
303 if(this.cache[self.auth][linkClass]) {
307 new openils.PermaCrud().retrieveAll(linkClass, {
308 async : !this.forceSync,
309 oncomplete : function(r) {
310 var list = openils.Util.readResponse(r, false, true);
320 * For widgets that run asynchronously, provide a callback for finishing up
322 _widgetLoaded : function(value) {
324 this.baseWidgetValue(this.getDisplayString());
326 this.baseWidgetValue(this.widgetValue);
327 if(this.idlField.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
328 this.widget.attr('disabled', true);
329 if(this.disableWidgetTest && this.disableWidgetTest(this.idlField.name, this.fmObject))
330 this.widget.attr('disabled', true);
333 this.onload(this.widget, this);
336 _buildOrgSelector : function() {
337 dojo.require('fieldmapper.OrgUtils');
338 dojo.require('openils.widget.FilteringTreeSelect');
339 this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
340 this.widget.searchAttr = 'shortname';
341 this.widget.labelAttr = 'shortname';
342 this.widget.parentField = 'parent_ou';
343 var user = new openils.User();
345 if(this.widgetValue == null)
346 this.widgetValue = user.user.ws_ou();
348 // if we have a limit perm, find the relevent orgs (async)
349 if(this.orgLimitPerms && this.orgLimitPerms.length > 0) {
352 user.getPermOrgList(this.orgLimitPerms,
354 self.widget.tree = orgList;
355 self.widget.startup();
356 self._widgetLoaded();
361 this.widget.tree = fieldmapper.aou.globalOrgTree;
362 this.widget.startup();
366 _buildPermGrpSelector : function() {
367 dojo.require('openils.widget.FilteringTreeSelect');
368 this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
369 this.widget.searchAttr = 'name';
371 if(this.cache.permGrpTree) {
372 this.widget.tree = this.cache.permGrpTree;
373 this.widget.startup();
379 new openils.PermaCrud().retrieveAll('pgt', {
380 async : !this.forceSync,
381 oncomplete : function(r) {
382 var list = openils.Util.readResponse(r, false, true);
387 map[list[l].id()] = list[l];
390 var pnode = map[node.parent()];
391 if(!pnode) {root = node; continue;}
392 if(!pnode.children()) pnode.children([]);
393 pnode.children().push(node);
395 self.widget.tree = self.cache.permGrpTree = root;
396 self.widget.startup();
397 self._widgetLoaded();
404 _buildCopyLocSelector : function() {
405 dojo.require('dijit.form.FilteringSelect');
406 this.widget = new dijit.form.FilteringSelect(this.dijitArgs, this.parentNode);
407 this.widget.searchAttr = this.widget.labalAttr = 'name';
408 this.widget.valueAttr = 'id';
410 if(this.cache.copyLocStore) {
411 this.widget.store = this.cache.copyLocStore;
412 this.widget.startup();
418 var ws_ou = openils.User.user.ws_ou();
419 var orgs = fieldmapper.aou.findOrgUnit(ws_ou).orgNodeTrail().map(function (i) { return i.id() });
420 orgs = orgs.concat(fieldmapper.aou.descendantNodeList(ws_ou).map(function (i) { return i.id() }));
423 new openils.PermaCrud().search('acpl', {owning_lib : orgs}, {
424 async : !this.forceSync,
425 oncomplete : function(r) {
426 var list = openils.Util.readResponse(r, false, true);
429 new dojo.data.ItemFileReadStore({data:fieldmapper.acpl.toStoreData(list)});
430 self.cache.copyLocStore = self.widget.store;
431 self.widget.startup();
432 self._widgetLoaded();