]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/volume_copy_creator.js
Change each tab to 4 spaces in the staff client javascript files.
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / volume_copy_creator.js
1 const g_max_copies_that_can_be_added_at_a_time_per_volume = 100;
2 var g = {};
3
4 function my_init() {
5     try {
6
7         /***********************************************************************************************************/
8         /* Initial setup */
9
10         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
11                 if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
12         JSAN.errorLevel = "die"; // none, warn, or die
13         JSAN.addRepository('/xul/server/');
14         JSAN.use('util.error'); g.error = new util.error();
15         g.error.sdump('D_TRACE','my_init() for cat/volume_copy_creator.xul');
16
17         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
18         JSAN.use('util.widgets'); JSAN.use('util.functional');
19
20         JSAN.use('util.network'); g.network = new util.network();
21
22         /***********************************************************************************************************/
23         /* What record am I dealing with?  Am I adding just copies or copies and volumes? */
24
25         g.doc_id = xul_param('doc_id');
26         var sb = document.getElementById('summary_box'); while(sb.firstChild) sb.removeChild(sb.lastChild);
27         var summary = document.createElement('iframe'); sb.appendChild(summary);
28         summary.setAttribute('src',urls.XUL_BIB_BRIEF);
29         summary.setAttribute('flex','1');
30         get_contentWindow(summary).xulG = { 'docid' : g.doc_id };
31
32         g.copy_shortcut = xul_param('copy_shortcut',{'JSON2js_if_cgi':true});
33         g.error.sdump('D_ERROR','location.href = ' + location.href + '\n\ncopy_short cut = ' + g.copy_shortcut + '\n\nou_ids = ' + xul_param('ou_ids'));
34
35         var ou_ids = xul_param('ou_ids',{'JSON2js_if_cgi' : true, 'concat' : true});;
36
37         /***********************************************************************************************************/
38         /* For the call number drop down */
39
40         var cn_blob;
41         try {
42             cn_blob = g.network.simple_request('BLOB_MARC_CALLNUMBERS_RETRIEVE',[g.doc_id]);
43         } catch(E) {
44             cn_blob = [];
45         }
46         if ((!g.copy_shortcut)) {
47             var hbox = document.getElementById('marc_cn');
48             var ml = util.widgets.make_menulist(
49                 util.functional.map_list(
50                     cn_blob,
51                     function(o) {
52                         for (var i in o) {
53                             return [ o[i], i ];
54                         }
55                     }
56                 ).sort(
57                     function(a,b) {
58                         a = a[1]; b = b[1];
59                         if (a == '082') return -1; 
60                         if (b == '082') return 1; 
61                         if (a == '092')  return -1; 
62                         if (b == '092')  return 1; 
63                         if (a < b) return -1; 
64                         if (a > b) return 1; 
65                         return 0;
66                     }
67                 )
68             ); hbox.appendChild(ml);
69             ml.setAttribute('editable','true');
70             ml.setAttribute('width', '200');
71             var btn = document.createElement('button');
72             btn.setAttribute('label',$('catStrings').getString('staff.cat.volume_copy_creator.my_init.btn.label'));
73             btn.setAttribute('accesskey','A');
74             btn.setAttribute('image','/xul/server/skin/media/images/down_arrow.gif');
75             hbox.appendChild(btn);
76             btn.addEventListener(
77                 'command',
78                 function() {
79                     var nl = document.getElementsByTagName('textbox');
80                     for (var i = 0; i < nl.length; i++) {
81                         if (nl[i].getAttribute('rel_vert_pos')==2 
82                             && !nl[i].disabled) nl[i].value = ml.value;
83                     }
84                     if (g.last_focus) setTimeout( function() { g.last_focus.focus(); }, 0 );
85                 }, 
86                 false
87             );
88         }
89
90         /***********************************************************************************************************/
91         /* render the orgs and volumes/input */
92
93         var rows = document.getElementById('rows');
94
95         var node_id = 0;
96         for (var i = 0; i < ou_ids.length; i++) {
97             try {
98                 var org = g.data.hash.aou[ ou_ids[i] ];
99                 if ( get_bool( g.data.hash.aout[ org.ou_type() ].can_have_vols() ) ) {
100                     var row = document.createElement('row'); rows.appendChild(row); row.setAttribute('ou_id',ou_ids[i]);
101                     g.render_library_label(row,ou_ids[i]);
102                     g.render_volume_count_entry(row,ou_ids[i]);
103                 }
104             } catch(E) {
105                 g.error.sdump('D_ERROR',E);
106             }
107         }
108
109         g.load_prefs();
110
111     } catch(E) {
112         var err_msg = $("commonStrings").getFormattedString('common.exception', ['cat/volume_copy_creator.js', E]);
113         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
114         alert(err_msg);
115     }
116 }
117
118 g.render_library_label = function(row,ou_id) {
119     var label = document.createElement('label'); row.appendChild(label);
120     label.setAttribute('ou_id',ou_id);
121     label.setAttribute('value',g.data.hash.aou[ ou_id ].shortname());
122 }
123
124 g.render_volume_count_entry = function(row,ou_id) {
125     var hb = document.createElement('vbox'); row.appendChild(hb);
126     var tb = document.createElement('textbox'); hb.appendChild(tb);
127     tb.setAttribute('ou_id',ou_id); tb.setAttribute('size','3'); tb.setAttribute('cols','3');
128     tb.setAttribute('rel_vert_pos','1'); 
129     if ( (!g.copy_shortcut) && (!g.last_focus) ) { tb.focus(); g.last_focus = tb; }
130     var node;
131     function render_copy_count_entry(ev) {
132         if (ev.target.disabled) return;
133         if (! isNaN( Number( ev.target.value) ) ) {
134             if ( Number( ev.target.value ) > g_max_copies_that_can_be_added_at_a_time_per_volume ) {
135                 g.error.yns_alert($("catStrings").getFormattedString('staff.cat.volume_copy_creator.render_volume_count_entry.message', [g_max_copies_that_can_be_added_at_a_time_per_volume]),
136                     $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.title'),
137                     $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.ok_label'),null,null,'');
138                 return;
139             }
140             if (node) { row.removeChild(node); node = null; }
141             //ev.target.disabled = true;
142             node = g.render_callnumber_copy_count_entry(row,ou_id,ev.target.value);
143         }
144     }
145     util.widgets.apply_vertical_tab_on_enter_handler( 
146         tb, 
147         function() { render_copy_count_entry({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
148     );
149     tb.addEventListener( 'change', render_copy_count_entry, false);
150     tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
151     setTimeout(
152         function() {
153             try {
154             if (g.copy_shortcut) {
155                 JSAN.use('util.functional');
156                 tb.value = util.functional.map_object_to_list(
157                     g.copy_shortcut[ou_id],
158                     function(o,i) {
159                         return g.copy_shortcut[ou_id][i];
160                     }
161                 ).length
162                 render_copy_count_entry({'target':tb});
163                 tb.disabled = true;
164             }
165             } catch(E) {
166                 alert(E);
167             }
168         }, 0
169     );
170 }
171
172 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
173     var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
174     grid.setAttribute('flex','1');
175     grid.setAttribute('ou_id',ou_id);
176     var rows = grid.lastChild;
177     var r = document.createElement('row'); rows.appendChild( r );
178     var x = document.createElement('label'); r.appendChild(x);
179     x.setAttribute('value', $("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.call_nums')); x.setAttribute('style','font-weight: bold');
180     x = document.createElement('label'); r.appendChild(x);
181     x.setAttribute('value',$("catStrings").getString('staff.cat.volume_copy_creator.render_callnumber_copy_count_entry.num_of_copies')); x.setAttribute('style','font-weight: bold');
182     x.setAttribute('size','3'); x.setAttribute('cols','3');
183
184
185     function handle_change(tb1,tb2,hb3) {
186         if (tb1.value == '') return;
187         if (isNaN( Number( tb2.value ) )) return;
188         if ( Number( tb2.value ) > g_max_copies_that_can_be_added_at_a_time_per_volume ) {
189             g.error.yns_alert($("catStrings").getFormattedString('staff.cat.volume_copy_creator.render_volume_count_entry.message', [g_max_copies_that_can_be_added_at_a_time_per_volume]),
190                 $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.title'),
191                 $("catStrings").getString('staff.cat.volume_copy_creator.render_volume_count_entry.ok_label'),null,null,'');
192             return;
193         }
194
195         //if (tb1.disabled || tb2.disabled) return;
196
197         //tb1.disabled = true;
198         //tb2.disabled = true;
199
200         util.widgets.remove_children(hb3);
201
202         g.render_barcode_entry(hb3,tb1.value,Number(tb2.value),ou_id);
203         document.getElementById("Create").disabled = false;
204     }
205
206     function handle_change_tb1(ev) {
207         var _tb1 = ev.target;    
208         var _hb1 = _tb1.parentNode;
209         var _hb2 = _hb1.nextSibling;
210         var _tb2 = _hb2.firstChild;
211         var _hb3 = _hb2.nextSibling;
212         handle_change(_tb1,_tb2,_hb3);
213     }
214
215     function handle_change_tb2(ev) {
216         var _tb2 = ev.target;    
217         var _hb2 = _tb2.parentNode;
218         var _hb1 = _hb2.previousSibling;
219         var _tb1 = _hb1.firstChild;
220         var _hb3 = _hb2.nextSibling;
221         handle_change(_tb1,_tb2,_hb3);
222     }
223
224     for (var i = 0; i < count; i++) {
225         var r = document.createElement('row'); rows.appendChild(r);
226         var hb1 = document.createElement('vbox'); r.appendChild(hb1);
227         var hb2 = document.createElement('vbox'); r.appendChild(hb2);
228         var hb3 = document.createElement('vbox'); r.appendChild(hb3);
229         var tb1 = document.createElement('textbox'); hb1.appendChild(tb1);
230         tb1.setAttribute('rel_vert_pos','2');
231         tb1.setAttribute('ou_id',ou_id);
232         util.widgets.apply_vertical_tab_on_enter_handler( 
233             tb1, 
234             function() { handle_change_tb1({'target':tb1}); setTimeout(function(){util.widgets.vertical_tab(tb1);},0); }
235         );
236         var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
237         tb2.setAttribute('size','3'); tb2.setAttribute('cols','3');
238         tb2.setAttribute('rel_vert_pos','3');
239         tb2.setAttribute('ou_id',ou_id);
240         util.widgets.apply_vertical_tab_on_enter_handler( 
241             tb2, 
242             function() { handle_change_tb2({'target':tb2}); setTimeout(function(){util.widgets.vertical_tab(tb2);},0); }
243         );
244
245         tb1.addEventListener( 'change', handle_change_tb1, false);
246         tb1.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
247         tb2.addEventListener( 'change', handle_change_tb2, false);
248         tb2.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
249         if ( !g.last_focus ) { tb2.focus(); g.last_focus = tb2; }
250
251         setTimeout(
252             function(idx,tb){
253                 return function() {
254                     try {
255                     JSAN.use('util.functional');
256                     if (g.copy_shortcut) {
257                         var label = util.functional.map_object_to_list(
258                             g.copy_shortcut[ou_id],
259                             function(o,i) {
260                                 return i;
261                             }
262                         )[idx];
263                         tb.value = label; handle_change_tb1({'target':tb});
264                         tb.disabled = true;
265                     }
266                     } catch(E) {
267                         alert(E);
268                     }
269                 }
270             }(i,tb1),0
271         );
272     }
273
274     return grid;
275 }
276
277 g.render_barcode_entry = function(node,callnumber,count,ou_id) {
278     try {
279         function ready_to_create(ev) {
280             document.getElementById("Create").disabled = false;
281         }
282
283         JSAN.use('util.barcode'); 
284
285         for (var i = 0; i < count; i++) {
286             var tb = document.createElement('textbox'); node.appendChild(tb);
287             tb.setAttribute('ou_id',ou_id);
288             tb.setAttribute('callnumber',callnumber);
289             tb.setAttribute('rel_vert_pos','4');
290             util.widgets.apply_vertical_tab_on_enter_handler( 
291                 tb, 
292                 function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
293             );
294             //tb.addEventListener('change',ready_to_create,false);
295             tb.addEventListener('change', function(ev) {
296                 var barcode = String( ev.target.value ).replace(/\s/g,'');
297                 if (barcode != ev.target.value) ev.target.value = barcode;
298                 if ($('check_barcodes').checked && ! util.barcode.check(barcode) ) {
299                     g.error.yns_alert($("catStrings").getFormattedString('staff.cat.volume_copy_creator.render_barcode_entry.alert_message', [barcode]),
300                         $("catStrings").getString('staff.cat.volume_copy_creator.render_barcode_entry.alert_title'),
301                         $("catStrings").getString('staff.cat.volume_copy_creator.render_barcode_entry.alert_ok_button'),null,null,
302                         $("catStrings").getString('staff.cat.volume_copy_creator.render_barcode_entry.alert_confirm'));
303                     setTimeout( function() { ev.target.select(); ev.target.focus(); }, 0);
304                 }
305             }, false);
306             tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
307         }
308     } catch(E) {
309         g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
310     }
311 }
312
313 g.new_node_id = -1;
314
315 g.stash_and_close = function() {
316
317     try {
318
319         var nl = document.getElementsByTagName('textbox');
320
321         var volumes_hash = {};
322
323         var barcodes = [];
324         
325         for (var i = 0; i < nl.length; i++) {
326             if ( nl[i].getAttribute('rel_vert_pos') == 4 ) barcodes.push( nl[i] );
327             if ( nl[i].getAttribute('rel_vert_pos') == 2 )  {
328                 var ou_id = nl[i].getAttribute('ou_id');
329                 var callnumber = nl[i].value;
330                 if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
331                 if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
332             }
333         };
334     
335         for (var i = 0; i < barcodes.length; i++) {
336             var ou_id = barcodes[i].getAttribute('ou_id');
337             var callnumber = barcodes[i].getAttribute('callnumber');
338             var barcode = barcodes[i].value;
339
340             if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
341             if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
342
343             if (barcode != '') volumes_hash[ou_id][callnumber].push( barcode );
344         }
345
346         var volumes = [];
347         var copies = [];
348         var volume_labels = {};
349
350         for (var ou_id in volumes_hash) {
351             for (var cn in volumes_hash[ou_id]) {
352
353                 var acn_id = g.network.simple_request(
354                     'FM_ACN_FIND_OR_CREATE',
355                     [ ses(), cn, g.doc_id, ou_id ]
356                 );
357
358                 if (typeof acn_id.ilsevent != 'undefined') {
359                     g.error.standard_unexpected_error_alert($("catStrings").getFormattedString('staff.cat.volume_copy_creator.stash_and_close.problem_with_volume', [cn]), acn_id);
360                     continue;
361                 }
362
363                 volume_labels[ acn_id ] = { 'label' : cn, 'owning_lib' : ou_id };
364
365                 for (var i = 0; i < volumes_hash[ou_id][cn].length; i++) {
366                     var copy = new acp();
367                     copy.id( g.new_node_id-- );
368                     copy.isnew('1');
369                     copy.barcode( volumes_hash[ou_id][cn][i] );
370                     copy.call_number( acn_id );
371                     copy.circ_lib(ou_id);
372                     /* FIXME -- use constants */
373                     copy.deposit(0);
374                     copy.price(0);
375                     copy.deposit_amount(0);
376                     copy.fine_level(2);
377                     copy.loan_duration(2);
378                     copy.location(1);
379                     copy.status(0);
380                     copy.circulate(get_db_true());
381                     copy.holdable(get_db_true());
382                     copy.opac_visible(get_db_true());
383                     copy.ref(get_db_false());
384                     copies.push( copy );
385                 }
386             }
387         }
388
389         JSAN.use('util.window'); var win = new util.window();
390         if (copies.length > 0) {
391             JSAN.use('cat.util');
392             copies = cat.util.spawn_copy_editor( { 'edit' : 1, 'docid' : g.doc_id, 'copies' : copies });
393             try {
394                 //case 1706 /* ITEM_BARCODE_EXISTS */ :
395                 if (copies && copies.length > 0 && $('print_labels').checked) {
396                     JSAN.use('util.functional');
397                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
398                     data.temp_barcodes_for_labels = util.functional.map_list( copies, function(o){return o.barcode();}) ; 
399                     data.stash('temp_barcodes_for_labels');
400                     var w = win.open(
401                         urls.XUL_SPINE_LABEL,
402                         'spine_labels',
403                         'chrome,resizable,width=750,height=550'
404                     );
405                 }
406             } catch(E) {
407                 g.error.standard_unexpected_error_alert($(catStrings).getString('staff.cat.volume_copy_creator.stash_and_close.tree_err2'),E);
408             }
409     }
410
411         if (typeof window.refresh == 'function') window.refresh();
412
413         window.close();
414
415     } catch(E) {
416         g.error.standard_unexpected_error_alert($(catStrings).getString('staff.cat.volume_copy_creator.stash_and_close.tree_err3'),E);
417     }
418 }
419
420 g.load_prefs = function() {
421     try {
422         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
423         JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
424         if (file._file.exists()) {
425             var prefs = file.get_object(); file.close();
426             if (prefs.check_barcodes) {
427                 if ( prefs.check_barcodes == 'false' ) {
428                     $('check_barcodes').checked = false;
429                 } else {
430                     $('check_barcodes').checked = prefs.check_barcodes;
431                 }
432             } else {
433                 $('check_barcodes').checked = false;
434             }
435             if (prefs.print_labels) {
436                 if ( prefs.print_labels == 'false' ) {
437                     $('print_labels').checked = false;
438                 } else {
439                     $('print_labels').checked = prefs.print_labels;
440                 }
441             } else {
442                 $('print_labels').checked = false;
443             }
444
445         }
446     } catch(E) {
447         g.error.standard_unexpected_error_alert($(catStrings).getString('staff.cat.volume_copy_creator.load_prefs.err_retrieving_prefs'),E);
448         
449     }
450 }
451
452 g.save_prefs = function () {
453     try {
454         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
455         JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
456         file.set_object(
457             {
458                 'check_barcodes' : $('check_barcodes').checked,
459                 'print_labels' : $('print_labels').checked,
460             }
461         );
462         file.close();
463     } catch(E) {
464         g.error.standard_unexpected_error_alert($(catStrings).getString('staff.cat.volume_copy_creator.save_prefs.err_storing_prefs'),E);
465     }
466 }
467
468