]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/search_result.js
LP#1086458: clean up after event listeners in circ/patron interface
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / search_result.js
1 dump('entering patron/search_result.js\n');
2
3 function $(id) { return document.getElementById(id); }
4
5 if (typeof patron == 'undefined') patron = {};
6 patron.search_result = function (params) {
7
8     JSAN.use('util.error'); this.error = new util.error();
9     JSAN.use('util.network'); this.network = new util.network();
10     this.w = window;
11 }
12
13 patron.search_result.prototype = {
14
15     'result_cap' : 50,
16
17     'init' : function( params ) {
18
19         var obj = this;
20
21         obj.query = params['query'];
22         obj.search_limit = params['search_limit'];
23         obj.search_sort = params['search_sort'];
24
25         JSAN.use('OpenILS.data'); this.OpenILS = {}; 
26         obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
27         var obscure_dob = String( obj.OpenILS.data.hash.aous['circ.obscure_dob'] ) == 'true';
28
29         var result_cap_setting = obj.OpenILS.data.hash.aous[
30             'ui.patron_search.result_cap'
31         ];
32         if (typeof result_cap_setting != 'undefined') {
33             obj.result_cap = Math.abs( result_cap_setting );
34         }
35
36         JSAN.use('util.list'); obj.list = new util.list('patron_list');
37
38         JSAN.use('patron.util');
39         var columns = obj.list.fm_columns('au',{
40             '*' : { 'remove_virtual' : true, 'expanded_label' : false, 'hidden' : true },
41             'au_barcode' : { 'hidden' : false },
42             'au_barred' : { 'hidden' : false },
43             'au_family_name' : { 'hidden' : false },
44             'au_first_given_name' : { 'hidden' : false },
45             'au_second_given_name' : { 'hidden' : false },
46             'au_dob' : { 'hidden' : false },
47             'au_profile' : { 'fleshed_display_field' : 'name' },
48             'au_ident_type' : { 'fleshed_display_field' : 'name' },
49             'au_ident_type2' : { 'fleshed_display_field' : 'name' },
50             'au_net_access_level' : { 'fleshed_display_field' : 'name' }
51         }).concat(
52             obj.list.fm_columns('ac',{
53                 '*' : { 'remove_virtual' : true, 'expanded_label' : true, 'hidden' : true },
54                 'ac_barcode' : { 'hidden' : false }
55             })
56         ).concat(
57             obj.list.fm_columns('aua',{
58                 '*' : {
59                     'dataobj' : 'billing_aua',
60                     'remove_virtual' : true,
61                     'label_prefix' : $('patronStrings').getString('staff.patron.search_result.billing_address_column_label_prefix'),
62                     'hidden' : true
63                 }
64             },'billing_')
65         ).concat(
66             obj.list.fm_columns('aua',{
67                 '*' : {
68                     'dataobj' : 'mailing_aua',
69                     'remove_virtual' : true,
70                     'label_prefix' : $('patronStrings').getString('staff.patron.search_result.mailing_address_column_label_prefix'),
71                     'hidden' : true
72                 }
73             },'mailing_')
74         );
75
76         obj.dblclick_handler = function(ev) {
77             JSAN.use('util.functional');
78             var sel = obj.list.retrieve_selection();
79             var list = util.functional.map_list(
80                 sel,
81                 function(o) { return o.getAttribute('retrieve_id'); }
82             );
83             obj.controller.view.cmd_sel_clip.setAttribute('disabled', list.length < 1 );
84             if (typeof obj.on_dblclick == 'function') {
85                 obj.on_dblclick(list);
86             }
87             if (typeof window.xulG == 'object' && typeof window.xulG.on_dblclick == 'function') {
88                 obj.error.sdump('D_PATRON','patron.search_result: Calling external .on_dblclick()\n');
89                 window.xulG.on_dblclick(list);
90             } else {
91                 obj.error.sdump('D_PATRON','patron.search_result: No external .on_dblclick()\n');
92             }
93         };
94
95         obj.list.init(
96             {
97                 'columns' : columns,
98                 'retrieve_row' : function(params) {
99                     var id = params.retrieve_id;
100                     var au_obj = patron.util.retrieve_fleshed_au_via_id(
101                         ses(),
102                         id,
103                         ["card","billing_address","mailing_address"],
104                         function(req) {
105                             try {
106                                 var row = params.row;
107                                 if (typeof row.my == 'undefined') row.my = {};
108                                 row.my.au = req.getResultObject();
109                                 row.my.ac = row.my.au.card();
110                                 row.my.billing_aua = row.my.au.billing_address();
111                                 row.my.mailing_aua = row.my.au.mailing_address();
112                                 if (typeof params.on_retrieve == 'function') {
113                                     params.on_retrieve(row);
114                                 } else {
115                                     alert($("patronStrings").getFormattedString('staff.patron.search_result.init.typeof_params', [typeof params.on_retrieve]));
116                                 }
117                             } catch(E) {
118                                 alert('error: ' + E);
119                             }
120                         }
121                     );
122                 },
123                 'on_dblclick' : obj.dblclick_handler,
124                 'on_select' : function(ev) {
125                     JSAN.use('util.functional');
126                     var sel = obj.list.retrieve_selection();
127                     var list = util.functional.map_list(
128                         sel,
129                         function(o) { return o.getAttribute('retrieve_id'); }
130                     );
131                     obj.controller.view.cmd_sel_clip.setAttribute('disabled', list.length < 1 );
132                     if (typeof obj.on_select == 'function') {
133                         obj.on_select(list);
134                     }
135                     if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
136                         obj.error.sdump('D_PATRON','patron.search_result: Calling external .on_select()\n');
137                         window.xulG.on_select(list);
138                     } else {
139                         obj.error.sdump('D_PATRON','patron.search_result: No external .on_select()\n');
140                     }
141                 }
142             }
143         );
144         JSAN.use('util.controller'); obj.controller = new util.controller();
145         obj.controller.init(
146             {
147                 control_map : {
148                     'cmd_broken' : [
149                         ['command'],
150                         function() { alert($("commonStrings").getString('common.unimplemented')); }
151                     ],
152                     'cmd_search_print' : [
153                         ['command'],
154                         function() {
155                             try {
156                                 obj.list.dump_csv_to_printer();
157                             } catch(E) {
158                                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.search_result.init.search_print'),E);
159                             }
160                         }
161                     ],
162                     'cmd_sel_clip' : [
163                         ['command'],
164                         function() {
165                             try {
166                                 obj.list.clipboard();
167                             } catch(E) {
168                                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.search_result.init.search_clipboard'),E);
169                             }
170                         }
171                     ],
172                     'cmd_save_cols' : [
173                         ['command'],
174                         function() {
175                             try {
176                                 obj.list.save_columns();
177                             } catch(E) {
178                                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.search_result.init.search_saving_columns'),E);
179                             }
180                         }
181                     ],
182                 }
183             }
184         );
185
186         if (obj.query) obj.search(obj.query);
187     },
188
189     'cleanup' : function( params ) {
190         var obj = this;
191         obj.controller.cleanup();
192         obj.list.cleanup();
193     },
194
195     'search' : function(query) {
196         var obj = this;
197         var search_hash = {};
198         obj.search_term_count = 0;
199         var inactive = false;
200         var search_depth = 0;
201         for (var i in query) {
202             switch( i ) {
203                 case 'card':
204                     search_hash[ i ] = {};
205                     search_hash[ i ].value = query[i];
206                     search_hash[i].group = 3; 
207                     obj.search_term_count++;
208                 break;
209
210                 case 'phone': case 'ident': 
211                 
212                     search_hash[ i ] = {};
213                     search_hash[ i ].value = query[i];
214                     search_hash[i].group = 2; 
215                     obj.search_term_count++;
216                 break;
217
218                 case 'street1': case 'street2': case 'city': case 'state': case 'post_code': 
219                 
220                     search_hash[ i ] = {};
221                     search_hash[ i ].value = query[i];
222                     search_hash[i].group = 1; 
223                     obj.search_term_count++;
224                 break;
225
226                 case 'family_name': case 'first_given_name': case 'second_given_name': case 'email': case 'alias': case 'usrname': case 'profile':
227
228                     search_hash[ i ] = {};
229                     search_hash[ i ].value = query[i];
230                     search_hash[i].group = 0; 
231                     obj.search_term_count++;
232                 break;
233
234                 case 'inactive':
235                     if (query[i] == 'checked' || query[i] == 'true') inactive = true;
236                 break;
237
238                 case 'search_depth':
239                     search_depth = function(a){return a;}(query[i]);
240                 break;
241             }
242         }
243         try {
244             var results = [];
245
246             var sort_params = obj.search_sort;
247             if (!sort_params) {
248                 sort_params = [ 'family_name ASC', 'first_given_name ASC', 'second_given_name ASC', 'dob DESC' ];
249             }
250             var params = [ 
251                 ses(), 
252                 search_hash, 
253                 typeof obj.search_limit != 'undefined' && typeof obj.search_limit != 'null' ? obj.search_limit : obj.result_cap + 1, 
254                 sort_params
255             ];
256             if (inactive) {
257                 params.push(1);
258                 if (document.getElementById('active')) {
259                     document.getElementById('active').setAttribute('hidden','false');
260                     document.getElementById('active').hidden = false;
261                 }
262             } else {
263                 params.push(0);
264             }
265             params.push(search_depth);
266             if (obj.search_term_count > 0) {
267                 //alert('search params = ' + obj.error.pretty_print( js2JSON( params ) ) );
268                 results = this.network.simple_request( 'FM_AU_IDS_RETRIEVE_VIA_HASH', params );
269                 if ( results == null ) results = [];
270                 if (typeof results.ilsevent != 'undefined') throw(results);
271                 if (results.length == 0) {
272                     alert($("patronStrings").getString('staff.patron.search_result.search.no_patrons_found'));
273                     return;
274                 }
275                 if (results.length == typeof obj.search_limit != 'undefined' && typeof obj.search_limit != 'null' ? obj.search_limit : obj.result_cap+1) {
276                     results.pop();
277                     alert($("patronStrings").getFormattedString('staff.patron.search_result.search.capped_results', [typeof obj.search_limit != 'undefined' && typeof obj.search_limit != 'null' ? obj.search_limit : obj.result_cap]));
278                 }
279             } else {
280                 alert($("patronStrings").getString('staff.patron.search_result.search.enter_search_terms'));
281                 return;
282             }
283
284             obj.list.clear();
285             //this.list.append( { 'retrieve_id' : results[i], 'row' : {} } );
286             var funcs = [];
287
288                 function gen_func(r) {
289                     return function() {
290                         obj.list.append( { 'retrieve_id' : r, 'row' : {}, 'to_bottom' : true, 'no_auto_select' : true } );
291                     }
292                 }
293
294             for (var i = 0; i < results.length; i++) {
295                 funcs.push( gen_func(results[i]) );
296             }
297             JSAN.use('util.exec'); var exec = new util.exec(4);
298             exec.chain( funcs );
299
300         } catch(E) {
301             this.error.standard_unexpected_error_alert('patron.search_result.search',E);
302         }
303     }
304
305 }
306
307 dump('exiting patron/search_result.js\n');