]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/search_form.js
LP#1359934: prevent error opening patron search form
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / search_form.js
1 dump('entering patron/search_form.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.search_form = function (params) {
5
6     JSAN.use('util.error'); this.error = new util.error();
7     JSAN.use('util.network'); this.network = new util.network();
8     this.w = window;
9 }
10
11 patron.search_form.prototype = {
12
13     'init' : function( params ) {
14
15         var obj = this;
16         obj.event_listeners = new EventListenerList();
17
18         // The bulk of params.query is getting parsed/rendered by obj.controller.init below, and will be reconstituted from possibly modified XUL elements upon Submit.
19         // But we're going to let search_limit and search_sort be configurable now by those spawning this interface, and let's assume there are no corresponding widgets for now.  
20         // I'm going to place them into the "obj" scope for this instance.
21         obj.search_limit = params.query.search_limit;
22         obj.search_sort = (typeof params.query.search_sort === 'undefined') ?
23                             null :
24                             JSON2js( params.query.search_sort ); // Let's assume this is encoded as JSON
25
26         JSAN.use('OpenILS.data'); this.OpenILS = {}; 
27         obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
28
29         JSAN.use('util.controller'); obj.controller = new util.controller();
30         obj.controller.init(
31             {
32                 control_map : {
33                     'cmd_broken' : [
34                         ['command'],
35                         function() { alert($("commonStrings").getString('common.unimplemented')); }
36                     ],
37                     'cmd_patron_search_submit' : [
38                         ['command'],
39                         function() {
40                             obj.submit();
41                         }
42                     ],
43                     'cmd_patron_search_clear' : [
44                         ['command'],
45                         function() { 
46                             obj.controller.render(); 
47                             window.xulG.clear_left_deck();
48                         }
49                     ],
50                     'family_name' : [
51                         ['render'],
52                         function(e) {
53                             return function() {
54                                 if (params.query&&params.query.family_name) {
55                                     e.setAttribute('value',params.query.family_name);
56                                     e.value = params.query.family_name;
57                                 } else {
58                                     e.value = '';
59                                 }
60                             };
61                         }
62                     ],
63                     'first_given_name' : [
64                         ['render'],
65                         function(e) {
66                             return function() {
67                                 if (params.query&&params.query.first_given_name) {
68                                     e.setAttribute('value',params.query.first_given_name);
69                                     e.value = params.query.first_given_name;
70                                 } else {
71                                     e.value = '';
72                                 }
73                             };
74                         }
75                     ],
76                     'second_given_name' : [
77                         ['render'],
78                         function(e) {
79                             return function() {
80                                 if (params.query&&params.query.second_given_name) {
81                                     e.setAttribute('value',params.query.second_given_name);
82                                     e.value = params.query.second_given_name;
83                                 } else {
84                                     e.value = '';
85                                 }
86                             };
87                         }
88                     ],
89                     'alias' : [
90                         ['render'],
91                         function(e) {
92                             return function() {
93                                 if (params.query&&params.query.alias) {
94                                     e.setAttribute('value',params.query.alias);
95                                     e.value = params.query.alias;
96                                 } else {
97                                     e.value = '';
98                                 }
99                             };
100                         }
101                     ],
102                     'usrname' : [
103                         ['render'],
104                         function(e) {
105                             return function() {
106                                 if (params.query&&params.query.usrname) {
107                                     e.setAttribute('value',params.query.usrname);
108                                     e.value = params.query.usrname;
109                                 } else {
110                                     e.value = '';
111                                 }
112                             };
113                         }
114                     ],
115                     'card' : [
116                         ['render'],
117                         function(e) {
118                             return function() {
119                                 if (params.query&&params.query.card) {
120                                     e.setAttribute('value',params.query.card);
121                                     e.value = params.query.card;
122                                 } else {
123                                     e.value = '';
124                                 }
125                             };
126                         }
127                     ],
128                     'email' : [
129                         ['render'],
130                         function(e) {
131                             return function() {
132                                 if (params.query&&params.query.email) {
133                                     e.setAttribute('value',params.query.email);
134                                     e.value = params.query.email;
135                                 } else {
136                                     e.value = '';
137                                 }
138                             };
139                         }
140                     ],
141                     'phone' : [
142                         ['render'],
143                         function(e) {
144                             return function() {
145                                 if (params.query&&params.query.phone) {
146                                     e.setAttribute('value',params.query.phone);
147                                     e.value = params.query.phone;
148                                 } else {
149                                     e.value = '';
150                                 }
151                             };
152                         }
153                     ],
154                     'ident' : [
155                         ['render'],
156                         function(e) {
157                             return function() {
158                                 if (params.query&&params.query.ident) {
159                                     e.setAttribute('value',params.query.ident);
160                                     e.value = params.query.ident;
161                                 } else if (params.query&&params.query.ident_value) {
162                                     e.setAttribute('value',params.query.ident_value);
163                                     e.value = params.query.ident_value;
164                                 } else if (params.query&&params.query.ident_value2) {
165                                     e.setAttribute('value',params.query.ident_value2);
166                                     e.value = params.query.ident_value2;
167                                 } else {
168                                     e.value = '';
169                                 }
170                             };
171                         }
172                     ],
173                     'street1' : [
174                         ['render'],
175                         function(e) {
176                             return function() {
177                                 if (params.query&&params.query.street1) {
178                                     e.setAttribute('value',params.query.street1);
179                                     e.value = params.query.street1;
180                                 } else {
181                                     e.value = '';
182                                 }
183                             };
184                         }
185                     ],
186                     'street2' : [
187                         ['render'],
188                         function(e) {
189                             return function() {
190                                 if (params.query&&params.query.street2) {
191                                     e.setAttribute('value',params.query.street2);
192                                     e.value = params.query.street2;
193                                 } else {
194                                     e.value = '';
195                                 }
196                             };
197                         }
198                     ],
199                     'city' : [
200                         ['render'],
201                         function(e) {
202                             return function() {
203                                 if (params.query&&params.query.city) {
204                                     e.setAttribute('value',params.query.city);
205                                     e.value = params.query.city;
206                                 } else {
207                                     e.value = '';
208                                 }
209                             };
210                         }
211                     ],
212                     'state' : [
213                         ['render'],
214                         function(e) {
215                             return function() {
216                                 if (params.query&&params.query.state) {
217                                     e.setAttribute('value',params.query.state);
218                                     e.value = params.query.state;
219                                 } else {
220                                     e.value = '';
221                                 }
222                             };
223                         }
224                     ],
225                     'post_code' : [
226                         ['render'],
227                         function(e) {
228                             return function() {
229                                 if (params.query&&params.query.post_code) {
230                                     e.setAttribute('value',params.query.post_code);
231                                     e.value = params.query.post_code;
232                                 } else {
233                                     e.value = '';
234                                 }
235                             };
236                         }
237                     ],
238                     'profile' : [ ['render'], function(e) {
239                             return function() {};
240                         } 
241                     ],
242                     'inactive' : [ ['render'], function(e) { 
243                             return function() {}; 
244                         } 
245                     ],
246                     'search_depth' : [ ['render'],function(e) {
247                             return function() {};
248                         }
249                     ]
250                 }
251             }
252         );
253
254         obj.controller.render();
255         var nl = document.getElementsByTagName('textbox');
256         for (var i = 0; i < nl.length; i++) {
257             obj.event_listeners.add(nl[i], 'keypress', function(ev) {
258                 if (ev.target.tagName != 'textbox') return;
259                 if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.submit(); }, 0);
260             },false);
261         }
262         document.getElementById('family_name').focus();
263
264         JSAN.use('util.file'); JSAN.use('util.widgets'); JSAN.use('util.functional');
265         util.widgets.remove_children(obj.controller.view.search_depth);
266         var ml = util.widgets.make_menulist(
267             util.functional.map_list( obj.OpenILS.data.list.my_aou,
268                 function(el,idx) {
269                     return [ el.name(), el.id() ]
270                 }
271             ).sort(
272                 function(a,b) {
273                     if (a[1] < b[1]) return -1;
274                     if (a[1] > b[1]) return 1;
275                     return 0;
276                 }
277             )
278         );
279         obj.event_listeners.add(ml, 'command', function() {
280                 ml.parentNode.setAttribute('value',ml.value);
281                 var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
282                 util.widgets.save_attributes(file, { 'search_depth_ml' : [ 'value' ], 'inactive' : [ 'value' ] });
283             }, false
284         );
285         ml.setAttribute('id','search_depth_ml');
286         obj.controller.view.search_depth.appendChild(ml);
287
288         var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
289         util.widgets.load_attributes(file);
290         ml.value = ml.getAttribute('value');
291         if (! ml.value) {
292             ml.value = 0;
293             ml.setAttribute('value',ml.value);
294         }
295
296         var cb = obj.controller.view.inactive;
297         obj.event_listeners.add(cb, 'command',function() {
298                 cb.setAttribute('value',cb.checked ? "true" : "false");
299                 var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
300                 util.widgets.save_attributes(file, { 'search_depth_ml' : [ 'value' ], 'inactive' : [ 'value' ] });
301             }, false
302         );
303         cb.checked = cb.getAttribute('value') == "true" ? true : false;
304
305         /* Populate the Patron Profile filter, if it exists */
306         if (obj.controller.view.profile) {
307             util.widgets.remove_children(obj.controller.view.profile);
308             var profile_ml = util.widgets.make_menulist(
309                 util.functional.map_list( obj.OpenILS.data.list.pgt,
310                     function(el,idx) {
311                         return [ el.name(), el.id() ]
312                     }
313                 ).sort(
314                     function(a,b) {
315                         if (a[0] < b[0]) return -1;
316                         if (a[0] > b[0]) return 1;
317                         return 0;
318                     }
319                 )
320             );
321             obj.event_listeners.add(profile_ml, 'command', function() {
322                     profile_ml.parentNode.setAttribute('value', profile_ml.value);
323                 }, false
324             );
325             profile_ml.setAttribute('id','profile_ml');
326
327             /* Add an empty menu item as the default profile */
328             var empty = document.createElement('menuitem'); 
329             profile_ml.firstChild.insertBefore(empty, profile_ml.firstChild.firstChild);
330             empty.setAttribute('label', '');
331             empty.setAttribute('value', ''); 
332             obj.controller.view.profile.appendChild(profile_ml);
333             profile_ml.value = profile_ml.getAttribute('value');
334         }
335     },
336
337     'cleanup' : function() {
338         var obj = this;
339         obj.controller.cleanup();
340         obj.event_listeners.removeAll();    
341     }, 
342
343     'on_submit' : function(q) {
344         var msg = 'Query = ' + q;
345         this.error.sdump('D_PATRON', msg);
346     },
347
348     'submit' : function() {
349         window.xulG.clear_left_deck();
350         var obj = this;
351         var query = {};
352         for (var i = 0; i < obj.controller.render_list.length; i++) {
353             var id = obj.controller.render_list[i][0];
354             var node = document.getElementById(id);
355             if (node && node.value != '') {
356                 if (id == 'inactive') {
357                     query[id] = node.getAttribute('value');
358                     obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + node.getAttribute('value') + '\n');
359                 } else if (id == 'profile') {
360                     query[id] = node.firstChild.getAttribute('value');
361                     obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + node.getAttribute('value') + '\n');
362                 } else {
363                     if (id == 'search_depth') {
364                         query[id] = node.firstChild.getAttribute('value'); 
365                     } else {
366                          var value = node.value.replace(/^\s+/,'').replace(/[\\\s]+$/,'');
367                         //value = value.replace(/\d/g,'');
368                         switch(id) {
369                             case 'family_name' :
370                             case 'first_given_name' :
371                             case 'second_given_name' :
372                                 value = value.replace(/^[\d\s]+/g,'').replace(/[\d\s]+$/g,'')
373                             break;
374                         }
375                         if (value != '') {
376                             query[id] = value;
377                             obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + value + '\n');
378                         }
379                     }
380                 } 
381             }
382         }
383         if (typeof obj.on_submit == 'function') {
384             obj.on_submit(query,obj.search_limit,obj.search_sort);
385         }
386         if (typeof window.xulG == 'object' 
387             && typeof window.xulG.on_submit == 'function') {
388             obj.error.sdump('D_PATRON','patron.search_form: Calling external .on_submit()\n');
389             window.xulG.on_submit(query,obj.search_limit,obj.search_sort);
390         } else {
391             obj.error.sdump('D_PATRON','patron.search_form: No external .on_query()\n');
392         }
393     },
394
395 }
396
397 dump('exiting patron/search_form.js\n');