]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/ue.js
the focus was firing before the interface was finished building
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / ue.js
1 var cgi                                 = null;
2 var advanced                    = false;
3 var SC_FETCH_ALL                = 'open-ils.circ:open-ils.circ.stat_cat.actor.retrieve.all';
4 var SC_CREATE_MAP               = 'open-ils.circ:open-ils.circ.stat_cat.actor.user_map.create';
5 var SV_FETCH_ALL                = 'open-ils.circ:open-ils.circ.survey.retrieve.all';
6 var FETCH_ID_TYPES      = 'open-ils.actor:open-ils.actor.user.ident_types.retrieve';
7 var FETCH_GROUPS                = 'open-ils.actor:open-ils.actor.groups.tree.retrieve';
8 var UPDATE_PATRON               = 'open-ils.actor:open-ils.actor.patron.update';
9 var defaultState                = 'GA';
10 var counter                             = 0;
11 var dataFields;
12 var patron;
13 var identTypesCache                     = {};
14 var statCatsCache                               = {};
15 var surveysCache                                = {};
16 var surveyQuestionsCache        = {};
17 var surveyAnswersCache          = {};
18 var groupsCache                         = {};
19
20
21 /* fetch the necessary data to start off */
22 function uEditInit() {
23
24         cgi             = new CGI();
25         session = cgi.param('ses');
26         if(cgi.param('adv')) advanced = true 
27         if(!session) throw "User session is not defined";
28
29
30         fetchUser(session);
31         $('uedit_user').appendChild(text(USER.usrname()));
32
33         setTimeout( function() { uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
34 }
35
36 /* ------------------------------------------------------------------------------ */
37 /* Fetch code
38 /* ------------------------------------------------------------------------------ */
39 function uEditFetchIdentTypes() {
40         var req = new Request(FETCH_ID_TYPES);
41         req.send(true);
42         return req.result();
43 }
44
45 function uEditFetchStatCats() {
46         var req = new Request(SC_FETCH_ALL, SESSION);
47         req.send(true);
48         return req.result();
49 }
50
51 function uEditFetchSurveys() {
52         var req = new Request(SV_FETCH_ALL, SESSION);
53         req.send(true);
54         return req.result();
55 }
56
57 function uEditFetchGroups() {
58         var req = new Request(FETCH_GROUPS);
59         req.send(true);
60         return req.result();
61 }
62 /* ------------------------------------------------------------------------------ */
63
64
65
66 /* fetches necessary and builds the UI */
67 function uEditBuild() {
68         //fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
69
70         uEditBuildLibSelector();
71         patron = fetchFleshedUser(cgi.param('usr'));
72         if(!patron) patron = uEditNewPatron(); 
73
74
75         uEditDraw( 
76                 uEditFetchIdentTypes(),
77                 uEditFetchGroups(),
78                 uEditFetchStatCats(),
79                 uEditFetchSurveys() );
80
81         if(patron.isnew()) uEditCreateNewAddr();
82 }
83
84
85 /* creates a new patron object with card attached */
86 function uEditNewPatron() {
87         var patron = new au(); 
88         patron.isnew(1);
89         patron.id(-1);
90         card = new ac();
91         card.id(-1);
92         card.isnew(1);
93         patron.card(card);
94         patron.cards([card]);
95         patron.stat_cat_entries([]);
96         patron.survey_responses([]);
97         patron.addresses([]);
98         patron.home_ou(USER.ws_ou());
99         var rand  = Math.random();
100         rand = parseInt( rand * 10000 );
101         patron.passwd(rand);
102         $('ue_password_plain').appendChild(text(rand));
103         return patron;
104 }
105
106 /* Creates a new blank address, adds it to the user and the fields array */
107 var uEditVirtualAddrId = -1;
108 function uEditCreateNewAddr() {
109         var addr = new aua();
110         addr.id(uEditVirtualAddrId--);
111         addr.isnew(1);
112         addr.usr(patron.id());
113         addr.state(defaultState);
114         addr.country(defaultCountry);
115         if(patron.addresses().length == 0) {
116                 patron.mailing_address(addr);
117                 patron.billing_address(addr);
118         }
119         uEditBuildAddrFields(patron, addr);
120         patron.addresses().push(addr);
121         uEditIterateFields(function(f) { uEditCheckValid(f); });
122         uEditCheckErrors();
123 }
124
125
126 /* kicks off the UI drawing */
127 function uEditDraw(identTypes, groups, statCats, surveys ) {
128         hideMe($('uedit_loading'));
129         unHideMe($('ue_maintd'));
130
131         dataFields = [];
132         uEditDrawIDTypes(identTypes);
133         uEditDrawGroups(groups);
134         uEditDrawStatCats(statCats);
135         uEditDrawSurveys(surveys);
136         uEditDefineData(patron);
137
138         uEditIterateFields(function(f) { uEditActivateField(f) });
139         uEditIterateFields(function(f) { uEditCheckValid(f); });
140         uEditCheckErrors();
141 }
142
143
144 /** Applies the event handlers and sets the data for the field */
145 function uEditActivateField(field) {
146
147         if( field.widget.id ) {
148                 field.widget.node = $(field.widget.id);
149
150         } else {
151                 field.widget.node = 
152                         $n(field.widget.base, field.widget.name);
153         }
154
155         uEditSetOnchange(field);
156
157         if(field.widget.onblur) {
158                 field.widget.node.onblur = 
159                         function() { field.widget.onblur(field); };
160         }
161
162         var val = field.object[field.key]();
163         if(val == null) return;
164
165         if( field.widget.type == 'input' )
166                 field.widget.node.value = val;
167
168         if( field.widget.type == 'select' )
169                 setSelector(field.widget.node, val);
170
171         if( field.widget.type == 'checkbox' )
172                 field.widget.node.checked = 
173                         (val && val != 'f') ? true : false;
174
175         if( field.widget.onload ) 
176                 field.widget.onload(val);
177
178         /*
179         alert(field.key);
180         if(field.key == 'ident_value') alert(field.widget.onblur);
181         */
182
183 }
184
185
186 /* set up the onchange event for the field */
187 function uEditSetOnchange(field) {
188         var func = function() {uEditOnChange( field );}
189         field.widget.node.onchange = func;
190
191         if(field.widget.type != 'select')
192                 field.widget.node.onkeyup = func;
193 }
194
195 /* find the current value of the field object's widget */
196 function uEditNodeVal(field) {
197         if(field.widget.type == 'input')
198                 return field.widget.node.value;
199
200         if(field.widget.type == 'checkbox')
201                 return field.widget.node.checked;
202
203         if(field.widget.type == 'select')
204                 return getSelectorVal(field.widget.node);
205 }
206
207
208 /* update a field value */
209 function uEditOnChange(field) {
210
211         var newval = uEditNodeVal(field);
212         field.object[field.key](newval);
213         field.object.ischanged(1);
214
215         if(field.widget.onpostchange)
216                 field.widget.onpostchange(field, newval);
217
218
219         uEditIterateFields(function(f) { uEditCheckValid(f); });
220         uEditCheckErrors();
221 }
222
223
224 function uEditCheckValid(field) {
225         var newval = uEditNodeVal(field);
226         if(newval) {
227
228                 if(field.widget.regex) { 
229                         if(newval.match(field.widget.regex)) 
230                                 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
231                         else
232                                 addCSSClass(field.widget.node, CSS_INVALID_DATA);
233
234                 } else {
235                         removeCSSClass(field.widget.node, CSS_INVALID_DATA);
236                 }
237
238         } else {
239
240                 if(field.required) {
241                         addCSSClass(field.widget.node, CSS_INVALID_DATA);
242
243                 } else {
244                         removeCSSClass(field.widget.node, CSS_INVALID_DATA);
245                 }
246         }
247 }
248
249 /* find a field object by object key */
250 function uEditFindFieldByKey(key) {
251         var fields = grep( dataFields,
252                 function(item) { return (item.key == key); });
253         return (fields) ? fields[0] : null;
254 }
255
256 /* find a list of fields by object key */
257 function uEditFindFieldsByKey(key) {
258         return grep( dataFields,
259                 function(item) { return (item.key == key); });
260 }
261
262 /* find a field object by widget id */
263 function uEditFindFieldByWId(id) {
264         var fields = grep( dataFields,
265                 function(item) { return (item.widget.id == id); });
266         return (fields) ? fields[0] : null;
267 }
268
269
270 function uEditIterateFields(callback) {
271         for( var f in dataFields ) {
272                 callback(dataFields[f]);
273         }
274 }
275
276
277 function uEditGetErrorStrings() {
278         var errors = [];
279         uEditIterateFields(
280                 function(field) { 
281                         if(field.errkey) {
282                                 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
283                                         var str = $(field.errkey).innerHTML;
284                                         if(str) errors.push(str);
285                                 }
286                         }
287                 }
288         );
289
290         if(errors[0]) return errors;
291         return null;
292 }
293
294 function uEditAlertErrors() {
295         var errors = uEditGetErrorStrings();
296         if(!errors) return false;
297         alert(errors.join("\n"));
298         return true;
299 }
300
301
302 /* send the user to the database */
303 function uEditSaveUser() {
304
305         if(uEditGetErrorStrings()) {
306                 uEditAlertErrors();
307                 return;
308         }
309
310         var req = new Request(UPDATE_PATRON, SESSION, patron);
311         req.send(true);
312         var result = req.result();
313
314         if( checkILSEvent(result) ) 
315                 alert(js2JSON(result));
316         else 
317                 alert($('ue_success').innerHTML);
318
319         if (window.xulG && typeof window.xulG.save == 'function') {
320                 window.xulG.on_save(patron); 
321         } else {
322                 location.href = location.href;
323         }
324 }
325
326
327 var uEditDupHashes = {};
328 var uEditDupTemplate;
329
330 function uEditRunDupeSearch(type, search_hash) {
331
332         if(!patron.isnew()) return;
333
334         _debug('dup search: ' + js2JSON(search_hash));
335
336         var req = new Request(PATRON_SEARCH, SESSION, search_hash);
337
338         var container = $('dup_div_container');
339         if(!uEditDupTemplate)
340                 uEditDupTemplate = container.removeChild($('dup_div'));
341
342         /* clear any existing dups for this type */
343         iterate( container.getElementsByTagName('div'),
344                 function(d) {
345                         if( d.getAttribute('type') == type ) {
346                                 container.removeChild(d)
347                                 return;
348                         }
349                 }
350         );
351
352         /*req.callback(uEditHandleDupResults);*/
353         req.callback(
354                 function(r) {
355                         uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
356                 }
357         );
358         req.send();
359 }
360
361
362 function uEditHandleDupResults(ids, search_hash, type, container) {
363
364         _debug('dup search results: ' + js2JSON(ids));
365
366         if(!(ids && ids[0]))  /* no results */
367                 return uEditDupHashes[type] = null;
368
369         /* add a dup link to the UI and plug in the data */
370         var node = uEditDupTemplate.cloneNode(true);
371         container.appendChild(node);
372         node.setAttribute('type', type);
373
374         var link = $n(node, 'link');
375         link.setAttribute('type', type);
376         unHideMe(link);
377         $n(node,'count').appendChild(text(ids.length));
378
379         for( var o in search_hash ) 
380                 $n(node, 'data').appendChild(
381                         text(search_hash[o].value + ' '));
382
383         uEditDupHashes[type] = search_hash;
384
385         switch(type) {
386                 case 'ident1' :
387                         if(confirm($('ue_dup_ident1').innerHTML)) 
388                                 uEditShowSearch(type);
389                         break;
390         }
391 }
392
393
394 function uEditShowSearch(link) {
395         var type = link.getAttribute('type');
396         if(window.xulG)
397                 window.xulG.spawn_search(uEditDupHashes[type]); 
398         else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
399 }
400