]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/li_table.js
added batch update for copies
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / common / li_table.js
1 dojo.require('dijit.form.Button');
2 dojo.require('dijit.form.TextBox');
3 dojo.require('dijit.form.FilteringSelect');
4 dojo.require('dijit.ProgressBar');
5 dojo.require('openils.User');
6 dojo.require('openils.Util');
7 dojo.require('openils.acq.Lineitem');
8 dojo.require('openils.widget.AutoFieldWidget');
9
10 function AcqLiTable() {
11
12     var self = this;
13     this.liCache = {};
14     this.toggleState = false;
15     this.tbody = dojo.byId('acq-lit-tbody');
16     this.selectors = [];
17     this.authtoken = openils.User.authtoken;
18     this.rowTemplate = this.tbody.removeChild(dojo.byId('acq-lit-row'));
19     this.copyTbody = dojo.byId('acq-lit-li-details-tbody');
20     this.copyRow = this.copyTbody.removeChild(dojo.byId('acq-lit-li-details-row'));
21     this.copyBatchRow = dojo.byId('acq-lit-li-details-batch-row');
22
23     dojo.byId('acq-lit-select-toggle').onclick = function(){self.toggleSelect()};
24     dojo.byId('acq-lit-info-back-button').onclick = function(){self.show('list')};
25     dojo.byId('acq-lit-copies-back-button').onclick = function(){self.show('list')};
26
27     this.reset = function() {
28         while(self.tbody.childNodes[0])
29             self.tbody.removeChild(self.tbody.childNodes[0]);
30         self.selectors = [];
31     };
32     
33     this.setNext = function(handler) {
34         var link = dojo.byId('acq-lit-next');
35         if(handler) {
36             dojo.style(link, 'visibility', 'visible');
37             link.onclick = handler;
38         } else {
39             dojo.style(link, 'visibility', 'hidden');
40         }
41     };
42
43     this.setPrev = function(handler) {
44         var link = dojo.byId('acq-lit-prev');
45         if(handler) {
46             dojo.style(link, 'visibility', 'visible'); 
47             link.onclick = handler; 
48         } else {
49             dojo.style(link, 'visibility', 'hidden');
50         }
51     };
52
53     this.show = function(div) {
54         openils.Util.hide('acq-lit-table-div');
55         openils.Util.hide('acq-lit-info-div');
56         openils.Util.hide('acq-lit-li-details');
57         switch(div){
58             case 'list':
59                 openils.Util.show('acq-lit-table-div');
60                 break;
61             case 'info':
62                 openils.Util.show('acq-lit-info-div');
63                 break;
64             case 'copies':
65                 openils.Util.show('acq-lit-li-details');
66                 break;
67             }
68     }
69
70     this.hide = function() {
71         this.show(null);
72     }
73
74     this.toggleSelect = function() {
75         if(self.toggleState) 
76             dojo.forEach(self.selectors, function(i){i.checked = false});
77         else 
78             dojo.forEach(self.selectors, function(i){i.checked = true});
79         self.toggleState = !self.toggleState;
80     };
81
82     /** @param all If true, assume all are selected */
83     this.getSelected = function(all) {
84         var selected = [];
85         dojo.forEach(self.selectors, 
86             function(i) { 
87                 if(i.checked || all)
88                     selected.push(self.liCache[i.parentNode.parentNode.getAttribute('li')]);
89             }
90         );
91         return selected;
92     };
93
94     this.setRowAttr = function(td, liWrapper, field) {
95         var val = liWrapper.findAttr(field, 'lineitem_marc_attr_definition') || '';
96         td.appendChild(document.createTextNode(val));
97     };
98
99     this.addLineitem = function(li) {
100         this.liCache[li.id()] = li;
101         var liWrapper = new openils.acq.Lineitem({lineitem:li});
102         var row = self.rowTemplate.cloneNode(true);
103         row.setAttribute('li', li.id());
104         var tds = dojo.query('[attr]', row);
105         dojo.forEach(tds, function(td) {self.setRowAttr(td, liWrapper, td.getAttribute('attr'));});
106         dojo.query('[name=source_label]', row)[0].appendChild(document.createTextNode(li.source_label()));
107         var isbn = liWrapper.findAttr('isbn', 'lineitem_marc_attr_definition');
108         if(isbn) {
109             // XXX media prefix for added content
110             dojo.query('[name=jacket]', row)[0].setAttribute('src', '/opac/extras/ac/jacket/small/' + isbn);
111         }
112         dojo.query('[name=infolink]', row)[0].onclick = function() {self.drawInfo(li.id())};
113         dojo.query('[name=copieslink]', row)[0].onclick = function() {self.drawCopies(li.id())};
114         dojo.query('[name=count]', row)[0].appendChild(document.createTextNode(li.item_count()));
115         self.tbody.appendChild(row);
116         self.selectors.push(dojo.query('[name=selectbox]', row)[0]);
117     };
118
119     this.drawInfo = function(liId) {
120         this.show('info');
121         openils.acq.Lineitem.fetchAttrDefs(
122             function() { 
123                 self._fetchLineitem(liId, function(li){self._drawInfo(li);}); 
124             } 
125         );
126     };
127
128     this._fetchLineitem = function(liId, handler) {
129
130         if(this.liCache[liId] && this.liCache[liId].marc()) {
131             return handler(this.liCache[liId]);
132         }
133         
134         fieldmapper.standardRequest(
135             ['open-ils.acq', 'open-ils.acq.lineitem.retrieve'],
136             {   async: true,
137
138                 params: [self.authtoken, liId, {
139                     flesh_attrs: true,
140                     flesh_li_details: true,
141                     flesh_fund_debit: true }],
142
143                 oncomplete: function(r) {
144                     var li = openils.Util.readResponse(r);
145                     handler(li)
146                 }
147             }
148         );
149     };
150
151     this._drawInfo = function(li) {
152         this.drawMarcHTML(li);
153         this.infoTbody = dojo.byId('acq-lit-info-tbody');
154         if(!this.infoRow)
155             this.infoRow = this.infoTbody.removeChild(dojo.byId('acq-lit-info-row'));
156         while(this.infoTbody.childNodes[0])
157             this.infoTbody.removeChild(this.infoTbody.childNodes[0]);
158         for(var i = 0; i < li.attributes().length; i++) {
159             var attr = li.attributes()[i];
160             var row = this.infoRow.cloneNode(true);
161
162             var type = attr.attr_type().replace(/lineitem_(.*)_attr_definition/, '$1');
163             var name = openils.acq.Lineitem.attrDefs[type].filter(
164                 function(a) {
165                     return (a.code() == attr.attr_name());
166                 }
167             ).pop().description();
168
169             dojo.query('[name=label]', row)[0].appendChild(document.createTextNode(name));
170             dojo.query('[name=value]', row)[0].appendChild(document.createTextNode(attr.attr_value()));
171             this.infoTbody.appendChild(row);
172         }
173     };
174
175     this.drawMarcHTML = function(li) {
176         fieldmapper.standardRequest(
177             ['open-ils.search', 'open-ils.search.biblio.record.html'],
178             {   async: true,
179                 params: [null, true, li.marc()],
180                 oncomplete: function(r) {
181                     dojo.byId('acq-lit-marc-div').innerHTML = 
182                         openils.Util.readResponse(r);
183                 }
184             }
185         );
186     }
187
188     this.drawCopies = function(liId) {
189         this.show('copies');
190         var self = this;
191         this.copyCache = {};
192         this.copyWidgetCache = {};
193         this.copyBatchWidgets = {};
194         acqLitSaveCopies.onClick = function() { self.saveCopyChanges(liId) };
195         acqLitBatchUpdateCopies.onClick = function() { self.batchCopyUpdate() };
196
197         while(this.copyTbody.childNodes[0])
198             this.copyTbody.removeChild(this.copyTbody.childNodes[0]);
199
200         var row = this.copyBatchRow;
201         if(!this.copyBatchRowDrawn) {
202             dojo.forEach(['fund', 'owning_lib', 'location'],
203                 function(field) {
204                     var widget = new openils.widget.AutoFieldWidget({
205                         fmField : field,
206                         fmClass : 'acqlid',
207                         parentNode : dojo.query('[name='+field+']', row)[0],
208                         orgLimitPerms : ['CREATE_PICKLIST'],
209                     });
210                     widget.build();
211                     self.copyBatchWidgets[field] = widget.widget;
212                 }
213             );
214             this.copyBatchRowDrawn = true;
215         };
216
217
218         openils.acq.Lineitem.fetchAttrDefs(
219             function() { 
220                 self._fetchLineitem(liId, function(li){self._drawCopies(li);}); 
221             } 
222         );
223     };
224
225     this.batchCopyUpdate = function() {
226         var self = this;
227         var fields = ['fund', 'owning_lib', 'location'];
228         for(var k in this.copyWidgetCache) {
229             var cache = this.copyWidgetCache[k];
230             dojo.forEach(fields, function(f) {
231                 var newval = self.copyBatchWidgets[f].attr('value');
232                 if(newval) cache[f].attr('value', newval);
233             });
234         }
235     };
236
237     this._drawCopies = function(li) {
238         acqLitAddCopyCount.onClick = function() { 
239             var count = acqLitCopyCountInput.attr('value');
240             for(var i = 0; i < count; i++)
241                 self.addCopy(li); 
242         }
243         if(li.lineitem_details().length > 0) {
244             dojo.forEach(li.lineitem_details(),
245                 function(copy) {
246                     self.addCopy(li, copy);
247                 }
248             );
249         } else {
250             self.addCopy(li);
251         }
252     };
253
254     this.virtCopyId = -1;
255     this.addCopy = function(li, copy) {
256         var row = this.copyRow.cloneNode(true);
257         this.copyTbody.appendChild(row);
258         var self = this;
259
260         if(!copy) {
261             copy = new fieldmapper.acqlid();
262             copy.isnew(true);
263             copy.id(this.virtCopyId--);
264             copy.lineitem(li.id());
265         }
266
267         this.copyCache[copy.id()] = copy;
268         row.setAttribute('copy_id', copy.id());
269         self.copyWidgetCache[copy.id()] = {};
270
271         dojo.forEach(['fund', 'owning_lib', 'location', 'barcode', 'cn_label'],
272             function(field) {
273                 var widget = new openils.widget.AutoFieldWidget({
274                     fmObject : copy,
275                     fmField : field,
276                     fmClass : 'acqlid',
277                     parentNode : dojo.query('[name='+field+']', row)[0],
278                     orgLimitPerms : ['CREATE_PICKLIST'],
279                 });
280                 widget.build();
281                 dojo.connect(widget.widget, 'onChange', 
282                     function(val) { 
283                         if(val != copy[field]()) {
284                             // prevent setting ischanged() automatically on widget load
285                             copy[field](widget.getFormattedValue()) 
286                             copy.ischanged(true);
287                         }
288                     }
289                 );
290                 self.copyWidgetCache[copy.id()][field] = widget.widget;
291             }
292         );
293
294         dojo.query('[name=delete]', row)[0].onclick = 
295             function() { self.deleteCopy(row) };
296     };
297
298     this.deleteCopy = function(row) {
299         var copy = this.copyCache[row.getAttribute('copy_id')];
300         copy.isdeleted(true);
301         if(copy.isnew())
302             delete this.copyCache[copy.id()];
303         this.copyTbody.removeChild(row);
304     }
305
306     this.saveCopyChanges = function(liId) {
307         var self = this;
308         var copies = [];
309
310         openils.Util.show('acq-lit-update-copies-progress');
311
312         for(var id in this.copyCache) {
313             var c = this.copyCache[id];
314             if(c.isnew() || c.ischanged() || c.isdeleted()) {
315                 if(c.id() < 0) c.id(null);
316                 copies.push(c);
317             }
318         }
319
320         if(copies.length == 0)
321             return;
322
323         fieldmapper.standardRequest(
324             ['open-ils.acq', 'open-ils.acq.lineitem_detail.cud.batch'],
325             {   async: true,
326                 params: [openils.User.authtoken, copies],
327                 onresponse: function(r) {
328                     var res = openils.Util.readResponse(r);
329                     litUpdateCopiesProgress.update(res);
330                 },
331                 oncomplete: function() {
332                     openils.Util.hide('acq-lit-update-copies-progress');
333                     self.drawCopies(liId); 
334                 }
335             }
336         );
337     }
338 }
339
340
341