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