added a new pile of settings. added a flag to indicate when a value is not inherited...
[Evergreen.git] / Open-ILS / xul / staff_client / server / admin / org_unit_settings.js
1 dojo.require("dijit.layout.LayoutContainer");
2 dojo.require("dijit.layout.ContentPane");
3 dojo.require('dijit.form.FilteringSelect');
4 dojo.require('dijit.Dialog');
5 dojo.require("dojox.grid.Grid");
6 dojo.require("fieldmapper.Fieldmapper");
7 dojo.require("fieldmapper.dojoData");
8 dojo.require("fieldmapper.OrgUtils");
9 dojo.require('dojo.cookie');
10 dojo.require('openils.CGI');
11 dojo.require('openils.User');
12 dojo.require('openils.Event');
13 dojo.require('openils.widget.OrgUnitFilteringSelect');
14
15 var authtoken;
16 var contextOrg;
17 var user;
18 var workOrgs;
19
20 function osInit(data) {
21     authtoken = dojo.cookie('ses') || new openils.CGI().param('ses');
22     user = new openils.User({authtoken:authtoken});
23     contextOrg = user.user.ws_ou();
24
25     fieldmapper.standardRequest(
26         [   'open-ils.actor',
27             'open-ils.actor.user.get_work_ous.ids'],
28         {   async: true,
29             params: [authtoken],
30             oncomplete: function(r) {
31                 var list = r.recv().content();
32                 if(e = openils.Event.parse(list))
33                     return alert(e);
34                 workOrgs = list;
35                 buildMergedOrgSelector(list);
36             }
37         }
38     );
39
40     osDraw();
41 }
42 dojo.addOnLoad(osInit);
43
44 function osDraw() {
45     var names = [];
46     for(var key in osSettings)
47         names.push(key);
48
49     fieldmapper.standardRequest(
50         [   'open-ils.actor', 
51             'open-ils.actor.ou_setting.ancestor_default.batch'],
52         {   async: true,
53             params: [contextOrg, names],
54             oncomplete: function(r) {
55                 var data = r.recv().content();
56                 if(e = openils.Event.parse(data))
57                     return alert(e);
58                 osLoadGrid(data);
59             }
60         }
61     );
62 }
63
64 function buildMergedOrgSelector(orgList) {
65     var orgNodeList = [];
66     for(var i = 0; i < orgList.length; i++) {
67         // add the work org parents
68         var parents = [];
69         var node = fieldmapper.aou.findOrgUnit(orgList[i]);
70         while(node.parent_ou() != null) {
71             node = fieldmapper.aou.findOrgUnit(node.parent_ou());
72             parents.push(node);
73         }
74         orgNodeList = orgNodeList.concat(parents.reverse());
75
76         // add the work org children
77         orgNodeList = orgNodeList.concat(
78             fieldmapper.aou.descendantNodeList(orgList[i]));
79     }
80
81     var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgNodeList)});
82     osContextSelector.store = store;
83     osContextSelector.startup();
84     osContextSelector.setValue(user.user.ws_ou());
85 }
86
87 function osChangeContext() {
88     if(contextOrg == osContextSelector.getValue())
89         return;
90     contextOrg = osContextSelector.getValue();
91     osDraw();
92 }
93
94 function osLoadGrid(data) {
95     var gridData = {items:[]}
96     for(var key in data) {
97         var setting = osSettings[key];
98         setting.context = null;
99         setting.value = null;
100         if(data[key]) {
101             setting.context = data[key].org;
102             setting.value = data[key].value;
103         }
104         gridData.items.push({name:key});
105     }
106     gridData.items = gridData.items.sort(
107         function(a, b) {
108             var seta = osSettings[a.name];
109             var setb = osSettings[b.name];
110             if(seta.label > setb.label) return 1;
111             if(seta.label < setb.label) return -1;
112             return 0;
113         }
114     );
115     gridData.identifier = 'name';
116     var store = new dojo.data.ItemFileReadStore({data:gridData});
117     var model = new dojox.grid.data.DojoData(
118         null, store, {rowsPerPage: 100, clientSort: true, query:{name:'*'}});
119
120     osGrid.setModel(model);
121     osGrid.setStructure(osGridLayout);
122     osGrid.update();
123 }
124
125 function osGetGridData(rowIdx) {
126     var data = this.grid.model.getRow(rowIdx);
127     if(!data) return '';
128     var setting = osSettings[data.name];
129     var value = setting[this.field];
130     if(value == null) return '';
131     switch(this.field) {
132         case 'context':
133             return fieldmapper.aou.findOrgUnit(value).shortname();
134         case 'label':
135             if(setting.noInherit)
136                 return '* ' + value;
137             return value;
138         case 'value':
139             if(setting.type == 'bool') {
140                 if(value) 
141                     return dojo.byId('os-true').innerHTML;
142                 return dojo.byId('os-false').innerHTML;
143             }
144         default:
145             return value;
146     }
147 }
148
149 function osGetEditLink(rowIdx) {
150     var data = this.grid.model.getRow(rowIdx);
151     if(!data) return '';
152     return this.value.replace(/SETTING/, data.name);
153 }
154
155 function osLaunchEditor(name) {
156     osEditDialog._osattr = name;
157     osEditDialog.show();
158     user.buildPermOrgSelector('UPDATE_ORG_UNIT_SETTING.' + name, osEditContextSelector);
159     dojo.byId('os-edit-name').innerHTML = osSettings[name].label;
160     dojo.byId('os-edit-desc').innerHTML = osSettings[name].desc || '';
161
162     dojo.style(osEditTextBox.domNode, 'display', 'none');
163     dojo.style(osEditCurrencyTextBox.domNode, 'display', 'none');
164     dojo.style(osEditNumberTextBox.domNode, 'display', 'none');
165     dojo.style(osEditBoolSelect.domNode, 'display', 'none');
166
167     switch(osSettings[name].type) {
168         case 'number':
169             dojo.style(osEditNumberTextBox.domNode, 'display', 'block');
170             break;
171         case 'currency':
172             dojo.style(osEditCurrencyTextBox.domNode, 'display', 'block');
173             break;
174         case 'bool':
175             dojo.style(osEditBoolSelect.domNode, 'display', 'block');
176             break;
177         default:
178             dojo.style(osEditTextBox.domNode, 'display', 'block');
179     }
180 }
181
182 function osEditSetting(deleteMe) {
183     osEditDialog.hide();
184     var name = osEditDialog._osattr;
185
186     var obj = {};
187     if(deleteMe) {
188         obj[name] = null;
189
190     } else {
191
192         switch(osSettings[name].type) {
193             case 'number':
194                 obj[name] = osEditNumberTextBox.getValue();
195                 if(obj[name] == null) return;
196                 break;
197             case 'currency':
198                 obj[name] = osEditCurrencyTextBox.getValue();
199                 if(obj[name] == null) return;
200                 break;
201             case 'bool':
202                 var val = osEditBoolSelect.getValue();
203                 obj[name] = (val == 'true') ? 1 : null;
204                 break;
205             default:
206                 obj[name] = osEditTextBox.getValue();
207                 if(obj[name] == null) return;
208         }
209     }
210
211     fieldmapper.standardRequest(
212         ['open-ils.actor', 'open-ils.actor.org_unit.settings.update'],
213         {   async: true,
214             params: [authtoken, osEditContextSelector.getValue(), obj],
215             oncomplete: function(r) {
216                 var res = r.recv().content();
217                 if(e = openils.Event.parse(res))
218                     return alert(e);
219                 osDraw();
220             }
221         }
222     );
223 }
224