]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/volume_copy_creator.js
fixed auto-focus bug for creating 1 volume
[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 (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 [ i + ' -> ' + o[i], o[i] ];
53                                                 }
54                                         }
55                                 ).sort(
56                                         function(b,a) {
57                                                 if (a == 82 && b == 92) return -1;
58                                                 if (a == 92 && b == 82) return 1;
59                                                 if (a == 82) return -1;
60                                                 if (a == 92) return -1;
61                                                 if (a < b) return -1;
62                                                 if (a > b) return 1;
63                                                 return 0;
64                                         }
65                                 )
66                         ); hbox.appendChild(ml);
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.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                         }
150                         } catch(E) {
151                                 alert(E);
152                         }
153                 }, 0
154         );
155 }
156
157 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
158         var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
159         grid.setAttribute('flex','1');
160         grid.setAttribute('ou_id',ou_id);
161         var rows = grid.lastChild;
162         var r = document.createElement('row'); rows.appendChild( r );
163         var x = document.createElement('label'); r.appendChild(x);
164         x.setAttribute('value','Call Numbers'); x.setAttribute('style','font-weight: bold');
165         x = document.createElement('label'); r.appendChild(x);
166         x.setAttribute('value','# of Copies'); x.setAttribute('style','font-weight: bold');
167
168         function handle_change(tb1,tb2,hb3) {
169                 if (tb1.value == '') return;
170                 if (isNaN( parseInt( tb2.value ) )) return;
171
172                 if (tb1.disabled || tb2.disabled) return;
173
174                 //tb1.disabled = true;
175                 //tb2.disabled = true;
176
177                 util.widgets.remove_children(hb3);
178
179                 g.render_barcode_entry(hb3,tb1.value,parseInt(tb2.value),ou_id);
180         }
181
182         function handle_change_tb1(ev) {
183                 var _tb1 = ev.target;   
184                 var _hb1 = _tb1.parentNode;
185                 var _hb2 = _hb1.nextSibling;
186                 var _tb2 = _hb2.firstChild;
187                 var _hb3 = _hb2.nextSibling;
188                 handle_change(_tb1,_tb2,_hb3);
189         }
190
191         function handle_change_tb2(ev) {
192                 var _tb2 = ev.target;   
193                 var _hb2 = _tb2.parentNode;
194                 var _hb1 = _hb2.previousSibling;
195                 var _tb1 = _hb1.firstChild;
196                 var _hb3 = _hb2.nextSibling;
197                 handle_change(_tb1,_tb2,_hb3);
198         }
199
200         for (var i = 0; i < count; i++) {
201                 var r = document.createElement('row'); rows.appendChild(r);
202                 var hb1 = document.createElement('vbox'); r.appendChild(hb1);
203                 var hb2 = document.createElement('vbox'); r.appendChild(hb2);
204                 var hb3 = document.createElement('vbox'); r.appendChild(hb3);
205                 var tb1 = document.createElement('textbox'); hb1.appendChild(tb1);
206                 tb1.setAttribute('rel_vert_pos','2');
207                 util.widgets.apply_vertical_tab_on_enter_handler( 
208                         tb1, 
209                         function() { handle_change_tb1({'target':tb1}); setTimeout(function(){util.widgets.vertical_tab(tb1);},0); }
210                 );
211                 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
212                 tb2.setAttribute('size','3'); tb2.setAttribute('cols','3');
213                 tb2.setAttribute('rel_vert_pos','3');
214                 util.widgets.apply_vertical_tab_on_enter_handler( 
215                         tb2, 
216                         function() { handle_change_tb2({'target':tb2}); setTimeout(function(){util.widgets.vertical_tab(tb2);},0); }
217                 );
218
219                 tb1.addEventListener( 'change', handle_change_tb1, false);
220                 tb1.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
221                 tb2.addEventListener( 'change', handle_change_tb2, false);
222                 tb2.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
223
224                 setTimeout(
225                         function(idx,tb){
226                                 return function() {
227                                         try {
228                                         JSAN.use('util.functional');
229                                         if (g.copy_shortcut) {
230                                                 var label = util.functional.map_object_to_list(
231                                                         g.copy_shortcut[ou_id],
232                                                         function(o,i) {
233                                                                 return i;
234                                                         }
235                                                 )[idx];
236                                                 tb.value = label; handle_change_tb1({'target':tb});
237                                         }
238                                         } catch(E) {
239                                                 alert(E);
240                                         }
241                                 }
242                         }(i,tb1),0
243                 );
244         }
245
246         return grid;
247 }
248
249 g.render_barcode_entry = function(node,callnumber,count,ou_id) {
250         try {
251                 function ready_to_create(ev) {
252                         document.getElementById("Create").disabled = false;
253                 }
254
255                 for (var i = 0; i < count; i++) {
256                         var tb = document.createElement('textbox'); node.appendChild(tb);
257                         tb.setAttribute('ou_id',ou_id);
258                         tb.setAttribute('callnumber',callnumber);
259                         tb.setAttribute('rel_vert_pos','4');
260                         util.widgets.apply_vertical_tab_on_enter_handler( 
261                                 tb, 
262                                 function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
263                         );
264                         tb.addEventListener('change',ready_to_create,false);
265                         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
266                 }
267         } catch(E) {
268                 g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
269         }
270 }
271
272 g.new_node_id = -1;
273
274 g.stash_and_close = function() {
275
276         try {
277
278                 var nl = document.getElementsByTagName('textbox');
279
280                 var volumes_hash = {};
281
282                 var barcodes = [];
283                 
284                 for (var i = 0; i < nl.length; i++) {
285                         if ( nl[i].getAttribute('rel_vert_pos') == 4 ) barcodes.push( nl[i] );
286                 };
287         
288                 for (var i = 0; i < barcodes.length; i++) {
289                         var ou_id = barcodes[i].getAttribute('ou_id');
290                         var callnumber = barcodes[i].getAttribute('callnumber');
291                         var barcode = barcodes[i].value;
292
293                         if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
294                         if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
295
296                         volumes_hash[ou_id][callnumber].push( barcode );
297                 }
298
299                 var volumes = [];
300                 var copies = [];
301                 var volume_labels = {};
302
303                 for (var ou_id in volumes_hash) {
304                         for (var cn in volumes_hash[ou_id]) {
305                                 var volume = new acn();
306                                 var acn_id;
307                                 if (!g.copy_shortcut) {
308                                         acn_id = g.new_node_id--;
309                                         volume.isnew('1');
310                                 } else {
311                                         acn_id = g.copy_shortcut[ou_id][cn];
312                                 }
313                                 volume.id( acn_id );
314                                 volume.record(g.doc_id);
315                                 volume.label(cn);
316                                 volume.owning_lib(ou_id);
317                                 volume.copies( [] );
318                                 volumes.push( volume );
319
320                                 volume_labels[ acn_id ] = cn;
321
322                                 for (var i = 0; i < volumes_hash[ou_id][cn].length; i++) {
323                                         var copy = new acp();
324                                         copy.id( g.new_node_id-- );
325                                         copy.isnew('1');
326                                         copy.barcode( volumes_hash[ou_id][cn][i] );
327                                         copy.call_number( acn_id );
328                                         copy.circ_lib(ou_id);
329                                         /* FIXME -- use constants */
330                                         copy.deposit(0);
331                                         copy.fine_level(2);
332                                         copy.loan_duration(2);
333                                         copy.location(1);
334                                         copy.status(0);
335                                         copies.push( copy );
336                                 }
337                         }
338                 }
339
340                 JSAN.use('util.window'); var win = new util.window();
341                 var w = win.open(
342                         urls.XUL_COPY_EDITOR
343                                 +'?copies='+window.escape(js2JSON(copies))
344                                 +'&callnumbers='+window.escape(js2JSON(volume_labels))
345                                 +'&edit=1',
346                         title,
347                         'chrome,modal,resizable'
348                 );
349                 /* FIXME -- need to unique the temp space, and not rely on modalness of window */
350                 g.data.stash_retrieve();
351                 copies = JSON2js( g.data.temp_copies );
352
353                 for (var i = 0; i < copies.length; i++) {
354                         var copy = copies[i];
355                         var volume = util.functional.find_id_object_in_list( volumes, copy.call_number() );
356                         var temp = volume.copies();
357                         temp.push( copy );
358                         volume.copies( temp );
359                 }
360
361                 try {
362                         var r = g.network.request(
363                                 api.FM_ACN_TREE_UPDATE.app,
364                                 api.FM_ACN_TREE_UPDATE.method,
365                                 [ ses(), volumes ]
366                         );
367                         if (typeof r.ilsevent != 'undefined') {
368                                 switch(r.ilsevent) {
369                                         case 1706 /* ITEM_BARCODE_EXISTS */ :
370                                                 alert('Some of these barcodes are or have been in use.  Please change them.');
371                                                 return;
372                                         break;
373                                         default: g.error.standard_unexpected_error_alert('volume tree update',r); break;
374                                 }
375                         } else {
376                                 JSAN.use('util.functional');
377                                 var w = win.open(
378                                         urls.XUL_SPINE_LABEL
379                                         + '?barcodes=' + window.escape( js2JSON( util.functional.map_list(copies,function(o){return o.barcode();}) ) ),
380                                         'spine_labels',
381                                         'chrome,modal,resizable,width=750,height=550'
382                                 );
383                         }
384                 } catch(E) {
385                         g.error.standard_unexpected_error_alert('volume tree update 2',E);
386                 }
387
388                 window.close();
389
390         } catch(E) {
391                 g.error.standard_unexpected_error_alert('volume tree update 3',E);
392         }
393 }
394
395