]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoWidget.js
added support for retrieving the formatted value from the widgets in a pane. all...
[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         /**
40          * Turn the value from the dojo widget into a value oils understands
41          */
42         getFormattedValue : function() {
43             var value = this.widget.attr('value');
44             switch(this.idlField.datatype) {
45                 case 'bool':
46                     return (value) ? 't' : 'f'
47                 case 'timestamp':
48                     return dojo.date.stamp.toISOString(value);
49                 default:
50                     return value;
51             }
52         },
53
54         build : function(onload) {
55             this.onload = onload;
56             this.widgetValue = (this.fmObject) ? this.fmObject[this.idlField.name]() : null;
57
58             switch(this.idlField.datatype) {
59                 
60                 case 'id':
61                     dojo.require('dijit.form.TextBox');
62                     this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
63                     this.widget.setDisabled(true); // never allow editing of IDs
64                     break;
65
66                 case 'org_unit':
67                     this._buildOrgSelector();
68                     break;
69
70                 case 'money':
71                     dojo.require('dijit.form.CurrencyTextBox');
72                     this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode);
73                     break;
74
75                 case 'timestamp':
76                     dojo.require('dijit.form.DateTextBox');
77                     dojo.require('dojo.date.stamp');
78                     this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode);
79                     if(this.widgetValue != null) 
80                         this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue);
81                     break;
82
83                 case 'bool':
84                     dojo.require('dijit.form.CheckBox');
85                     this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode);
86                     this.widgetValue = openils.Util.isTrue(this.widgetValue);
87                     break;
88
89                 default:
90                     dojo.require('dijit.form.TextBox');
91                     this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
92             }
93
94             if(!this.async) this._widgetLoaded();
95             return this.widget;
96         },
97
98         /**
99          * For widgets that run asynchronously, provide a callback for finishing up
100          */
101         _widgetLoaded : function(value) {
102             if(this.fmObject) 
103                 this.widget.attr('value', this.widgetValue);
104             if(this.onload)
105                 this.onload(this.widget, self);
106         },
107
108         _buildOrgSelector : function() {
109             dojo.require('fieldmapper.OrgUtils');
110             dojo.require('openils.widget.FilteringTreeSelect');
111             this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
112             this.widget.searchAttr = 'shortname';
113
114             // if we have a limit perm, find the relevent orgs (async)
115             if(this.orgLimitPerms && this.orgLimitPerms.length > 0) {
116                 this.async = true;
117                 var user = new openils.User();
118                 var self = this;
119                 user.getPermOrgList(this.orgLimitPerms, 
120                     function(orgList) {
121                         self.widget.tree = orgList;
122                         self.widget.startup();
123                         self._widgetLoaded();
124                     }
125                 );
126
127             } else {
128                 this.widget.tree = fieldmapper.aou.globalOrgTree;
129             }
130         }
131     });
132 }
133