]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/staged.js
246a57d1614af85f9528ce1730e1ab58e5a05129
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / staged.js
1 var list; var error; var net; var rows; var row_id_usrname_map; var menu_lib;
2
3 function $(id) { return document.getElementById(id); }
4
5 //// parent interfaces often call these
6 function default_focus() { $('lib_menu').focus(); }
7 function refresh() { populate_list(); }
8 ////
9
10 function staged_init() {
11     try {
12         commonStrings = $('commonStrings');
13         patronStrings = $('patronStrings');
14
15         if (typeof JSAN == 'undefined') {
16             throw(
17                 commonStrings.getString('common.jsan.missing')
18             );
19         }
20
21         JSAN.errorLevel = "die"; // none, warn, or die
22         JSAN.addRepository('..');
23
24         JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
25         XML_HTTP_SERVER = data.server_unadorned;
26
27         JSAN.use('util.error'); error = new util.error();
28         JSAN.use('util.network'); net = new util.network();
29         JSAN.use('patron.util'); 
30         JSAN.use('util.list'); 
31         JSAN.use('util.functional'); 
32         JSAN.use('util.widgets');
33
34         dojo.require('openils.Util');
35
36         window.staged_event_listeners = new EventListenerList();
37         populate_lib_menu();
38         init_list();
39         $('list_actions').appendChild( list.render_list_actions() );
40         list.set_list_actions();
41         window.staged_event_listeners.add($('cmd_cancel'), 'command', gen_event_handler('cancel'), false);
42         window.staged_event_listeners.add($('cmd_load'), 'command', gen_event_handler('load'), false);
43         window.staged_event_listeners.add($('cmd_reload'), 'command', function() { populate_list(); }, false);
44         populate_list();
45         default_focus();
46
47     } catch(E) {
48         var err_prefix = 'staged.js -> staged_init() : ';
49         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
50     }
51 }
52
53 function staged_cleanup() {
54     try {
55         list.cleanup();
56         window.staged_event_listeners.removeAll();
57     } catch(E) {
58         var err_prefix = 'staged.js -> staged_cleanup() : ';
59         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
60     }
61 }
62
63 function populate_lib_menu() {
64     try {
65         JSAN.use('util.widgets');
66         var x = document.getElementById('lib_menu_placeholder');
67         if (!x) { return; }
68         util.widgets.remove_children( x );
69
70         JSAN.use('util.file');
71         var file = new util.file('offline_ou_list');
72         if (file._file.exists()) {
73             var list_data = file.get_object(); file.close();
74             menu_lib = x.getAttribute('value') || ses('ws_ou');
75             var ml = util.widgets.make_menulist( list_data[0], menu_lib );
76             ml.setAttribute('id','lib_menu');
77             x.appendChild( ml );
78             window.staged_event_listeners.add(ml, 
79                 'command',
80                 function(ev) {
81                     menu_lib = ev.target.value;
82                     x.setAttribute('value',ev.target.value); oils_persist(x);
83                     populate_list();
84                 },
85                 false
86             );
87         } else {
88             alert($("patronStrings").getString('staff.patron.staged.lib_menus.missing_library_list'));
89         }
90     } catch(E) {
91         alert('Error in staged.js, populate_lib_menu(): ' + E);
92     }
93 }
94
95 function gen_event_handler(method) { // cancel or load?
96     return function(ev) {
97         try {
98             var sel = list.retrieve_selection();
99             var row_ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ).row_id; } );
100             var usrnames = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ).usrname; } );
101
102             if (method == 'cancel') {
103                 cancel( row_ids );
104             } else {
105                 load( usrnames );
106             }
107
108         } catch(E) {
109             alert('Error in patron/staged.js, handle_???_event(): ' + E);
110         }
111     };
112 }
113
114 function cancel(ids) {
115     try {
116
117         if (! window.confirm( $('patronStrings').getString('staff.patron.staged.confirm_patron_delete') ) ) { return; }
118         var pm = $('progress'); pm.value = 0; pm.hidden = false;
119         var idx = 0;
120
121         function gen_req_handler(id) {
122             return function(req) {
123                 try {
124                     idx++; pm.value = Number( pm.value ) + 100/ids.length; 
125                     if (idx == ids.length) { pm.value = 0; pm.hidden = true; }
126                     var robj = req.getResultObject();
127                     if (robj == '1') {
128                         var node = rows[ row_id_usrname_map[ id ] ].treeitem_node;
129                         var parentNode = node.parentNode;
130                         parentNode.removeChild( node );
131                         delete(rows[ row_id_usrname_map[ id ] ]);
132                         delete(row_id_usrname_map[ id ]);
133                     } else {
134                         alert( $('patronStrings').getFormattedString('staff.patron.staged.error_on_delete',[ id ]) );
135                     }
136                 } catch(E) {
137                     alert('Error in staged.js, cancel patron request handler: ' + E);
138                 }
139             }
140         }
141
142         for (var i = 0; i < ids.length; i++) {
143             net.simple_request('FM_STGU_DELETE', [ ses(), ids[i] ], gen_req_handler( ids[i] ));
144         }
145     } catch(E) {
146         alert('Error in staged.js, cancel(): ' + E);
147     }
148 }
149
150 function spawn_search(s) {
151     data.stash_retrieve();
152     xulG.new_patron_tab( {}, { 'doit' : 1, 'query' : s } );
153 }
154
155 function spawn_editor(p,func) {
156     var url = urls.XUL_PATRON_EDIT;
157     var loc = xulG.url_prefix('XUL_REMOTE_BROWSER');
158     xulG.new_tab(
159         loc, 
160         {}, 
161         { 
162             'url' : url,
163             'show_print_button' : true , 
164             'tab_name' : $("patronStrings").getFormattedString('staff.patron.staged.register_patron',[p.stage]),
165             'passthru_content_params' : {
166                 'spawn_search' : spawn_search,
167                 'spawn_editor' : spawn_editor,
168                 'url_prefix' : xulG.url_prefix,
169                 'new_tab' : xulG.new_tab,
170                 'new_patron_tab' : xulG.new_patron_tab,
171                 'on_save' : function(p) { patron.util.work_log_patron_edit(p); if (typeof func == 'function') { func(p); } },
172                 'params' : p
173             },
174             'lock_tab' : xulG.lock_tab,
175             'unlock_tab' : xulG.unlock_tab
176         }
177     );
178 }
179
180 function load( usrnames ) {
181     try {
182
183         function gen_on_save_handler(usrname) {
184             return function() {
185                 try {
186                     var node = rows[ usrname ].treeitem_node;
187                     var parentNode = node.parentNode;
188                     parentNode.removeChild( node );
189                     delete(row_id_usrname_map[ rows[ usrname ].row.my.stgu.row_id() ]);
190                     delete(rows[ usrname ]);
191                 } catch(E) {
192                     alert('Error in staged.js, load on save handler: ' + E);
193                 }
194             }
195         }
196
197         var seen = {};
198
199         for (var i = 0; i < usrnames.length; i++) {
200             if (! seen[ usrnames[i] ]) {
201                 seen[ usrnames[i] ] = true;
202                 spawn_editor( { 'stage' : usrnames[i] }, gen_on_save_handler( usrnames[i] ) );
203             }
204         }
205
206     } catch(E) {
207         alert('Error in staged.js, load(): ' + E);
208     }
209 }
210
211 function init_list() {
212     try {
213
214         list = new util.list( 'stgu_list' );
215         list.init( 
216             {
217                 'columns' : list.fm_columns(
218                     'stgu', {
219                         'stgu_ident_type' : { 'render' : function(my) { return data.hash.cit[ my.stgu.ident_type() ].name(); } },
220                         'stgu_home_ou' : { 'render' : function(my) { return data.hash.aou[ my.stgu.home_ou() ].shortname(); } }
221                     }
222                 ),
223                 'retrieve_row' : retrieve_row,
224                 'on_select' : handle_selection
225             }
226         );
227
228     } catch(E) {
229         var err_prefix = 'staged.js -> init_list() : ';
230         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
231     }
232 }
233
234 function retrieve_row(params) { // callback function for fleshing rows in a list
235     try {
236         params.treeitem_node.setAttribute('retrieve_id',js2JSON( { 'row_id' : params.row.my.stgu.row_id(), 'usrname' : params.row.my.stgu.usrname() } )); 
237         params.on_retrieve(params.row); 
238     } catch(E) {
239         alert('Error in staged.js, retrieve_row(): ' + E);
240     }
241     return params.row; 
242 }
243
244 function handle_selection(ev) { // handler for list row selection event
245     var sel = list.retrieve_selection();
246     if (sel.length > 0) {
247         $('cmd_cancel').setAttribute('disabled','false');
248         $('cmd_load').setAttribute('disabled','false');
249     } else {
250         $('cmd_cancel').setAttribute('disabled','true');
251         $('cmd_load').setAttribute('disabled','true');
252     }
253 };
254
255 function populate_list() {
256     try {
257
258         rows = {}; row_id_usrname_map = {};
259         list.clear();
260
261         function onResponse(r) {
262             var blob = openils.Util.readResponse(r);
263             var row_params = {
264                 'row' : {
265                     'my' : {
266                         'stgu' : blob.user
267                     }
268                 }
269             };
270             rows[ blob.user.usrname() ] = list.append( row_params );
271             row_id_usrname_map[ blob.user.row_id() ] = blob.user.usrname();
272         }
273
274         function onError(r) {
275             var my_stgu = openils.Util.readResponse(r);
276             alert('error, my_stgu = ' + js2JSON(my_stgu));
277         }
278
279         fieldmapper.standardRequest(
280             [api['FM_STGU_RETRIEVE'].app, api['FM_STGU_RETRIEVE'].method ],
281             {   async: true,
282                 params: [ses(), menu_lib || ses('ws_ou'), $('limit').value || 100],
283                 onresponse : onResponse,
284                 onerror : onError,
285                 oncomplete : function() {
286                 }
287             }
288         );
289
290     } catch(E) {
291         var err_prefix = 'staged.js -> populate_list() : ';
292         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
293     }
294 }