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