]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoWidget.js
disable editing of ID fields
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / AutoWidget.js
1 if(!dojo._hasResource['openils.widget.AutoWidget']) {
2     dojo.provide('openils.widget.AutoWidget');
3     dojo.require('openils.Util');
4     dojo.require('openils.User');
5     dojo.require('fieldmapper.IDL');
6
7     dojo.declare('openils.widget.AutoWidget', null, {
8
9         async : false,
10
11         /**
12          * args:
13          *  idlField -- Field description object from fieldmapper.IDL.fmclasses
14          *  fmObject -- If available, the object being edited.  This will be used 
15          *      to set the value of the widget.
16          *  fmClass -- Class name (not required if idlField or fmObject is set)
17          *  fmField -- Field name (not required if idlField)
18          *  parentNode -- If defined, the widget will be appended to this DOM node
19          *  dijitArgs -- Optional parameters object, passed directly to the dojo widget
20          *  orgLimitPerms -- If this field defines a set of org units and an orgLimitPerms 
21          *      is defined, the code will limit the org units in the set to those
22          *      allowed by the permission
23          */
24         constructor : function(args) {
25             for(var k in args)
26                 this[k] = args[k];
27
28             // find the field description in the IDL if not provided
29             if(!this.idlField) {
30                 if(this.fmObject)
31                     this.fmClass = this.fmObject.classname;
32                 var fields = fieldmapper.IDL.fmclasses[this.fmClass][fields];
33                 for(var f in fields) 
34                     if(fields[f].name == this.fmField)
35                         this.idlField = fields[f];
36             }
37         },
38
39         build : function(onload) {
40             this.onload = onload;
41             this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null;
42
43             switch(this.idlField.datatype) {
44                 
45                 case 'id':
46                     dojo.require('dijit.form.TextBox');
47                     this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
48                     this.widget.setDisabled(true); // never allow editing of IDs
49                     break;
50
51                 case 'org_unit':
52                     this._buildOrgSelector();
53                     break;
54
55                 case 'money':
56                     dojo.require('dijit.form.CurrencyTextBox');
57                     this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode);
58                     break;
59
60                 case 'timestamp':
61                     dojo.require('dijit.form.DateTextBox');
62                     dojo.require('dojo.date.stamp');
63                     this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode);
64                     if(this.widgetValue != null) 
65                         this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue);
66                     break;
67
68                 case 'bool':
69                     dojo.require('dijit.form.CheckBox');
70                     this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode);
71                     this.widgetValue = openils.Util.isTrue(this.widgetValue);
72                     break;
73
74                 default:
75                     dojo.require('dijit.form.TextBox');
76                     this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
77             }
78
79             if(!this.async) this._widgetLoaded();
80             return this.widget;
81         },
82
83         /**
84          * For widgets that run asynchronously, provide a callback for finishing up
85          */
86         _widgetLoaded : function(value) {
87             if(this.fmObject) 
88                 this.widget.attr('value', this.widgetValue);
89             if(this.onload)
90                 this.onload(this.widget, self);
91         },
92
93         _buildOrgSelector : function() {
94             dojo.require('fieldmapper.OrgUtils');
95             dojo.require('openils.widget.FilteringTreeSelect');
96             this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
97             this.widget.searchAttr = 'shortname';
98
99             // if we have a limit perm, find the relevent orgs (async)
100             if(this.orgLimitPerms && this.orgLimitPerms.length > 0) {
101                 this.async = true;
102                 var user = new openils.User();
103                 var self = this;
104                 user.getPermOrgList(this.orgLimitPerms, 
105                     function(orgList) {
106                         self.widget.tree = orgList;
107                         self.widget.startup();
108                         self._widgetLoaded();
109                     }
110                 );
111
112             } else {
113                 this.widget.tree = fieldmapper.aou.globalOrgTree;
114             }
115         }
116     });
117 }
118