Move serials interfaces closer together
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / serial / manage_subs.js
1 dump('entering serial/manage_subs.js\n');
2 // vim:et:sw=4:ts=4:
3
4 if (typeof serial == 'undefined') serial = {};
5 serial.manage_subs = function (params) {
6     try {
7         JSAN.use('util.error'); this.error = new util.error();
8     } catch(E) {
9         dump('serial/manage_subs: ' + E + '\n');
10     }
11 };
12
13 serial.manage_subs.prototype = {
14
15     'map_tree' : {},
16     'map_ssub' : {},
17     'map_sdist' : {},
18     'map_siss' : {},
19     'map_scap' : {},
20     'sel_list' : [],
21     'funcs' : [],
22     'editor_indexes' : { 'ssub' : 1, 'sdist' : 2, 'siss' : 3, 'scap' : 4 },
23
24     'ids_from_sel_list' : function(type) {
25         var obj = this;
26         JSAN.use('util.functional');
27
28         var list = util.functional.map_list(
29             util.functional.filter_list(
30                 obj.sel_list,
31                 function (o) {
32                     return o.split(/_/)[0] == type;
33                 }
34             ),
35             function (o) {
36                 return o.split(/_/)[1];
37             }
38         );
39
40         return list;
41     },
42
43     'editor_init' : function(type, mode, params) {
44         var obj = this;
45         try {
46             $('serial_manage_subs_editor_deck').selectedIndex = obj.editor_indexes[type];
47
48             if (type == "siss") { // begin transition from xul to dojo editors
49                 var iframe = dojo.byId('alt_siss_editor');
50                 var src;
51                 if (mode == "add") {
52                     src = '/eg/serial/edit_siss/new/' + params.sisses[0].subscription();
53                     iframe.refresh_command = function () {obj.refresh_list();};
54                 } else {
55                     src = '/eg/serial/edit_siss/' + params.siss_ids[0];
56                     iframe.refresh_command = function () { /* TODO: redraw tree node */ };
57                 }
58                 iframe.setAttribute("src", src);
59             } else if (type == "ssub") {
60                 var iframe = dojo.byId('alt_ssub_editor');
61                 var src;
62                 if (mode == "add") {
63                     src = '/eg/serial/subscription?id=new&owning_lib='+params.owning_lib+'&record_entry='+params.record_entry+'&context=scv';
64                     iframe.refresh_command = function () {obj.refresh_list();};
65                 } else {
66                     src = '/eg/serial/subscription?id=' + params.ssub_ids[0] + '&context=scv';
67                     iframe.refresh_command = function () {}; //TODO: redraw tree node
68                 }
69                 iframe.setAttribute("src", src);
70             } else {
71                 var editor_type = type + '_editor';
72                 if (typeof obj[editor_type] == 'undefined') {
73                     JSAN.use('serial.' + editor_type);
74                     obj[editor_type] = new serial[editor_type]();
75                 }
76
77                 params.do_edit = true;
78                 params.handle_update = true;
79                 params.trigger_refresh = true;
80                 if (mode == 'add') {
81                     params.refresh_command = function () {obj.refresh_list();};
82                 } else {
83                     params.refresh_command = function () {obj.remap_node(type, this);};
84                 }
85
86                 obj[editor_type].init(params);
87             }
88         } catch(E) {
89             obj.error.standard_unexpected_error_alert('editor_init() error',E);
90         }
91     },
92
93     // while not a true tree node repace, this should at least prevent
94     // non-display side-effects.  True node replace is TODO
95     'remap_node' : function(type, editor_obj) {
96         var obj = this;
97         try {
98             for (i = 0; i < editor_obj[editor_obj.fm_type_plural].length; i++) {
99                 var new_obj = editor_obj[editor_obj.fm_type_plural][i];
100                 var old_obj = obj['map_' + type][type + '_' + new_obj.id()];
101                 if (type == 'ssub') { // add children back on
102                     new_obj.distributions(old_obj.distributions());
103                     new_obj.issuances(old_obj.issuances());
104                     new_obj.scaps(old_obj.scaps());
105                 }
106                 obj['map_' + type][type + '_' + new_obj.id()] = new_obj;
107             }
108             editor_obj.render();
109         } catch(E) {
110             obj.error.standard_unexpected_error_alert('remap_node() error',E);
111         }
112     },
113
114     'do_delete' : function(type, method, overridable_events) {
115         var obj = this;
116         try {
117             JSAN.use('util.functional');
118
119             var list = util.functional.filter_list(
120                 obj.sel_list,
121                 function (o) {
122                     return o.split(/_/)[0] == type;
123                 }
124             );
125
126             list = util.functional.map_list(
127                 list,
128                 function (o) {
129                     return JSON2js( js2JSON( obj['map_' + type][ type + '_' + o.split(/_/)[1] ] ) );
130                 }
131             );
132
133             //TODO: proper messages
134             var delete_msg;
135             if (list.length != 1) {
136                 delete_msg = document.getElementById('serialStrings').getFormattedString('staff.serial.manage_subs.delete_' + type + '.confirm.plural', [list.length]);
137             } else {
138                 delete_msg = document.getElementById('serialStrings').getString('staff.serial.manage_subs.delete_' + type + '.confirm');
139             }
140             var r = obj.error.yns_alert(
141                     delete_msg,
142                     document.getElementById('serialStrings').getString('staff.serial.manage_subs.delete_' + type + '.title'),
143                     document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.delete'),
144                     document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_items.cancel'),
145                     null,
146                     document.getElementById('commonStrings').getString('common.confirm')
147             );
148
149             if (r == 0) {
150                 for (var i = 0; i < list.length; i++) {
151                     list[i].isdeleted('1');
152                 }
153                 var robj = obj.network.request(
154                     'open-ils.serial', 
155                     method, 
156                     [ ses(), list, true ],
157                     null,
158                     {
159                         'title' : document.getElementById('serialStrings').getString('staff.serial.manage_subs.delete_' + type + '.override'),
160                         'overridable_events' : overridable_events
161                     }
162                 );
163                 if (robj == null) throw(robj);
164                 if (typeof robj.ilsevent != 'undefined') {
165                     if (robj.ilsevent != 0) {
166                         var overridable = false;
167                         for (i = 0; i < overridable_events.length; i++) {
168                             if (overridable_events[i] == robj.ilsevent) {
169                                 overridable = true;
170                                 break;
171                             }
172                         }
173                         if (!overridable) throw(robj);
174                     }
175                 }
176                 obj.refresh_list();
177             }
178         } catch(E) {
179             obj.error.standard_unexpected_error_alert(document.getElementById('serialStrings').getString('staff.serial.manage_subs.delete.error'),E);
180             obj.refresh_list();
181         }
182     },
183
184     'init' : function( params ) {
185
186         try {
187             var obj = this;
188
189             obj.docid = params.docid;
190
191             JSAN.use('util.network'); obj.network = new util.network();
192             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
193             JSAN.use('util.controller'); obj.controller = new util.controller();
194             obj.controller.init(
195                 {
196                     control_map : {
197                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
198                         'sel_clip' : [
199                             ['command'],
200                             function() { obj.list.clipboard(); }
201                         ],
202                         'cmd_broken' : [
203                             ['command'],
204                             function() { 
205                                 alert(document.getElementById('commonStrings').getString('common.unimplemented'));
206                             }
207                         ],
208                         'cmd_show_my_libs' : [
209                             ['command'],
210                             function() { 
211                                 obj.show_my_libs(); 
212                             }
213                         ],
214                         'cmd_show_all_libs' : [
215                             ['command'],
216                             function() {
217                                 obj.show_all_libs();
218                             }
219                         ],
220                         'cmd_clear' : [
221                             ['command'],
222                             function() {
223                                 obj.map_tree = {};
224                                 obj.list.clear();
225                             }
226                         ],
227                         'cmd_add_scap' : [
228                             ['command'],
229                             function() {
230                                 try {
231                                     var list = obj.ids_from_sel_list('ssub');
232                                     if (list.length == 0) list = obj.ids_from_sel_list('scap-group');
233                                     if (list.length == 0) return;
234
235                                     /*TODO: permission check?
236                                     //populate 'list' with owning_libs of subs, TODO
237                                     var edit = 0;
238                                     try {
239                                         edit = obj.network.request(
240                                             api.PERM_MULTI_ORG_CHECK.app,
241                                             api.PERM_MULTI_ORG_CHECK.method,
242                                             [ 
243                                                 ses(), 
244                                                 obj.data.list.au[0].id(), 
245                                                 list,
246                                                 [ 'CREATE_COPY' ]
247                                             ]
248                                         ).length == 0 ? 1 : 0;
249                                     } catch(E) {
250                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
251                                     }
252
253                                     if (edit==0) return; // no read-only view for this interface */
254                                     var new_scap = new scap();
255                                     new_scap.subscription(list[0]);//TODO: add multiple at once support?
256                                     new_scap.isnew(1);
257                                     var params = {};
258                                     params.scaps = [new_scap];
259                                     obj.editor_init('scap', 'add', params);
260                                 } catch(E) {
261                                     obj.error.standard_unexpected_error_alert(document.getElementById('serialStrings').getString('staff.serial.manage_subs.add.error'),E);
262                                 }
263                             }
264                         ],
265                         'cmd_add_siss' : [
266                             ['command'],
267                             function() {
268                                 try {
269                                     var list = obj.ids_from_sel_list('ssub');
270                                     if (list.length == 0) list = obj.ids_from_sel_list('siss-group');
271                                     if (list.length == 0) return;
272
273                                     /*TODO: permission check?
274                                     //populate 'list' with owning_libs of subs, TODO
275                                     var edit = 0;
276                                     try {
277                                         edit = obj.network.request(
278                                             api.PERM_MULTI_ORG_CHECK.app,
279                                             api.PERM_MULTI_ORG_CHECK.method,
280                                             [ 
281                                                 ses(), 
282                                                 obj.data.list.au[0].id(), 
283                                                 list,
284                                                 [ 'CREATE_COPY' ]
285                                             ]
286                                         ).length == 0 ? 1 : 0;
287                                     } catch(E) {
288                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
289                                     }
290
291                                     if (edit==0) return; // no read-only view for this interface */
292                                     var new_siss = new siss();
293                                     new_siss.subscription(list[0]);//TODO: add multiple at once support?
294                                     new_siss.isnew(1);
295                                     var params = {};
296                                     params.sisses = [new_siss];
297                                     obj.editor_init('siss', 'add', params);
298                                 } catch(E) {
299                                     obj.error.standard_unexpected_error_alert(document.getElementById('serialStrings').getString('staff.serial.manage_subs.add.error'),E);
300                                 }
301                             }
302                         ],
303                         'cmd_add_sdist' : [
304                             ['command'],
305                             function() {
306                                 try {
307                                     var list = obj.ids_from_sel_list('ssub');
308                                     if (list.length == 0) list = obj.ids_from_sel_list('sdist-group');
309                                     if (list.length == 0) return;
310
311                                     /*TODO: permission check?
312                                     //populate 'list' with owning_libs of subs, TODO
313                                     var edit = 0;
314                                     try {
315                                         edit = obj.network.request(
316                                             api.PERM_MULTI_ORG_CHECK.app,
317                                             api.PERM_MULTI_ORG_CHECK.method,
318                                             [ 
319                                                 ses(), 
320                                                 obj.data.list.au[0].id(), 
321                                                 list,
322                                                 [ 'CREATE_COPY' ]
323                                             ]
324                                         ).length == 0 ? 1 : 0;
325                                     } catch(E) {
326                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
327                                     }
328
329                                     if (edit==0) return; // no read-only view for this interface */
330                                     var new_sdist = new sdist();
331                                     new_sdist.subscription(list[0]);//TODO: add multiple at once support?
332                                     new_sdist.holding_lib(obj.map_ssub['ssub_' + list[0]].owning_lib());//default to sub owning lib
333                                     new_sdist.label($('serialStrings').getString('serial.common.default'));
334                                     new_sdist.isnew(1);
335                                     var params = {};
336                                     params.sdists = [new_sdist];
337                                     obj.editor_init('sdist', 'add', params);
338                                 } catch(E) {
339                                     obj.error.standard_unexpected_error_alert(document.getElementById('serialStrings').getString('staff.serial.manage_subs.add.error'),E);
340                                 }
341                             }
342                         ],
343                         'cmd_delete_scap' : [
344                             ['command'],
345                             function() {
346                                 var overridable_events = [
347                                     11001 // SERIAL_CAPTION_AND_PATTERN_HAS_ISSUANCES
348                                 ];
349                                 obj.do_delete('scap', 'open-ils.serial.caption_and_pattern.batch.update', overridable_events);
350                             }
351                         ],
352                         'cmd_delete_sdist' : [
353                             ['command'],
354                             function() {
355                                 var overridable_events = [ //TODO: proper overrides
356                                 ];
357                                 obj.do_delete('sdist', 'open-ils.serial.distribution.fleshed.batch.update', overridable_events);
358                             }
359                         ],
360                         'cmd_delete_siss' : [
361                             ['command'],
362                             function() {
363                                 var overridable_events = [ //TODO: proper overrides
364                                 ];
365                                 obj.do_delete('siss', 'open-ils.serial.issuance.fleshed.batch.update', overridable_events);
366                             }
367                         ],
368                         'cmd_delete_ssub' : [
369                             ['command'],
370                             function() {
371                                 var overridable_events = [
372                                     11000 // SERIAL_SUBSCRIPTION_NOT_EMPTY
373                                 ];
374                                 obj.do_delete('ssub', 'open-ils.serial.subscription.fleshed.batch.update', overridable_events);
375                             }
376                         ],
377                         /*dbw2 'cmd_delete_ssub' : [
378                             ['command'],
379                             function() {
380                                 try {
381                                     JSAN.use('util.functional');
382
383                                     var list = util.functional.filter_list(
384                                         obj.sel_list,
385                                         function (o) {
386                                             return o.split(/_/)[0] == 'ssub';
387                                         }
388                                     );
389
390                                     list = util.functional.map_list(
391                                         list,
392                                         function (o) {
393                                             return JSON2js( js2JSON( obj.map_ssub[ 'ssub_' + o.split(/_/)[1] ] ) );
394                                         }
395                                     );
396
397                                     var del_prompt;
398                                     if (list.length == 1) {
399                                         //TODO: correct prompts
400                                         del_prompt = document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.prompt');
401                                     } else {
402                                         del_prompt = document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.delete_volume.prompt.plural', [list.length]);
403                                     }
404
405                                     var r = obj.error.yns_alert(
406                                             del_prompt,
407                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.title'),
408                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.delete'),
409                                             document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.cancel'),
410                                             null,
411                                             document.getElementById('commonStrings').getString('common.confirm')
412                                     );
413
414                                     if (r == 0) {
415                                         for (var i = 0; i < list.length; i++) {
416                                             list[i].isdeleted('1');
417                                         }
418                                         var robj = obj.network.simple_request(
419                                             'FM_ACN_TREE_UPDATE', 
420                                             [ ses(), list, true ],
421                                             null,
422                                             {
423                                                 'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.override'),
424                                                 'overridable_events' : [
425                                                 ]
426                                             }
427                                         );
428                                         if (robj == null) throw(robj);
429                                         if (typeof robj.ilsevent != 'undefined') {
430                                             if (robj.ilsevent == 1206 ) { // VOLUME_NOT_EMPTY
431                                                 alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.copies_remain'));
432                                                 return;
433                                             }
434                                             if (robj.ilsevent != 0) throw(robj);
435                                         }
436                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.success'));
437                                         obj.refresh_list();
438                                     }
439                                 } catch(E) {
440                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.delete_volume.exception'),E);
441                                     obj.refresh_list();
442                                 }
443
444                             }
445                         ], dbw2*/
446                         'cmd_mark_library' : [
447                             ['command'],
448                             function() {
449                                 try {
450                                     var list = obj.ids_from_sel_list('aou');
451                                     if (list.length == 1) {
452                                         obj.data.marked_library = { 'lib' : list[0], 'docid' : obj.docid };
453                                         obj.data.stash('marked_library');
454                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.alert'));
455                                     } else {
456                                         obj.error.yns_alert(
457                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.prompt'),
458                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_library.title'),
459                                                 document.getElementById('commonStrings').getString('common.ok'),
460                                                 null,
461                                                 null,
462                                                 document.getElementById('commonStrings').getString('common.confirm')
463                                                 );
464                                     }
465                                 } catch(E) {
466                                     obj.error.standard_unexpected_error_alert('manage_subs.js -> mark library',E);
467                                 }
468                             }
469                         ],
470
471                         'cmd_mark_subscription' : [
472                             ['command'],
473                             function() {
474                                 try {
475                                     var list = obj.ids_from_sel_list('ssub');
476                                     if (list.length == 1) {
477                                         obj.data.marked_subscription = list[0];
478                                         obj.data.stash('marked_subscription');
479                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.alert'));
480                                     } else {
481                                         obj.error.yns_alert(
482                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.prompt'),
483                                                 document.getElementById('catStrings').getString('staff.cat.copy_browser.mark_volume.title'),
484                                                 document.getElementById('commonStrings').getString('common.ok'),
485                                                 null,
486                                                 null,
487                                                 document.getElementById('commonStrings').getString('common.confirm')
488                                                 );
489                                     }
490                                 } catch(E) {
491                                     obj.error.standard_unexpected_error_alert('manage_subs.js -> mark subscription',E);
492                                 }
493                             }
494                         ],
495                         'cmd_add_subscriptions' : [
496                             ['command'],
497                             function() {
498                                 try {
499                                     var list = obj.ids_from_sel_list('aou');
500                                     if (list.length == 0) return;
501                                     //TODO: permission check?
502                                     /*var edit = 0;
503                                     try {
504                                         edit = obj.network.request(
505                                             api.PERM_MULTI_ORG_CHECK.app,
506                                             api.PERM_MULTI_ORG_CHECK.method,
507                                             [ 
508                                                 ses(), 
509                                                 obj.data.list.au[0].id(), 
510                                                 list,
511                                                 [ 'CREATE_VOLUME', 'CREATE_COPY' ]
512                                             ]
513                                         ).length == 0 ? 1 : 0;
514                                     } catch(E) {
515                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
516                                     }
517
518                                     if (edit==0) {
519                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.add_volume.permission_error'));
520                                         return; // no read-only view for this interface
521                                     } */
522                                     var params = {};
523                                     params.owning_lib = list[0];
524                                     params.record_entry = obj.docid;
525                                     obj.editor_init('ssub', 'add', params);
526                                 } catch(E) {
527                                     obj.error.standard_unexpected_error_alert(document.getElementById('serialStrings').getString('staff.serial.manage_subs.add.error'),E);
528                                 }
529                             }
530                         ],
531                         'cmd_transfer_subscription' : [
532                             ['command'],
533                             function() {
534                                 try {
535                                     obj.data.stash_retrieve();
536                                     if (!obj.data.marked_library) {
537                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_volume.alert'));
538                                         return;
539                                     }
540                                     
541                                     var list = obj.ids_from_sel_list('ssub');
542
543                                     JSAN.use('util.functional');
544
545                                     var ssub_list = util.functional.map_list(
546                                         list,
547                                         function (o) {
548                                             return obj.map_ssub[ 'ssub_' + o ].start_date();
549                                         }
550                                     ).join(document.getElementById('commonStrings').getString('common.grouping_string'));
551
552                                     var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
553                                     xml += '<description>';
554                                     xml += document.getElementById('catStrings').getFormattedString('staff.cat.copy_browser.transfer.prompt', [ssub_list, obj.data.hash.aou[ obj.data.marked_library.lib ].shortname()]);
555                                     xml += '</description>';
556                                     xml += '<hbox><button label="' + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.submit.label') + '" name="fancy_submit"/>';
557                                     xml += '<button label="' 
558                                         + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.cancel.label') 
559                                         + '" accesskey="' 
560                                         + document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.cancel.accesskey') 
561                                         + '" name="fancy_cancel"/></hbox>';
562                                     xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + obj.data.marked_library.docid + '" oils_force_external="true"/>';
563                                     xml += '</vbox>';
564                                     JSAN.use('OpenILS.data');
565                                     var data = new OpenILS.data(); data.init({'via':'stash'});
566                                     //data.temp_transfer = xml; data.stash('temp_transfer');
567                                     JSAN.use('util.window'); var win = new util.window();
568                                     var fancy_prompt_data = win.open(
569                                         urls.XUL_FANCY_PROMPT,
570                                         //+ '?xml_in_stash=temp_transfer'
571                                         //+ '&title=' + window.escape('Volume Transfer'),
572                                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
573                                         {
574                                             'xml' : xml,
575                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.title')
576                                         }
577                                     );
578
579                                     if (fancy_prompt_data.fancy_status == 'incomplete') {
580                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.incomplete'));
581                                         return;
582                                     }
583
584                                     var robj = obj.network.simple_request(
585                                         'FM_ACN_TRANSFER', 
586                                         [ ses(), { 'docid' : obj.data.marked_library.docid, 'lib' : obj.data.marked_library.lib, 'subscriptions' : list } ],
587                                         null,
588                                         {
589                                             'title' : document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.override.failure'),
590                                             'overridable_events' : [
591                                                 1208, // TITLE_LAST_COPY
592                                                 1219, // COPY_REMOTE_CIRC_LIB
593                                             ],
594                                         }
595                                     );
596
597                                     if (typeof robj.ilsevent != 'undefined') {
598                                         if (robj.ilsevent == 1221) { // ORG_CANNOT_HAVE_VOLS
599                                             alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.ineligible_destination'));
600                                         } else {
601                                             throw(robj);
602                                         }
603                                     } else {
604                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.success'));
605                                     }
606
607                                 } catch(E) {
608                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer.unexpected_error'),E);
609                                 }
610                                 obj.refresh_list();
611                             }
612                         ],
613
614                         'cmd_transfer_sdists' : [
615                             ['command'],
616                             function() {
617                                 try {
618                                     obj.data.stash_retrieve();
619                                     if (!obj.data.marked_subscription) {
620                                         alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.missing_volume'));
621                                         return;
622                                     }
623                                     
624                                     JSAN.use('util.functional');
625
626                                     var list = obj.ids_from_sel_list('sdist');
627                                     var subscription = obj.network.simple_request('FM_ACN_RETRIEVE.authoritative',[ obj.data.marked_subscription ]);
628
629                                     JSAN.use('cat.util'); cat.util.transfer_copies( { 
630                                         'distribution_ids' : list, 
631                                         'docid' : subscription.record(),
632                                         'subscription_label' : subscription.start_date(),
633                                         'owning_lib' : subscription.owning_lib(),
634                                     } );
635
636                                 } catch(E) {
637                                     obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.transfer_items.unexpected_error'),E);
638                                 }
639                                 obj.refresh_list();
640                             }
641                         ],
642                         'cmd_refresh_list' : [
643                             ['command'],
644                             function() {
645                                 obj.refresh_list();
646                             }
647                         ],
648                         'cmd_make_predictions' : [
649                             ['command'],
650                             function() {
651                                 try {
652                                     var list = obj.ids_from_sel_list('ssub');
653                                     if (list.length == 0) {
654                                         alert($('serialStrings').getString('serial.manage_subs.predict.alert')); //TODO: better error
655                                         return;
656                                     }
657
658                                     var num_to_predict = prompt($('serialStrings').getString('serial.manage_subs.predict.prompt'),
659                                             '12',
660                                             $('serialStrings').getString('serial.manage_subs.predict.prompt.text'));
661                                     num_to_predict = String( num_to_predict ).replace(/\D/g,'');
662                                     if (num_to_predict == '') {
663                                         alert($('serialStrings').getString('serial.manage_subs.invalid_number')); //TODO: better error
664                                         return;
665                                     }
666
667                                     for (i = 0; i < list.length; i++) {
668                                         var robj = obj.network.request(
669                                                 'open-ils.serial',
670                                                 'open-ils.serial.make_predictions',
671                                                 [ ses(), {"ssub_id":list[i], "num_to_predict":num_to_predict}]
672                                         );
673                                         alert($('serialStrings').getFormattedString('serial.manage_subs.predict_success', [robj.length, list[i]]));
674                                     }
675
676                                     obj.refresh_list();
677
678                                 } catch(E) {
679                                     obj.error.standard_unexpected_error_alert('cmd_make_predictions failed!',E);
680                                 }
681                             }
682                         ]
683                     }
684                 }
685             );
686
687             obj.list_init(params);
688
689             obj.org_ids = obj.network.simple_request('FM_SSUB_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
690             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
691             JSAN.use('util.functional'); 
692             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
693
694             var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
695             //obj.show_libs( org );
696
697             //obj.show_my_libs();
698
699             JSAN.use('util.file'); JSAN.use('util.widgets');
700
701             var file; var list_data; var ml; 
702
703             file = new util.file('offline_ou_list'); 
704             if (file._file.exists()) {
705                 list_data = file.get_object(); file.close();
706                 for (var i = 0; i < list_data[0].length; i++) { // make sure all entries are enabled
707                     list_data[0][i][2] = false;
708                 }
709                 ml = util.widgets.make_menulist( list_data[0], list_data[1] );
710                 ml.setAttribute('id','lib_menu'); document.getElementById('serial_sub_lib_menu').appendChild(ml);
711                 //TODO: class this menu properly
712                 for (var i = 0; i < obj.org_ids.length; i++) {
713                     ml.getElementsByAttribute('value',obj.org_ids[i])[0].setAttribute('class','has_distributions');
714                 }
715                 ml.firstChild.addEventListener(
716                     'popupshown',
717                     function(ev) {
718                         document.getElementById('legend').setAttribute('hidden','false');
719                     },
720                     false
721                 );
722                 ml.firstChild.addEventListener(
723                     'popuphidden',
724                     function(ev) {
725                         document.getElementById('legend').setAttribute('hidden','true');
726                     },
727                     false
728                 );
729                 ml.addEventListener(
730                     'command',
731                     function(ev) {
732                         if (document.getElementById('refresh_button')) document.getElementById('refresh_button').focus(); 
733                         JSAN.use('util.file'); var file = new util.file('manage_subs_prefs.'+obj.data.server_unadorned);
734                         util.widgets.save_attributes(file, { 'lib_menu' : [ 'value' ], 'show_ssubs' : [ 'checked' ], 'show_ssub_groups' : [ 'checked' ] });
735                         obj.refresh_list();
736                     },
737                     false
738                 );
739             } else {
740                 throw(document.getElementById('catStrings').getString('staff.cat.copy_browser.missing_library') + '\n');
741             }
742
743             file = new util.file('manage_subs_prefs.'+obj.data.server_unadorned);
744             util.widgets.load_attributes(file);
745             ml.value = ml.getAttribute('value');
746             if (! ml.value) {
747                 ml.value = org.id();
748                 ml.setAttribute('value',ml.value);
749             }
750
751             document.getElementById('show_ssubs').addEventListener(
752                 'command',
753                 function(ev) {
754                     JSAN.use('util.file'); var file = new util.file('manage_subs_prefs.'+obj.data.server_unadorned);
755                     util.widgets.save_attributes(file, { 'lib_menu' : [ 'value' ], 'show_ssubs' : [ 'checked' ], 'show_ssub_groups' : [ 'checked' ] });
756                 },
757                 false
758             );
759
760             document.getElementById('show_ssub_groups').addEventListener(
761                 'command',
762                 function(ev) {
763                     JSAN.use('util.file'); var file = new util.file('manage_subs_prefs.'+obj.data.server_unadorned);
764                     util.widgets.save_attributes(file, { 'lib_menu' : [ 'value' ], 'show_ssubs' : [ 'checked' ], 'show_ssub_groups' : [ 'checked' ] });
765                 },
766                 false
767             );
768
769             obj.show_my_libs( ml.value );
770
771             JSAN.use('util.exec'); var exec = new util.exec(20); exec.timer(obj.funcs,100);
772
773             obj.toggle_actions(); // disable menus initially
774
775         } catch(E) {
776             this.error.standard_unexpected_error_alert('serial/manage_subs.init: ',E);
777         }
778     },
779
780     'show_my_libs' : function(org) {
781         var obj = this;
782         try {
783             if (!org) {
784                 org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
785             } else {
786                 if (typeof org != 'object') org = obj.data.hash.aou[ org ];
787             }
788             obj.show_libs( org, false );
789         } catch(E) {
790             alert(E);
791         }
792     },
793
794     'show_libs' : function(start_aou,show_open) {
795         var obj = this;
796         try {
797             if (!start_aou) throw('show_libs: Need a start_aou');
798             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
799             JSAN.use('util.functional'); 
800
801             var parents = [];
802             var temp_aou = start_aou;
803             while ( temp_aou.parent_ou() ) {
804                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
805                 parents.push( temp_aou );
806             }
807             parents.reverse();
808
809             for (var i = 0; i < parents.length; i++) {
810                 obj.funcs.push(
811                     function(o,p) {
812                         return function() { 
813                             obj.append_org(o,p,{'container':'true','open':'true'}); 
814                         };
815                     }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
816                 );
817             }
818
819             obj.funcs.push(
820                 function(o,p) {
821                     return function() { obj.append_org(o,p); };
822                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
823             );
824
825             obj.funcs.push(
826                 function() {
827                     if (start_aou.children()) {
828                         var x = obj.map_tree[ 'aou_' + start_aou.id() ];
829                         x.setAttribute('container','true');
830                         if (show_open) x.setAttribute('open','true');
831                         for (var i = 0; i < start_aou.children().length; i++) {
832                             obj.funcs.push(
833                                 function(o,p) {
834                                     return function() { obj.append_org(o,p); };
835                                 }( start_aou.children()[i], start_aou )
836                             );
837                         }
838                     }
839                 }
840             );
841
842         } catch(E) {
843             alert(E);
844         }
845     },
846
847     'on_select' : function(list,twisty) {
848         var obj = this;
849         var sel_lists = {};
850
851         for (var i = 0; i < list.length; i++) {
852             var row_type = list[i].split('_')[0];
853             var id = list[i].split('_')[1];
854
855             if (!sel_lists[row_type]) sel_lists[row_type] = [];
856             sel_lists[row_type].push(id);
857
858             switch(row_type) {
859                 case 'aou' : obj.on_click_aou(id,twisty); break;
860                 case 'ssub' : obj.on_select_ssub(id,twisty); break;
861                 default: break;
862             }
863         }
864
865         if (!obj.focused_node_retrieve_id) return;
866
867         var row_type = obj.focused_node_retrieve_id.split('_')[0];
868         var id = obj.focused_node_retrieve_id.split('_')[1];
869
870         if (sel_lists[row_type]) { // the type focused is in the selection (usually the case)
871             switch(row_type) {
872                 case 'aou' : obj.on_click_aou(id,twisty); break;
873                 default: if (obj['on_click_' + row_type]) obj['on_click_' + row_type](sel_lists[row_type],twisty);
874             }
875         }
876     },
877
878     'on_select_ssub' : function(ssub_id,twisty) {
879         var obj = this;
880         try {
881             //typo? var ssub_tree = obj.map_sdist[ 'ssub_' + ssub_id ];
882             var ssub_tree = obj.map_ssub[ 'ssub_' + ssub_id ];
883             obj.funcs.push( function() { 
884                 document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
885                 document.getElementById('lib_menu').setAttribute('disabled','true'); 
886             } );
887             if (ssub_tree.distributions()) {
888                 for (var i = 0; i < ssub_tree.distributions().length; i++) {
889                     obj.funcs.push(
890                         function(c,a) {
891                             return function() {
892                                 obj.append_member(c,a,[],'sdist');
893                             }
894                         }( ssub_tree.distributions()[i], ssub_tree )
895                     )
896                 }
897             }
898             if (ssub_tree.issuances()) {
899                 for (var i = 0; i < ssub_tree.issuances().length; i++) {
900                     obj.funcs.push(
901                         function(c,a) {
902                             return function() {
903                                 obj.append_member(c,a,[],'siss');
904                             }
905                         }( ssub_tree.issuances()[i], ssub_tree )
906                     )
907                 }
908             }
909             if (ssub_tree.scaps()) {
910                 for (var i = 0; i < ssub_tree.scaps().length; i++) {
911                     obj.funcs.push(
912                         function(c,a) {
913                             return function() {
914                                 obj.append_member(c,a,[],'scap');
915                             }
916                         }( ssub_tree.scaps()[i], ssub_tree )
917                     )
918                 }
919             }
920             obj.funcs.push( function() { 
921                 document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
922                 document.getElementById('lib_menu').setAttribute('disabled','false'); 
923             } );
924         } catch(E) {
925             alert(E);
926         }
927     },
928
929     'on_click_ssub' : function(ssub_ids,twisty) {
930         var obj = this;
931         try {
932             // draw sdist editor
933             if (typeof twisty == 'undefined') {
934                 var params = {};
935                 params.ssub_ids = ssub_ids;
936                 obj.editor_init('ssub', 'edit', params);
937             }
938         } catch(E) {
939             alert(E);
940         }
941     },
942
943     'on_click_sdist' : function(sdist_ids,twisty) {
944         var obj = this;
945         try {
946             // draw sdist editor
947             if (typeof twisty == 'undefined') {
948                 var params = {};
949                 params.sdist_ids = sdist_ids;
950                 obj.editor_init('sdist', 'edit', params);
951             }
952         } catch(E) {
953             alert(E);
954         }
955     },
956
957     'on_click_siss' : function(siss_ids,twisty) {
958         var obj = this;
959         try {
960             // draw siss editor
961             if (typeof twisty == 'undefined') {
962                 var params = {};
963                 params.siss_ids = siss_ids;
964                 obj.editor_init('siss', 'edit', params);
965             }
966         } catch(E) {
967             alert(E);
968         }
969     },
970
971     'on_click_scap' : function(scap_ids,twisty) {
972         var obj = this;
973         try {
974             // draw scap editor
975             if (typeof twisty == 'undefined') {
976                 var params = {};
977                 params.scap_ids = scap_ids;
978                 obj.editor_init('scap', 'edit', params);
979             }
980         } catch(E) {
981             alert(E);
982         }
983     },
984
985     'on_click_aou' : function(org_id,twisty) {
986         var obj = this;
987         var org = obj.data.hash.aou[ org_id ];
988         var default_aou = obj.data.hash.aou[obj.default_lib];
989         obj.funcs.push( function() { 
990             document.getElementById('cmd_refresh_list').setAttribute('disabled','true'); 
991             document.getElementById('lib_menu').setAttribute('disabled','true'); 
992         } );
993         if (org.children()) {
994             for (var i = 0; i < org.children().length; i++) {
995                 var child = org.children()[i];
996                 if (orgIsMine(default_aou,child)) {
997                     obj.funcs.push(
998                         function(o,p) {
999                             return function() {
1000                                 obj.append_org(o,p)
1001                             }
1002                         }(child,org)
1003                     );
1004                 }
1005             }
1006         } 
1007         if (obj.map_ssub[ 'aou_' + org_id ]) {
1008             for (var i = 0; i < obj.map_ssub[ 'aou_' + org_id ].length; i++) {
1009                 obj.funcs.push(
1010                     function(o,a) {
1011                         return function() {
1012                             obj.append_ssub(o,a);
1013                         }
1014                     }( org, obj.map_ssub[ 'aou_' + org_id ][i] )
1015                 );
1016             }
1017         }
1018         obj.funcs.push( function() { 
1019             document.getElementById('cmd_refresh_list').setAttribute('disabled','false'); 
1020             document.getElementById('lib_menu').setAttribute('disabled','false'); 
1021         } );
1022
1023         // remove current editor
1024         if (typeof twisty == 'undefined') {
1025             document.getElementById('serial_manage_subs_editor_deck').selectedIndex = 0;
1026         }
1027     },
1028
1029     'append_org' : function (org,parent_org,params) {
1030         var obj = this;
1031         try {
1032             if (obj.map_tree[ 'aou_' + org.id() ]) {
1033                 var x = obj.map_tree[ 'aou_' + org.id() ];
1034                 if (params) {
1035                     for (var i in params) {
1036                         x.setAttribute(i,params[i]);
1037                     }
1038                 }
1039                 return x;
1040             }
1041
1042             var data = {
1043                 'row' : {
1044                     'my' : {
1045                         'aou' : org,
1046                     }
1047                 },
1048                 'skip_all_columns_except' : [0,1,2],
1049                 'retrieve_id' : 'aou_' + org.id(),
1050                 'to_bottom' : true,
1051                 'no_auto_select' : true,
1052             };
1053         
1054             var ssub_tree_list;
1055             if ( obj.org_ids.indexOf( Number( org.id() ) ) == -1 ) {
1056                 data.row.my.subscription_count = '0';
1057                 //data.row.my.distribution_count = '<0>';
1058             } else {
1059                 var s_count = 0; //var d_count = 0;
1060                 ssub_tree_list = obj.network.simple_request(
1061                     'FM_SSUB_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS.authoritative',
1062                     [ ses(), obj.docid, [ org.id() ] ]
1063                 );
1064                 for (var i = 0; i < ssub_tree_list.length; i++) {
1065                     s_count++;
1066                     obj.map_ssub[ 'ssub_' + ssub_tree_list[i].id() ] = function(r){return r;}(ssub_tree_list[i]);
1067                     var distributions = ssub_tree_list[i].distributions();
1068                     //if (distributions) d_count += distributions.length;
1069                     for (var j = 0; j < distributions.length; j++) {
1070                         obj.map_sdist[ 'sdist_' + distributions[j].id() ] = function(r){return r;}(distributions[j]);
1071                     }
1072                     var issuances = ssub_tree_list[i].issuances();
1073                     for (var j = 0; j < issuances.length; j++) {
1074                         obj.map_siss[ 'siss_' + issuances[j].id() ] = function(r){return r;}(issuances[j]);
1075                     }
1076                     var scaps = ssub_tree_list[i].scaps();
1077                     for (var j = 0; j < scaps.length; j++) {
1078                         obj.map_scap[ 'scap_' + scaps[j].id() ] = function(r){return r;}(scaps[j]);
1079                     }
1080                 }
1081                 data.row.my.subscription_count = s_count;
1082                 //data.row.my.distribution_count = '<' + d_count + '>';
1083             }
1084             if (parent_org) {
1085                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
1086             }
1087             var nparams = obj.list.append(data);
1088             var node = nparams.treeitem_node;
1089             if (params) {
1090                 for (var i in params) {
1091                     node.setAttribute(i,params[i]);
1092                 }
1093             }
1094             obj.map_tree[ 'aou_' + org.id() ] = node;
1095
1096             if (org.children()) {
1097                 node.setAttribute('container','true');
1098             }
1099
1100             if (parent_org) {
1101                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
1102                     data.node.setAttribute('open','true');
1103                     obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
1104                 }
1105             } else {
1106                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
1107                 obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
1108             }
1109
1110             if (ssub_tree_list) {
1111                 obj.map_ssub[ 'aou_' + org.id() ] = ssub_tree_list;
1112                 node.setAttribute('container','true');
1113             }
1114
1115             if (document.getElementById('show_ssubs').checked) {
1116                 obj.funcs.push( function() { obj.on_click_aou( org.id() ); } );
1117                 node.setAttribute('open','true');
1118             }
1119
1120         } catch(E) {
1121             dump(E+'\n');
1122             alert(E);
1123         }
1124     },
1125
1126     'append_ssub' : function( org, ssub_tree, params ) {
1127         var obj = this;
1128         try {
1129             if (obj.map_tree[ 'ssub_' + ssub_tree.id() ]) {
1130                 var x = obj.map_tree[ 'ssub_' + ssub_tree.id() ];
1131                 if (params) {
1132                     for (var i in params) {
1133                         x.setAttribute(i,params[i]);
1134                     }
1135                 }
1136                 return x;
1137             }
1138
1139             var parent_node = obj.map_tree[ 'aou_' + org.id() ];
1140             var data = {
1141                 'row' : {
1142                     'my' : {
1143                         'aou' : org,
1144                         'ssub' : ssub_tree,
1145                         'subscription_count' : '',
1146                         //'distribution_count' : ssub_tree.distributions() ? ssub_tree.distributions().length : '0',
1147                     }
1148                 },
1149                 'skip_all_columns_except' : [0,1,2],
1150                 'retrieve_id' : 'ssub_' + ssub_tree.id(),
1151                 'node' : parent_node,
1152                 'to_bottom' : true,
1153                 'no_auto_select' : true,
1154             };
1155             var nparams = obj.list.append(data);
1156             var node = nparams.treeitem_node;
1157             obj.map_tree[ 'ssub_' + ssub_tree.id() ] =  node;
1158             if (params) {
1159                 for (var i in params) {
1160                     node.setAttribute(i,params[i]);
1161                 }
1162             }
1163             if (ssub_tree.distributions() || ssub_tree.scaps() || ssub_tree.issuances()) {
1164                 //did this support a later typo? obj.map_sdist[ 'ssub_' + ssub_tree.id() ] = ssub_tree;
1165                 node.setAttribute('container','true');
1166             }
1167             if (document.getElementById('show_ssub_groups').checked) {
1168                 node.setAttribute('open','true');
1169                 obj.funcs.push( function() { obj.on_select_ssub( ssub_tree.id(), true ); } );
1170             }
1171             var sdist_group_node_data = {
1172                 'row' : {
1173                     'my' : {
1174                         'label' : $('serialStrings').getString('serial.manage_subs.distributions'),
1175                     }
1176                 },
1177                 'retrieve_id' : 'sdist-group_' + ssub_tree.id(),
1178                 'node' : node,
1179                 'to_bottom' : true,
1180                 'no_auto_select' : true,
1181             };
1182             nparams = obj.list.append(sdist_group_node_data);
1183             obj.map_tree[ 'ssub_sdist_group_' + ssub_tree.id() ] =  nparams.treeitem_node;
1184
1185             var siss_group_node_data = {
1186                 'row' : {
1187                     'my' : {
1188                         'label' : $('serialStrings').getString('serial.manage_subs.issuances'),
1189                     }
1190                 },
1191                 'retrieve_id' : 'siss-group_' + ssub_tree.id(),
1192                 'node' : node,
1193                 'to_bottom' : true,
1194                 'no_auto_select' : true,
1195             };
1196             nparams = obj.list.append(siss_group_node_data);
1197             obj.map_tree[ 'ssub_siss_group_' + ssub_tree.id() ] =  nparams.treeitem_node;
1198
1199             var scap_group_node_data = {
1200                 'row' : {
1201                     'my' : {
1202                         'label' : $('serialStrings').getString('serial.manage_subs.captions_patterns'),
1203                     }
1204                 },
1205                 'retrieve_id' : 'scap-group_' + ssub_tree.id(),
1206                 'node' : node,
1207                 'to_bottom' : true,
1208                 'no_auto_select' : true,
1209             };
1210             nparams = obj.list.append(scap_group_node_data);
1211             obj.map_tree[ 'ssub_scap_group_' + ssub_tree.id() ] =  nparams.treeitem_node;
1212         } catch(E) {
1213             dump(E+'\n');
1214             alert(E);
1215         }
1216     },
1217
1218     'append_member' : function( item, ssub_tree, attributes, type ) {
1219         var obj = this;
1220         try {
1221             if (obj.map_tree[ type + '_' + item.id() ]) {
1222                 var x = obj.map_tree[ type + '_' + item.id() ];
1223                 if (attributes) {
1224                     for (var i in attributes) {
1225                         x.setAttribute(i,attributes[i]);
1226                     }
1227                 }
1228                 return x;
1229             }
1230
1231             var parent_node = obj.map_tree[ 'ssub_' + type + '_group_' + ssub_tree.id() ];
1232             var data = {
1233                 'row' : {
1234                     'my' : {
1235                         'aou' : obj.data.hash.aou[ ssub_tree.owning_lib() ],
1236                         'ssub' : ssub_tree,
1237                         'subscription_count' : '',
1238                         //'distribution_count' : '',
1239                     }
1240                 },
1241                 'retrieve_id' : type + '_' + item.id(),
1242                 'node' : parent_node,
1243                 'to_bottom' : true,
1244                 'no_auto_select' : true,
1245             };
1246             data['row']['my'][type] = item; // TODO: future optimization: get only the IDs of these leaves, then fetch the full row in 'retrieve_row'
1247             var nparams = obj.list.append(data);
1248             var node = nparams.treeitem_node;
1249             obj.map_tree[ type + '_' + item.id() ] =  node;
1250             if (attributes) {
1251                 for (var i in attributes) {
1252                     node.setAttribute(i,attributes[i]);
1253                 }
1254             }
1255
1256         } catch(E) {
1257             dump(E+'\n');
1258             alert(E);
1259         }
1260     },
1261
1262     'list_init' : function( params ) {
1263
1264         try {
1265             var obj = this;
1266             
1267             JSAN.use('circ.util');
1268             var columns = [
1269                 {
1270                     'id' : 'tree_location',
1271                     'label' : $('serialStrings').getString('serial.manage_subs.tree_location'),
1272                     'flex' : 1, 'primary' : true, 'hidden' : false, 
1273                     'render' : function(my) { 
1274                         if (my.sdist) { return my.sdist.label(); }
1275                         if (my.siss) { return my.siss.label(); }
1276                         if (my.scap) { return $('serialStrings').getFormattedString('serial.manage_subs.scap_id', [my.scap.id()]); }
1277                         if (my.ssub) { return $('serialStrings').getFormattedString('serial.manage_subs.ssub_id', [my.ssub.id()]); }
1278                         if (my.aou) { return $('serialStrings').getFormattedString('serial.manage_dists.library_label', [my.aou.shortname(), my.aou.name()]); }
1279                         if (my.label) { return my.label; }
1280                         /* If all else fails... */
1281                         return "???"; 
1282                     },
1283                 },
1284                 {
1285                     'id' : 'subscription_count',
1286                     'label' : $('serialStrings').getString('serial.manage_subs.subscriptions'),
1287                     'flex' : 0, 'primary' : false, 'hidden' : false, 
1288                     'render' : function(my) { return my.subscription_count; },
1289                 },
1290                 /*{
1291                     'id' : 'distribution_count',
1292                     'label' : 'Members',
1293                     'flex' : 0,
1294                     'primary' : false, 'hidden' : false, 
1295                     'render' : function(my) { return my.distribution_count; },
1296                 },*/
1297             ];
1298             JSAN.use('util.list'); obj.list = new util.list('subs_tree');
1299             obj.list.init(
1300                 {
1301                     'no_auto_select' : true,
1302                     'columns' : columns,
1303                     'retrieve_row' : function(params) {
1304
1305                         var row = params.row;
1306                         obj.funcs.push(
1307                             function() {
1308
1309                                 if (typeof params.on_retrieve == 'function') {
1310                                     params.on_retrieve(row);
1311                                 }
1312
1313                             }
1314                         );
1315
1316                         return row;
1317                     },
1318                     'on_click' : function(ev) {
1319                         var row = {}; var col = {}; var nobj = {};
1320                         obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj); 
1321                         if ((row.value == -1)||(nobj.value != 'twisty')) { return; } // on_click runs for twistys only
1322
1323                         var node = obj.list.node.contentView.getItemAtIndex(row.value);
1324                         var list = [ node.getAttribute('retrieve_id') ];
1325                         if (typeof obj.on_select == 'function') {
1326                             obj.on_select(list,true);
1327                         }
1328                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1329                             window.xulG.on_select(list);
1330                         }
1331                     },
1332                     'on_select' : function(ev) {
1333                         JSAN.use('util.functional');
1334                         
1335                         // get the actual node clicked to determine which editor to use
1336                         if (obj.list.node.view.selection.currentIndex > -1) {
1337                             var node = obj.list.node.contentView.getItemAtIndex(obj.list.node.view.selection.currentIndex);
1338                             obj.focused_node_retrieve_id = node.getAttribute('retrieve_id');
1339                         }
1340
1341                         var sel = obj.list.retrieve_selection();
1342                         obj.controller.view.sel_clip.disabled = sel.length < 1;
1343                         obj.sel_list = util.functional.map_list(
1344                             sel,
1345                             function(o) { return o.getAttribute('retrieve_id'); }
1346                         );
1347                         obj.toggle_actions();
1348                         if (typeof obj.on_select == 'function') {
1349                             obj.on_select(obj.sel_list);
1350                         }
1351                         if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
1352                             window.xulG.on_select(obj.sel_list);
1353                         }
1354                     },
1355                 }
1356             );
1357
1358             obj.controller.render();
1359
1360         } catch(E) {
1361             this.error.sdump('D_ERROR','serial/manage_subs.list_init: ' + E + '\n');
1362             alert(E);
1363         }
1364     },
1365
1366     'toggle_actions' : function() {
1367         var obj = this;
1368         try {
1369             var found_aou = false; var found_ssub = false; var found_sdist = false; var found_siss = false; var found_scap = false; var found_sdist_group = false; var found_siss_group = false; var found_scap_group = false;
1370             for (var i = 0; i < obj.sel_list.length; i++) {
1371                 var type = obj.sel_list[i].split(/_/)[0];
1372                 switch(type) {
1373                     case 'aou' : 
1374                         found_aou = true; 
1375                     break;
1376                     case 'ssub' : found_ssub = true; break;
1377                     case 'sdist' : found_sdist = true; break;
1378                     case 'siss' : found_siss = true; break;
1379                     case 'scap' : found_scap = true; break;
1380                     case 'sdist-group' : found_sdist_group = true; break;
1381                     case 'siss-group' : found_siss_group = true; break;
1382                     case 'scap-group' : found_scap_group = true; break;
1383                 }
1384             }
1385             obj.controller.view.cmd_add_sdist.setAttribute('disabled','true');
1386             obj.controller.view.cmd_add_siss.setAttribute('disabled','true');
1387             obj.controller.view.cmd_add_scap.setAttribute('disabled','true');
1388             obj.controller.view.cmd_make_predictions.setAttribute('disabled','true');
1389             obj.controller.view.cmd_delete_sdist.setAttribute('disabled','true');
1390             obj.controller.view.cmd_delete_siss.setAttribute('disabled','true');
1391             obj.controller.view.cmd_delete_scap.setAttribute('disabled','true');
1392             obj.controller.view.cmd_add_subscriptions.setAttribute('disabled','true');
1393             obj.controller.view.cmd_mark_library.setAttribute('disabled','true');
1394             obj.controller.view.cmd_delete_ssub.setAttribute('disabled','true');
1395             obj.controller.view.cmd_mark_subscription.setAttribute('disabled','true');
1396             obj.controller.view.cmd_transfer_subscription.setAttribute('disabled','true');
1397             obj.controller.view.cmd_transfer_sdists.setAttribute('disabled','true');
1398             if (found_aou) {
1399                 obj.controller.view.cmd_add_subscriptions.setAttribute('disabled','false');
1400                 obj.controller.view.cmd_mark_library.setAttribute('disabled','false');
1401             }
1402             if (found_ssub) {
1403                 obj.controller.view.cmd_delete_ssub.setAttribute('disabled','false');
1404                 obj.controller.view.cmd_mark_subscription.setAttribute('disabled','false');
1405                 obj.controller.view.cmd_add_sdist.setAttribute('disabled','false');
1406                 obj.controller.view.cmd_add_siss.setAttribute('disabled','false');
1407                 obj.controller.view.cmd_add_scap.setAttribute('disabled','false');
1408                 obj.controller.view.cmd_transfer_subscription.setAttribute('disabled','false');
1409                 obj.controller.view.cmd_make_predictions.setAttribute('disabled','false');
1410             }
1411             if (found_sdist_group) {
1412                 obj.controller.view.cmd_add_sdist.setAttribute('disabled','false');
1413             }
1414             if (found_siss_group) {
1415                 obj.controller.view.cmd_add_siss.setAttribute('disabled','false');
1416             }
1417             if (found_scap_group) {
1418                 obj.controller.view.cmd_add_scap.setAttribute('disabled','false');
1419             }
1420             if (found_sdist) {
1421                 obj.controller.view.cmd_delete_sdist.setAttribute('disabled','false');
1422                 obj.controller.view.cmd_transfer_sdists.setAttribute('disabled','false');
1423             }
1424             if (found_siss) {
1425                 obj.controller.view.cmd_delete_siss.setAttribute('disabled','false');
1426             }
1427             if (found_scap) {
1428                 obj.controller.view.cmd_delete_scap.setAttribute('disabled','false');
1429             }
1430         } catch(E) {
1431             obj.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.actions.error'),E);
1432         }
1433     },
1434
1435     'refresh_list' : function() { 
1436         try {
1437             var obj = this;
1438             obj.list.clear();
1439             obj.map_tree = {};
1440             obj.map_ssub = {};
1441             obj.map_sdist = {};
1442             obj.map_siss = {};
1443             obj.map_scap = {};
1444             obj.org_ids = obj.network.simple_request('FM_SSUB_AOU_IDS_RETRIEVE_VIA_RECORD_ID.authoritative',[ obj.docid ]);
1445             if (typeof obj.org_ids.ilsevent != 'undefined') throw(obj.org_ids);
1446             JSAN.use('util.functional'); 
1447             obj.org_ids = util.functional.map_list( obj.org_ids, function (o) { return Number(o); });
1448             /*
1449             var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
1450             obj.show_libs( org );
1451             */
1452             obj.default_lib = document.getElementById('lib_menu').value;
1453             obj.show_my_libs( obj.default_lib );
1454         } catch(E) {
1455             this.error.standard_unexpected_error_alert(document.getElementById('catStrings').getString('staff.cat.copy_browser.refresh_list.error'),E);
1456         }
1457     },
1458 };
1459
1460 dump('exiting serial/manage_subs.js\n');