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