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