]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/serial/manage_dists.js
LP1915464 follow-up: use spaces, not tabs; remove extra comma
[working/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                                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
305                                         {
306                                             'xml' : xml,
307                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.title')
308                                         }
309                                     );
310
311                                     if (fancy_prompt_data.fancy_status == 'incomplete') {
312                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.incomplete'));
313                                         return;
314                                     }
315
316                                     var robj = obj.network.simple_request(
317                                         'FM_ACN_TRANSFER', 
318                                         [ ses(), { 'docid' : obj.data.marked_library.docid, 'lib' : obj.data.marked_library.lib, 'distributions' : list } ],
319                                         null,
320                                         {
321                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.override.failure'),
322                                             'overridable_events' : [
323                                                 1208, // TITLE_LAST_COPY
324                                                 1219, // COPY_REMOTE_CIRC_LIB
325                                             ],
326                                         }
327                                     );
328
329                                     if (typeof robj.ilsevent != 'undefined') {
330                                         if (robj.ilsevent == 1221) { // ORG_CANNOT_HAVE_VOLS
331                                             alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.ineligible_destination'));
332                                         } else {
333                                             throw(robj);
334                                         }
335                                     } else {
336                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.success'));
337                                     }
338
339                                 } catch(E) {
340                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.unexpected_error'),E);
341                                 }
342                                 obj.refresh_list();
343                             }
344                         ],
345
346                         'cmd_transfer_sstrs' : [
347                             ['command'],
348                             function() {
349                                 try {
350                                     obj.data.stash_retrieve();
351                                     if (!obj.data.marked_distribution) {
352                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.missing_volume'));
353                                         return;
354                                     }
355                                     
356                                     JSAN.use('util.functional');
357
358                                     var list = obj.ids_from_sel_list('sstr');
359                                     var distribution = obj.network.simple_request('FM_ACN_RETRIEVE.authoritative',[ obj.data.marked_distribution ]);
360
361                                     JSAN.use('cat.util'); cat.util.transfer_copies( { 
362                                         'distribution_ids' : list, 
363                                         'docid' : distribution.record(),
364                                         'distribution_label' : distribution.start_date(),
365                                         'owning_lib' : distribution.owning_lib(),
366                                     } );
367
368                                 } catch(E) {
369                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.unexpected_error'),E);
370                                 }
371                                 obj.refresh_list();
372                             }
373                         ],
374                         'cmd_refresh_list' : [
375                             ['command'],
376                             function() {
377                                 obj.refresh_list();
378                             }
379                         ],
380 /*dbw2                      'sel_distribution_details' : [
381                             ['command'],
382                             function() {
383                                 JSAN.use('util.functional');
384
385                                 var list = util.functional.filter_list(
386                                     obj.sel_list,
387                                     function (o) {
388                                         return o.split(/_/)[0] == 'sstr';
389                                     }
390                                 );
391
392                                 list = util.functional.map_list(
393                                     list,
394                                     function (o) {
395                                         return o.split(/_/)[1];
396                                     }
397                                 );
398     
399                                 JSAN.use('circ.util');
400                                 for (var i = 0; i < list.length; i++) {
401                                     circ.util.show_copy_details( list[i] );
402                                 }
403                             }
404                         ],
405                         'cmd_edit_sstrs' : [
406                             ['command'],
407                             function() {
408                                 try {
409                                     JSAN.use('util.functional');
410
411                                     var list = util.functional.filter_list(
412                                         obj.sel_list,
413                                         function (o) {
414                                             return o.split(/_/)[0] == 'sstr';
415                                         }
416                                     );
417
418                                     list = util.functional.map_list(
419                                         list,
420                                         function (o) {
421                                             return o.split(/_/)[1];
422                                         }
423                                     );
424
425                                     JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : list, 'edit' : 1 } );
426                                     obj.refresh_list();
427
428                                 } catch(E) {
429                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.edit_items.error'),E);
430                                 }
431                             }
432                         ], dbw2*/
433
434 /*dbw2                      'cmd_print_spine_labels' : [
435                             ['command'],
436                             function() {
437                                 try {
438                                     JSAN.use('util.functional');
439                                     
440                                     var list = util.functional.filter_list(
441                                         obj.sel_list,
442                                         function (o) {
443                                             return o.split(/_/)[0] == 'sstr';
444                                         }
445                                     );
446
447                                     list = util.functional.map_list(
448                                         list,
449                                         function (o) {
450                                             return obj.map_sstr[ o ];
451                                         }
452                                     );
453
454                                     obj.data.temp_barcodes_for_labels = util.functional.map_list( list, function(o){return o.barcode();}) ; 
455                                     obj.data.stash('temp_barcodes_for_labels');
456                                     xulG.new_tab(
457                                         xulG.url_prefix('XUL_SPINE_LABEL'),
458                                         { 'tab_name' : document.getElementById('catStrings').getString('staff.cat.copy_browser.print_spine.tab') },
459                                         {}
460                                     );
461                                 } catch(E) {
462                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.print_spine.error'),E);
463                                 }
464                             }
465                         ]
466                         dbw2*/
467                     }
468                 }
469             );
470
471             obj.list_init(params);
472
473             obj.org_ids = obj.network.simple_request('FM_SDIST_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
474             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
475             JSAN.use('util.functional'); 
476             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
477
478             var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
479             //obj.show_libs( org );
480
481             //obj.show_my_libs();
482
483             JSAN.use('util.file'); JSAN.use('util.widgets');
484
485             var file; var list_data; var ml; 
486
487             file = new util.file('offline_ou_list'); 
488             if (file._file.exists()) {
489                 list_data = file.get_object(); file.close();
490                 for (var i = 0; i < list_data[0].length; i++) { // make sure all entries are enabled
491                     list_data[0][i][2] = false;
492                 }
493                 ml = util.widgets.make_menulist( list_data[0], list_data[1] );
494                 ml.setAttribute('id','sdist_lib_menu'); document.getElementById('serial_dist_lib_menu').appendChild(ml);
495                 //TODO: class this menu properly
496                 for (var i = 0; i < obj.org_ids.length; i++) {
497                     ml.getElementsByAttribute('value',obj.org_ids[i])[0].setAttribute('class','has_distributions');
498                 }
499                 ml.firstChild.addEventListener(
500                     'popupshown',
501                     function(ev) {
502                         document.getElementById('legend').setAttribute('hidden','false');
503                     },
504                     false
505                 );
506                 ml.firstChild.addEventListener(
507                     'popuphidden',
508                     function(ev) {
509                         document.getElementById('legend').setAttribute('hidden','true');
510                     },
511                     false
512                 );
513                 ml.addEventListener(
514                     'command',
515                     function(ev) {
516                         if (document.getElementById('refresh_button')) document.getElementById('refresh_button').focus(); 
517                         JSAN.use('util.file'); var file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
518                         util.widgets.save_attributes(file, { 'sdist_lib_menu' : [ 'value' ], 'show_sdists' : [ 'checked' ], 'show_sdist_groups' : [ 'checked' ] });
519                         obj.refresh_list();
520                     },
521                     false
522                 );
523             } else {
524                 throw(document.getElementById('catStrings').getString('staff.cat.copy_browser.missing_library') + '\n');
525             }
526
527             file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
528             util.widgets.load_attributes(file);
529             obj.default_lib = ml.getAttribute('value');
530             ml.value = obj.default_lib;
531             if (! obj.default_lib) {
532                 obj.default_lib = org.id();
533                 ml.setAttribute('value',obj.default_lib);
534                 ml.value = obj.default_lib;
535             }
536
537             document.getElementById('show_sdists').addEventListener(
538                 'command',
539                 function(ev) {
540                     JSAN.use('util.file'); var file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
541                     util.widgets.save_attributes(file, { 'sdist_lib_menu' : [ 'value' ], 'show_sdists' : [ 'checked' ], 'show_sdist_groups' : [ 'checked' ] });
542                 },
543                 false
544             );
545
546             document.getElementById('show_sdist_groups').addEventListener(
547                 'command',
548                 function(ev) {
549                     JSAN.use('util.file'); var file = new util.file('manage_dists_prefs.'+obj.data.server_unadorned);
550                     util.widgets.save_attributes(file, { 'sdist_lib_menu' : [ 'value' ], 'show_sdists' : [ 'checked' ], 'show_sdist_groups' : [ 'checked' ] });
551                 },
552                 false
553             );
554
555             obj.show_my_libs( obj.default_lib );
556
557             JSAN.use('util.exec'); var exec = new util.exec(20); exec.timer(obj.funcs,100);
558
559             obj.toggle_actions(); // disable menus initially
560
561         } catch(E) {
562             this.error.standard_unexpected_error_alert('serial/manage_dists.init: ',E);
563         }
564     },
565
566     'show_my_libs' : function(org) {
567         var obj = this;
568         try {
569             if (!org) {
570                 org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
571             } else {
572                 if (typeof org != 'object') org = obj.data.hash.aou[ org ];
573             }
574             obj.show_libs( org, false );
575         } catch(E) {
576             alert(E);
577         }
578     },
579
580     'show_libs' : function(start_aou,show_open) {
581         var obj = this;
582         try {
583             if (!start_aou) throw('show_libs: Need a start_aou');
584             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
585             JSAN.use('util.functional'); 
586
587             var parents = [];
588             var temp_aou = start_aou;
589             while ( temp_aou.parent_ou() ) {
590                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
591                 parents.push( temp_aou );
592             }
593             parents.reverse();
594
595             for (var i = 0; i < parents.length; i++) {
596                 obj.funcs.push(
597                     function(o,p) {
598                         return function() { 
599                             obj.append_org(o,p,{'container':'true','open':'true'}); 
600                         };
601                     }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
602                 );
603             }
604
605             obj.funcs.push(
606                 function(o,p) {
607                     return function() { obj.append_org(o,p); };
608                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
609             );
610
611             obj.funcs.push(
612                 function() {
613                     if (start_aou.children()) {
614                         var x = obj.map_tree[ 'aou_' + start_aou.id() ];
615                         x.setAttribute('container','true');
616                         if (show_open) x.setAttribute('open','true');
617                         for (var i = 0; i < start_aou.children().length; i++) {
618                             obj.funcs.push(
619                                 function(o,p) {
620                                     return function() { obj.append_org(o,p); };
621                                 }( start_aou.children()[i], start_aou )
622                             );
623                         }
624                     }
625                 }
626             );
627
628         } catch(E) {
629             alert(E);
630         }
631     },
632
633     'on_select' : function(list,twisty) {
634         var obj = this;
635         var sel_lists = {};
636
637         for (var i = 0; i < list.length; i++) {
638             var row_type = list[i].split('_')[0];
639             var id = list[i].split('_')[1];
640
641             if (!sel_lists[row_type]) sel_lists[row_type] = [];
642             sel_lists[row_type].push(id);
643
644             switch(row_type) {
645                 case 'aou' : obj.on_click_aou(id,twisty); break;
646                 case 'sdist' : obj.on_select_sdist(id,twisty); break;
647                 default: break;
648             }
649         }
650
651         if (!obj.focused_node_retrieve_id) return;
652
653         var row_type = obj.focused_node_retrieve_id.split('_')[0];
654         var id = obj.focused_node_retrieve_id.split('_')[1];
655
656         if (sel_lists[row_type]) { // the type focused is in the selection (usually the case)
657             switch(row_type) {
658                 case 'aou' : obj.on_click_aou(id,twisty); break;
659                 default: if (obj['on_click_' + row_type]) obj['on_click_' + row_type](sel_lists[row_type],twisty);
660             }
661         }
662     },
663
664     'on_select_sdist' : function(sdist_id,twisty) {
665         var obj = this;
666         try {
667             var sdist_tree = obj.map_sdist[ 'sdist_' + sdist_id ];
668             obj.funcs.push( function() { 
669                 document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
670                 document.getElementById('sdist_lib_menu').setAttribute('disabled','true'); 
671             } );
672             if (sdist_tree.basic_summary()) {
673                 obj.funcs.push(
674                     function(c,a) {
675                         return function() {
676                             obj.append_member(c,a,[],'sbsum', false);
677                         }
678                     }( sdist_tree.basic_summary(), sdist_tree )
679                 );
680             }
681             if (sdist_tree.supplement_summary()) {
682                 obj.funcs.push(
683                     function(c,a) {
684                         return function() {
685                             obj.append_member(c,a,[],'sssum', false);
686                         }
687                     }( sdist_tree.supplement_summary(), sdist_tree )
688                 );
689             }
690             if (sdist_tree.index_summary()) {
691                 obj.funcs.push(
692                     function(c,a) {
693                         return function() {
694                             obj.append_member(c,a,[],'sisum', false);
695                         }
696                     }( sdist_tree.index_summary(), sdist_tree )
697                 );
698             }
699             if (sdist_tree.streams()) {
700                 for (var i = 0; i < sdist_tree.streams().length; i++) {
701                     obj.funcs.push(
702                         function(c,a) {
703                             return function() {
704                                 obj.append_member(c,a,[],'sstr', true);
705                             }
706                         }( sdist_tree.streams()[i], sdist_tree )
707                     )
708                 }
709             }
710             /* TODO: template editing would be convenient here, but a little too confusing
711             // add template nodes
712             var same_templates;
713             var has_bind_template;
714             if (sdist_tree.receive_unit_template()) {
715                 if (sdist_tree.bind_unit_template()) {
716                     has_bind_template = true;                    
717                     if (sdist_tree.receive_unit_template().id() == sdist_tree.bind_unit_template().id()) {
718                         same_templates = true;
719                         obj.funcs.push(
720                             function(c,a) {
721                                 return function() {
722                                     obj.append_member(c,a,[],'act', false, 'Receive/Bind Unit Template');
723                                 }
724                             }( sdist_tree.receive_unit_template(), sdist_tree )
725                         )
726                     }
727                 }
728
729                 if (!same_templates) {
730                     obj.funcs.push(
731                         function(c,a) {
732                             return function() {
733                                 obj.append_member(c,a,[],'act', false, 'Receive Unit Template');
734                             }
735                         }( sdist_tree.receive_unit_template(), sdist_tree )
736                     )
737                 }
738             }
739             if (has_bind_template && !same_templates) {
740                 obj.funcs.push(
741                     function(c,a) {
742                         return function() {
743                             obj.append_member(c,a,[],'act', false, 'Bind Unit Template');
744                         }
745                     }( sdist_tree.bind_unit_template(), sdist_tree )
746                 )
747             }
748             */
749             obj.funcs.push( function() { 
750                 document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
751                 document.getElementById('sdist_lib_menu').setAttribute('disabled','false'); 
752             } );
753         } catch(E) {
754             alert(E);
755         }
756     },
757
758     'on_click_sdist' : function(sdist_ids,twisty) {
759         var obj = this;
760         try {
761             // draw sdist editor
762             if (typeof twisty == 'undefined') {
763                 var params = {};
764                 params.sdist_ids = sdist_ids;
765                 params.xul_id_prefix = 'sdist2';
766                 obj.editor_init('sdist', 'edit', params);
767             }
768         } catch(E) {
769             alert(E);
770         }
771     },
772
773     'on_click_sstr' : function(sstr_ids,twisty) {
774         var obj = this;
775         try {
776             // draw sstr editor
777             if (typeof twisty == 'undefined') {
778                 var params = {};
779                 params.sstr_ids = sstr_ids;
780                 obj.editor_init('sstr', 'edit', params);
781             }
782         } catch(E) {
783             alert(E);
784         }
785     },
786
787     'on_click_sbsum' : function(sbsum_ids,twisty) {
788         var obj = this;
789         try {
790             // draw sbsum editor
791             if (typeof twisty == 'undefined') {
792                 var params = {};
793                 params.sbsum_ids = sbsum_ids;
794                 obj.editor_init('sbsum', 'edit', params);
795             }
796         } catch(E) {
797             alert(E);
798         }
799     },
800
801     'on_click_sssum' : function(sssum_ids,twisty) {
802         var obj = this;
803         try {
804             // draw sssum editor
805             if (typeof twisty == 'undefined') {
806                 var params = {};
807                 params.sssum_ids = sssum_ids;
808                 obj.editor_init('sssum', 'edit', params);
809             }
810         } catch(E) {
811             alert(E);
812         }
813     },
814
815     'on_click_sisum' : function(sisum_ids,twisty) {
816         var obj = this;
817         try {
818             // draw sisum editor
819             if (typeof twisty == 'undefined') {
820                 var params = {};
821                 params.sisum_ids = sisum_ids;
822                 obj.editor_init('sisum', 'edit', params);
823             }
824         } catch(E) {
825             alert(E);
826         }
827     },
828
829     'on_click_aou' : function(org_id,twisty) {
830         var obj = this;
831         var org = obj.data.hash.aou[ org_id ];
832         var default_aou = obj.data.hash.aou[obj.default_lib];
833         obj.funcs.push( function() { 
834             document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
835             document.getElementById('sdist_lib_menu').setAttribute('disabled','true'); 
836         } );
837         if (org.children()) {
838             for (var i = 0; i < org.children().length; i++) {
839                 var child = org.children()[i];
840                 if (orgIsMine(default_aou,child)) {
841                     obj.funcs.push(
842                         function(o,p) {
843                             return function() {
844                                 obj.append_org(o,p)
845                             }
846                         }(child,org)
847                     );
848                 }
849             }
850         } 
851         if (obj.map_sdist[ 'aou_' + org_id ]) {
852             for (var i = 0; i < obj.map_sdist[ 'aou_' + org_id ].length; i++) {
853                 obj.funcs.push(
854                     function(o,a) {
855                         return function() {
856                             obj.append_sdist(o,a);
857                         }
858                     }( org, obj.map_sdist[ 'aou_' + org_id ][i] )
859                 );
860             }
861         }
862         obj.funcs.push( function() { 
863             document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
864             document.getElementById('sdist_lib_menu').setAttribute('disabled','false'); 
865         } );
866
867         // remove current editor
868         if (typeof twisty == 'undefined') {
869             document.getElementById('serial_manage_dists_editor_deck').selectedIndex = 0;
870         }
871     },
872
873     'append_org' : function (org,parent_org,params) {
874         var obj = this;
875         try {
876             if (obj.map_tree[ 'aou_' + org.id() ]) {
877                 var x = obj.map_tree[ 'aou_' + org.id() ];
878                 if (params) {
879                     for (var i in params) {
880                         x.setAttribute(i,params[i]);
881                     }
882                 }
883                 return x;
884             }
885
886             var data = {
887                 'row' : {
888                     'my' : {
889                         'aou' : org,
890                     }
891                 },
892                 'skip_all_columns_except' : [0,1,2],
893                 'retrieve_id' : 'aou_' + org.id(),
894                 'to_bottom' : true,
895                 'no_auto_select' : true,
896             };
897         
898             var sdist_tree_list;
899             if ( obj.org_ids.indexOf( Number( org.id() ) ) == -1 ) {
900                 if ( get_bool( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) ) {
901                     data.row.my.distribution_count = '0';
902                 } else {
903                     data.row.my.distribution_count = '';
904                 }
905             } else {
906                 var d_count = 0;
907                 sdist_tree_list = obj.network.simple_request(
908                     'FM_SDIST_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS.authoritative',
909                     [ ses(), obj.docid, [ org.id() ] ]
910                 );
911                 for (var i = 0; i < sdist_tree_list.length; i++) {
912                     d_count++;
913                     obj.map_sdist[ 'sdist_' + sdist_tree_list[i].id() ] = function(r){return r;}(sdist_tree_list[i]);
914                     var streams = sdist_tree_list[i].streams();
915                     for (var j = 0; j < streams.length; j++) {
916                         obj.map_sstr[ 'sstr_' + streams[j].id() ] = function(r){return r;}(streams[j]);
917                     }
918                 }
919                 data.row.my.distribution_count = d_count;
920             }
921             if (parent_org) {
922                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
923             }
924             var nparams = obj.list.append(data);
925             var node = nparams.treeitem_node;
926             if (params) {
927                 for (var i in params) {
928                     node.setAttribute(i,params[i]);
929                 }
930             }
931             obj.map_tree[ 'aou_' + org.id() ] = node;
932
933             if (org.children()) {
934                 node.setAttribute('container','true');
935             }
936
937             if (parent_org) {
938                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
939                     data.node.setAttribute('open','true');
940                     obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
941                 }
942             } else {
943                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
944                 obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
945             }
946
947             if (sdist_tree_list) {
948                 obj.map_sdist[ 'aou_' + org.id() ] = sdist_tree_list;
949                 node.setAttribute('container','true');
950             }
951
952             if (document.getElementById('show_sdists').checked) {
953                 obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
954                 node.setAttribute('open','true');
955             }
956
957         } catch(E) {
958             dump(E+'\n');
959             alert(E);
960         }
961     },
962
963     'append_sdist' : function( org, sdist_tree, params ) {
964         var obj = this;
965         try {
966             if (obj.map_tree[ 'sdist_' + sdist_tree.id() ]) {
967                 var x = obj.map_tree[ 'sdist_' + sdist_tree.id() ];
968                 if (params) {
969                     for (var i in params) {
970                         x.setAttribute(i,params[i]);
971                     }
972                 }
973                 return x;
974             }
975
976             var parent_node = obj.map_tree[ 'aou_' + org.id() ];
977             var data = {
978                 'row' : {
979                     'my' : {
980                         'aou' : org,
981                         'sdist' : sdist_tree,
982                         'distribution_count' : ''
983                     }
984                 },
985                 'skip_all_columns_except' : [0,1,2],
986                 'retrieve_id' : 'sdist_' + sdist_tree.id(),
987                 'node' : parent_node,
988                 'to_bottom' : true,
989                 'no_auto_select' : true,
990             };
991             var nparams = obj.list.append(data);
992             var node = nparams.treeitem_node;
993             obj.map_tree[ 'sdist_' + sdist_tree.id() ] =  node;
994             if (params) {
995                 for (var i in params) {
996                     node.setAttribute(i,params[i]);
997                 }
998             }
999             node.setAttribute('container','true');
1000             if (document.getElementById('show_sdist_groups').checked) {
1001                 node.setAttribute('open','true');
1002                 obj.funcs.push( function() { obj.on_select_sdist( sdist_tree.id(), true ); } );
1003             }
1004             var sstr_group_node_data = {
1005                 'row' : {
1006                     'my' : {
1007                         'label' : $('serialStrings').getString('serial.manage_dists.streams'),
1008                     }
1009                 },
1010                 'retrieve_id' : 'sstr-group_' + sdist_tree.id(),
1011                 'node' : node,
1012                 'to_bottom' : true,
1013                 'no_auto_select' : true,
1014             };
1015             nparams = obj.list.append(sstr_group_node_data);
1016             obj.map_tree[ 'sdist_sstr_group_' + sdist_tree.id() ] =  nparams.treeitem_node;
1017         } catch(E) {
1018             dump(E+'\n');
1019             alert(E);
1020         }
1021     },
1022
1023     'append_member' : function( item, sdist_tree, attributes, type, group, label ) {
1024         var obj = this;
1025         try {
1026             if (obj.map_tree[ type + '_' + sdist_tree.id() + '_' + item.id() ]) {
1027                 var x = obj.map_tree[ type + '_' + item.id() ];
1028                 if (attributes) {
1029                     for (var i in attributes) {
1030                         x.setAttribute(i,attributes[i]);
1031                     }
1032                 }
1033                 return x;
1034             }
1035
1036             var parent_node;
1037             if (group) {
1038                 parent_node = obj.map_tree[ 'sdist_' + type + '_group_' + sdist_tree.id() ];
1039             } else {
1040                 parent_node = obj.map_tree[ 'sdist_' + sdist_tree.id() ];
1041             }
1042             var data = {
1043                 'row' : {
1044                     'my' : {
1045                         'aou' : obj.data.hash.aou[ sdist_tree.holding_lib() ],
1046                         'sdist' : sdist_tree,
1047                         'distribution_count' : ''
1048                     }
1049                 },
1050                 'retrieve_id' : type + '_' + item.id(),
1051                 'node' : parent_node,
1052                 'to_bottom' : true,
1053                 'no_auto_select' : true,
1054             };
1055             data['row']['my'][type] = item; // TODO: future optimization: get only the IDs of these leaves, then fetch the full row in 'retrieve_row'
1056             var nparams = obj.list.append(data);
1057             var node = nparams.treeitem_node;
1058             obj.map_tree[ type + '_' + sdist_tree.id() + '_' + item.id() ] =  node;
1059             if (label) {
1060                 data['row']['my']['label'] = label;
1061             }
1062             if (attributes) {
1063                 for (var i in attributes) {
1064                     node.setAttribute(i,attributes[i]);
1065                 }
1066             }
1067
1068         } catch(E) {
1069             dump(E+'\n');
1070             alert(E);
1071         }
1072     },
1073
1074     'list_init' : function( params ) {
1075
1076         try {
1077             var obj = this;
1078             
1079             JSAN.use('circ.util');
1080             var columns = [
1081                 {
1082                     'id' : 'tree_location',
1083                     'label' : 'Location',
1084                     'flex' : 1, 'primary' : true, 'hidden' : false, 
1085                     'render' : function(my) { 
1086                         if (my.label) { return my.label; }
1087                         if (my.sstr) { return $('serialStrings').getFormattedString('serial.manage_dists.stream_num', [my.sstr.id()]); }
1088                         if (my.sbsum) { return $('serialStrings').getString('serial.manage_dists.sbsum'); }
1089                         if (my.sssum) { return $('serialStrings').getString('serial.manage_dists.sssum'); }
1090                         if (my.sisum) { return $('serialStrings').getString('serial.manage_dists.sisum'); }
1091                         if (my.sdist) { return my.sdist.label(); }
1092                         if (my.aou) { return $('serialStrings').getFormattedString('serial.manage_dists.library_label', [my.aou.shortname(), my.aou.name()]); }
1093                         return "???";
1094                     },
1095                 },
1096                 {
1097                     'id' : 'distribution_count',
1098                     'label' : $('serialStrings').getString('serial.manage_dists.distributions'),
1099                     'flex' : 0, 'primary' : false, 'hidden' : false, 
1100                     'render' : function(my) { return my.distribution_count; },
1101                 }
1102             ];
1103             JSAN.use('util.list'); obj.list = new util.list('sdists_tree');
1104             obj.list.init(
1105                 {
1106                     'no_auto_select' : true,
1107                     'columns' : columns,
1108                     'retrieve_row' : function(params) {
1109
1110                         var row = params.row;
1111                         obj.funcs.push(
1112                             function() {
1113
1114                                 if (typeof params.on_retrieve == 'function') {
1115                                     params.on_retrieve(row);
1116                                 }
1117
1118                             }
1119                         );
1120
1121                         return row;
1122                     },
1123                     'on_click' : function(ev) {
1124                         var row = {}; var col = {}; var nobj = {};
1125                         obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj); 
1126                         if ((row.value == -1)||(nobj.value != 'twisty')) { return; } // on_click runs for twistys only
1127
1128                         var node = obj.list.node.contentView.getItemAtIndex(row.value);
1129                         var list = [ node.getAttribute('retrieve_id') ];
1130                         if (typeof obj.on_select == 'function') {
1131                             obj.on_select(list,true);
1132                         }
1133                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1134                             window.xulG.on_select(list);
1135                         }
1136                     },
1137                     'on_select' : function(ev) {
1138                         JSAN.use('util.functional');
1139                         
1140                         // get the actual node clicked to determine which editor to use
1141                         if (obj.list.node.view.selection.currentIndex > -1) {
1142                             var node = obj.list.node.contentView.getItemAtIndex(obj.list.node.view.selection.currentIndex);
1143                             obj.focused_node_retrieve_id = node.getAttribute('retrieve_id');
1144                         }
1145
1146                         var sel = obj.list.retrieve_selection();
1147                         obj.controller.view.sel_clip.disabled = sel.length < 1;
1148                         obj.sel_list = util.functional.map_list(
1149                             sel,
1150                             function(o) { return o.getAttribute('retrieve_id'); }
1151                         );
1152                         obj.toggle_actions();
1153                         if (typeof obj.on_select == 'function') {
1154                             obj.on_select(obj.sel_list);
1155                         }
1156                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1157                             window.xulG.on_select(obj.sel_list);
1158                         }
1159                     },
1160                 }
1161             );
1162
1163             obj.controller.render();
1164
1165         } catch(E) {
1166             this.error.sdump('D_ERROR','serial/manage_dists.list_init: ' + E + '\n');
1167             alert(E);
1168         }
1169     },
1170
1171     'toggle_actions' : function() {
1172         var obj = this;
1173         try {
1174             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;
1175             for (var i = 0; i < obj.sel_list.length; i++) {
1176                 var type = obj.sel_list[i].split(/_/)[0];
1177                 switch(type) {
1178                     case 'aou' : 
1179                         found_aou = true; 
1180                     break;
1181                     case 'sdist' : found_sdist = true; break;
1182                     case 'sstr' : found_sstr = true; break;
1183                     case 'sbsum' : found_sbsum = true; break;
1184                     case 'sssum' : found_sssum = true; break;
1185                     case 'sisum' : found_sisum = true; break;
1186                     case 'sstr-group' : found_sstr_group = true; break;
1187                 }
1188             }
1189             obj.controller.view.cmd_add_sstr.setAttribute('disabled','true');
1190             obj.controller.view.cmd_delete_sstr.setAttribute('disabled','true');
1191             obj.controller.view.cmd_mark_library.setAttribute('disabled','true');
1192             //obj.controller.view.cmd_delete_sdist.setAttribute('disabled','true');
1193             if (found_aou) {
1194                 obj.controller.view.cmd_mark_library.setAttribute('disabled','false');
1195             }
1196             if (found_sdist) {
1197                 //obj.controller.view.cmd_delete_sdist.setAttribute('disabled','false');
1198                 obj.controller.view.cmd_add_sstr.setAttribute('disabled','false');
1199             }
1200             if (found_sstr_group) {
1201                 obj.controller.view.cmd_add_sstr.setAttribute('disabled','false');
1202             }
1203             if (found_sstr) {
1204                 obj.controller.view.cmd_delete_sstr.setAttribute('disabled','false');
1205                 obj.controller.view.cmd_transfer_sstrs.setAttribute('disabled','false');
1206             }
1207             if (found_sbsum) {
1208             }
1209             if (found_sssum) {
1210             }
1211             if (found_sisum) {
1212             }
1213         } catch(E) {
1214             obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.actions.error'),E);
1215         }
1216     },
1217
1218     'refresh_list' : function() { 
1219         try {
1220             var obj = this;
1221             obj.list.clear();
1222             obj.map_tree = {};
1223             obj.map_sdist = {};
1224             obj.map_sstr = {};
1225             obj.org_ids = obj.network.simple_request('FM_SDIST_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
1226             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
1227             JSAN.use('util.functional'); 
1228             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
1229             /*
1230             var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
1231             obj.show_libs( org );
1232             */
1233             obj.default_lib = document.getElementById('sdist_lib_menu').value;
1234             obj.show_my_libs( obj.default_lib );
1235         } catch(E) {
1236             this.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.refresh_list.error'),E);
1237         }
1238     },
1239 };
1240
1241 dump('exiting serial/manage_dists.js\n');