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