]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/serial/manage_dists.js
Improve Firefox/XULRunner Support
[Evergreen.git] / Open-ILS / xul / staff_client / server / serial / manage_dists.js
1 dump('entering serial/manage_dists.js\n');
2 // vim:et:sw=4:ts=4:
3
4 if (typeof serial == 'undefined') serial = {};
5 serial.manage_dists = function (params) {
6     try {
7         JSAN.use('util.error'); this.error = new util.error();
8     } catch(E) {
9         dump('serial/manage_dists: ' + E + '\n');
10     }
11 };
12
13 serial.manage_dists.prototype = {
14
15     'map_tree' : {},
16     'map_sdist' : {},
17     'map_sstr' : {},
18     'sel_list' : [],
19     'funcs' : [],
20     'editor_indexes' : { 'sdist' : 1, 'sstr' : 2, 'sbsum' : 3, 'sssum' : 4, 'sisum' : 5 },
21
22     'ids_from_sel_list' : function(type) {
23         var obj = this;
24         JSAN.use('util.functional');
25
26         var list = util.functional.map_list(
27             util.functional.filter_list(
28                 obj.sel_list,
29                 function (o) {
30                     return o.split(/_/)[0] == type;
31                 }
32             ),
33             function (o) {
34                 return o.split(/_/)[1];
35             }
36         );
37
38         return list;
39     },
40
41     'editor_init' : function(type, mode, params) {
42         var obj = this;
43         try {
44             $('serial_manage_dists_editor_deck').selectedIndex = obj.editor_indexes[type];
45             var editor_type = type + '_editor';
46             if (typeof obj[editor_type] == 'undefined') {
47                 JSAN.use('serial.' + editor_type);
48                 obj[editor_type] = new serial[editor_type](); 
49             }
50
51             params.do_edit = true;
52             params.handle_update = true;
53             if (mode == 'add') {
54                 params.trigger_refresh = true;
55                 params.refresh_command = function () {obj.refresh_list();};
56             }
57             obj[editor_type].init(params);
58         } catch(E) {
59             obj.error.standard_unexpected_error_alert('editor_init() error',E);
60         }
61     },
62
63     'do_delete' : function(type, method, overridable_events) {
64         var obj = this;
65         try {
66             JSAN.use('util.functional');
67
68             var list = util.functional.filter_list(
69                 obj.sel_list,
70                 function (o) {
71                     return o.split(/_/)[0] == type;
72                 }
73             );
74
75             list = util.functional.map_list(
76                 list,
77                 function (o) {
78                     return JSON2js( js2JSON( obj['map_' + type][ type + '_' + o.split(/_/)[1] ] ) );
79                 }
80             );
81
82             //TODO: proper messages
83             var delete_msg;
84             if (list.length != 1) {
85                 delete_msg = document.getElementById('serialStrings').getFormattedString('staff.serial.manage_dists.delete_' + type + '.confirm.plural', [list.length]);
86             } else {
87                 delete_msg = document.getElementById('serialStrings').getString('staff.serial.manage_dists.delete_' + type + '.confirm');
88             }
89             var r = obj.error.yns_alert(
90                     delete_msg,
91                     document.getElementById('serialStrings').getString('staff.serial.manage_dists.delete_' + type + '.title'),
92                     document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.delete'),
93                     document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.cancel'),
94                     null,
95                     document.getElementById('commonStrings').getString('common.confirm')
96             );
97
98             if (r == 0) {
99                 for (var i = 0; i < list.length; i++) {
100                     list[i].isdeleted('1');
101                 }
102                 var robj = obj.network.request(
103                     'open-ils.serial', 
104                     method, 
105                     [ ses(), list, true ],
106                     null,
107                     {
108                         'title' : document.getElementById('serialStrings').getString('staff.serial.manage_dists.delete_' + type + '.override'),
109                         'overridable_events' : overridable_events
110                     }
111                 );
112                 if (robj == null) throw(robj);
113                 if (typeof robj.ilsevent != 'undefined') {
114                     if (robj.ilsevent != 0) {
115                         var overridable = false;
116                         for (i = 0; i < overridable_events.length; i++) {
117                             if (overridable_events[i] == robj.ilsevent) {
118                                 overridable = true;
119                                 break;
120                             }
121                         }
122                         if (!overridable) throw(robj);
123                     }
124                 }
125                 obj.refresh_list();
126             }
127         } catch(E) {
128             obj.error.standard_unexpected_error_alert(document.getElementById('serialStrings').getString('staff.serial.manage_dists.delete.error'),E);
129             obj.refresh_list();
130         }
131     },
132
133     'init' : function( params ) {
134
135         try {
136             var obj = this;
137
138             obj.docid = params.docid;
139
140             JSAN.use('util.network'); obj.network = new util.network();
141             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
142             JSAN.use('util.controller'); obj.controller = new util.controller();
143             obj.controller.init(
144                 {
145                     control_map : {
146                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
147                         'sel_clip' : [
148                             ['command'],
149                             function() { obj.list.clipboard(); }
150                         ],
151                         'cmd_broken' : [
152                             ['command'],
153                             function() { 
154                                 alert(document.getElementById('commonStrings').getString('common.unimplemented'));
155                             }
156                         ],
157                         'cmd_show_my_libs' : [
158                             ['command'],
159                             function() { 
160                                 obj.show_my_libs(); 
161                             }
162                         ],
163                         'cmd_clear' : [
164                             ['command'],
165                             function() {
166                                 obj.map_tree = {};
167                                 obj.list.clear();
168                             }
169                         ],
170                         'cmd_add_sstr' : [
171                             ['command'],
172                             function() {
173                                 try {
174                                     var list = obj.ids_from_sel_list('sdist');
175                                     if (list.length == 0) list = obj.ids_from_sel_list('sstr-group');
176                                     if (list.length == 0) return;
177
178                                     /*TODO: permission check?
179                                     //populate 'list' with owning_libs of subs, TODO
180                                     var edit = 0;
181                                     try {
182                                         edit = obj.network.request(
183                                             api.PERM_MULTI_ORG_CHECK.app,
184                                             api.PERM_MULTI_ORG_CHECK.method,
185                                             [ 
186                                                 ses(), 
187                                                 obj.data.list.au[0].id(), 
188                                                 list,
189                                                 [ 'CREATE_COPY' ]
190                                             ]
191                                         ).length == 0 ? 1 : 0;
192                                     } catch(E) {
193                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
194                                     }
195
196                                     if (edit==0) return; // no read-only view for this interface */
197                                     var new_sstr = new sstr();
198                                     new_sstr.distribution(list[0]);//TODO: add multiple at once support?
199                                     new_sstr.isnew(1);
200                                     var params = {};
201                                     params.sstrs = [new_sstr];
202                                     obj.editor_init('sstr', 'add', params);
203                                 } catch(E) {
204                                     obj.error.standard_unexpected_error_alert($('serialStrings').getString('staff.serial.manage_dists.add.error'),E);
205                                 }
206                             }
207                         ],
208                         'cmd_delete_sstr' : [
209                             ['command'],
210                             function() {
211                                 var overridable_events = [ //TODO: proper overrides
212                                 ];
213                                 obj.do_delete('sstr', 'open-ils.serial.stream.batch.update', overridable_events);
214                             }
215                         ],
216                         'cmd_mark_library' : [
217                             ['command'],
218                             function() {
219                                 try {
220                                     var list = obj.ids_from_sel_list('aou');
221                                     if (list.length == 1) {
222                                         obj.data.marked_library = { 'lib' : list[0], 'docid' : obj.docid };
223                                         obj.data.stash('marked_library');
224                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.alert'));
225                                     } else {
226                                         obj.error.yns_alert(
227                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.prompt'),
228                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.title'),
229                                                 document.getElementById('commonStrings').getString('common.ok'),
230                                                 null,
231                                                 null,
232                                                 document.getElementById('commonStrings').getString('common.confirm')
233                                                 );
234                                     }
235                                 } catch(E) {
236                                     obj.error.standard_unexpected_error_alert('manage_dists.js -> mark library',E);
237                                 }
238                             }
239                         ],
240
241                         'cmd_mark_distribution' : [
242                             ['command'],
243                             function() {
244                                 try {
245                                     var list = obj.ids_from_sel_list('sdist');
246                                     if (list.length == 1) {
247                                         obj.data.marked_distribution = list[0];
248                                         obj.data.stash('marked_distribution');
249                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.alert'));
250                                     } else {
251                                         obj.error.yns_alert(
252                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.prompt'),
253                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.title'),
254                                                 document.getElementById('commonStrings').getString('common.ok'),
255                                                 null,
256                                                 null,
257                                                 document.getElementById('commonStrings').getString('common.confirm')
258                                                 );
259                                     }
260                                 } catch(E) {
261                                     obj.error.standard_unexpected_error_alert('manage_dists.js -> mark distribution',E);
262                                 }
263                             }
264                         ],
265                         'cmd_transfer_distribution' : [
266                             ['command'],
267                             function() {
268                                 try {
269                                     obj.data.stash_retrieve();
270                                     if (!obj.data.marked_library) {
271                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_volume.alert'));
272                                         return;
273                                     }
274                                     
275                                     var list = obj.ids_from_sel_list('sdist');
276
277                                     JSAN.use('util.functional');
278
279                                     var sdist_list = util.functional.map_list(
280                                         list,
281                                         function (o) {
282                                             return obj.map_sdist[ 'sdist_' + o ].start_date();
283                                         }
284                                     ).join(document.getElementById('commonStrings').getString('common.grouping_string'));
285
286                                     var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
287                                     xml += '<description>';
288                                     xml += document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.transfer.prompt', [sdist_list, obj.data.hash.aou[ obj.data.marked_library.lib ].shortname()]);
289                                     xml += '</description>';
290                                     xml += '<hbox><button label="' + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.submit.label') + '" name="fancy_submit"/>';
291                                     xml += '<button label="' 
292                                         + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.cancel.label') 
293                                         + '" accesskey="' 
294                                         + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.cancel.accesskey') 
295                                         + '" name="fancy_cancel"/></hbox>';
296                                     xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + obj.data.marked_library.docid + '" oils_force_external="true"/>';
297                                     xml += '</vbox>';
298                                     JSAN.use('OpenILS.data');
299                                     var data = new OpenILS.data(); data.init({'via':'stash'});
300                                     //data.temp_transfer = xml; data.stash('temp_transfer');
301                                     JSAN.use('util.window'); var win = new util.window();
302                                     var fancy_prompt_data = win.open(
303                                         urls.XUL_FANCY_PROMPT,
304                                         //+ '?xml_in_stash=temp_transfer'
305                                         //+ '&title=' + window.escape('Volume Transfer'),
306                                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
307                                         {
308                                             'xml' : xml,
309                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.title')
310                                         }
311                                     );
312
313                                     if (fancy_prompt_data.fancy_status == 'incomplete') {
314                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.incomplete'));
315                                         return;
316                                     }
317
318                                     var robj = obj.network.simple_request(
319                                         'FM_ACN_TRANSFER', 
320                                         [ ses(), { 'docid' : obj.data.marked_library.docid, 'lib' : obj.data.marked_library.lib, 'distributions' : list } ],
321                                         null,
322                                         {
323                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.override.failure'),
324                                             'overridable_events' : [
325                                                 1208, // TITLE_LAST_COPY
326                                                 1219, // COPY_REMOTE_CIRC_LIB
327                                             ],
328                                         }
329                                     );
330
331                                     if (typeof robj.ilsevent != 'undefined') {
332                                         if (robj.ilsevent == 1221) { // ORG_CANNOT_HAVE_VOLS
333                                             alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.ineligible_destination'));
334                                         } else {
335                                             throw(robj);
336                                         }
337                                     } else {
338                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.success'));
339                                     }
340
341                                 } catch(E) {
342                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.unexpected_error'),E);
343                                 }
344                                 obj.refresh_list();
345                             }
346                         ],
347
348                         'cmd_transfer_sstrs' : [
349                             ['command'],
350                             function() {
351                                 try {
352                                     obj.data.stash_retrieve();
353                                     if (!obj.data.marked_distribution) {
354                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.missing_volume'));
355                                         return;
356                                     }
357                                     
358                                     JSAN.use('util.functional');
359
360                                     var list = obj.ids_from_sel_list('sstr');
361                                     var distribution = obj.network.simple_request('FM_ACN_RETRIEVE.authoritative',[ obj.data.marked_distribution ]);
362
363                                     JSAN.use('cat.util'); cat.util.transfer_copies( { 
364                                         'distribution_ids' : list, 
365                                         'docid' : distribution.record(),
366                                         'distribution_label' : distribution.start_date(),
367                                         'owning_lib' : distribution.owning_lib(),
368                                     } );
369
370                                 } catch(E) {
371                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.unexpected_error'),E);
372                                 }
373                                 obj.refresh_list();
374                             }
375                         ],
376                         'cmd_refresh_list' : [
377                             ['command'],
378                             function() {
379                                 obj.refresh_list();
380                             }
381                         ],
382 /*dbw2                      'sel_distribution_details' : [
383                             ['command'],
384                             function() {
385                                 JSAN.use('util.functional');
386
387                                 var list = util.functional.filter_list(
388                                     obj.sel_list,
389                                     function (o) {
390                                         return o.split(/_/)[0] == 'sstr';
391                                     }
392                                 );
393
394                                 list = util.functional.map_list(
395                                     list,
396                                     function (o) {
397                                         return o.split(/_/)[1];
398                                     }
399                                 );
400     
401                                 JSAN.use('circ.util');
402                                 for (var i = 0; i < list.length; i++) {
403                                     circ.util.show_copy_details( list[i] );
404                                 }
405                             }
406                         ],
407                         'cmd_edit_sstrs' : [
408                             ['command'],
409                             function() {
410                                 try {
411                                     JSAN.use('util.functional');
412
413                                     var list = util.functional.filter_list(
414                                         obj.sel_list,
415                                         function (o) {
416                                             return o.split(/_/)[0] == 'sstr';
417                                         }
418                                     );
419
420                                     list = util.functional.map_list(
421                                         list,
422                                         function (o) {
423                                             return o.split(/_/)[1];
424                                         }
425                                     );
426
427                                     JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : list, 'edit' : 1 } );
428                                     obj.refresh_list();
429
430                                 } catch(E) {
431                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.edit_items.error'),E);
432                                 }
433                             }
434                         ], dbw2*/
435
436 /*dbw2                      'cmd_print_spine_labels' : [
437                             ['command'],
438                             function() {
439                                 try {
440                                     JSAN.use('util.functional');
441                                     
442                                     var list = util.functional.filter_list(
443                                         obj.sel_list,
444                                         function (o) {
445                                             return o.split(/_/)[0] == 'sstr';
446                                         }
447                                     );
448
449                                     list = util.functional.map_list(
450                                         list,
451                                         function (o) {
452                                             return obj.map_sstr[ o ];
453                                         }
454                                     );
455
456                                     obj.data.temp_barcodes_for_labels = util.functional.map_list( list, function(o){return o.barcode();}) ; 
457                                     obj.data.stash('temp_barcodes_for_labels');
458                                     xulG.new_tab(
459                                         xulG.url_prefix('XUL_SPINE_LABEL'),
460                                         { 'tab_name' : document.getElementById('catStrings').getString('staff.cat.copy_browser.print_spine.tab') },
461                                         {}
462                                     );
463                                 } catch(E) {
464                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.print_spine.error'),E);
465                                 }
466                             }
467                         ]
468                         dbw2*/
469                     }
470                 }
471             );
472
473             obj.list_init(params);
474
475             obj.org_ids = obj.network.simple_request('FM_SDIST_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
476             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
477             JSAN.use('util.functional'); 
478             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
479
480             var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
481             //obj.show_libs( org );
482
483             //obj.show_my_libs();
484
485             JSAN.use('util.file'); JSAN.use('util.widgets');
486
487             var file; var list_data; var ml; 
488
489             file = new util.file('offline_ou_list'); 
490             if (file._file.exists()) {
491                 list_data = file.get_object(); file.close();
492                 for (var i = 0; i < list_data[0].length; i++) { // make sure all entries are enabled
493                     list_data[0][i][2] = false;
494                 }
495                 ml = util.widgets.make_menulist( list_data[0], list_data[1] );
496                 ml.setAttribute('id','sdist_lib_menu'); document.getElementById('serial_dist_lib_menu').appendChild(ml);
497                 //TODO: class this menu properly
498                 for (var i = 0; i < obj.org_ids.length; i++) {
499                     ml.getElementsByAttribute('value',obj.org_ids[i])[0].setAttribute('class','has_distributions');
500                 }
501                 ml.firstChild.addEventListener(
502                     'popupshown',
503                     function(ev) {
504                         document.getElementById('legend').setAttribute('hidden','false');
505                     },
506                     false
507                 );
508                 ml.firstChild.addEventListener(
509                     'popuphidden',
510                     function(ev) {
511                         document.getElementById('legend').setAttribute('hidden','true');
512                     },
513                     false
514                 );
515                 ml.addEventListener(
516                     'command',
517                     function(ev) {
518                         if (document.getElementById('refresh_button')) document.getElementById('refresh_button').focus(); 
519                         JSAN.use('util.file'); var file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
520                         util.widgets.save_attributes(file, { 'sdist_lib_menu' : [ 'value' ], 'show_sdists' : [ 'checked' ], 'show_sdist_groups' : [ 'checked' ] });
521                         obj.refresh_list();
522                     },
523                     false
524                 );
525             } else {
526                 throw(document.getElementById('catStrings').getString('staff.cat.copy_browser.missing_library') + '\n');
527             }
528
529             file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
530             util.widgets.load_attributes(file);
531             obj.default_lib = ml.getAttribute('value');
532             ml.value = obj.default_lib;
533             if (! obj.default_lib) {
534                 obj.default_lib = org.id();
535                 ml.setAttribute('value',obj.default_lib);
536                 ml.value = obj.default_lib;
537             }
538
539             document.getElementById('show_sdists').addEventListener(
540                 'command',
541                 function(ev) {
542                     JSAN.use('util.file'); var file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
543                     util.widgets.save_attributes(file, { 'sdist_lib_menu' : [ 'value' ], 'show_sdists' : [ 'checked' ], 'show_sdist_groups' : [ 'checked' ] });
544                 },
545                 false
546             );
547
548             document.getElementById('show_sdist_groups').addEventListener(
549                 'command',
550                 function(ev) {
551                     JSAN.use('util.file'); var file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
552                     util.widgets.save_attributes(file, { 'sdist_lib_menu' : [ 'value' ], 'show_sdists' : [ 'checked' ], 'show_sdist_groups' : [ 'checked' ] });
553                 },
554                 false
555             );
556
557             obj.show_my_libs( obj.default_lib );
558
559             JSAN.use('util.exec'); var exec = new util.exec(20); exec.timer(obj.funcs,100);
560
561             obj.toggle_actions(); // disable menus initially
562
563         } catch(E) {
564             this.error.standard_unexpected_error_alert('serial/manage_dists.init: ',E);
565         }
566     },
567
568     'show_my_libs' : function(org) {
569         var obj = this;
570         try {
571             if (!org) {
572                 org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
573             } else {
574                 if (typeof org != 'object') org = obj.data.hash.aou[ org ];
575             }
576             obj.show_libs( org, false );
577         } catch(E) {
578             alert(E);
579         }
580     },
581
582     'show_libs' : function(start_aou,show_open) {
583         var obj = this;
584         try {
585             if (!start_aou) throw('show_libs: Need a start_aou');
586             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
587             JSAN.use('util.functional'); 
588
589             var parents = [];
590             var temp_aou = start_aou;
591             while ( temp_aou.parent_ou() ) {
592                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
593                 parents.push( temp_aou );
594             }
595             parents.reverse();
596
597             for (var i = 0; i < parents.length; i++) {
598                 obj.funcs.push(
599                     function(o,p) {
600                         return function() { 
601                             obj.append_org(o,p,{'container':'true','open':'true'}); 
602                         };
603                     }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
604                 );
605             }
606
607             obj.funcs.push(
608                 function(o,p) {
609                     return function() { obj.append_org(o,p); };
610                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
611             );
612
613             obj.funcs.push(
614                 function() {
615                     if (start_aou.children()) {
616                         var x = obj.map_tree[ 'aou_' + start_aou.id() ];
617                         x.setAttribute('container','true');
618                         if (show_open) x.setAttribute('open','true');
619                         for (var i = 0; i < start_aou.children().length; i++) {
620                             obj.funcs.push(
621                                 function(o,p) {
622                                     return function() { obj.append_org(o,p); };
623                                 }( start_aou.children()[i], start_aou )
624                             );
625                         }
626                     }
627                 }
628             );
629
630         } catch(E) {
631             alert(E);
632         }
633     },
634
635     'on_select' : function(list,twisty) {
636         var obj = this;
637         var sel_lists = {};
638
639         for (var i = 0; i < list.length; i++) {
640             var row_type = list[i].split('_')[0];
641             var id = list[i].split('_')[1];
642
643             if (!sel_lists[row_type]) sel_lists[row_type] = [];
644             sel_lists[row_type].push(id);
645
646             switch(row_type) {
647                 case 'aou' : obj.on_click_aou(id,twisty); break;
648                 case 'sdist' : obj.on_select_sdist(id,twisty); break;
649                 default: break;
650             }
651         }
652
653         if (!obj.focused_node_retrieve_id) return;
654
655         var row_type = obj.focused_node_retrieve_id.split('_')[0];
656         var id = obj.focused_node_retrieve_id.split('_')[1];
657
658         if (sel_lists[row_type]) { // the type focused is in the selection (usually the case)
659             switch(row_type) {
660                 case 'aou' : obj.on_click_aou(id,twisty); break;
661                 default: if (obj['on_click_' + row_type]) obj['on_click_' + row_type](sel_lists[row_type],twisty);
662             }
663         }
664     },
665
666     'on_select_sdist' : function(sdist_id,twisty) {
667         var obj = this;
668         try {
669             var sdist_tree = obj.map_sdist[ 'sdist_' + sdist_id ];
670             obj.funcs.push( function() { 
671                 document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
672                 document.getElementById('sdist_lib_menu').setAttribute('disabled','true'); 
673             } );
674             if (sdist_tree.basic_summary()) {
675                 obj.funcs.push(
676                     function(c,a) {
677                         return function() {
678                             obj.append_member(c,a,[],'sbsum', false);
679                         }
680                     }( sdist_tree.basic_summary(), sdist_tree )
681                 );
682             }
683             if (sdist_tree.supplement_summary()) {
684                 obj.funcs.push(
685                     function(c,a) {
686                         return function() {
687                             obj.append_member(c,a,[],'sssum', false);
688                         }
689                     }( sdist_tree.supplement_summary(), sdist_tree )
690                 );
691             }
692             if (sdist_tree.index_summary()) {
693                 obj.funcs.push(
694                     function(c,a) {
695                         return function() {
696                             obj.append_member(c,a,[],'sisum', false);
697                         }
698                     }( sdist_tree.index_summary(), sdist_tree )
699                 );
700             }
701             if (sdist_tree.streams()) {
702                 for (var i = 0; i < sdist_tree.streams().length; i++) {
703                     obj.funcs.push(
704                         function(c,a) {
705                             return function() {
706                                 obj.append_member(c,a,[],'sstr', true);
707                             }
708                         }( sdist_tree.streams()[i], sdist_tree )
709                     )
710                 }
711             }
712             /* TODO: template editing would be convenient here, but a little too confusing
713             // add template nodes
714             var same_templates;
715             var has_bind_template;
716             if (sdist_tree.receive_unit_template()) {
717                 if (sdist_tree.bind_unit_template()) {
718                     has_bind_template = true;                    
719                     if (sdist_tree.receive_unit_template().id() == sdist_tree.bind_unit_template().id()) {
720                         same_templates = true;
721                         obj.funcs.push(
722                             function(c,a) {
723                                 return function() {
724                                     obj.append_member(c,a,[],'act', false, 'Receive/Bind Unit Template');
725                                 }
726                             }( sdist_tree.receive_unit_template(), sdist_tree )
727                         )
728                     }
729                 }
730
731                 if (!same_templates) {
732                     obj.funcs.push(
733                         function(c,a) {
734                             return function() {
735                                 obj.append_member(c,a,[],'act', false, 'Receive Unit Template');
736                             }
737                         }( sdist_tree.receive_unit_template(), sdist_tree )
738                     )
739                 }
740             }
741             if (has_bind_template && !same_templates) {
742                 obj.funcs.push(
743                     function(c,a) {
744                         return function() {
745                             obj.append_member(c,a,[],'act', false, 'Bind Unit Template');
746                         }
747                     }( sdist_tree.bind_unit_template(), sdist_tree )
748                 )
749             }
750             */
751             obj.funcs.push( function() { 
752                 document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
753                 document.getElementById('sdist_lib_menu').setAttribute('disabled','false'); 
754             } );
755         } catch(E) {
756             alert(E);
757         }
758     },
759
760     'on_click_sdist' : function(sdist_ids,twisty) {
761         var obj = this;
762         try {
763             // draw sdist editor
764             if (typeof twisty == 'undefined') {
765                 var params = {};
766                 params.sdist_ids = sdist_ids;
767                 params.xul_id_prefix = 'sdist2';
768                 obj.editor_init('sdist', 'edit', params);
769             }
770         } catch(E) {
771             alert(E);
772         }
773     },
774
775     'on_click_sstr' : function(sstr_ids,twisty) {
776         var obj = this;
777         try {
778             // draw sstr editor
779             if (typeof twisty == 'undefined') {
780                 var params = {};
781                 params.sstr_ids = sstr_ids;
782                 obj.editor_init('sstr', 'edit', params);
783             }
784         } catch(E) {
785             alert(E);
786         }
787     },
788
789     'on_click_sbsum' : function(sbsum_ids,twisty) {
790         var obj = this;
791         try {
792             // draw sbsum editor
793             if (typeof twisty == 'undefined') {
794                 var params = {};
795                 params.sbsum_ids = sbsum_ids;
796                 obj.editor_init('sbsum', 'edit', params);
797             }
798         } catch(E) {
799             alert(E);
800         }
801     },
802
803     'on_click_sssum' : function(sssum_ids,twisty) {
804         var obj = this;
805         try {
806             // draw sssum editor
807             if (typeof twisty == 'undefined') {
808                 var params = {};
809                 params.sssum_ids = sssum_ids;
810                 obj.editor_init('sssum', 'edit', params);
811             }
812         } catch(E) {
813             alert(E);
814         }
815     },
816
817     'on_click_sisum' : function(sisum_ids,twisty) {
818         var obj = this;
819         try {
820             // draw sisum editor
821             if (typeof twisty == 'undefined') {
822                 var params = {};
823                 params.sisum_ids = sisum_ids;
824                 obj.editor_init('sisum', 'edit', params);
825             }
826         } catch(E) {
827             alert(E);
828         }
829     },
830
831     'on_click_aou' : function(org_id,twisty) {
832         var obj = this;
833         var org = obj.data.hash.aou[ org_id ];
834         var default_aou = obj.data.hash.aou[obj.default_lib];
835         obj.funcs.push( function() { 
836             document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
837             document.getElementById('sdist_lib_menu').setAttribute('disabled','true'); 
838         } );
839         if (org.children()) {
840             for (var i = 0; i < org.children().length; i++) {
841                 var child = org.children()[i];
842                 if (orgIsMine(default_aou,child)) {
843                     obj.funcs.push(
844                         function(o,p) {
845                             return function() {
846                                 obj.append_org(o,p)
847                             }
848                         }(child,org)
849                     );
850                 }
851             }
852         } 
853         if (obj.map_sdist[ 'aou_' + org_id ]) {
854             for (var i = 0; i < obj.map_sdist[ 'aou_' + org_id ].length; i++) {
855                 obj.funcs.push(
856                     function(o,a) {
857                         return function() {
858                             obj.append_sdist(o,a);
859                         }
860                     }( org, obj.map_sdist[ 'aou_' + org_id ][i] )
861                 );
862             }
863         }
864         obj.funcs.push( function() { 
865             document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
866             document.getElementById('sdist_lib_menu').setAttribute('disabled','false'); 
867         } );
868
869         // remove current editor
870         if (typeof twisty == 'undefined') {
871             document.getElementById('serial_manage_dists_editor_deck').selectedIndex = 0;
872         }
873     },
874
875     'append_org' : function (org,parent_org,params) {
876         var obj = this;
877         try {
878             if (obj.map_tree[ 'aou_' + org.id() ]) {
879                 var x = obj.map_tree[ 'aou_' + org.id() ];
880                 if (params) {
881                     for (var i in params) {
882                         x.setAttribute(i,params[i]);
883                     }
884                 }
885                 return x;
886             }
887
888             var data = {
889                 'row' : {
890                     'my' : {
891                         'aou' : org,
892                     }
893                 },
894                 'skip_all_columns_except' : [0,1,2],
895                 'retrieve_id' : 'aou_' + org.id(),
896                 'to_bottom' : true,
897                 'no_auto_select' : true,
898             };
899         
900             var sdist_tree_list;
901             if ( obj.org_ids.indexOf( Number( org.id() ) ) == -1 ) {
902                 if ( get_bool( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) ) {
903                     data.row.my.distribution_count = '0';
904                 } else {
905                     data.row.my.distribution_count = '';
906                 }
907             } else {
908                 var d_count = 0;
909                 sdist_tree_list = obj.network.simple_request(
910                     'FM_SDIST_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS.authoritative',
911                     [ ses(), obj.docid, [ org.id() ] ]
912                 );
913                 for (var i = 0; i < sdist_tree_list.length; i++) {
914                     d_count++;
915                     obj.map_sdist[ 'sdist_' + sdist_tree_list[i].id() ] = function(r){return r;}(sdist_tree_list[i]);
916                     var streams = sdist_tree_list[i].streams();
917                     for (var j = 0; j < streams.length; j++) {
918                         obj.map_sstr[ 'sstr_' + streams[j].id() ] = function(r){return r;}(streams[j]);
919                     }
920                 }
921                 data.row.my.distribution_count = d_count;
922             }
923             if (parent_org) {
924                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
925             }
926             var nparams = obj.list.append(data);
927             var node = nparams.treeitem_node;
928             if (params) {
929                 for (var i in params) {
930                     node.setAttribute(i,params[i]);
931                 }
932             }
933             obj.map_tree[ 'aou_' + org.id() ] = node;
934
935             if (org.children()) {
936                 node.setAttribute('container','true');
937             }
938
939             if (parent_org) {
940                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
941                     data.node.setAttribute('open','true');
942                     obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
943                 }
944             } else {
945                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
946                 obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
947             }
948
949             if (sdist_tree_list) {
950                 obj.map_sdist[ 'aou_' + org.id() ] = sdist_tree_list;
951                 node.setAttribute('container','true');
952             }
953
954             if (document.getElementById('show_sdists').checked) {
955                 obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
956                 node.setAttribute('open','true');
957             }
958
959         } catch(E) {
960             dump(E+'\n');
961             alert(E);
962         }
963     },
964
965     'append_sdist' : function( org, sdist_tree, params ) {
966         var obj = this;
967         try {
968             if (obj.map_tree[ 'sdist_' + sdist_tree.id() ]) {
969                 var x = obj.map_tree[ 'sdist_' + sdist_tree.id() ];
970                 if (params) {
971                     for (var i in params) {
972                         x.setAttribute(i,params[i]);
973                     }
974                 }
975                 return x;
976             }
977
978             var parent_node = obj.map_tree[ 'aou_' + org.id() ];
979             var data = {
980                 'row' : {
981                     'my' : {
982                         'aou' : org,
983                         'sdist' : sdist_tree,
984                         'distribution_count' : ''
985                     }
986                 },
987                 'skip_all_columns_except' : [0,1,2],
988                 'retrieve_id' : 'sdist_' + sdist_tree.id(),
989                 'node' : parent_node,
990                 'to_bottom' : true,
991                 'no_auto_select' : true,
992             };
993             var nparams = obj.list.append(data);
994             var node = nparams.treeitem_node;
995             obj.map_tree[ 'sdist_' + sdist_tree.id() ] =  node;
996             if (params) {
997                 for (var i in params) {
998                     node.setAttribute(i,params[i]);
999                 }
1000             }
1001             node.setAttribute('container','true');
1002             if (document.getElementById('show_sdist_groups').checked) {
1003                 node.setAttribute('open','true');
1004                 obj.funcs.push( function() { obj.on_select_sdist( sdist_tree.id(), true ); } );
1005             }
1006             var sstr_group_node_data = {
1007                 'row' : {
1008                     'my' : {
1009                         'label' : $('serialStrings').getString('serial.manage_dists.streams'),
1010                     }
1011                 },
1012                 'retrieve_id' : 'sstr-group_' + sdist_tree.id(),
1013                 'node' : node,
1014                 'to_bottom' : true,
1015                 'no_auto_select' : true,
1016             };
1017             nparams = obj.list.append(sstr_group_node_data);
1018             obj.map_tree[ 'sdist_sstr_group_' + sdist_tree.id() ] =  nparams.treeitem_node;
1019         } catch(E) {
1020             dump(E+'\n');
1021             alert(E);
1022         }
1023     },
1024
1025     'append_member' : function( item, sdist_tree, attributes, type, group, label ) {
1026         var obj = this;
1027         try {
1028             if (obj.map_tree[ type + '_' + sdist_tree.id() + '_' + item.id() ]) {
1029                 var x = obj.map_tree[ type + '_' + item.id() ];
1030                 if (attributes) {
1031                     for (var i in attributes) {
1032                         x.setAttribute(i,attributes[i]);
1033                     }
1034                 }
1035                 return x;
1036             }
1037
1038             var parent_node;
1039             if (group) {
1040                 parent_node = obj.map_tree[ 'sdist_' + type + '_group_' + sdist_tree.id() ];
1041             } else {
1042                 parent_node = obj.map_tree[ 'sdist_' + sdist_tree.id() ];
1043             }
1044             var data = {
1045                 'row' : {
1046                     'my' : {
1047                         'aou' : obj.data.hash.aou[ sdist_tree.holding_lib() ],
1048                         'sdist' : sdist_tree,
1049                         'distribution_count' : ''
1050                     }
1051                 },
1052                 'retrieve_id' : type + '_' + item.id(),
1053                 'node' : parent_node,
1054                 'to_bottom' : true,
1055                 'no_auto_select' : true,
1056             };
1057             data['row']['my'][type] = item; // TODO: future optimization: get only the IDs of these leaves, then fetch the full row in 'retrieve_row'
1058             var nparams = obj.list.append(data);
1059             var node = nparams.treeitem_node;
1060             obj.map_tree[ type + '_' + sdist_tree.id() + '_' + item.id() ] =  node;
1061             if (label) {
1062                 data['row']['my']['label'] = label;
1063             }
1064             if (attributes) {
1065                 for (var i in attributes) {
1066                     node.setAttribute(i,attributes[i]);
1067                 }
1068             }
1069
1070         } catch(E) {
1071             dump(E+'\n');
1072             alert(E);
1073         }
1074     },
1075
1076     'list_init' : function( params ) {
1077
1078         try {
1079             var obj = this;
1080             
1081             JSAN.use('circ.util');
1082             var columns = [
1083                 {
1084                     'id' : 'tree_location',
1085                     'label' : 'Location',
1086                     'flex' : 1, 'primary' : true, 'hidden' : false, 
1087                     'render' : function(my) { 
1088                         if (my.label) { return my.label; }
1089                         if (my.sstr) { return $('serialStrings').getFormattedString('serial.manage_dists.stream_num', [my.sstr.id()]); }
1090                         if (my.sbsum) { return $('serialStrings').getString('serial.manage_dists.sbsum'); }
1091                         if (my.sssum) { return $('serialStrings').getString('serial.manage_dists.sssum'); }
1092                         if (my.sisum) { return $('serialStrings').getString('serial.manage_dists.sisum'); }
1093                         if (my.sdist) { return my.sdist.label(); }
1094                         if (my.aou) { return $('serialStrings').getFormattedString('serial.manage_dists.library_label', [my.aou.shortname(), my.aou.name()]); }
1095                         return "???";
1096                     },
1097                 },
1098                 {
1099                     'id' : 'distribution_count',
1100                     'label' : $('serialStrings').getString('serial.manage_dists.distributions'),
1101                     'flex' : 0, 'primary' : false, 'hidden' : false, 
1102                     'render' : function(my) { return my.distribution_count; },
1103                 }
1104             ];
1105             JSAN.use('util.list'); obj.list = new util.list('sdists_tree');
1106             obj.list.init(
1107                 {
1108                     'no_auto_select' : true,
1109                     'columns' : columns,
1110                     'retrieve_row' : function(params) {
1111
1112                         var row = params.row;
1113                         obj.funcs.push(
1114                             function() {
1115
1116                                 if (typeof params.on_retrieve == 'function') {
1117                                     params.on_retrieve(row);
1118                                 }
1119
1120                             }
1121                         );
1122
1123                         return row;
1124                     },
1125                     'on_click' : function(ev) {
1126                         var row = {}; var col = {}; var nobj = {};
1127                         obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj); 
1128                         if ((row.value == -1)||(nobj.value != 'twisty')) { return; } // on_click runs for twistys only
1129
1130                         var node = obj.list.node.contentView.getItemAtIndex(row.value);
1131                         var list = [ node.getAttribute('retrieve_id') ];
1132                         if (typeof obj.on_select == 'function') {
1133                             obj.on_select(list,true);
1134                         }
1135                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1136                             window.xulG.on_select(list);
1137                         }
1138                     },
1139                     'on_select' : function(ev) {
1140                         JSAN.use('util.functional');
1141                         
1142                         // get the actual node clicked to determine which editor to use
1143                         if (obj.list.node.view.selection.currentIndex > -1) {
1144                             var node = obj.list.node.contentView.getItemAtIndex(obj.list.node.view.selection.currentIndex);
1145                             obj.focused_node_retrieve_id = node.getAttribute('retrieve_id');
1146                         }
1147
1148                         var sel = obj.list.retrieve_selection();
1149                         obj.controller.view.sel_clip.disabled = sel.length < 1;
1150                         obj.sel_list = util.functional.map_list(
1151                             sel,
1152                             function(o) { return o.getAttribute('retrieve_id'); }
1153                         );
1154                         obj.toggle_actions();
1155                         if (typeof obj.on_select == 'function') {
1156                             obj.on_select(obj.sel_list);
1157                         }
1158                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1159                             window.xulG.on_select(obj.sel_list);
1160                         }
1161                     },
1162                 }
1163             );
1164
1165             obj.controller.render();
1166
1167         } catch(E) {
1168             this.error.sdump('D_ERROR','serial/manage_dists.list_init: ' + E + '\n');
1169             alert(E);
1170         }
1171     },
1172
1173     'toggle_actions' : function() {
1174         var obj = this;
1175         try {
1176             var found_aou = false; var found_sdist = false; var found_sstr = false; var found_sbsum = false; var found_sssum = false; var found_sisum = false; var found_sstr_group = false;
1177             for (var i = 0; i < obj.sel_list.length; i++) {
1178                 var type = obj.sel_list[i].split(/_/)[0];
1179                 switch(type) {
1180                     case 'aou' : 
1181                         found_aou = true; 
1182                     break;
1183                     case 'sdist' : found_sdist = true; break;
1184                     case 'sstr' : found_sstr = true; break;
1185                     case 'sbsum' : found_sbsum = true; break;
1186                     case 'sssum' : found_sssum = true; break;
1187                     case 'sisum' : found_sisum = true; break;
1188                     case 'sstr-group' : found_sstr_group = true; break;
1189                 }
1190             }
1191             obj.controller.view.cmd_add_sstr.setAttribute('disabled','true');
1192             obj.controller.view.cmd_delete_sstr.setAttribute('disabled','true');
1193             obj.controller.view.cmd_mark_library.setAttribute('disabled','true');
1194             //obj.controller.view.cmd_delete_sdist.setAttribute('disabled','true');
1195             if (found_aou) {
1196                 obj.controller.view.cmd_mark_library.setAttribute('disabled','false');
1197             }
1198             if (found_sdist) {
1199                 //obj.controller.view.cmd_delete_sdist.setAttribute('disabled','false');
1200                 obj.controller.view.cmd_add_sstr.setAttribute('disabled','false');
1201             }
1202             if (found_sstr_group) {
1203                 obj.controller.view.cmd_add_sstr.setAttribute('disabled','false');
1204             }
1205             if (found_sstr) {
1206                 obj.controller.view.cmd_delete_sstr.setAttribute('disabled','false');
1207                 obj.controller.view.cmd_transfer_sstrs.setAttribute('disabled','false');
1208             }
1209             if (found_sbsum) {
1210             }
1211             if (found_sssum) {
1212             }
1213             if (found_sisum) {
1214             }
1215         } catch(E) {
1216             obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.actions.error'),E);
1217         }
1218     },
1219
1220     'refresh_list' : function() { 
1221         try {
1222             var obj = this;
1223             obj.list.clear();
1224             obj.map_tree = {};
1225             obj.map_sdist = {};
1226             obj.map_sstr = {};
1227             obj.org_ids = obj.network.simple_request('FM_SDIST_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
1228             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
1229             JSAN.use('util.functional'); 
1230             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
1231             /*
1232             var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
1233             obj.show_libs( org );
1234             */
1235             obj.default_lib = document.getElementById('sdist_lib_menu').value;
1236             obj.show_my_libs( obj.default_lib );
1237         } catch(E) {
1238             this.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.refresh_list.error'),E);
1239         }
1240     },
1241 };
1242
1243 dump('exiting serial/manage_dists.js\n');