changed file type to xhtml to match other admin files. added support for choosing...
[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("dojox.grid.Grid");
5 dojo.require("fieldmapper.Fieldmapper");
6 dojo.require("fieldmapper.dojoData");
7 dojo.require("fieldmapper.OrgUtils");
8 dojo.require('dojo.cookie');
9 dojo.require('openils.CGI');
10 dojo.require('openils.User');
11 dojo.require('openils.Event');
12 dojo.require('openils.widget.OrgUnitFilteringSelect');
13
14 var authtoken;
15 var contextOrg;
16 var user;
17 var workOrgs;
18
19 function osInit(data) {
20     authtoken = dojo.cookie('ses') || new openils.CGI().param('ses');
21     user = new openils.User({authtoken:authtoken}).user;
22     contextOrg = user.ws_ou();
23
24     fieldmapper.standardRequest(
25         [   'open-ils.actor',
26             'open-ils.actor.user.get_work_ous.ids'],
27         {   async: true,
28             params: [authtoken],
29             oncomplete: function(r) {
30                 var list = r.recv().content();
31                 if(e = openils.Event.parse(list))
32                     return alert(e);
33                 workOrgs = list;
34                 buildMergedOrgSelector(list);
35             }
36         }
37     );
38
39     osDraw();
40 }
41 dojo.addOnLoad(osInit);
42
43 function osDraw() {
44     var names = [];
45     for(var key in osSettings)
46         names.push(key);
47
48     fieldmapper.standardRequest(
49         [   'open-ils.actor', 
50             'open-ils.actor.ou_setting.ancestor_default.batch'],
51         {   async: true,
52             params: [contextOrg, names],
53             oncomplete: function(r) {
54                 var data = r.recv().content();
55                 if(e = openils.Event.parse(data))
56                     return alert(e);
57                 osLoadGrid(data);
58             }
59         }
60     );
61 }
62
63 function buildMergedOrgSelector(orgList) {
64     var orgNodeList = [];
65     for(var i = 0; i < orgList.length; i++) {
66         // add the work org parents
67         var parents = [];
68         var node = fieldmapper.aou.findOrgUnit(orgList[i]);
69         while(node.parent_ou() != null) {
70             node = fieldmapper.aou.findOrgUnit(node.parent_ou());
71             parents.push(node);
72         }
73         orgNodeList = orgNodeList.concat(parents.reverse());
74
75         // add the work org children
76         orgNodeList = orgNodeList.concat(
77             fieldmapper.aou.descendantNodeList(orgList[i]));
78     }
79
80     var store = new dojo.data.ItemFileReadStore({data:aou.toStoreData(orgNodeList)});
81     osContextSelector.store = store;
82     osContextSelector.startup();
83     osContextSelector.setValue(user.ws_ou());
84 }
85
86 function osChangeContext() {
87     if(contextOrg == osContextSelector.getValue())
88         return;
89     contextOrg = osContextSelector.getValue();
90     osDraw();
91 }
92
93 function osLoadGrid(data) {
94     var gridData = {items:[]}
95     for(var key in data) {
96         var setting = osSettings[key];
97         setting.context = null;
98         setting.value = null;
99         if(data[key]) {
100             setting.context = data[key].org;
101             setting.value = data[key].value;
102         }
103         gridData.items.push({name:key});
104     }
105     gridData.identifier = 'name';
106     var store = new dojo.data.ItemFileReadStore({data:gridData});
107     var model = new dojox.grid.data.DojoData(
108         null, store, {rowsPerPage: 100, clientSort: true, query:{name:'*'}});
109
110     osGrid.setModel(model);
111     osGrid.setStructure(osGridLayout);
112     osGrid.update();
113 }
114
115 function osGetGridData(rowIdx) {
116     var data = this.grid.model.getRow(rowIdx);
117     if(!data) return '';
118     var value = osSettings[data.name][this.field];
119     if(value == null) return '';
120     switch(this.field) {
121         case 'context':
122             return fieldmapper.aou.findOrgUnit(value).shortname();
123         default:
124             return value;
125     }
126 }
127
128 function osGetEditLink(rowIdx) {
129     var data = this.grid.model.getRow(rowIdx);
130     if(!data) return '';
131     return this.value.replace(/SETTING/, data.name);
132 }
133
134 function osLaunchEditor(name) {
135 }
136