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