e73731aab68edf8c7e9048cbf4edb0eb3638b46f
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_buckets.js
1 dump('entering cat.copy_buckets.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_buckets = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8         JSAN.use('util.date');
9         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
10 }
11
12 cat.copy_buckets.prototype = {
13         'selection_list1' : [],
14         'selection_list2' : [],
15
16         'init' : function( params ) {
17
18                 var obj = this;
19
20                 obj.session = params['session'];
21                 obj.copy_ids = params['copy_ids'];
22
23                 JSAN.use('circ.util');
24                 var columns = circ.util.columns( 
25                         { 
26                                 'barcode' : { 'hidden' : false },
27                                 'title' : { 'hidden' : false },
28                                 'location' : { 'hidden' : false },
29                                 'call_number' : { 'hidden' : false },
30                                 'status' : { 'hidden' : false },
31                         } 
32                 );
33
34                 JSAN.use('util.list'); 
35
36                 obj.list1 = new util.list('pending_copies_list');
37                 obj.list1.init(
38                         {
39                                 'columns' : columns,
40                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
41                                 'on_select' : function(ev) {
42                                         try {
43                                                 JSAN.use('util.functional');
44                                                 var sel = obj.list1.retrieve_selection();
45                                                 obj.selection_list1 = util.functional.map_list(
46                                                         sel,
47                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
48                                                 );
49                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 1 = ' + js2JSON(obj.selection_list1) );
50                                                 if (obj.selection_list1.length == 0) {
51                                                         obj.controller.view.copy_buckets_sel_add.disabled = true;
52                                                 } else {
53                                                         obj.controller.view.copy_buckets_sel_add.disabled = false;
54                                                 }
55                                         } catch(E) {
56                                                 alert('FIXME: ' + E);
57                                         }
58                                 },
59
60                         }
61                 );
62                 for (var i = 0; i < obj.copy_ids.length; i++) {
63                         var item = obj.flesh_item_for_list( obj.copy_ids[i] );
64                         if (item) obj.list1.append( item );
65                 }
66                 
67                 obj.list2 = new util.list('copies_in_bucket_list');
68                 obj.list2.init(
69                         {
70                                 'columns' : columns,
71                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
72                                 'on_select' : function(ev) {
73                                         try {
74                                                 JSAN.use('util.functional');
75                                                 var sel = obj.list2.retrieve_selection();
76                                                 obj.selection_list2 = util.functional.map_list(
77                                                         sel,
78                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
79                                                 );
80                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 2 = ' + js2JSON(obj.selection_list2) );
81                                                 if (obj.selection_list2.length == 0) {
82                                                         obj.controller.view.copy_buckets_delete_item.disabled = true;
83                                                 } else {
84                                                         obj.controller.view.copy_buckets_delete_item.disabled = false;
85                                                 }
86                                         } catch(E) {
87                                                 alert('FIXME: ' + E);
88                                         }
89                                 },
90                         }
91                 );
92                 
93                 JSAN.use('util.controller'); obj.controller = new util.controller();
94                 obj.controller.init(
95                         {
96                                 'control_map' : {
97                                         'copy_buckets_menulist_placeholder' : [
98                                                 ['render'],
99                                                 function(e) {
100                                                         return function() {
101                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
102                                                                 var items = util.functional.map_list(
103                                                                         obj.network.simple_request(
104                                                                                 'BUCKET_RETRIEVE_VIA_USER',
105                                                                                 [ obj.session, obj.data.list.au[0].id() ]
106                                                                         ).copy,
107                                                                         function(o) {
108                                                                                 return [ o.name(), o.id() ];
109                                                                         }
110                                                                 );
111                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
112                                                                 util.widgets.remove_children( e );
113                                                                 var ml = util.widgets.make_menulist(
114                                                                         items
115                                                                 );
116                                                                 e.appendChild( ml );
117                                                                 ml.setAttribute('id','bucket_menulist');
118                                                                 ml.setAttribute('accesskey','');
119
120                                                                 function change_bucket(ev) {
121                                                                         var bucket_id = ev.target.value;
122                                                                         var bucket = obj.network.simple_request(
123                                                                                 'BUCKET_FLESH',
124                                                                                 [ obj.session, 'copy', bucket_id ]
125                                                                         );
126                                                                         var items = bucket.items() || [];
127                                                                         obj.list2.clear();
128                                                                         for (var i = 0; i < items.length; i++) {
129                                                                                 var item = obj.flesh_item_for_list( 
130                                                                                         items[i].target_copy(),
131                                                                                         items[i].id()
132                                                                                 );
133                                                                                 if (item) obj.list2.append( item );
134                                                                         }
135                                                                 }
136
137                                                                 ml.addEventListener( 'command', change_bucket , false);
138                                                                 obj.controller.view.bucket_menulist = ml;
139                                                                 change_bucket( 
140                                                                         { 'target' : { 
141                                                                                 'value' : ml.firstChild.firstChild.getAttribute('value') 
142                                                                                 } 
143                                                                         } 
144                                                                 );
145                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('command',ml);
146                                                         };
147                                                 },
148                                         ],
149
150                                         'copy_buckets_add' : [
151                                                 ['command'],
152                                                 function() {
153                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
154                                                         if (!bucket_id) return;
155                                                         for (var i = 0; i < obj.copy_ids.length; i++) {
156                                                                 var bucket_item = new ccbi();
157                                                                 bucket_item.isnew('1');
158                                                                 bucket_item.bucket(bucket_id);
159                                                                 bucket_item.target_copy( obj.copy_ids[i] );
160                                                                 try {
161                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
162                                                                                 [ obj.session, 'copy', bucket_item ]);
163
164                                                                         if (typeof robj == 'object') throw robj;
165
166                                                                         var item = obj.flesh_item_for_list( obj.copy_ids[i], robj );
167                                                                         if (!item) continue;
168
169                                                                         obj.list2.append( item );
170                                                                 } catch(E) {
171                                                                         alert( js2JSON(E) );
172                                                                 }
173                                                         }
174                                                 }
175                                         ],
176                                         'copy_buckets_sel_add' : [
177                                                 ['command'],
178                                                 function() {                                                        
179                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
180                                                         if (!bucket_id) return;
181                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
182                                                                 var acp_id = obj.selection_list1[i][0];
183                                                                 //var barcode = obj.selection_list1[i][1];
184                                                                 var bucket_item = new ccbi();
185                                                                 bucket_item.isnew('1');
186                                                                 bucket_item.bucket(bucket_id);
187                                                                 bucket_item.target_copy( acp_id );
188                                                                 try {
189                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
190                                                                                 [ obj.session, 'copy', bucket_item ]);
191
192                                                                         if (typeof robj == 'object') throw robj;
193
194                                                                         var item = obj.flesh_item_for_list( acp_id, robj );
195                                                                         if (!item) continue;
196
197                                                                         obj.list2.append( item );
198                                                                 } catch(E) {
199                                                                         alert( js2JSON(E) );
200                                                                 }
201                                                         }
202
203                                                 }
204                                         ],
205                                         'copy_buckets_export' : [
206                                                 ['command'],
207                                                 function() {                                                        
208                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
209                                                                 var acp_id = obj.selection_list2[i][0];
210                                                                 //var barcode = obj.selection_list1[i][1];
211                                                                 //var bucket_item_id = obj.selection_list1[i][2];
212                                                                 var item = obj.flesh_item_for_list( acp_id );
213                                                                 if (item) obj.list1.append( item );
214                                                         }
215
216                                                 }
217                                         ],
218
219                                         'copy_buckets_delete_item' : [
220                                                 ['command'],
221                                                 function() {
222                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
223                                                                 try {
224                                                                         //var acp_id = obj.selection_list2[i][0];
225                                                                         //var barcode = obj.selection_list2[i][1];
226                                                                         var bucket_item_id = obj.selection_list2[i][2];
227                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
228                                                                                 [ obj.session, 'copy', bucket_item_id ]);
229                                                                         if (typeof robj == 'object') throw robj;
230                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
231                                                                 } catch(E) {
232                                                                         alert(js2JSON(E));
233                                                                 }
234                                                         }
235                                                 }
236                                         ],
237                                         'copy_buckets_delete_bucket' : [
238                                                 ['command'],
239                                                 function() {
240                                                         try {
241                                                                 var bucket = obj.controller.view.bucket_menulist.value;
242                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[obj.session,'copy',bucket]);
243                                                                 if (typeof robj == 'object') throw robj;
244                                                                 obj.controller.render('copy_buckets_menulist_placeholder');
245                                                         } catch(E) {
246                                                                 alert('FIXME -- ' + E);
247                                                         }
248                                                 }
249                                         ],
250                                         'copy_buckets_new_bucket' : [
251                                                 ['command'],
252                                                 function() {
253                                                         try {
254                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
255
256                                                                 if (name) {
257                                                                         var bucket = new ccb();
258                                                                         bucket.btype('staff_client');
259                                                                         bucket.owner( obj.data.list.au[0].id() );
260                                                                         bucket.name( name );
261
262                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[obj.session,'copy',bucket]);
263
264                                                                         if (typeof robj == 'object') throw robj;
265
266                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
267                                                                 }
268                                                         } catch(E) {
269                                                                 alert( js2JSON(E) );
270                                                         }
271                                                 }
272                                         ],
273                                         'cmd_broken' : [
274                                                 ['command'],
275                                                 function() { alert('Not Yet Implemented'); }
276                                         ],
277                                         'cmd_copy_buckets_print' : [
278                                                 ['command'],
279                                                 function() {
280                                                         dump( js2JSON( obj.list2.dump() ) );
281                                                         alert( js2JSON( obj.list2.dump() ) );
282                                                 }
283                                         ],
284                                         'cmd_copy_buckets_reprint' : [
285                                                 ['command'],
286                                                 function() {
287                                                 }
288                                         ],
289                                         'cmd_copy_buckets_done' : [
290                                                 ['command'],
291                                                 function() {
292                                                         window.close();
293                                                 }
294                                         ],
295                                 }
296                         }
297                 );
298                 this.controller.render();
299
300         },
301
302         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
303                 var obj = this;
304                 try {
305                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
306                         if (copy == null) {
307                                 throw('COPY NOT FOUND');
308                         } else {
309                                 var item = {
310                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
311                                         'row' : {
312                                                 'my' : {
313                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
314                                                         'acp' : copy,
315                                                 }
316                                         }
317                                 };
318                                 return item;
319                         }
320                 } catch(E) {
321                         alert('FIXME: need special alert and error handling\n' + js2JSON(E));
322                         return null;
323                 }
324
325         },
326         
327 }
328
329 dump('exiting cat.copy_buckets.js\n');