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