]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/volume_copy_creator.js
put bib summary in volume create and copy editor, and get rid of maximized
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / volume_copy_creator.js
1 function my_init() {
2         try {
3
4                 /***********************************************************************************************************/
5                 /* Initial setup */
6
7                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
8                                 if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); }
9                 JSAN.errorLevel = "die"; // none, warn, or die
10                 JSAN.addRepository('/xul/server/');
11                 JSAN.use('util.error'); g.error = new util.error();
12                 g.error.sdump('D_TRACE','my_init() for cat/volume_copy_creator.xul');
13
14                 JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
15                 JSAN.use('util.widgets'); JSAN.use('util.functional');
16
17                 JSAN.use('util.network'); g.network = new util.network();
18
19                 g.cgi = new CGI();
20
21                 /***********************************************************************************************************/
22                 /* What record am I dealing with?  Am I adding just copies or copies and volumes? */
23
24                 g.doc_id = g.cgi.param('doc_id');
25                 document.getElementById('summary').setAttribute('src',urls.XUL_BIB_BRIEF + '?docid=' + window.escape(g.doc_id));
26
27                 g.copy_shortcut = g.cgi.param('copy_shortcut');
28                 g.error.sdump('D_ERROR','location.href = ' + location.href + '\n\ncopy_short cut = ' + g.copy_shortcut + '\n\nou_ids = ' + g.cgi.param('ou_ids'));
29                 if (g.copy_shortcut) g.copy_shortcut = JSON2js( g.copy_shortcut );
30
31                 var ou_ids = [];
32                 if (g.cgi.param('ou_ids')) 
33                         ou_ids = JSON2js( g.cgi.param('ou_ids') );
34                 if (!ou_ids) ou_ids = [];
35                 if (window.xulG && window.xulG.ou_ids) 
36                         ou_ids = ou_ids.concat( window.xulG.ou_ids );
37
38                 /***********************************************************************************************************/
39                 /* For the call number drop down */
40
41                 var cn_blob;
42                 try {
43                         cn_blob = g.network.simple_request('BLOB_MARC_CALLNUMBERS_RETRIEVE',[g.doc_id]);
44                 } catch(E) {
45                         cn_blob = [];
46                 }
47                 if ((!g.copy_shortcut)) {
48                         var hbox = document.getElementById('marc_cn');
49                         var ml = util.widgets.make_menulist(
50                                 util.functional.map_list(
51                                         cn_blob,
52                                         function(o) {
53                                                 for (var i in o) {
54                                                         return [ o[i], i ];
55                                                 }
56                                         }
57                                 ).sort(
58                                         function(a,b) {
59                                                 a = a[1]; b = b[1];
60                                                 if (a == '082' || b == '082') return -1; 
61                                                 if (a == '092' || b == '092') return -1; 
62                                                 if (a < b) return -1; 
63                                                 if (a > b) return 1; 
64                                                 return 0;
65                                         }
66                                 ).reverse()
67                         ); hbox.appendChild(ml);
68                         ml.setAttribute('editable','true');
69                         ml.setAttribute('width', '200');
70                         var btn = document.createElement('button');
71                         btn.setAttribute('label','Apply');
72                         btn.setAttribute('accesskey','A');
73                         btn.setAttribute('image','/xul/server/skin/media/images/down_arrow.gif');
74                         hbox.appendChild(btn);
75                         btn.addEventListener(
76                                 'command',
77                                 function() {
78                                         var nl = document.getElementsByTagName('textbox');
79                                         for (var i = 0; i < nl.length; i++) {
80                                                 if (nl[i].getAttribute('rel_vert_pos')==2 
81                                                         && !nl[i].disabled) nl[i].value = ml.value;
82                                         }
83                                         if (g.last_focus) setTimeout( function() { g.last_focus.focus(); }, 0 );
84                                 }, 
85                                 false
86                         );
87                 }
88
89                 /***********************************************************************************************************/
90                 /* render the orgs and volumes/input */
91
92                 var rows = document.getElementById('rows');
93
94                 var node_id = 0;
95                 for (var i = 0; i < ou_ids.length; i++) {
96                         try {
97                                 var row = document.createElement('row'); rows.appendChild(row); row.setAttribute('ou_id',ou_ids[i]);
98                                 g.render_library_label(row,ou_ids[i]);
99                                 g.render_volume_count_entry(row,ou_ids[i]);
100                         } catch(E) {
101                                 g.error.sdump('D_ERROR',E);
102                         }
103                 }
104
105         } catch(E) {
106                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
107                         "system administrator or software developer the following:\ncat/volume_copy_creator.xul\n" +E+ '\n';
108                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
109                 alert(err_msg);
110         }
111 }
112
113 g.render_library_label = function(row,ou_id) {
114         var label = document.createElement('label'); row.appendChild(label);
115         label.setAttribute('ou_id',ou_id);
116         label.setAttribute('value',g.data.hash.aou[ ou_id ].shortname());
117 }
118
119 g.render_volume_count_entry = function(row,ou_id) {
120         var hb = document.createElement('vbox'); row.appendChild(hb);
121         var tb = document.createElement('textbox'); hb.appendChild(tb);
122         tb.setAttribute('ou_id',ou_id); tb.setAttribute('size','3'); tb.setAttribute('cols','3');
123         tb.setAttribute('rel_vert_pos','1'); 
124         if ( (!g.copy_shortcut) && (!g.last_focus) ) { tb.focus(); g.last_focus = tb; }
125         var node;
126         function render_copy_count_entry(ev) {
127                 if (ev.target.disabled) return;
128                 if (! isNaN( Number( ev.target.value) ) ) {
129                         if (node) { row.removeChild(node); node = null; }
130                         //ev.target.disabled = true;
131                         node = g.render_callnumber_copy_count_entry(row,ou_id,ev.target.value);
132                 }
133         }
134         util.widgets.apply_vertical_tab_on_enter_handler( 
135                 tb, 
136                 function() { render_copy_count_entry({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
137         );
138         tb.addEventListener( 'change', render_copy_count_entry, false);
139         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
140         setTimeout(
141                 function() {
142                         try {
143                         if (g.copy_shortcut) {
144                                 JSAN.use('util.functional');
145                                 tb.value = util.functional.map_object_to_list(
146                                         g.copy_shortcut[ou_id],
147                                         function(o,i) {
148                                                 return g.copy_shortcut[ou_id][i];
149                                         }
150                                 ).length
151                                 render_copy_count_entry({'target':tb});
152                                 tb.disabled = true;
153                         }
154                         } catch(E) {
155                                 alert(E);
156                         }
157                 }, 0
158         );
159 }
160
161 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
162         var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
163         grid.setAttribute('flex','1');
164         grid.setAttribute('ou_id',ou_id);
165         var rows = grid.lastChild;
166         var r = document.createElement('row'); rows.appendChild( r );
167         var x = document.createElement('label'); r.appendChild(x);
168         x.setAttribute('value','Call Numbers'); x.setAttribute('style','font-weight: bold');
169         x = document.createElement('label'); r.appendChild(x);
170         x.setAttribute('value','# of Copies'); x.setAttribute('style','font-weight: bold');
171
172         function handle_change(tb1,tb2,hb3) {
173                 if (tb1.value == '') return;
174                 if (isNaN( Number( tb2.value ) )) return;
175
176                 //if (tb1.disabled || tb2.disabled) return;
177
178                 //tb1.disabled = true;
179                 //tb2.disabled = true;
180
181                 util.widgets.remove_children(hb3);
182
183                 g.render_barcode_entry(hb3,tb1.value,Number(tb2.value),ou_id);
184                 document.getElementById("Create").disabled = false;
185         }
186
187         function handle_change_tb1(ev) {
188                 var _tb1 = ev.target;   
189                 var _hb1 = _tb1.parentNode;
190                 var _hb2 = _hb1.nextSibling;
191                 var _tb2 = _hb2.firstChild;
192                 var _hb3 = _hb2.nextSibling;
193                 handle_change(_tb1,_tb2,_hb3);
194         }
195
196         function handle_change_tb2(ev) {
197                 var _tb2 = ev.target;   
198                 var _hb2 = _tb2.parentNode;
199                 var _hb1 = _hb2.previousSibling;
200                 var _tb1 = _hb1.firstChild;
201                 var _hb3 = _hb2.nextSibling;
202                 handle_change(_tb1,_tb2,_hb3);
203         }
204
205         for (var i = 0; i < count; i++) {
206                 var r = document.createElement('row'); rows.appendChild(r);
207                 var hb1 = document.createElement('vbox'); r.appendChild(hb1);
208                 var hb2 = document.createElement('vbox'); r.appendChild(hb2);
209                 var hb3 = document.createElement('vbox'); r.appendChild(hb3);
210                 var tb1 = document.createElement('textbox'); hb1.appendChild(tb1);
211                 tb1.setAttribute('rel_vert_pos','2');
212                 tb1.setAttribute('ou_id',ou_id);
213                 util.widgets.apply_vertical_tab_on_enter_handler( 
214                         tb1, 
215                         function() { handle_change_tb1({'target':tb1}); setTimeout(function(){util.widgets.vertical_tab(tb1);},0); }
216                 );
217                 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
218                 tb2.setAttribute('size','3'); tb2.setAttribute('cols','3');
219                 tb2.setAttribute('rel_vert_pos','3');
220                 tb2.setAttribute('ou_id',ou_id);
221                 util.widgets.apply_vertical_tab_on_enter_handler( 
222                         tb2, 
223                         function() { handle_change_tb2({'target':tb2}); setTimeout(function(){util.widgets.vertical_tab(tb2);},0); }
224                 );
225
226                 tb1.addEventListener( 'change', handle_change_tb1, false);
227                 tb1.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
228                 tb2.addEventListener( 'change', handle_change_tb2, false);
229                 tb2.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
230                 if ( !g.last_focus ) { tb2.focus(); g.last_focus = tb2; }
231
232                 setTimeout(
233                         function(idx,tb){
234                                 return function() {
235                                         try {
236                                         JSAN.use('util.functional');
237                                         if (g.copy_shortcut) {
238                                                 var label = util.functional.map_object_to_list(
239                                                         g.copy_shortcut[ou_id],
240                                                         function(o,i) {
241                                                                 return i;
242                                                         }
243                                                 )[idx];
244                                                 tb.value = label; handle_change_tb1({'target':tb});
245                                                 tb.disabled = true;
246                                         }
247                                         } catch(E) {
248                                                 alert(E);
249                                         }
250                                 }
251                         }(i,tb1),0
252                 );
253         }
254
255         return grid;
256 }
257
258 g.render_barcode_entry = function(node,callnumber,count,ou_id) {
259         try {
260                 function ready_to_create(ev) {
261                         document.getElementById("Create").disabled = false;
262                 }
263
264                 JSAN.use('util.barcode'); 
265
266                 for (var i = 0; i < count; i++) {
267                         var tb = document.createElement('textbox'); node.appendChild(tb);
268                         tb.setAttribute('ou_id',ou_id);
269                         tb.setAttribute('callnumber',callnumber);
270                         tb.setAttribute('rel_vert_pos','4');
271                         util.widgets.apply_vertical_tab_on_enter_handler( 
272                                 tb, 
273                                 function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
274                         );
275                         //tb.addEventListener('change',ready_to_create,false);
276                         tb.addEventListener('change', function(ev) {
277                                 var barcode = ev.target.value;
278                                 if ($('check_barcodes').checked && ! util.barcode.check(barcode) ) {
279                                         g.error.yns_alert( '"' + barcode + '" is an invalid barcode.','Invalid Barcode','OK',null,null,'Check here to confirm this message.');
280                                         setTimeout( function() { ev.target.select(); ev.target.focus(); }, 0);
281                                 }
282                         }, false);
283                         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
284                 }
285         } catch(E) {
286                 g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
287         }
288 }
289
290 g.new_node_id = -1;
291
292 g.stash_and_close = function() {
293
294         try {
295
296                 var nl = document.getElementsByTagName('textbox');
297
298                 var volumes_hash = {};
299
300                 var barcodes = [];
301                 
302                 for (var i = 0; i < nl.length; i++) {
303                         if ( nl[i].getAttribute('rel_vert_pos') == 4 ) barcodes.push( nl[i] );
304                         if ( nl[i].getAttribute('rel_vert_pos') == 2 )  {
305                                 var ou_id = nl[i].getAttribute('ou_id');
306                                 var callnumber = nl[i].value;
307                                 if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
308                                 if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
309                         }
310                 };
311         
312                 for (var i = 0; i < barcodes.length; i++) {
313                         var ou_id = barcodes[i].getAttribute('ou_id');
314                         var callnumber = barcodes[i].getAttribute('callnumber');
315                         var barcode = barcodes[i].value;
316
317                         if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
318                         if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
319
320                         if (barcode != '') volumes_hash[ou_id][callnumber].push( barcode );
321                 }
322
323                 var volumes = [];
324                 var copies = [];
325                 var volume_labels = {};
326
327                 for (var ou_id in volumes_hash) {
328                         for (var cn in volumes_hash[ou_id]) {
329                                 var volume = new acn();
330                                 var acn_id;
331                                 if (!g.copy_shortcut) {
332                                         acn_id = g.new_node_id--;
333                                         volume.isnew('1');
334                                 } else {
335                                         acn_id = g.copy_shortcut[ou_id][cn];
336                                 }
337                                 volume.id( acn_id );
338                                 volume.record(g.doc_id);
339                                 volume.label(cn);
340                                 volume.owning_lib(ou_id);
341                                 volume.copies( [] );
342                                 volumes.push( volume );
343
344                                 volume_labels[ acn_id ] = { 'label' : cn, 'owning_lib' : ou_id };
345
346                                 for (var i = 0; i < volumes_hash[ou_id][cn].length; i++) {
347                                         var copy = new acp();
348                                         copy.id( g.new_node_id-- );
349                                         copy.isnew('1');
350                                         copy.barcode( volumes_hash[ou_id][cn][i] );
351                                         copy.call_number( acn_id );
352                                         copy.circ_lib(ou_id);
353                                         /* FIXME -- use constants */
354                                         copy.deposit(0);
355                                         copy.fine_level(2);
356                                         copy.loan_duration(2);
357                                         copy.location(1);
358                                         copy.status(0);
359                                         copies.push( copy );
360                                 }
361                         }
362                 }
363
364                 JSAN.use('util.window'); var win = new util.window();
365                 if (copies.length > 0) {
366                         g.data.temp_copies = js2JSON(copies); g.data.stash('temp_copies');
367                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
368                         g.data.temp_callnumbers = js2JSON(volume_labels); g.data.stash('temp_callnumbers');
369                         var w = win.open(
370                                 urls.XUL_COPY_EDITOR
371                                         +'?edit=1&docid='+window.escape(g.doc_id),
372                                 title,
373                                 'chrome,modal,resizable'
374                         );
375                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
376                         g.data.stash_retrieve();
377                         copies = JSON2js( g.data.temp_copies );
378                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
379                         g.data.temp_copies = undefined; g.data.stash('temp_copies');
380                         g.data.temp_callnumbers = undefined; g.data.stash('temp_callnumbers');
381                         if (!copies) {
382                                 alert('Items were not created.');
383                                 return;
384                         }
385                 }
386
387                 for (var i = 0; i < copies.length; i++) {
388                         var copy = copies[i];
389                         var volume = util.functional.find_id_object_in_list( volumes, copy.call_number() );
390                         var temp = volume.copies();
391                         temp.push( copy );
392                         volume.copies( temp );
393                 }
394
395                 try {
396                         var r = g.network.request(
397                                 api.FM_ACN_TREE_UPDATE.app,
398                                 api.FM_ACN_TREE_UPDATE.method,
399                                 [ ses(), volumes, true ]
400                         );
401                         if (typeof r.ilsevent != 'undefined') {
402                                 switch(r.ilsevent) {
403                                         case 1706 /* ITEM_BARCODE_EXISTS */ :
404                                                 alert('Some of these barcodes are or have been in use.  Please change them.');
405                                                 return;
406                                         break;
407                                         default: g.error.standard_unexpected_error_alert('volume tree update',r); break;
408                                 }
409                         } else {
410                                 if (copies.length > 0 && $('print_labels').checked) {
411                                         JSAN.use('util.functional');
412                                         var w = win.open(
413                                                 urls.XUL_SPINE_LABEL
414                                                 + '?barcodes=' + window.escape( js2JSON( util.functional.map_list(copies,function(o){return o.barcode();}) ) ),
415                                                 'spine_labels',
416                                                 'chrome,modal,resizable,width=750,height=550'
417                                         );
418                                 }
419                         }
420                 } catch(E) {
421                         g.error.standard_unexpected_error_alert('volume tree update 2',E);
422                 }
423
424                 window.close();
425
426         } catch(E) {
427                 g.error.standard_unexpected_error_alert('volume tree update 3',E);
428         }
429 }
430
431