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