]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoWidget.js
added initial auto-widget class. this takes an IDL class and optional fieldmapper...
[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 'org_unit':
46                     this._buildOrgSelector();
47                     break;
48
49                 case 'money':
50                     dojo.require('dijit.form.CurrencyTextBox');
51                     this.widget = new dijit.form.CurrencyTextBox(this.dijitArgs, this.parentNode);
52                     break;
53
54                 case 'timestamp':
55                     dojo.require('dijit.form.DateTextBox');
56                     dojo.require('dojo.date.stamp');
57                     this.widget = new dijit.form.DateTextBox(this.dijitArgs, this.parentNode);
58                     if(this.widgetValue != null) 
59                         this.widgetValue = dojo.date.stamp.fromISOString(this.widgetValue);
60                     break;
61
62                 case 'bool':
63                     dojo.require('dijit.form.CheckBox');
64                     this.widget = new dijit.form.CheckBox(this.dijitArgs, this.parentNode);
65                     this.widgetValue = openils.Util.isTrue(this.widgetValue);
66                     break;
67
68                 default:
69                     dojo.require('dijit.form.TextBox');
70                     this.widget = new dijit.form.TextBox(this.dijitArgs, this.parentNode);
71             }
72
73             if(!this.async) this._widgetLoaded();
74             return this.widget;
75         },
76
77         /**
78          * For widgets that run asynchronously, provide a callback for finishing up
79          */
80         _widgetLoaded : function(value) {
81             if(this.fmObject) 
82                 this.widget.attr('value', this.widgetValue);
83             if(this.onload)
84                 this.onload(this.widget, self);
85         },
86
87         _buildOrgSelector : function() {
88             dojo.require('fieldmapper.OrgUtils');
89             dojo.require('openils.widget.FilteringTreeSelect');
90             this.widget = new openils.widget.FilteringTreeSelect(this.dijitArgs, this.parentNode);
91             this.widget.searchAttr = 'shortname';
92
93             // if we have a limit perm, find the relevent orgs (async)
94             if(this.orgLimitPerms && this.orgLimitPerms.length > 0) {
95                 this.async = true;
96                 var user = new openils.User();
97                 var self = this;
98                 user.getPermOrgList(this.orgLimitPerms, 
99                     function(orgList) {
100                         self.widget.tree = orgList;
101                         self.widget.startup();
102                         self._widgetLoaded();
103                     }
104                 );
105
106             } else {
107                 this.widget.tree = fieldmapper.aou.globalOrgTree;
108             }
109         }
110     });
111 }
112