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