]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_buckets.js
refactor
[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('cat.util');
310                                                                 cat.util.spawn_copy_editor(
311                                                                         util.functional.map_list(
312                                                                                 obj.list2.dump_retrieve_ids(),
313                                                                                 function (o) {
314                                                                                         return JSON2js(o)[0]; // acp_id
315                                                                                 }
316                                                                         ),
317                                                                         true
318                                                                 );
319                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
320                                                                 setTimeout(
321                                                                         function() {
322                                                                                 JSAN.use('util.widgets'); 
323                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
324                                                                         }, 0
325                                                                 );
326                                                         } catch(E) {
327                                                                 alert( js2JSON(E) );
328                                                         }
329                                                 }
330                                         ],
331                                         'copy_buckets_transfer_to_volume' : [
332                                                 ['command'],
333                                                 function() {
334                                                         try {
335                                                                 // FM_ACN_RETRIEVE
336                                                                 obj.data.stash_retrieve();
337                                                                 if (!obj.data.marked_volume) {
338                                                                         alert('Please mark a volume as the destination from within the copy browser and then try this again.');
339                                                                         return;
340                                                                 }
341                                                                 var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
342                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
343                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
344                                                                 xml += '<description>Transfer the copies in bucket "';
345                                                                 xml += obj.controller.view.bucket_menulist.getAttribute('label') + '" ';
346                                                                 xml += 'from their original volumes to ';
347                                                                 xml += obj.data.hash.aou[ volume.owning_lib() ].shortname() + "'s volume labelled ";
348                                                                 xml += '"' + volume.label() + '" on the following record?</description>';
349                                                                 xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
350                                                                 xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
351                                                                 xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + volume.record() + '"/>';
352                                                                 xml += '</vbox>';
353                                                                 obj.data.temp_transfer = xml; obj.data.stash('temp_transfer');
354                                                                 window.open(
355                                                                         urls.XUL_FANCY_PROMPT
356                                                                         + '?xml_in_stash=temp_transfer'
357                                                                         + '&title=' + window.escape('Copy Transfer'),
358                                                                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300'
359                                                                 );
360                                                                 JSAN.use('OpenILS.data');
361                                                                 var data = new OpenILS.data(); data.init({'via':'stash'});
362                                                                 if (data.fancy_prompt_data == '') { alert('Transfer Aborted'); return; }
363
364                                                                 JSAN.use('util.functional');
365
366                                                                 var copies = obj.network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [
367                                                                         util.functional.map_list(
368                                                                                 obj.list2.dump_retrieve_ids(),
369                                                                                 function (o) {
370                                                                                         return JSON2js(o)[0]; // acp_id
371                                                                                 }
372                                                                         )
373                                                                 ]);
374
375                                                                 for (var i = 0; i < copies.length; i++) {
376                                                                         copies[i].call_number( obj.data.marked_volume );
377                                                                         copies[i].ischanged( 1 );
378                                                                 }
379
380                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE', [ ses(), copies ]);
381
382                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
383                                                                 setTimeout(
384                                                                         function() {
385                                                                                 JSAN.use('util.widgets'); 
386                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
387                                                                         }, 0
388                                                                 );
389                                                                 
390                                                                 if (typeof robj.ilsevent != 'undefined') {
391                                                                         throw(robj);
392                                                                 } else {
393                                                                         alert('Copies transferred.');
394                                                                 }
395
396                                                         } catch(E) {
397                                                                 obj.error.standard_unexpected_error_alert('Copies not likely transferred.',E);
398                                                         }
399                                                 }
400                                         ],
401                                         'cmd_broken' : [
402                                                 ['command'],
403                                                 function() { alert('Not Yet Implemented'); }
404                                         ],
405                                         'cmd_copy_buckets_print' : [
406                                                 ['command'],
407                                                 function() {
408                                                         dump( js2JSON( obj.list2.dump() ) );
409                                                         alert( js2JSON( obj.list2.dump() ) );
410                                                 }
411                                         ],
412                                         'cmd_copy_buckets_reprint' : [
413                                                 ['command'],
414                                                 function() {
415                                                 }
416                                         ],
417                                         'cmd_copy_buckets_done' : [
418                                                 ['command'],
419                                                 function() {
420                                                         window.close();
421                                                 }
422                                         ],
423                                         'cmd_export_to_copy_status' : [
424                                                 ['command'],
425                                                 function() {
426                                                         try {
427                                                                 JSAN.use('util.functional');
428                                                                 var barcodes = util.functional.map_list(
429                                                                         obj.list2.dump_retrieve_ids(),
430                                                                         function(o) { return JSON2js(o)[1]; }
431                                                                 );
432                                                                 var url = urls.XUL_COPY_STATUS + '?barcodes=' + window.escape( js2JSON(barcodes) );
433                                                                 xulG.new_tab( url, {}, {});
434                                                         } catch(E) {
435                                                                 obj.error.standard_unexpected_error_alert('Copy Status from Copy Buckets',E);
436                                                         }
437                                                 }
438                                         ],
439                                 }
440                         }
441                 );
442                 this.controller.render();
443
444                 if (typeof xulG == 'undefined') {
445                         obj.controller.view.cmd_export_to_copy_status.disabled = true;
446                         obj.controller.view.cmd_export_to_copy_status.setAttribute('disabled',true);
447                 } else {
448                         obj.controller.view.cmd_copy_buckets_done.disabled = true;
449                         obj.controller.view.cmd_copy_buckets_done.setAttribute('disabled',true);
450                 }
451         
452         },
453
454         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
455                 var obj = this;
456                 try {
457                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
458                         if (copy == null || typeof copy.ilsevent != 'undefined') {
459                                 throw(copy);
460                         } else {
461                                 var item = {
462                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
463                                         'row' : {
464                                                 'my' : {
465                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
466                                                         'acp' : copy,
467                                                 }
468                                         }
469                                 };
470                                 return item;
471                         }
472                 } catch(E) {
473                         obj.error.standard_unexpected_error_alert('List building failed.',E);
474                         return null;
475                 }
476
477         },
478         
479 }
480
481 dump('exiting cat.copy_buckets.js\n');