]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/search_form.js
Change each tab to 4 spaces in the staff client javascript files.
[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                     'inactive' : [ ['render'], function(e) { 
236                             return function() {}; 
237                         } 
238                     ],
239                     'search_depth' : [ ['render'],function(e) {
240                             return function() {};
241                         }
242                     ],
243                 }
244             }
245         );
246
247         obj.controller.render();
248         var nl = document.getElementsByTagName('textbox');
249         for (var i = 0; i < nl.length; i++) {
250             nl[i].addEventListener('keypress',function(ev){
251                 if (ev.target.tagName != 'textbox') return;
252                 if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.submit(); }, 0);
253             },false);
254         }
255         document.getElementById('family_name').focus();
256
257         JSAN.use('util.file'); JSAN.use('util.widgets'); JSAN.use('util.functional');
258         util.widgets.remove_children(obj.controller.view.search_depth);
259         var ml = util.widgets.make_menulist(
260             util.functional.map_list( obj.OpenILS.data.list.aout, 
261                 function(el,idx) {
262                     return [ el.opac_label(), el.depth() ]
263                 }
264             ).sort(
265                 function(a,b) {
266                     if (a[1] < b[1]) return -1;
267                     if (a[1] > b[1]) return 1;
268                     return 0;
269                 }
270             )
271         );
272         ml.addEventListener('command', function() {
273                 ml.parentNode.setAttribute('value',ml.value);
274                 var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
275                 util.widgets.save_attributes(file, { 'search_depth_ml' : [ 'value' ], 'inactive' : [ 'value' ] });
276             }, false
277         );
278         ml.setAttribute('id','search_depth_ml');
279         obj.controller.view.search_depth.appendChild(ml);
280
281         var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
282         util.widgets.load_attributes(file);
283         ml.value = ml.getAttribute('value');
284         if (! ml.value) {
285             ml.value = 0
286             ml.setAttribute('value',ml.value);
287         }
288
289         var cb = obj.controller.view.inactive;
290         cb.addEventListener('command',function() { 
291                 cb.setAttribute('value',cb.checked ? "true" : "false");
292                 var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
293                 util.widgets.save_attributes(file, { 'search_depth_ml' : [ 'value' ], 'inactive' : [ 'value' ] });
294             }, false
295         );
296         cb.checked = cb.getAttribute('value') == "true" ? true : false;
297
298     },
299
300     'on_submit' : function(q) {
301         var msg = 'Query = ' + q;
302         this.error.sdump('D_PATRON', msg);
303     },
304
305     'submit' : function() {
306         window.xulG.clear_left_deck();
307         var obj = this;
308         var query = {};
309         for (var i = 0; i < obj.controller.render_list.length; i++) {
310             var id = obj.controller.render_list[i][0];
311             var node = document.getElementById(id);
312             if (node && node.value != '') {
313                 if (id == 'inactive') {
314                     query[id] = node.getAttribute('value');
315                     obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + node.getAttribute('value') + '\n');
316                 } else {
317                     if (id == 'search_depth') {
318                         query[id] = node.firstChild.getAttribute('value'); 
319                     } else {
320                          var value = node.value.replace(/^\s+/,'').replace(/[\\\s]+$/,'');
321                         //value = value.replace(/\d/g,'');
322                         switch(id) {
323                             case 'family_name' :
324                             case 'first_given_name' :
325                             case 'second_given_name' :
326                                 value = value.replace(/^[\d\s]+/g,'').replace(/[\d\s]+$/g,'')
327                             break;
328                         }
329                         if (value != '') {
330                             query[id] = value;
331                             obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + value + '\n');
332                         }
333                     }
334                 }
335             }
336         }
337         if (typeof obj.on_submit == 'function') {
338             obj.on_submit(query,obj.search_limit,obj.search_sort);
339         }
340         if (typeof window.xulG == 'object' 
341             && typeof window.xulG.on_submit == 'function') {
342             obj.error.sdump('D_PATRON','patron.search_form: Calling external .on_submit()\n');
343             window.xulG.on_submit(query,obj.search_limit,obj.search_sort);
344         } else {
345             obj.error.sdump('D_PATRON','patron.search_form: No external .on_query()\n');
346         }
347     },
348
349 }
350
351 dump('exiting patron/search_form.js\n');