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