]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_browser.js
doh
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_browser.js
1 dump('entering cat.copy_browser.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_browser = function (params) {
5         try {
6                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7                 JSAN.use('util.error'); this.error = new util.error();
8         } catch(E) {
9                 dump('cat.copy_browser: ' + E + '\n');
10         }
11 }
12
13 cat.copy_browser.prototype = {
14
15         'map_tree' : {},
16         'map_acn' : {},
17         'map_acp' : {},
18
19         'init' : function( params ) {
20
21                 try {
22                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
23                         var obj = this;
24
25                         obj.docid = params.docid;
26
27                         JSAN.use('util.network'); obj.network = new util.network();
28                         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
29                         JSAN.use('util.controller'); obj.controller = new util.controller();
30                         obj.controller.init(
31                                 {
32                                         control_map : {
33                                                 'cmd_broken' : [
34                                                         ['command'],
35                                                         function() { alert('Not Yet Implemented'); }
36                                                 ],
37                                                 'cmd_show_my_libs' : [
38                                                         ['command'],
39                                                         function() { 
40                                                                 obj.show_my_libs(); 
41                                                         }
42                                                 ],
43                                                 'cmd_show_all_libs' : [
44                                                         ['command'],
45                                                         function() {
46                                                                 obj.show_all_libs();
47                                                         }
48                                                 ],
49                                                 'cmd_show_libs_with_copies' : [
50                                                         ['command'],
51                                                         function() {
52                                                                 obj.show_libs_with_copies();
53                                                         }
54                                                 ],
55                                                 'cmd_clear' : [
56                                                         ['command'],
57                                                         function() {
58                                                                 obj.map_tree = {};
59                                                                 obj.list.clear();
60                                                         }
61                                                 ],
62                                         }
63                                 }
64                         );
65
66                         obj.list_init(params);
67
68                         obj.org_ids = obj.network.simple_request('FM_AOU_IDS_RETRIEVE_VIA_RECORD_ID',[ obj.docid ]);
69
70                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
71                         obj.show_libs( org );
72
73                 } catch(E) {
74                         this.error.sdump('D_ERROR','cat.copy_browser.init: ' + E + '\n');
75                 }
76         },
77
78         'show_my_libs' : function() {
79                 var obj = this;
80                 try {
81                         var org = obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ];
82                         obj.show_libs( org, true );
83                 
84                         var p_org = obj.data.hash.aou[ org.parent_ou() ];
85                         if (p_org) {
86                                 JSAN.use('util.exec'); var exec = new util.exec();
87                                 var funcs = [];
88                                 for (var i = 0; i < p_org.children().length; i++) {
89                                         funcs.push(
90                                                 function(o) {
91                                                         return function() {
92                                                                 obj.show_libs( o, true );
93                                                         }
94                                                 }( p_org.children()[i] )
95                                         );
96                                 }
97                                 exec.chain( funcs );
98                         }
99                 } catch(E) {
100                         alert(E);
101                 }
102         },
103
104         'show_all_libs' : function() {
105                 var obj = this;
106                 try {
107                         obj.show_my_libs();
108
109                         obj.show_libs( obj.data.tree.aou );
110
111                         JSAN.use('util.exec'); var exec = new util.exec();
112                         var funcs = [];
113                         for (var i = 0; i < obj.data.tree.aou.children().length; i++) {
114                                 funcs.push(
115                                         function(o) {
116                                                 return function() {
117                                                         obj.show_libs( o );
118                                                 }
119                                         }( obj.data.tree.aou.children()[i] )
120                                 );
121                         }
122                         exec.chain( funcs );
123                 } catch(E) {
124                         alert(E);
125                 }
126         },
127
128         'show_libs_with_copies' : function() {
129                 var obj = this;
130                 try {
131                         JSAN.use('util.exec'); var exec = new util.exec();
132                         JSAN.use('util.functional');
133
134                         var orgs = util.functional.map_list(
135                                 obj.org_ids,
136                                 function(id) { return obj.data.hash.aou[id]; }
137                         ).sort(
138                                 function( a, b ) {
139                                         if (a.shortname() < b.shortname()) return -1;
140                                         if (a.shortname() > b.shortname()) return 1;
141                                         return 0;
142                                 }
143                         );
144                         var funcs = [];
145                         for (var i = 0; i < orgs.length; i++) {
146                                 funcs.push(
147                                         function(o) {
148                                                 return function() {
149                                                         obj.show_libs(o,true);
150                                                 }
151                                         }( orgs[i] )
152                                 );
153                         }
154                         exec.chain( funcs );
155                 } catch(E) {
156                         alert(E);
157                 }
158         },
159
160         'show_libs' : function(start_aou,show_open) {
161                 var obj = this;
162                 try {
163                         if (!start_aou) throw('show_libs: Need a start_aou');
164                         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
165                         JSAN.use('util.functional'); JSAN.use('util.exec'); var exec = new util.exec();
166
167                         var funcs = [];
168
169                         var parents = [];
170                         var temp_aou = start_aou;
171                         while ( temp_aou.parent_ou() ) {
172                                 temp_aou = obj.data.hash.aou[ temp_aou.parent_ou() ];
173                                 parents.push( temp_aou );
174                         }
175                         parents.reverse();
176
177                         for (var i = 0; i < parents.length; i++) {
178                                 funcs.push(
179                                         function(o,p) {
180                                                 return function() { 
181                                                         if (show_open) {
182                                                                 obj.append_org(o,p,{'container':'true','open':'true'}); 
183                                                         } else {
184                                                                 obj.append_org(o,p,{'container':'true'}); 
185                                                         }
186                                                 };
187                                         }(parents[i], obj.data.hash.aou[ parents[i].parent_ou() ])
188                                 );
189                         }
190
191                         funcs.push(
192                                 function(o,p) {
193                                         return function() { obj.append_org(o,p); };
194                                 }(start_aou,obj.data.hash.aou[ start_aou.parent_ou() ])
195                         );
196
197                         funcs.push(
198                                 function() {
199                                         if (start_aou.children()) {
200                                                 var x = obj.map_tree[ 'aou_' + start_aou.id() ];
201                                                 x.setAttribute('container','true');
202                                                 if (show_open) x.setAttribute('open','true');
203                                                 for (var i = 0; i < start_aou.children().length; i++) {
204                                                         funcs.push(
205                                                                 function(o,p) {
206                                                                         return function() { obj.append_org(o,p); };
207                                                                 }( start_aou.children()[i], start_aou )
208                                                         );
209                                                 }
210                                         }
211                                 }
212                         );
213
214                         exec.chain( funcs );
215
216                 } catch(E) {
217                         alert(E);
218                 }
219         },
220
221         'on_select' : function(list,twisty) {
222                 var obj = this;
223                 for (var i = 0; i < list.length; i++) {
224                         var node = obj.map_tree[ list[i] ];
225                         //if (node.lastChild.nodeName == 'treechildren') { continue; } else { alert(node.lastChild.nodeName); }
226                         var row_type = list[i].split('_')[0];
227                         var id = list[i].split('_')[1];
228                         switch(row_type) {
229                                 case 'aou' : obj.on_select_org(id,twisty); break;
230                                 case 'acn' : obj.on_select_acn(id,twisty); break;
231                                 default: break;
232                         }
233                 }
234         },
235
236         'on_select_acn' : function(acn_id,twisty) {
237                 var obj = this;
238                 try {
239                         var acn_tree = obj.map_acp[ 'acn_' + acn_id ];
240                         var funcs = [];
241                         if (acn_tree.copies()) {
242                                 for (var i = 0; i < acn_tree.copies().length; i++) {
243                                         funcs.push(
244                                                 function(c,a) {
245                                                         return function() {
246                                                                 obj.append_acp(c,a);
247                                                         }
248                                                 }( acn_tree.copies()[i], acn_tree )
249                                         )
250                                 }
251                         }
252                         JSAN.use('util.exec'); var exec = new util.exec();
253                         exec.chain( funcs );
254                 } catch(E) {
255                         alert(E);
256                 }
257         },
258
259         'on_select_org' : function(org_id,twisty) {
260                 var obj = this;
261                 var org = obj.data.hash.aou[ org_id ];
262                 var funcs = [];
263                 if (org.children()) {
264                         for (var i = 0; i < org.children().length; i++) {
265                                 funcs.push(
266                                         function(o,p) {
267                                                 return function() {
268                                                         obj.append_org(o,p)
269                                                 }
270                                         }(org.children()[i],org)
271                                 );
272                         }
273                 } 
274                 if (obj.map_acn[ 'aou_' + org_id ]) {
275                         for (var i = 0; i < obj.map_acn[ 'aou_' + org_id ].length; i++) {
276                                 funcs.push(
277                                         function(o,a) {
278                                                 return function() {
279                                                         obj.append_acn(o,a);
280                                                 }
281                                         }( org, obj.map_acn[ 'aou_' + org_id ][i] )
282                                 );
283                         }
284                 }
285                 JSAN.use('util.exec'); var exec = new util.exec();
286                 exec.chain( funcs );
287         },
288
289         'append_org' : function (org,parent_org,params) {
290                 var obj = this;
291                 try {
292                         if (obj.map_tree[ 'aou_' + org.id() ]) {
293                                 var x = obj.map_tree[ 'aou_' + org.id() ];
294                                 if (params) {
295                                         for (var i in params) {
296                                                 x.setAttribute(i,params[i]);
297                                         }
298                                 }
299                                 return x;
300                         }
301
302                         var data = {
303                                 'row' : {
304                                         'my' : {
305                                                 'aou' : org,
306                                         }
307                                 },
308                                 'skip_all_columns_except' : [0,1,2],
309                                 'retrieve_id' : 'aou_' + org.id(),
310                         };
311                 
312                         var acn_tree_list;
313                         if ( obj.org_ids.indexOf( org.id() ) == -1 ) {
314                                 if ( obj.data.hash.aout[ org.ou_type() ].can_have_vols() ) {
315                                         data.row.my.volume_count = '0';
316                                         data.row.my.copy_count = '<0>';
317                                 } else {
318                                         data.row.my.volume_count = '';
319                                         data.row.my.copy_count = '';
320                                 }
321                         } else {
322                                 var v_count = 0; var c_count = 0;
323                                 acn_tree_list = obj.network.simple_request(
324                                         'FM_ACN_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS',
325                                         [ ses(), obj.docid, [ org.id() ] ]
326                                 );
327                                 for (var i = 0; i < acn_tree_list.length; i++) {
328                                         v_count++;
329                                         if (acn_tree_list[i].copies()) c_count += acn_tree_list[i].copies().length;
330                                 }
331                                 data.row.my.volume_count = v_count;
332                                 data.row.my.copy_count = '<' + c_count + '>';
333                         }
334                         if (parent_org) {
335                                 data.node = obj.map_tree[ 'aou_' + parent_org.id() ];
336                         }
337
338                         var node = obj.list.append(data);
339                         if (params) {
340                                 for (var i in params) {
341                                         node.setAttribute(i,params[i]);
342                                 }
343                         }
344                         obj.map_tree[ 'aou_' + org.id() ] = node;
345
346                         if (org.children()) {
347                                 node.setAttribute('container','true');
348                         }
349
350                         if (parent_org) {
351                                 if ( obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou() == parent_org.id() ) {
352                                         data.node.setAttribute('open','true');
353                                 }
354                         } else {
355                                 obj.map_tree[ 'aou_' + org.id() ].setAttribute('open','true');
356                         }
357
358                         if (acn_tree_list) {
359                                 obj.map_acn[ 'aou_' + org.id() ] = acn_tree_list;
360                                 node.setAttribute('container','true');
361                         }
362
363                 } catch(E) {
364                         dump(E+'\n');
365                         alert(E);
366                 }
367         },
368
369         'append_acn' : function( org, acn_tree, params ) {
370                 var obj = this;
371                 try {
372                         if (obj.map_tree[ 'acn_' + acn_tree.id() ]) {
373                                 var x = obj.map_tree[ 'acn_' + acn_tree.id() ];
374                                 if (params) {
375                                         for (var i in params) {
376                                                 x.setAttribute(i,params[i]);
377                                         }
378                                 }
379                                 return x;
380                         }
381
382                         var parent_node = obj.map_tree[ 'aou_' + org.id() ];
383                         var data = {
384                                 'row' : {
385                                         'my' : {
386                                                 'aou' : org,
387                                                 'acn' : acn_tree,
388                                                 'volume_count' : '',
389                                                 'copy_count' : acn_tree.copies() ? acn_tree.copies().length : '0',
390                                         }
391                                 },
392                                 'skip_all_columns_except' : [0,1,2],
393                                 'retrieve_id' : 'acn_' + acn_tree.id(),
394                                 'node' : parent_node,
395                         };
396                         var node = obj.list.append(data);
397                         obj.map_tree[ 'acn_' + acn_tree.id() ] =  node;
398                         if (params) {
399                                 for (var i in params) {
400                                         node.setAttribute(i,params[i]);
401                                 }
402                         }
403                         if (acn_tree.copies()) {
404                                 obj.map_acp[ 'acn_' + acn_tree.id() ] = acn_tree;
405                                 node.setAttribute('container','true');
406                         }
407
408                 } catch(E) {
409                         dump(E+'\n');
410                         alert(E);
411                 }
412         },
413
414         'append_acp' : function( acp_item, acn_tree, params ) {
415                 var obj = this;
416                 try {
417                         if (obj.map_tree[ 'acp_' + acp_item.id() ]) {
418                                 var x = obj.map_tree[ 'acp_' + acp_item.id() ];
419                                 if (params) {
420                                         for (var i in params) {
421                                                 x.setAttribute(i,params[i]);
422                                         }
423                                 }
424                                 return x;
425                         }
426
427                         var parent_node = obj.map_tree[ 'acn_' + acn_tree.id() ];
428                         var data = {
429                                 'row' : {
430                                         'my' : {
431                                                 'aou' : obj.data.hash.aou[ acn_tree.owning_lib() ],
432                                                 'acn' : acn_tree,
433                                                 'acp' : acp_item,
434                                                 'volume_count' : '',
435                                                 'copy_count' : '',
436                                         }
437                                 },
438                                 'retrieve_id' : 'acp_' + acp_item.id(),
439                                 'node' : parent_node,
440                         };
441                         var node = obj.list.append(data);
442                         obj.map_tree[ 'acp_' + acp_item.id() ] =  node;
443                         if (params) {
444                                 for (var i in params) {
445                                         node.setAttribute(i,params[i]);
446                                 }
447                         }
448
449                 } catch(E) {
450                         dump(E+'\n');
451                         alert(E);
452                 }
453         },
454
455         'list_init' : function( params ) {
456
457                 try {
458                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
459                         var obj = this;
460                         
461                         JSAN.use('circ.util');
462                         var columns = [
463                                 {
464                                         'id' : 'tree_location', 'label' : 'Location/Barcode', 'flex' : 1,
465                                         'primary' : true, 'hidden' : false, 
466                                         'render' : 'my.acp ? my.acp.barcode() : my.acn ? my.acn.label() : my.aou ? my.aou.shortname() + " : " + my.aou.name() : "???"'
467                                 },
468                                 {
469                                         'id' : 'volume_count', 'label' : 'Volumes', 'flex' : 0,
470                                         'primary' : false, 'hidden' : false, 
471                                         'render' : 'my.volume_count'
472                                 },
473                                 {
474                                         'id' : 'copy_count', 'label' : 'Copies', 'flex' : 0,
475                                         'primary' : false, 'hidden' : false, 
476                                         'render' : 'my.copy_count'
477                                 },
478                         ].concat(
479                                 circ.util.columns( 
480                                         { 
481                                                 'location' : { 'hidden' : false },
482                                                 'circ_lib' : { 'hidden' : false },
483                                                 'owning_lib' : { 'hidden' : false },
484                                                 'call_number' : { 'hidden' : false },
485                                                 'status' : { 'hidden' : false },
486                                         },
487                                         {
488                                                 'just_these' : [
489                                                         'owning_lib',
490                                                         'circ_lib',
491                                                         'call_number',
492                                                         'copy_number',
493                                                         'location',
494                                                         'barcode',
495                                                         'loan_duration',
496                                                         'fine_level',
497                                                         'circulate',
498                                                         'holdable',
499                                                         'opac_visible',
500                                                         'ref',
501                                                         'deposit',
502                                                         'deposit_amount',
503                                                         'price',
504                                                         'circ_as_type',
505                                                         'circ_modifier',
506                                                         'status',
507                                                         'alert_message',
508                                                         'acp_id',
509                                                 ]
510                                         }
511                                 )
512                         );
513                         JSAN.use('util.list'); obj.list = new util.list('copy_tree');
514                         obj.list.init(
515                                 {
516                                         'columns' : columns,
517                                         'map_row_to_column' : circ.util.std_map_row_to_column(' '),
518                                         'retrieve_row' : function(params) {
519
520                                                 var row = params.row;
521
522                                                 var funcs = [];
523                                         /*      
524                                                 if (!row.my.mvr) funcs.push(
525                                                         function() {
526
527                                                                 row.my.mvr = obj.network.request(
528                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.app,
529                                                                         api.MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.method,
530                                                                         [ row.my.circ.target_copy() ]
531                                                                 );
532
533                                                         }
534                                                 );
535                                                 if (!row.my.acp) {
536                                                         funcs.push(     
537                                                                 function() {
538
539                                                                         row.my.acp = obj.network.request(
540                                                                                 api.FM_ACP_RETRIEVE.app,
541                                                                                 api.FM_ACP_RETRIEVE.method,
542                                                                                 [ row.my.circ.target_copy() ]
543                                                                         );
544
545                                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
546
547                                                                 }
548                                                         );
549                                                 } else {
550                                                         params.row_node.setAttribute( 'retrieve_id',row.my.acp.barcode() );
551                                                 }
552                                         */
553                                                 funcs.push(
554                                                         function() {
555
556                                                                 if (typeof params.on_retrieve == 'function') {
557                                                                         params.on_retrieve(row);
558                                                                 }
559
560                                                         }
561                                                 );
562
563                                                 JSAN.use('util.exec'); var exec = new util.exec();
564                                                 exec.on_error = function(E) {
565                                                         var err = 'items chain: ' + js2JSON(E);
566                                                         obj.error.sdump('D_ERROR',err);
567                                                         return true; /* keep going */
568                                                 }
569                                                 exec.chain( funcs );
570
571                                                 return row;
572                                         },
573                                         'on_click' : function(ev) {
574                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserRead');
575                                                 var row = {}; var col = {}; var nobj = {};
576                                                 obj.list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj);
577                                                 if ((row.value == -1)||(nobj.value != 'twisty')) { return; }
578                                                 var node = obj.list.node.contentView.getItemAtIndex(row.value);
579                                                 var list = [ node.getAttribute('retrieve_id') ];
580                                                 if (typeof obj.on_select == 'function') {
581                                                         obj.on_select(list,true);
582                                                 }
583                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
584                                                         window.xulG.on_select(list);
585                                                 }
586                                         },
587                                         'on_select' : function(ev) {
588                                                 JSAN.use('util.functional');
589                                                 var sel = obj.list.retrieve_selection();
590                                                 var list = util.functional.map_list(
591                                                         sel,
592                                                         function(o) { return o.getAttribute('retrieve_id'); }
593                                                 );
594                                                 if (typeof obj.on_select == 'function') {
595                                                         obj.on_select(list);
596                                                 }
597                                                 if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
598                                                         window.xulG.on_select(list);
599                                                 }
600                                         },
601                                 }
602                         );
603         
604
605                 } catch(E) {
606                         this.error.sdump('D_ERROR','cat.copy_browser.list_init: ' + E + '\n');
607                         alert(E);
608                 }
609         },
610 }
611
612 dump('exiting cat.copy_browser.js\n');