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