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