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