]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/conify/global/config/circ_limit_set.js
eca7c988f19a60d36fc5fe5cc2d55fd67698dd8b
[working/Evergreen.git] / Open-ILS / web / js / ui / default / conify / global / config / circ_limit_set.js
1 dojo.require('dijit.layout.ContentPane');
2 dojo.require('dijit.form.Button');
3 dojo.require('openils.widget.AutoGrid');
4 dojo.require('openils.widget.AutoFieldWidget');
5 dojo.require('openils.PermaCrud');
6 dojo.require('openils.widget.ProgressDialog');
7 dojo.require('openils.User');
8
9 var linkedEditor = null;
10 var circModEntryCache = [];
11 var copyLocEntryCache = [];
12 var limitGroupEntryCache = [];
13 var circModCache = {};
14 var copyLocCache = {};
15 var limitGroupCache = {};
16 var curLinkedEditor;
17
18 function load(){
19     clsGrid.loadAll({order_by:{ccls:'name'}});
20     clsGrid.onEditPane = buildEditPaneAdditions;
21     clsGrid.onPostUpdate = updateLinked;
22     clsGrid.onPostCreate = updateLinked;
23     linkedEditor = dojo.byId('linked-editor').parentNode.removeChild(dojo.byId('linked-editor'));
24
25     // Cache circ mod/limit group info for later display
26     var pcrud = new openils.PermaCrud();
27     var temp = pcrud.retrieveAll('ccm');
28     dojo.forEach(temp, function(g) { circModCache[g.code()] = g; } );
29     temp = pcrud.retrieveAll('cclg');
30     dojo.forEach(temp, function(g) { limitGroupCache[g.id()] = g; } );
31
32     // Avoid fetching all copy locations because there can be many 
33     // thousands.  Limit to those within permission range of the user.
34     new openils.User().getPermOrgList(
35         'ADMIN_CIRC_MATRIX_MATCHPOINT',
36         function (orgList) {
37             temp = pcrud.search('acpl', {owning_lib : orgList});
38             dojo.forEach(temp, function(g) { copyLocCache[g.id()] = g; } );
39         }, 
40         true, true 
41     );
42 }
43
44 function byName(name, ctxt) {
45     return dojo.query('[name=' + name + ']', ctxt)[0];
46 }
47
48 function buildEditPaneAdditions(editPane) {
49     circModEntryCache = [];
50     limitGroupEntryCache = [];
51     copyLocEntryCache = [];
52     var tr = document.createElement('tr');
53     var td = document.createElement('td');
54     td.setAttribute('colspan','2');
55     // Explanation....
56     // editPane.domNode.lastChild = Table
57     // .lastChild = Table Body
58     // .lastChild = Table Row containing Action Buttons
59     editPane.domNode.lastChild.lastChild.insertBefore(tr, editPane.domNode.lastChild.lastChild.lastChild);
60     tr.appendChild(td);
61     curLinkedEditor = linkedEditor.cloneNode(true);
62     td.appendChild(curLinkedEditor);
63     var circModTmpl = byName('circ-mod-entry-tbody', curLinkedEditor).removeChild(byName('circ-mod-entry-row', curLinkedEditor));
64     var copyLocTmpl = byName('copy-loc-entry-tbody', curLinkedEditor).removeChild(byName('copy-loc-entry-row', curLinkedEditor));
65     var limitGroupTmpl = byName('limit-group-entry-tbody', curLinkedEditor).removeChild(byName('limit-group-entry-row', curLinkedEditor));
66
67     var cm_selector = new openils.widget.AutoFieldWidget({
68         fmClass : 'cclscmm',
69         fmField : 'circ_mod',
70         parentNode : byName('circ-mod-selector', curLinkedEditor)
71     });
72     cm_selector.build();
73
74     var cl_selector = new openils.widget.AutoFieldWidget({
75         fmClass : 'cclsacpl',
76         fmField : 'copy_loc',
77         parentNode : byName('copy-loc-selector', curLinkedEditor)
78     });
79     cl_selector.build();
80
81     var lg_selector = new openils.widget.AutoFieldWidget({
82         fmClass : 'cclsgm',
83         fmField : 'limit_group',
84         parentNode : byName('limit-group-selector', curLinkedEditor)
85     });
86     lg_selector.build();
87
88     function addMod(code) {
89         var row = circModTmpl.cloneNode(true);
90         row.setAttribute('code', code);
91         byName('circ-mod', row).innerHTML = code + ' : ' + circModCache[code].name();
92         byName('remove-circ-mod', row).onclick = function() {
93             byName('circ-mod-entry-tbody', clsGrid.editPane.domNode).removeChild(row);
94         }
95         byName('circ-mod-entry-tbody', editPane.domNode).appendChild(row);
96     }
97
98     function addLoc(id) {
99         var row = copyLocTmpl.cloneNode(true);
100         row.setAttribute('loc_id', id);
101         var copyloc = copyLocCache[id];
102         byName('copy-loc', row).innerHTML = 
103             fieldmapper.aou.findOrgUnit(copyloc.owning_lib()).shortname() +
104             ' : ' + copyloc.name();
105         byName('remove-copy-loc', row).onclick = function() {
106             byName('copy-loc-entry-tbody', clsGrid.editPane.domNode).removeChild(row);
107         }
108         byName('copy-loc-entry-tbody', editPane.domNode).appendChild(row);
109     }
110
111     function addGroup(group) {
112         var row = limitGroupTmpl.cloneNode(true);
113         row.setAttribute('limit_group', group);
114         byName('limit-group', row).innerHTML = limitGroupCache[group].name();
115         byName('remove-limit-group', row).onclick = function() {
116             byName('limit-group-entry-tbody', clsGrid.editPane.domNode).removeChild(row);
117         }
118         byName('limit-group-entry-tbody', editPane.domNode).appendChild(row);
119     }
120
121     byName('add-circ-mod', editPane.domNode).onclick = function() {
122         addMod(cm_selector.widget.attr('value'));
123     }
124
125     byName('add-copy-loc', editPane.domNode).onclick = function() {
126         addLoc(cl_selector.widget.attr('value'));
127     }
128
129     byName('add-limit-group', editPane.domNode).onclick = function() {
130         addGroup(lg_selector.widget.attr('value'));
131     }
132
133     // On edit we need to load existing entries.
134     // On create, not so much.
135     if(!editPane.fmObject) return; 
136     var limitSet = editPane.fmObject.id();
137
138     if(editPane.mode == 'update') {
139         var pcrud = new openils.PermaCrud();
140         circModEntryCache = pcrud.search('cclscmm', {limit_set: limitSet});
141         copyLocEntryCache = pcrud.search('cclsacpl', {limit_set: limitSet});
142         limitGroupEntryCache = pcrud.search('cclsgm', {limit_set: limitSet});
143         dojo.forEach(circModEntryCache, function(g) { addCircMod(circModTmpl, g); } );
144         dojo.forEach(copyLocEntryCache, function(g) { addCopyLoc(copyLocTmpl, g); } );
145         dojo.forEach(limitGroupEntryCache, function(g) { addLimitGroup(limitGroupTmpl, g); } );
146     } 
147 }
148
149 function addCircMod(tmpl, circ_mod_entry) {
150     var row = tmpl.cloneNode(true);
151     var code = circ_mod_entry.circ_mod();
152     row.setAttribute('code', code);
153     byName('circ-mod', row).innerHTML = code + ' : ' + circModCache[code].name();
154     byName('remove-circ-mod', row).onclick = function() {
155         byName('circ-mod-entry-tbody', clsGrid.editPane.domNode).removeChild(row);
156     }
157     byName('circ-mod-entry-tbody', clsGrid.editPane.domNode).appendChild(row);
158 }
159
160 function addCopyLoc(tmpl, copy_loc_entry) {
161     var row = tmpl.cloneNode(true);
162     var id = copy_loc_entry.copy_loc();
163     var copyloc = copyLocCache[id];
164     row.setAttribute('loc_id', id);
165     byName('copy-loc', row).innerHTML = 
166         fieldmapper.aou.findOrgUnit(copyloc.owning_lib()).shortname() +
167         ' : ' + copyloc.name();
168     byName('remove-copy-loc', row).onclick = function() {
169         byName('copy-loc-entry-tbody', clsGrid.editPane.domNode).removeChild(row);
170     }
171     byName('copy-loc-entry-tbody', clsGrid.editPane.domNode).appendChild(row);
172 }
173
174 function addLimitGroup(tmpl, limit_group_entry) {
175     var row = tmpl.cloneNode(true);
176     var group = limit_group_entry.limit_group();
177     row.setAttribute('limit_group', group);
178     byName('limit-group', row).innerHTML = limitGroupCache[group].name();
179     if(limit_group_entry.check_only() == 't') {
180         byName('limit-group-check-only', row).setAttribute('checked', 'true');
181     }
182     byName('remove-limit-group', row).onclick = function() {
183         byName('limit-group-entry-tbody', clsGrid.editPane.domNode).removeChild(row);
184     }
185     byName('limit-group-entry-tbody', clsGrid.editPane.domNode).appendChild(row);
186 }
187
188 function updateLinked(fmObject, rowindex) {
189     var id = null;
190     if(rowindex != undefined && this.editPane && this.editPane.fmObject) {
191         // Edit, grab existing ID
192         id = this.editPane.fmObject.id();
193     } else if(fmObject.id) {
194         // Create, grab new ID
195         id = fmObject.id();
196     }
197     // If we don't have an ID, drop out.
198     if(id == null) return;
199     var pcrud = new openils.PermaCrud();
200     progressDialog.show(true);
201
202     var add = [];
203     var remove = [];
204     var update = [];
205
206     // First up, circ mods.
207     var circ_mods = [];
208     dojo.query('[name=circ-mod-entry-row]', this.editPane.domNode).forEach(
209         function(row) {
210             var mod = row.getAttribute('code');
211             circ_mods.push(mod);
212             if(!circModEntryCache.filter(function(i) { return (i.circ_mod() == mod); })[0]) {
213                 var entry = new fieldmapper.cclscmm();
214                 entry.isnew(true);
215                 entry.limit_set(id);
216                 entry.circ_mod(mod);
217                 add.push(entry);
218             }
219         }
220     );
221     dojo.forEach(circModEntryCache, function(eMod) {
222             if(!circ_mods.filter(function(i) { return (i == eMod.circ_mod()); })[0]) {
223                 eMod.isdeleted(true);
224                 remove.push(eMod);
225             }
226         }
227     );
228
229     // Next, copy locations
230     var copy_locs = [];
231     dojo.query('[name=copy-loc-entry-row]', this.editPane.domNode).forEach(
232         function(row) {
233             var loc_id = row.getAttribute('loc_id');
234             copy_locs.push(loc_id);
235             if(!copyLocEntryCache.filter(function(i) { return (i.copy_loc() == loc_id); })[0]) {
236                 var entry = new fieldmapper.cclsacpl();
237                 entry.isnew(true);
238                 entry.limit_set(id);
239                 entry.copy_loc(loc_id);
240                 add.push(entry);
241             }
242         }
243     );
244     dojo.forEach(copyLocEntryCache, function(eLoc) {
245             if(!copy_locs.filter(function(i) { return (i == eLoc.copy_loc()); })[0]) {
246                 eLoc.isdeleted(true);
247                 remove.push(eLoc);
248             }
249         }
250     );
251
252
253     // Next, limit groups
254     var limit_groups = [];
255     dojo.query('[name=limit-group-entry-row]', this.editPane.domNode).forEach(
256         function(row) {
257             var group = row.getAttribute('limit_group');
258             limit_groups.push(group);
259             var cached = limitGroupEntryCache.filter(function(i) { return (i.limit_group() == group); })[0];
260             if(!cached) {
261                 var entry = new fieldmapper.cclsgm();
262                 entry.isnew(true);
263                 entry.limit_set(id);
264                 entry.limit_group(group);
265                 entry.check_only(byName('limit-group-check-only', row).checked ? 't' : 'f');
266                 add.push(entry);
267             } else {
268                 var check_only = byName('limit-group-check-only', row).checked;
269                 if(check_only != (cached.check_only() == 't')) {
270                     cached.check_only(check_only ? 't' : 'f');
271                     cached.ischanged(true);
272                     update.push(cached);
273                 }
274             }
275         }
276     );
277     dojo.forEach(limitGroupEntryCache, function(eGroup) {
278             if(!limit_groups.filter(function(i) { return (i == eGroup.limit_group()); })[0]) {
279                 eGroup.isdeleted(true);
280                 remove.push(eGroup);
281             }
282         }
283     );
284
285     function updateEntries() {
286         pcrud.update(update, {
287             oncomplete : function () {
288                 progressDialog.hide();
289             }
290         });
291     }
292
293     function removeEntries() {
294         pcrud.eliminate(remove, {
295             oncomplete : function () {
296                 if(update.length) {
297                     updateEntries();
298                 } else {
299                     progressDialog.hide();
300                 }
301             }
302         });
303     }
304
305     function addEntries() {
306         pcrud.create(add, {
307             oncomplete : function () {
308                 if(remove.length) {
309                     removeEntries();
310                 } else if (update.length) {
311                     updateEntries();
312                 } else {
313                     progressDialog.hide();
314                 }
315             }
316         });
317     }
318
319     if(add.length)
320         addEntries();
321     else if (remove.length)
322         removeEntries();
323     else if (update.length)
324         updateEntries();
325     else
326         progressDialog.hide();
327 }
328
329 openils.Util.addOnLoad(load);
330