rethinking record bucket interface
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / record_buckets.js
1 dump('entering cat.record_buckets.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.record_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         this.first_pause = true;
11     var x = document.getElementById("record_buckets_tabbox");
12     if (x) x.selectedIndex = 2;
13 };
14
15 cat.record_buckets.pick_file = function (defaultFileName) {
16         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
17
18         var nsIFilePicker = Components.interfaces.nsIFilePicker;
19         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker );
20
21         fp.init( window, "Save File As", nsIFilePicker.modeSave );
22         if (defaultFileName)
23                 fp.defaultString = defaultFileName;
24
25         fp.appendFilters( nsIFilePicker.filterAll );
26
27         var result = fp.show(); 
28         if ( (result == nsIFilePicker.returnOK || result == nsIFilePicker.returnReplace) && fp.file ) {
29                 return fp.file;
30         } else {
31                 return null;
32         }
33 };
34
35 cat.record_buckets.export_records = function(obj, output_type) {
36         try {
37                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
38                 obj.list2.select_all();
39                 obj.data.stash_retrieve();
40                 JSAN.use('util.functional');
41
42                 var record_ids = util.functional.map_list(
43                         obj.list2.dump_retrieve_ids(),
44                         function (o) { return JSON2js(o).docid }
45                 );
46
47                 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
48                         .createInstance(Components.interfaces.nsIWebBrowserPersist);
49
50                 var proto_uri = 'http://' + window.location.hostname + '/exporter?format=' + output_type;
51
52                 dump('Record Export URI is ' + proto_uri + '&id=' + record_ids.join('&id=') + '\n');
53
54                 var uri = Components.classes["@mozilla.org/network/io-service;1"]
55                         .getService(Components.interfaces.nsIIOService)
56                         .newURI( proto_uri + '&id=' + record_ids.join('&id='), null, null );
57
58                 var file = cat.record_buckets.pick_file('bucket.' + output_type);
59                                                                 
60                 if (file) {
61                         persist.saveURI(uri,null,null,null,null,file);
62                 } else {
63                         alert("File not downloaded.");
64                 }
65
66         } catch(E) {
67                 obj.error.standard_unexpected_error_alert('Records could not be exported.',E);
68         }
69 };
70
71
72 cat.record_buckets.prototype = {
73         'selection_list1' : [],
74         'selection_list2' : [],
75         'bucket_id_name_map' : {},
76
77         'render_pending_records' : function() {
78                 if (this.first_pause) {
79                         this.first_pause = false;
80                 } else {
81                         alert("Action completed.");
82                 }
83                 var obj = this;
84                 obj.list1.clear();
85                 for (var i = 0; i < obj.record_ids.length; i++) {
86                         var item = obj.flesh_item_for_list( obj.record_ids[i] );
87                         if (item) obj.list1.append( item );
88                 }
89         },
90
91         'init' : function( params ) {
92
93                 var obj = this;
94
95                 obj.record_ids = params['record_ids'] || [];
96
97                 JSAN.use('circ.util');
98                 var columns = circ.util.columns( 
99                         { 
100                                 'title' : { 'hidden' : false },
101                                 'author' : { 'hidden' : false },
102                                 'edition' : { 'hidden' : false },
103                                 'publisher' : { 'hidden' : false },
104                                 'pubdate' : { 'hidden' : false },
105                                 'isbn' : { 'hidden' : false },
106                                 'tcn' : { 'hidden' : false },
107                         } 
108                 );
109
110                 JSAN.use('util.list'); 
111
112                 obj.list1 = new util.list('pending_records_list');
113                 obj.list1.init(
114                         {
115                                 'columns' : columns,
116                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
117                                 'on_select' : function(ev) {
118                                         try {
119                                                 JSAN.use('util.functional');
120                                                 var sel = obj.list1.retrieve_selection();
121                                                 obj.controller.view.sel_clip1.setAttribute('disabled', sel.length < 1 ? "true" : "false");
122                                                 obj.selection_list1 = util.functional.map_list(
123                                                         sel,
124                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
125                                                 );
126                                                 obj.error.sdump('D_TRACE','circ/record_buckets: selection list 1 = ' + js2JSON(obj.selection_list1) );
127                                                 if (obj.selection_list1.length == 0) {
128                                                         obj.controller.view.cmd_record_buckets_sel_add.setAttribute('disabled','true');
129                                                 } else {
130                                                         obj.controller.view.cmd_record_buckets_sel_add.setAttribute('disabled','false');
131                                                 }
132                                         } catch(E) {
133                                                 alert('FIXME: ' + E);
134                                         }
135                                 },
136
137                         }
138                 );
139
140                 obj.render_pending_records();
141         
142                 obj.list2 = new util.list('records_in_bucket_list');
143                 obj.list2.init(
144                         {
145                                 'columns' : columns,
146                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
147                                 'on_select' : function(ev) {
148                                         try {
149                                                 JSAN.use('util.functional');
150                                                 var sel = obj.list2.retrieve_selection();
151                                                 obj.controller.view.sel_clip2.setAttribute('disabled', sel.length < 1 ? "true" : "false");
152                                                 obj.selection_list2 = util.functional.map_list(
153                                                         sel,
154                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
155                                                 );
156                                                 obj.error.sdump('D_TRACE','circ/record_buckets: selection list 2 = ' + js2JSON(obj.selection_list2) );
157                                                 if (obj.selection_list2.length == 0) {
158                                                         obj.controller.view.cmd_record_buckets_delete_item.setAttribute('disabled','true');
159                                                         obj.controller.view.cmd_pending_buckets_to_record_buckets.setAttribute('disabled','true');
160                                                 } else {
161                                                         obj.controller.view.cmd_record_buckets_delete_item.setAttribute('disabled','false');
162                                                         obj.controller.view.cmd_pending_buckets_to_record_buckets.setAttribute('disabled','false');
163                                                 }
164                                         } catch(E) {
165                                                 alert('FIXME: ' + E);
166                                         }
167                                 },
168                         }
169                 );
170                 
171                 JSAN.use('util.controller'); obj.controller = new util.controller();
172                 obj.controller.init(
173                         {
174                                 'control_map' : {
175                                         'save_columns2' : [
176                                                 ['command'],
177                                                 function() { obj.list2.save_columns(); }
178                                         ],
179                                         'save_columns1' : [
180                                                 ['command'],
181                                                 function() { obj.list1.save_columns(); }
182                                         ],
183                                         'sel_clip2' : [
184                                                 ['command'],
185                                                 function() { obj.list2.clipboard(); }
186                                         ],
187                                         'sel_clip1' : [
188                                                 ['command'],
189                                                 function() { obj.list1.clipboard(); }
190                                         ],
191                                         'record_buckets_menulist_placeholder' : [
192                                                 ['render'],
193                                                 function(e) {
194                                                         return function() {
195                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
196                                                                 var buckets = obj.network.simple_request(
197                                                                         'BUCKET_RETRIEVE_VIA_USER',
198                                                                         [ ses(), obj.data.list.au[0].id() ]
199                                                                 );
200                                                                 if (typeof buckets.ilsevent != 'undefined') {
201                                                                         obj.error.standard_unexpected_error_alert('Could not retrieve your buckets.',buckets);
202                                                                         return;
203                                                                 }
204                                                                 var items = [ ['Choose a bucket...',''], ['Retrieve shared bucket...',-1] ].concat(
205                                                                         util.functional.map_list(
206                                                                                 util.functional.filter_list(
207                                                                                         buckets.biblio,
208                                                                                         function(o) {
209                                                                                                 return o.btype() == 'staff_client';
210                                                                                         }
211                                                                                 ),
212                                                                                 function(o) {
213                                                                                         obj.bucket_id_name_map[ o.id() ] = o.name();
214                                                                                         return [ o.name(), o.id() ];
215                                                                                 }
216                                                                         ).sort( 
217                                                         function( a, b ) {
218                                                             if (a[0] < b[0]) return -1;
219                                                             if (a[0] > b[0]) return 1;
220                                                             return 0;
221                                                         }
222                                                                         )
223                                                                 );
224                                                                 obj.error.sdump('D_TRACE','items = ' + js2JSON(items));
225                                                                 util.widgets.remove_children( e );
226                                                                 var ml = util.widgets.make_menulist(
227                                                                         items
228                                                                 );
229                                                                 e.appendChild( ml );
230                                                                 ml.setAttribute('id','bucket_menulist');
231                                                                 ml.setAttribute('accesskey','');
232
233                                                                 function change_bucket(ev) {
234                                                                         var bucket_id = ev.target.value;
235                                                                         if (bucket_id < 0 ) {
236                                                                                 bucket_id = window.prompt('Enter bucket number:');
237                                                                                 ev.target.value = bucket_id;
238                                                                                 ev.target.setAttribute('value',bucket_id);
239                                                                         }
240                                                                         if (!bucket_id) return;
241                                                                         var x = document.getElementById('info_box');
242                                                                         x.setAttribute('hidden','true');
243                                     obj.controller.view.cmd_record_buckets_delete_bucket.setAttribute('disabled','true');
244                                     obj.controller.view.cmd_record_buckets_refresh.setAttribute('disabled','true');
245                                     obj.controller.view.record_buckets_export_records.disabled = true;
246                                     obj.controller.view.cmd_merge_records.setAttribute('disabled','true');
247                                     obj.controller.view.cmd_delete_records.setAttribute('disabled','true');
248                                     obj.controller.view.cmd_sel_opac.setAttribute('disabled','true');
249                                     obj.controller.view.record_buckets_list_actions.disabled = true;
250                                                                         var bucket = obj.network.simple_request(
251                                                                                 'BUCKET_FLESH',
252                                                                                 [ ses(), 'biblio', bucket_id ]
253                                                                         );
254                                                                         if (typeof bucket.ilsevent != 'undefined') {
255                                                                                 if (bucket.ilsevent == 1506 /* CONTAINER_NOT_FOUND */) {
256                                                                                         alert('Could not find a bucket with ID = ' + bucket_id);
257                                                                                 } else {
258                                                                                         obj.error.standard_unexpected_error_alert('Error retrieving bucket.  Did you use a valid bucket id?',bucket);
259                                                                                 }
260                                                                                 return;
261                                                                         }
262                                                                         try {
263                                         obj.controller.view.cmd_record_buckets_delete_bucket.setAttribute('disabled','false');
264                                         obj.controller.view.cmd_record_buckets_refresh.setAttribute('disabled','false');
265                                         obj.controller.view.record_buckets_export_records.disabled = false;
266                                         obj.controller.view.cmd_merge_records.setAttribute('disabled','false');
267                                         obj.controller.view.cmd_delete_records.setAttribute('disabled','false');
268                                         obj.controller.view.cmd_sel_opac.setAttribute('disabled','false');
269                                         obj.controller.view.record_buckets_list_actions.disabled = false;
270
271                                                                                 var x = document.getElementById('info_box');
272                                                                                 x.setAttribute('hidden','false');
273                                                                                 x = document.getElementById('bucket_number');
274                                                                                 x.setAttribute('value',bucket.id());
275                                                                                 x = document.getElementById('bucket_name');
276                                                                                 x.setAttribute('value',bucket.name());
277                                                                                 x = document.getElementById('bucket_owner');
278                                                                                 var s = bucket.owner(); JSAN.use('patron.util');
279                                                                                 if (s && typeof s != "object") s = patron.util.retrieve_fleshed_au_via_id(ses(),s); 
280                                                                                 x.setAttribute('value',s.card().barcode() + " @ " + obj.data.hash.aou[ s.home_ou() ].shortname());
281                                                                         } catch(E) {
282                                                                                 alert(E);
283                                                                         }
284                                                                         var items = bucket.items() || [];
285                                                                         obj.list2.clear();
286                                                                         for (var i = 0; i < items.length; i++) {
287                                                                                 var item = obj.flesh_item_for_list( 
288                                                                                         items[i].target_biblio_record_entry(),
289                                                                                         items[i].id()
290                                                                                 );
291                                                                                 if (item) obj.list2.append( item );
292                                                                         }
293                                                                 }
294
295                                                                 ml.addEventListener( 'change_bucket', change_bucket , false);
296                                                                 ml.addEventListener( 'command', function() {
297                                                                         JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
298                                                                 }, false);
299                                                                 obj.controller.view.bucket_menulist = ml;
300                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
301                                                         };
302                                                 },
303                                         ],
304
305                     'cmd_record_buckets_refresh' : [
306                         ['command'],
307                         function() {
308                             JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
309                         }
310                     ],
311
312                                         'cmd_record_buckets_add' : [
313                                                 ['command'],
314                                                 function() {
315                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
316                                                         if (!bucket_id) return;
317                                                         for (var i = 0; i < obj.record_ids.length; i++) {
318                                                                 var bucket_item = new cbrebi();
319                                                                 bucket_item.isnew('1');
320                                                                 bucket_item.bucket(bucket_id);
321                                                                 bucket_item.target_biblio_record_entry( obj.record_ids[i] );
322                                                                 try {
323                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
324                                                                                 [ ses(), 'biblio', bucket_item ]);
325
326                                                                         if (typeof robj == 'object') throw robj;
327
328                                                                         var item = obj.flesh_item_for_list( obj.record_ids[i], robj );
329                                                                         if (!item) continue;
330
331                                                                         obj.list2.append( item );
332                                                                 } catch(E) {
333                                                                         alert( js2JSON(E) );
334                                                                 }
335                                                         }
336                                                 }
337                                         ],
338                                         'cmd_record_buckets_sel_add' : [
339                                                 ['command'],
340                                                 function() {                                                        
341                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
342                                                         if (!bucket_id) return;
343                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
344                                                                 var docid = obj.selection_list1[i].docid;
345                                                                 var bucket_item = new cbrebi();
346                                                                 bucket_item.isnew('1');
347                                                                 bucket_item.bucket(bucket_id);
348                                                                 bucket_item.target_biblio_record_entry( docid );
349                                                                 try {
350                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
351                                                                                 [ ses(), 'biblio', bucket_item ]);
352
353                                                                         if (typeof robj == 'object') throw robj;
354
355                                                                         var item = obj.flesh_item_for_list( docid, robj );
356                                                                         if (!item) continue;
357
358                                                                         obj.list2.append( item );
359                                                                 } catch(E) {
360                                                                         alert( js2JSON(E) );
361                                                                 }
362                                                         }
363
364                                                 }
365                                         ],
366                                         'cmd_pending_buckets_to_record_buckets' : [
367                                                 ['command'],
368                                                 function() {                                                        
369                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
370                                                                 var docid = obj.selection_list2[i].docid;
371                                                                 var item = obj.flesh_item_for_list( docid );
372                                                                 if (item) {
373                                                                         obj.list1.append( item );
374                                                                         obj.record_ids.push( docid );
375                                                                 }
376                                                         }
377                                                 }
378                                         ],
379
380                                         'cmd_record_buckets_delete_item' : [
381                                                 ['command'],
382                                                 function() {
383                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
384                                                                 try {
385                                                                         var bucket_item_id = obj.selection_list2[i].bucket_item_id;
386                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
387                                                                                 [ ses(), 'biblio', bucket_item_id ]);
388                                                                         if (typeof robj == 'object') throw robj;
389                                                                 } catch(E) {
390                                                                         alert(js2JSON(E));
391                                                                 }
392                                                         }
393                                                         alert("Action completed.");
394                                                         setTimeout(
395                                                                 function() {
396                                                                         JSAN.use('util.widgets'); 
397                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
398                                                                 }, 0
399                                                         );
400                                                 }
401                                         ],
402                                         'cmd_record_buckets_delete_bucket' : [
403                                                 ['command'],
404                                                 function() {
405                                                         try {
406                                                                 var bucket = obj.controller.view.bucket_menulist.value;
407                                                                 var name = obj.bucket_id_name_map[ bucket ];
408                                                                 var conf = window.confirm('Delete the bucket named ' + name + '?');
409                                                                 if (!conf) return;
410                                                                 obj.list2.clear();
411                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[ses(),'biblio',bucket]);
412                                                                 if (typeof robj == 'object') throw robj;
413                                                                 alert("Action completed.");
414                                                                 var x = document.getElementById('info_box');
415                                 x.setAttribute('hidden','true');
416                                 obj.controller.view.cmd_record_buckets_delete_bucket.setAttribute('disabled','true');
417                                 obj.controller.view.cmd_record_buckets_refresh.setAttribute('disabled','true');
418                                 obj.controller.view.record_buckets_export_records.disabled = true;
419                                 obj.controller.view.cmd_merge_records.setAttribute('disabled','true');
420                                 obj.controller.view.cmd_delete_records.setAttribute('disabled','true');
421                                 obj.controller.view.cmd_sel_opac.setAttribute('disabled','true');
422                                 obj.controller.view.record_buckets_list_actions.disabled = true;
423                                                                 obj.controller.render('record_buckets_menulist_placeholder');
424                                                                 setTimeout(
425                                                                         function() {
426                                                                                 JSAN.use('util.widgets'); 
427                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
428                                                                         }, 0
429                                                                 );
430
431                                                         } catch(E) {
432                                                                 alert('FIXME -- ' + E);
433                                                         }
434                                                 }
435                                         ],
436                                         'cmd_record_buckets_new_bucket' : [
437                                                 ['command'],
438                                                 function() {
439                                                         try {
440                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
441
442                                                                 if (name) {
443                                                                         var bucket = new cbreb();
444                                                                         bucket.btype('staff_client');
445                                                                         bucket.owner( obj.data.list.au[0].id() );
446                                                                         bucket.name( name );
447
448                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[ses(),'biblio',bucket]);
449
450                                                                         if (typeof robj == 'object') {
451                                                                                 if (robj.ilsevent == 1710 /* CONTAINER_EXISTS */) {
452                                                                                         alert('You already have a bucket with that name.');
453                                                                                         return;
454                                                                                 }
455                                                                                 throw robj;
456                                                                         }
457
458
459                                                                         alert('Bucket "' + name + '" created.');
460
461                                                                         obj.controller.render('record_buckets_menulist_placeholder');
462                                                                         obj.controller.view.bucket_menulist.value = robj;
463                                                                         setTimeout(
464                                                                                 function() {
465                                                                                         JSAN.use('util.widgets'); 
466                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
467                                                                                 }, 0
468                                                                         );
469                                                                 }
470                                                         } catch(E) {
471                                                                 alert( js2JSON(E) );
472                                                         }
473                                                 }
474                                         ],
475                                         
476                                         'cmd_pending_buckets_csv_to_clipboard' : [ ['command'], function() { obj.list1.dump_csv_to_clipboard(); } ], 
477                                         'cmd_record_buckets_csv_to_clipboard' : [ ['command'], function() { obj.list2.dump_csv_to_clipboard(); } ],
478                     'cmd_pending_buckets_csv_to_printer' : [ ['command'], function() { obj.list1.dump_csv_to_printer(); } ],
479                     'cmd_record_buckets_csv_printer' : [ ['command'], function() { obj.list2.dump_csv_to_printer(); } ], 
480                     'cmd_pending_buckets_csv_to_file' : [ ['command'], function() { obj.list1.dump_csv_to_file( { 'defaultFileName' : 'pending_records.txt' } ); } ],
481                     'cmd_record_buckets_csv_file' : [ ['command'], function() { obj.list2.dump_csv_to_file( { 'defaultFileName' : 'bucket_records.txt' } ); } ], 
482
483                                         'cmd_export_records_usmarc' : [
484                                                 ['command'],
485                                                 function () { return cat.record_buckets.export_records(obj, 'usmarc') }
486                                         ],
487
488                                         'cmd_export_records_unimarc' : [
489                                                 ['command'],
490                                                 function () { return cat.record_buckets.export_records(obj, 'unimarc') }
491                                         ],
492
493                                         'cmd_export_records_xml' : [
494                                                 ['command'],
495                                                 function () { return cat.record_buckets.export_records(obj, 'xml') }
496                                         ],
497
498                                         'cmd_export_records_bre' : [
499                                                 ['command'],
500                                                 function () { return cat.record_buckets.export_records(obj, 'bre') }
501                                         ],
502
503                                         'cmd_merge_records' : [
504                                                 ['command'],
505                                                 function() {
506                                                         try {
507                                                                 obj.list2.select_all();
508                                                                 obj.data.stash_retrieve();
509                                                                 JSAN.use('util.functional');
510
511                                                                 var record_ids = util.functional.map_list(
512                                                                         obj.list2.dump_retrieve_ids(),
513                                                                         function (o) {
514                                                                                 return JSON2js(o).docid; // docid
515                                                                         }
516                                                                 );
517
518                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
519                                                                 var top_xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" >';
520                                                                 top_xml += '<description>Merge these records? (Select the "lead" record first)</description>';
521                                                                 top_xml += '<hbox><button id="lead" disabled="true" label="Merge" name="fancy_submit"/><button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox></vbox>';
522
523                                                                 var xml = '<form xmlns="http://www.w3.org/1999/xhtml">';
524                                                                 xml += '<table><tr valign="top">';
525                                                                 for (var i = 0; i < record_ids.length; i++) {
526                                                                         xml += '<td><input value="Lead" id="record_' + record_ids[i] + '" type="radio" name="lead"';
527                                                                         xml += ' onclick="' + "try { var x = document.getElementById('lead'); x.setAttribute('value',";
528                                                                         xml += record_ids[i] + '); x.disabled = false; } catch(E) { alert(E); }">';
529                                                                         xml += '</input>Lead Record? #' + record_ids[i] + '</td>';
530                                                                 }
531                                                                 xml += '</tr><tr valign="top">';
532                                                                 for (var i = 0; i < record_ids.length; i++) {
533                                                                         xml += '<td nowrap="nowrap"><iframe src="' + urls.XUL_BIB_BRIEF; 
534                                                                         xml += '?docid=' + record_ids[i] + '"/></td>';
535                                                                 }
536                                                                 xml += '</tr><tr valign="top">';
537                                                                 for (var i = 0; i < record_ids.length; i++) {
538                                                                         html = obj.network.simple_request('MARC_HTML_RETRIEVE',[ record_ids[i] ]);
539                                                                         xml += '<td nowrap="nowrap"><iframe style="min-height: 1000px; min-width: 300px;" flex="1" src="data:text/html,' + window.escape(html) + '"/></td>';
540                                                                 }
541                                                                 xml += '</tr></table></form>';
542                                                                 //obj.data.temp_merge_top = top_xml; obj.data.stash('temp_merge_top');
543                                                                 //obj.data.temp_merge_mid = xml; obj.data.stash('temp_merge_mid');
544                                                                 JSAN.use('util.window'); var win = new util.window();
545                                                                 var fancy_prompt_data = win.open(
546                                                                         urls.XUL_FANCY_PROMPT,
547                                                                         //+ '?xml_in_stash=temp_merge_mid'
548                                                                         //+ '&top_xml_in_stash=temp_merge_top'
549                                                                         //+ '&title=' + window.escape('Record Merging'),
550                                                                         'fancy_prompt', 'chrome,resizable,modal,width=700,height=500',
551                                                                         {
552                                                                                 'top_xml' : top_xml, 'xml' : xml, 'title' : 'Record Merging'
553                                                                         }
554                                                                 );
555                                                                 //obj.data.stash_retrieve();
556
557                                                                 if (typeof fancy_prompt_data.fancy_status == 'undefined' || fancy_prompt_data.fancy_status == 'incomplete') { alert('Merge Aborted'); return; }
558                                                                 var robj = obj.network.simple_request('MERGE_RECORDS', 
559                                                                         [ 
560                                                                                 ses(), 
561                                                                                 fancy_prompt_data.lead, 
562                                                                                 util.functional.filter_list( record_ids,
563                                                                                         function(o) {
564                                                                                                 return o != fancy_prompt_data.lead;
565                                                                                         }
566                                                                                 )
567                                                                         ]
568                                                                 );
569                                                                 if (typeof robj.ilsevent != 'undefined') {
570                                                                         throw(robj);
571                                                                 } else {
572                                                                         alert('Records were successfully merged.');
573                                                                 }
574
575                                                                 obj.render_pending_records(); // FIXME -- need a generic refresh for lists
576                                                                 setTimeout(
577                                                                         function() {
578                                                                                 JSAN.use('util.widgets'); 
579                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
580                                                                         }, 0
581                                                                 );
582                                                         } catch(E) {
583                                                                 obj.error.standard_unexpected_error_alert('Records were not likely merged.',E);
584                                                         }
585
586                                                 }
587                                         ],
588                                         
589                                         'cmd_delete_records' : [
590                                                 ['command'],
591                                                 function() {
592                                                         try {
593                                                                 obj.list2.select_all();
594                                                                 obj.data.stash_retrieve();
595                                                                 JSAN.use('util.functional');
596
597                                                                 var record_ids = util.functional.map_list(
598                                                                         obj.list2.dump_retrieve_ids(),
599                                                                         function (o) {
600                                                                                 return JSON2js(o).docid; // docid
601                                                                         }
602                                                                 );
603
604                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
605                                                                 var top_xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" >';
606                                                                 top_xml += '<description>Delete these records?</description>';
607                                                                 top_xml += '<hbox><button id="lead" disabled="false" label="Delete" name="fancy_submit"/><button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox></vbox>';
608
609                                                                 var xml = '<form xmlns="http://www.w3.org/1999/xhtml">';
610                                                                 xml += '<table><tr valign="top">';
611                                                                 for (var i = 0; i < record_ids.length; i++) {
612                                                                         xml += '<td>Record #' + record_ids[i] + '</td>';
613                                                                 }
614                                                                 xml += '</tr><tr valign="top">';
615                                                                 for (var i = 0; i < record_ids.length; i++) {
616                                                                         xml += '<td nowrap="nowrap"><iframe src="' + urls.XUL_BIB_BRIEF; 
617                                                                         xml += '?docid=' + record_ids[i] + '"/></td>';
618                                                                 }
619                                                                 xml += '</tr><tr valign="top">';
620                                                                 for (var i = 0; i < record_ids.length; i++) {
621                                                                         html = obj.network.simple_request('MARC_HTML_RETRIEVE',[ record_ids[i] ]);
622                                                                         xml += '<td nowrap="nowrap"><iframe style="min-height: 1000px; min-width: 300px;" flex="1" src="data:text/html,' + window.escape(html) + '"/></td>';
623                                                                 }
624                                                                 xml += '</tr></table></form>';
625                                                                 //obj.data.temp_merge_top = top_xml; obj.data.stash('temp_merge_top');
626                                                                 //obj.data.temp_merge_mid = xml; obj.data.stash('temp_merge_mid');
627                                                                 JSAN.use('util.window'); var win = new util.window();
628                                                                 var fancy_prompt_data = win.open(
629                                                                         urls.XUL_FANCY_PROMPT,
630                                                                         //+ '?xml_in_stash=temp_merge_mid'
631                                                                         //+ '&top_xml_in_stash=temp_merge_top'
632                                                                         //+ '&title=' + window.escape('Record Purging'),
633                                                                         'fancy_prompt', 'chrome,resizable,modal,width=700,height=500',
634                                                                         {
635                                                                                 'top_xml' : top_xml, 'xml' : xml, 'title' : 'Record Purging'
636                                                                         }
637                                                                 );
638                                                                 //obj.data.stash_retrieve();
639                                                                 if (typeof fancy_prompt_data.fancy_status == 'undefined' || fancy_prompt_data.fancy_status != 'complete') { alert('Delete Aborted'); return; }
640                                                                 var s = '';
641                                                                 for (var i = 0; i < record_ids.length; i++) {
642                                                                         var robj = obj.network.simple_request('FM_BRE_DELETE',[ses(),record_ids[i]]);
643                                                                         if (typeof robj.ilsevent != 'undefined') {
644                                                                                 if (!s) s = 'Error deleting these records:\n';
645                                                                                 s += 'Record #' + record_ids[i] + ' : ' + robj.textcode + ' : ' + robj.desc + '\n';
646                                                                         }
647                                                                 }
648                                                                 if (s) { alert(s); } else { alert('Records deleted.'); }
649
650                                                                 obj.render_pending_records(); // FIXME -- need a generic refresh for lists
651                                                                 setTimeout(
652                                                                         function() {
653                                                                                 JSAN.use('util.widgets'); 
654                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
655                                                                         }, 0
656                                                                 );
657                                                         } catch(E) {
658                                                                 obj.error.standard_unexpected_error_alert('Records were not likely deleted.',E);
659                                                         }
660
661                                                 }
662                                         ],
663
664                                         'cmd_broken' : [
665                                                 ['command'],
666                                                 function() { alert('Not Yet Implemented'); }
667                                         ],
668                                         'cmd_sel_opac' : [
669                                                 ['command'],
670                                                 function() {
671                                                         try {
672                                                                 obj.list2.select_all();
673                                                                 JSAN.use('util.functional');
674                                                                 var docids = util.functional.map_list(
675                                                                         obj.list2.dump_retrieve_ids(),
676                                                                         function (o) {
677                                                                                 return JSON2js(o).docid; // docid
678                                                                         }
679                                                                 );
680                                                                 for (var i = 0; i < docids.length; i++) {
681                                                                         var doc_id = docids[i];
682                                                                         var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
683                                                                         var content_params = { 
684                                                                                 'session' : ses(),
685                                                                                 'authtime' : ses('authtime'),
686                                                                                 'opac_url' : opac_url,
687                                                                         };
688                                                                         xulG.new_tab(
689                                                                                 xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
690                                                                                 {'tab_name':'Retrieving title...'}, 
691                                                                                 content_params
692                                                                         );
693                                                                 }
694                                                         } catch(E) {
695                                                                 obj.error.standard_unexpected_error_alert('Showing in OPAC',E);
696                                                         }
697                                                 }
698                                         ],
699                     'record_buckets_export_records' : [ ['render'], function(){} ],
700                     'record_buckets_list_actions' : [ ['render'], function(){} ]
701                                 }
702                         }
703                 );
704                 this.controller.render();
705
706                 if (typeof xulG == 'undefined') {
707                         obj.controller.view.cmd_sel_opac.disabled = true;
708                         obj.controller.view.cmd_sel_opac.setAttribute('disabled',true);
709                 }
710         },
711
712         'flesh_item_for_list' : function(docid,bucket_item_id) {
713                 var obj = this;
714                 try {
715                         var record = obj.network.simple_request( 'MODS_SLIM_RECORD_RETRIEVE.authoritative', [ docid ]);
716                         if (record == null || typeof(record.ilsevent) != 'undefined') {
717                                 throw(record);
718                         } else {
719                                 var item = {
720                                         'retrieve_id' : js2JSON( { 'docid' : docid, 'bucket_item_id' : bucket_item_id } ),
721                                         'row' : {
722                                                 'my' : {
723                                                         'mvr' : record,
724                                                 }
725                                         }
726                                 };
727                                 return item;
728                         }
729                 } catch(E) {
730                         obj.error.standard_unexpected_error_alert('Could not retrieve this record: ' + docid,E);
731                         return null;
732                 }
733
734         },
735         
736 };
737
738 dump('exiting cat.record_buckets.js\n');