]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_buckets.js
fixes for bucket interfaces
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_buckets.js
1 dump('entering cat.copy_buckets.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_buckets = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8         JSAN.use('util.date');
9         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
10 }
11
12 cat.copy_buckets.prototype = {
13         'selection_list1' : [],
14         'selection_list2' : [],
15         'bucket_id_name_map' : {},
16
17         'render_pending_copies' : function() {
18                 var obj = this;
19                 obj.list1.clear();
20                 for (var i = 0; i < obj.copy_ids.length; i++) {
21                         var item = obj.flesh_item_for_list( obj.copy_ids[i] );
22                         if (item) obj.list1.append( item );
23                 }
24         },
25
26         'init' : function( params ) {
27
28                 var obj = this;
29
30                 obj.copy_ids = params['copy_ids'] || [];
31
32                 JSAN.use('circ.util');
33                 var columns = circ.util.columns( 
34                         { 
35                                 'barcode' : { 'hidden' : false },
36                                 'title' : { 'hidden' : false },
37                                 'location' : { 'hidden' : false },
38                                 'call_number' : { 'hidden' : false },
39                                 'status' : { 'hidden' : false },
40                         } 
41                 );
42
43                 JSAN.use('util.list'); 
44
45                 obj.list1 = new util.list('pending_copies_list');
46                 obj.list1.init(
47                         {
48                                 'columns' : columns,
49                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
50                                 'on_select' : function(ev) {
51                                         try {
52                                                 JSAN.use('util.functional');
53                                                 var sel = obj.list1.retrieve_selection();
54                                                 obj.selection_list1 = util.functional.map_list(
55                                                         sel,
56                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
57                                                 );
58                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 1 = ' + js2JSON(obj.selection_list1) );
59                                                 if (obj.selection_list1.length == 0) {
60                                                         obj.controller.view.copy_buckets_sel_add.disabled = true;
61                                                 } else {
62                                                         obj.controller.view.copy_buckets_sel_add.disabled = false;
63                                                 }
64                                         } catch(E) {
65                                                 alert('FIXME: ' + E);
66                                         }
67                                 },
68
69                         }
70                 );
71
72                 obj.render_pending_copies();
73         
74                 obj.list2 = new util.list('copies_in_bucket_list');
75                 obj.list2.init(
76                         {
77                                 'columns' : columns,
78                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
79                                 'on_select' : function(ev) {
80                                         try {
81                                                 JSAN.use('util.functional');
82                                                 var sel = obj.list2.retrieve_selection();
83                                                 obj.selection_list2 = util.functional.map_list(
84                                                         sel,
85                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
86                                                 );
87                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 2 = ' + js2JSON(obj.selection_list2) );
88                                                 if (obj.selection_list2.length == 0) {
89                                                         obj.controller.view.copy_buckets_delete_item.disabled = true;
90                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','true');
91                                                         obj.controller.view.copy_buckets_export.disabled = true;
92                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','true');
93                                                 } else {
94                                                         obj.controller.view.copy_buckets_delete_item.disabled = false;
95                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','false');
96                                                         obj.controller.view.copy_buckets_export.disabled = false;
97                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','false');
98                                                 }
99                                         } catch(E) {
100                                                 alert('FIXME: ' + E);
101                                         }
102                                 },
103                         }
104                 );
105                 
106                 JSAN.use('util.controller'); obj.controller = new util.controller();
107                 obj.controller.init(
108                         {
109                                 'control_map' : {
110                                         'copy_buckets_menulist_placeholder' : [
111                                                 ['render'],
112                                                 function(e) {
113                                                         return function() {
114                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
115                                                                 var items = [ ['Choose a bucket...',''] ].concat(
116                                                                         util.functional.map_list(
117                                                                                 obj.network.simple_request(
118                                                                                         'BUCKET_RETRIEVE_VIA_USER',
119                                                                                         [ ses(), obj.data.list.au[0].id() ]
120                                                                                 ).copy,
121                                                                                 function(o) {
122                                                                                         obj.bucket_id_name_map[ o.id() ] = o.name();
123                                                                                         return [ o.name(), o.id() ];
124                                                                                 }
125                                                                         )
126                                                                 );
127                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
128                                                                 util.widgets.remove_children( e );
129                                                                 var ml = util.widgets.make_menulist(
130                                                                         items
131                                                                 );
132                                                                 e.appendChild( ml );
133                                                                 ml.setAttribute('id','bucket_menulist');
134                                                                 ml.setAttribute('accesskey','');
135
136                                                                 function change_bucket(ev) {
137                                                                         var bucket_id = ev.target.value;
138                                                                         if (!bucket_id) return;
139                                                                         var bucket = obj.network.simple_request(
140                                                                                 'BUCKET_FLESH',
141                                                                                 [ ses(), 'copy', bucket_id ]
142                                                                         );
143                                                                         var items = bucket.items() || [];
144                                                                         obj.list2.clear();
145                                                                         for (var i = 0; i < items.length; i++) {
146                                                                                 var item = obj.flesh_item_for_list( 
147                                                                                         items[i].target_copy(),
148                                                                                         items[i].id()
149                                                                                 );
150                                                                                 if (item) obj.list2.append( item );
151                                                                         }
152                                                                 }
153
154                                                                 ml.addEventListener( 'change_bucket', change_bucket , false);
155                                                                 ml.addEventListener( 'command', function() {
156                                                                         JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
157                                                                 }, false);
158                                                                 obj.controller.view.bucket_menulist = ml;
159                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
160                                                         };
161                                                 },
162                                         ],
163
164                                         'copy_buckets_add' : [
165                                                 ['command'],
166                                                 function() {
167                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
168                                                         if (!bucket_id) return;
169                                                         for (var i = 0; i < obj.copy_ids.length; i++) {
170                                                                 var bucket_item = new ccbi();
171                                                                 bucket_item.isnew('1');
172                                                                 bucket_item.bucket(bucket_id);
173                                                                 bucket_item.target_copy( obj.copy_ids[i] );
174                                                                 try {
175                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
176                                                                                 [ ses(), 'copy', bucket_item ]);
177
178                                                                         if (typeof robj == 'object') throw robj;
179
180                                                                         var item = obj.flesh_item_for_list( obj.copy_ids[i], robj );
181                                                                         if (!item) continue;
182
183                                                                         obj.list2.append( item );
184                                                                 } catch(E) {
185                                                                         obj.error.standard_unexpected_error_alert('Addition likely failed.',E);
186                                                                 }
187                                                         }
188                                                 }
189                                         ],
190                                         'copy_buckets_sel_add' : [
191                                                 ['command'],
192                                                 function() {                                                        
193                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
194                                                         if (!bucket_id) return;
195                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
196                                                                 var acp_id = obj.selection_list1[i][0];
197                                                                 //var barcode = obj.selection_list1[i][1];
198                                                                 var bucket_item = new ccbi();
199                                                                 bucket_item.isnew('1');
200                                                                 bucket_item.bucket(bucket_id);
201                                                                 bucket_item.target_copy( acp_id );
202                                                                 try {
203                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
204                                                                                 [ ses(), 'copy', bucket_item ]);
205
206                                                                         if (typeof robj == 'object') throw robj;
207
208                                                                         var item = obj.flesh_item_for_list( acp_id, robj );
209                                                                         if (!item) continue;
210
211                                                                         obj.list2.append( item );
212                                                                 } catch(E) {
213                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
214                                                                 }
215                                                         }
216
217                                                 }
218                                         ],
219                                         'copy_buckets_export' : [
220                                                 ['command'],
221                                                 function() {                                                        
222                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
223                                                                 var acp_id = obj.selection_list2[i][0];
224                                                                 //var barcode = obj.selection_list1[i][1];
225                                                                 //var bucket_item_id = obj.selection_list1[i][2];
226                                                                 var item = obj.flesh_item_for_list( acp_id );
227                                                                 if (item) {
228                                                                         obj.list1.append( item );
229                                                                         obj.copy_ids.push( acp_id );
230                                                                 }
231                                                         }
232                                                 }
233                                         ],
234
235                                         'copy_buckets_delete_item' : [
236                                                 ['command'],
237                                                 function() {
238                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
239                                                                 try {
240                                                                         //var acp_id = obj.selection_list2[i][0];
241                                                                         //var barcode = obj.selection_list2[i][1];
242                                                                         var bucket_item_id = obj.selection_list2[i][2];
243                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
244                                                                                 [ ses(), 'copy', bucket_item_id ]);
245                                                                         if (typeof robj == 'object') throw robj;
246                                                                 } catch(E) {
247                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
248                                                                 }
249                                                         }
250                                                         setTimeout(
251                                                                 function() {
252                                                                         JSAN.use('util.widgets'); 
253                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
254                                                                 }, 0
255                                                         );
256                                                 }
257                                         ],
258                                         'copy_buckets_delete_bucket' : [
259                                                 ['command'],
260                                                 function() {
261                                                         try {
262                                                                 var bucket = obj.controller.view.bucket_menulist.value;
263                                                                 var name = obj.bucket_id_name_map[ bucket ];
264                                                                 var conf = window.confirm('Delete the bucket named ' + name + '?');
265                                                                 if (!conf) return;
266                                                                 obj.list2.clear();
267                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[ses(),'copy',bucket]);
268                                                                 if (typeof robj == 'object') throw robj;
269                                                                 obj.controller.render('copy_buckets_menulist_placeholder');
270                                                         } catch(E) {
271                                                                 obj.error.standard_unexpected_error_alert('Bucket deletion likely failed.',E);
272                                                         }
273                                                 }
274                                         ],
275                                         'copy_buckets_new_bucket' : [
276                                                 ['command'],
277                                                 function() {
278                                                         try {
279                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
280
281                                                                 if (name) {
282                                                                         var bucket = new ccb();
283                                                                         bucket.btype('staff_client');
284                                                                         bucket.owner( obj.data.list.au[0].id() );
285                                                                         bucket.name( name );
286
287                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[ses(),'copy',bucket]);
288
289                                                                         if (typeof robj == 'object') throw robj;
290
291                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
292                                                                         obj.controller.view.bucket_menulist.value = robj;
293                                                                         setTimeout(
294                                                                                 function() {
295                                                                                         JSAN.use('util.widgets'); 
296                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
297                                                                                 }, 0
298                                                                         );
299                                                                 }
300                                                         } catch(E) {
301                                                                 obj.error.standard_unexpected_error_alert('Bucket creation failed.',E);
302                                                         }
303                                                 }
304                                         ],
305                                         'copy_buckets_batch_copy_edit' : [
306                                                 ['command'],
307                                                 function() {
308                                                         try {
309                                                                 JSAN.use('util.functional');
310                                                                 JSAN.use('util.window'); var win = new util.window();
311                                                                 win.open(
312                                                                         urls.XUL_COPY_EDITOR 
313                                                                         + '?copy_ids=' + window.escape( js2JSON(
314                                                                                 util.functional.map_list(
315                                                                                         obj.list2.dump_retrieve_ids(),
316                                                                                         function (o) {
317                                                                                                 return JSON2js(o)[0]; // acp_id
318                                                                                         }
319                                                                                 )
320                                                                         ) )
321                                                                         + '&single_edit=1'
322                                                                         + '&handle_update=1',
323                                                                         'batch_copy_editor_win_' + win.window_name_increment(),
324                                                                         'chrome,resizable,modal'
325                                                                 );
326                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
327                                                                 setTimeout(
328                                                                         function() {
329                                                                                 JSAN.use('util.widgets'); 
330                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
331                                                                         }, 0
332                                                                 );
333                                                         } catch(E) {
334                                                                 alert( js2JSON(E) );
335                                                         }
336                                                 }
337                                         ],
338                                         'copy_buckets_transfer_to_volume' : [
339                                                 ['command'],
340                                                 function() {
341                                                         try {
342                                                                 // FM_ACN_RETRIEVE
343                                                                 obj.data.stash_retrieve();
344                                                                 if (!obj.data.marked_volume) {
345                                                                         alert('Please mark a volume as the destination from within the copy browser and then try this again.');
346                                                                         return;
347                                                                 }
348                                                                 var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
349                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
350                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
351                                                                 xml += '<description>Transfer the copies in bucket "';
352                                                                 xml += obj.controller.view.bucket_menulist.getAttribute('label') + '" ';
353                                                                 xml += 'from their original volumes to ';
354                                                                 xml += obj.data.hash.aou[ volume.owning_lib() ].shortname() + "'s volume labelled ";
355                                                                 xml += '"' + volume.label() + '" on the following record?</description>';
356                                                                 xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
357                                                                 xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
358                                                                 xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + volume.record() + '"/>';
359                                                                 xml += '</vbox>';
360                                                                 obj.data.temp_transfer = xml; obj.data.stash('temp_transfer');
361                                                                 window.open(
362                                                                         urls.XUL_FANCY_PROMPT
363                                                                         + '?xml_in_stash=temp_transfer'
364                                                                         + '&title=' + window.escape('Copy Transfer'),
365                                                                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300'
366                                                                 );
367                                                                 JSAN.use('OpenILS.data');
368                                                                 var data = new OpenILS.data(); data.init({'via':'stash'});
369                                                                 if (data.fancy_prompt_data == '') { alert('Transfer Aborted'); return; }
370
371                                                                 JSAN.use('util.functional');
372
373                                                                 var copies = obj.network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [
374                                                                         util.functional.map_list(
375                                                                                 obj.list2.dump_retrieve_ids(),
376                                                                                 function (o) {
377                                                                                         return JSON2js(o)[0]; // acp_id
378                                                                                 }
379                                                                         )
380                                                                 ]);
381
382                                                                 for (var i = 0; i < copies.length; i++) {
383                                                                         copies[i].call_number( obj.data.marked_volume );
384                                                                         copies[i].ischanged( 1 );
385                                                                 }
386
387                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE', [ ses(), copies ]);
388
389                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
390                                                                 setTimeout(
391                                                                         function() {
392                                                                                 JSAN.use('util.widgets'); 
393                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
394                                                                         }, 0
395                                                                 );
396                                                                 
397                                                                 if (typeof robj.ilsevent != 'undefined') {
398                                                                         throw(robj);
399                                                                 } else {
400                                                                         alert('Copies transferred.');
401                                                                 }
402
403                                                         } catch(E) {
404                                                                 obj.error.standard_unexpected_error_alert('Copies not likely transferred.',E);
405                                                         }
406                                                 }
407                                         ],
408                                         'cmd_broken' : [
409                                                 ['command'],
410                                                 function() { alert('Not Yet Implemented'); }
411                                         ],
412                                         'cmd_copy_buckets_print' : [
413                                                 ['command'],
414                                                 function() {
415                                                         dump( js2JSON( obj.list2.dump() ) );
416                                                         alert( js2JSON( obj.list2.dump() ) );
417                                                 }
418                                         ],
419                                         'cmd_copy_buckets_reprint' : [
420                                                 ['command'],
421                                                 function() {
422                                                 }
423                                         ],
424                                         'cmd_copy_buckets_done' : [
425                                                 ['command'],
426                                                 function() {
427                                                         window.close();
428                                                 }
429                                         ],
430                                         'cmd_export_to_copy_status' : [
431                                                 ['command'],
432                                                 function() {
433                                                         try {
434                                                                 JSAN.use('util.functional');
435                                                                 var barcodes = util.functional.map_list(
436                                                                         obj.list2.dump_retrieve_ids(),
437                                                                         function(o) { return JSON2js(o)[1]; }
438                                                                 );
439                                                                 var url = urls.XUL_COPY_STATUS + '?barcodes=' + window.escape( js2JSON(barcodes) );
440                                                                 xulG.new_tab( url, {}, {});
441                                                         } catch(E) {
442                                                                 obj.error.standard_unexpected_error_alert('Copy Status from Copy Buckets',E);
443                                                         }
444                                                 }
445                                         ],
446                                 }
447                         }
448                 );
449                 this.controller.render();
450
451                 if (typeof xulG == 'undefined') {
452                         obj.controller.view.cmd_export_to_copy_status.disabled = true;
453                         obj.controller.view.cmd_export_to_copy_status.setAttribute('disabled',true);
454                 } else {
455                         obj.controller.view.cmd_copy_buckets_done.disabled = true;
456                         obj.controller.view.cmd_copy_buckets_done.setAttribute('disabled',true);
457                 }
458         
459         },
460
461         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
462                 var obj = this;
463                 try {
464                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
465                         if (copy == null || typeof copy.ilsevent != 'undefined') {
466                                 throw(copy);
467                         } else {
468                                 var item = {
469                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
470                                         'row' : {
471                                                 'my' : {
472                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
473                                                         'acp' : copy,
474                                                 }
475                                         }
476                                 };
477                                 return item;
478                         }
479                 } catch(E) {
480                         obj.error.standard_unexpected_error_alert('List building failed.',E);
481                         return null;
482                 }
483
484         },
485         
486 }
487
488 dump('exiting cat.copy_buckets.js\n');