added evergreen branch of user editor
[Evergreen.git] / Evergreen / xul / staff_client / server / patron / ue_ui.js
1 /* -----------------------------------------------------------------------
2         UI code for the user editor.  Handles breaking up the components
3         into a wizard-like interface.
4         ----------------------------------------------------------------------- */
5
6
7 var pages = [ 
8         'uedit_userid', 
9         'uedit_contact_info', 
10         'uedit_addresses', 
11         'uedit_groups', 
12         'uedit_stat_cats', 
13         'uedit_surveys',
14         'uedit_finalize',
15         ];
16
17 /* ID's of objects that should be focused when their page is made visible */
18 var pageFocus = [
19         'ue_barcode',
20         'ue_email',
21         'ue_addr_label_1',
22         'ue_profile',
23         'ue_stat_cat_selector_1',
24         'ue_survey_selector_1',
25         'ue_view_summary'
26 ];
27
28
29 function uEditNext() {
30         var i = _findActive();
31         if( i < (pages.length - 1)) uEditShowPage(pages[i+1]);
32 }
33
34
35 function uEditPrev() {
36         var i = _findActive();
37         if( i > 0 ) uEditShowPage(pages[i-1]);
38 }
39
40 function uEditCheckErrors() {
41         var errors = uEditGetErrorStrings();
42         if(errors) unHideMe($('ue_errors'));
43         else hideMe($('ue_errors'));
44 }
45
46 /*
47 function uEditFetchError(id) { if($(id)) return $(id).innerHTML + "\n"; return "";}
48 */
49
50 function uEditShowPage(id) {
51         if( id == null ) return;
52
53         for( var p in pages ) {
54                 var page = pages[p];
55                 hideMe($(page));
56                 removeCSSClass($(page+'_label'), 'label_active');
57         }
58
59         var idx = _findPageIdx(id);
60
61         unHideMe($(id));
62         addCSSClass($(id+'_label'), 'label_active');
63         var fpage = pageFocus[idx];
64
65         var pnode = $(fpage);
66         if( id == 'uedit_addresses' ) 
67                 pnode = $n( $('ue_address_tbody').
68                         getElementsByTagName('tr')[0], 'ue_addr_label');
69
70         if(pnode) { 
71                 pnode.focus(); 
72                 try{pnode.select()}catch(e){} 
73         }
74
75         unHideMe($('ue_back'));
76         unHideMe($('ue_fwd'));
77
78         if(idx == 0) hideMe($('ue_back'));
79         if(idx == (pages.length-1)) hideMe($('ue_fwd'));
80 }
81
82 function _findActive() {
83         for( var p in pages ) {
84                 if(! $(pages[p]).className.match(/hide_me/) )
85                         return parseInt(p);
86         }
87         return null;
88 }
89
90 function _findPageIdx(name) {
91         for( var i in pages ) {
92                 var page = pages[i];
93                 if( page == name ) return i;
94         }
95         return -1;
96 }
97
98 /* ------------------------------------------------------------------------------ */
99 /* Below are the various UI components built from retrieved data */
100 /* ------------------------------------------------------------------------------ */
101
102
103 /* org selector */
104 function uEditBuildLibSelector( node, depth, selector ) {
105         if(!selector) selector = $('ue_org_selector');
106         if(!node) { depth = 0; node = globalOrgTree; }
107         var opt = insertSelectorVal( 
108                 selector, -1, node.name(), node.id(), null, depth++ );
109         if(!findOrgType(node.ou_type()).can_have_users()) opt.disabled = true; 
110         if( node.id() == USER.ws_ou() ) 
111                 setSelector(selector, node.id());
112
113         for( var c in node.children() ) 
114                 uEditBuildLibSelector(node.children()[c], depth, selector);
115 }
116
117
118 /* group tree selector */
119 function uEditDrawGroups(tree, depth, selector) {
120         if(!selector) {
121                 selector = $('ue_profile');
122                 depth = 0;
123         }
124         groupsCache[tree.id()] = tree;
125
126         /* if the staff does not have perms to access this group, 
127                 remove it from the tree and don't add it's children */
128         var perm = uEditFindGroupPerm(tree);
129         var org = PERMS[perm];
130         if( org == -1 ) return;
131
132         var opt = insertSelectorVal( selector, -1, tree.name(), tree.id(), null, depth++ );     
133         if(!isTrue(tree.usergroup())) opt.disabled = true;
134
135         for( var c in tree.children() ) 
136                 uEditDrawGroups( tree.children()[c], depth, selector );
137 }
138
139
140 function uEditFindGroupPerm(group) {
141         if(!group) return null;
142         if( group.application_perm() ) 
143                 return group.application_perm();
144         return uEditFindGroupPerm(groupsCache[group.parent()]);
145 }
146
147
148 /* user identification types */
149 function uEditDrawIDTypes(types) {
150         var pri_sel = $('ue_primary_ident_type');
151         //var sec_sel = $('ue_secondary_ident_type');
152         var idx = 1;
153         for( var t in types ) {
154                 var type = types[t];
155                 if(!type.name()) continue;
156                 identTypesCache[type.id()] = type;
157                 setSelectorVal( pri_sel, idx, type.name(), type.id() );
158                 //setSelectorVal( sec_sel, idx++, type.name(), type.id() );
159                 idx++;
160         }
161 }
162
163 /* user statistical catagories */
164 function uEditDrawStatCats(cats) {
165         var tbody = $('ue_stat_cat_tbody');
166         var templ = tbody.removeChild($('ue_stat_cat_row'));
167
168         for( var c in cats ) {
169                 var row = templ.cloneNode(true);
170                 uEditInsertCat( row, cats[c], c );
171                 tbody.appendChild(row);
172         }
173 }
174
175
176 function uEditInsertCat( row, cat, idx ) {
177
178         cat.entries().sort(  /* sort the entries by value */
179                 function( a, b ) { 
180                         if( !a.value() ) return -1;
181                         if( !b.value() ) return 1;
182                         if( (a.value()+'').toLowerCase() > (b.value()+'').toLowerCase()) return 1;
183                         if( (a.value()+'').toLowerCase() < (b.value()+'').toLowerCase()) return -1;
184                         return 0;
185                 }
186         );
187
188         statCatsCache[cat.id()] = cat;
189
190         /* register the new map object */
191         uEditBuildSCMField(cat, row);
192
193         var newval = $n(row, 'ue_stat_cat_newval');
194         var selector = $n(row, 'ue_stat_cat_selector');
195
196         selector.onchange = function() { 
197                 newval.value = getSelectorVal(selector);
198                 if(newval.onchange) newval.onchange();
199         }
200
201         if( idx == 0 ) selector.id = 'ue_stat_cat_selector_1'; 
202
203         $n(row, 'ue_stat_cat_name').appendChild(text(cat.name()));
204         $n(row, 'ue_stat_cat_owner').appendChild(
205                 text(fetchOrgUnit(cat.owner()).shortname()));
206
207         for( var e in cat.entries() ) {
208                 var entry = cat.entries()[e];
209                 setSelectorVal( selector, 
210                         (parseInt(e)+1), entry.value(), entry.value() );
211         }
212 }
213
214 /* draw the surveys */
215 function uEditDrawSurveys(surveys) {
216
217         var div = $('uedit_surveys');
218         var table = div.removeChild($('ue_survey_table'));
219         if( surveys.length == 0 ) unHideMe($('uedit_no_surveys'));
220
221         for( var s in surveys ) {
222                 var survey = surveys[s];
223                 surveysCache[survey.id()] = survey;
224                 var clone = table.cloneNode(true);
225                 uEditInsertSurvey( div, clone, survey, s );
226                 div.appendChild(clone);
227         }
228 }
229
230 /* insert the servey then insert each of that surveys questions */
231 function uEditInsertSurvey( div, table, survey, sidx ) {
232
233         $n(table, 'ue_survey_name').appendChild(text(survey.name()));
234         $n(table, 'ue_survey_desc').appendChild(text(survey.description()));
235
236         var tbody = $n(table, 'ue_survey_tbody');
237         var templ = tbody.removeChild($n(table, 'ue_survey_row'));
238
239         for( var q in survey.questions() ) {
240                 var row = templ.cloneNode(true);
241                 var quest = survey.questions()[q];
242                 uEditInsertSurveyQuestion( row, survey, quest );
243                 tbody.appendChild(row);
244         }
245 }
246
247 function uEditInsertSurveyQuestion( row, survey, question ) {
248
249         var selector = $n(row, 'ue_survey_answer');
250         row.setAttribute('question', question.id());
251         $n(row, 'ue_survey_question').appendChild(text(question.question()));
252
253         for( var a in question.answers() ) {
254                 var answer = question.answers()[a];
255                 surveyAnswersCache[answer.id()] = answer;
256                 insertSelectorVal(selector, -1, answer.answer(), answer.id() );
257         }
258
259         surveyQuestionsCache[question.id()] = question;
260
261         if( patron.isnew() && isTrue(survey.required()) ) 
262                 addCSSClass(selector, 'invalid_value');
263
264         selector.onchange = function() {
265
266                 /* remove any existing responses for this survey */
267                 patron.survey_responses(
268                         grep( patron.survey_responses(),
269                                 function(item) {
270                                         return (item.survey() != survey.id());
271                                 }
272                         )
273                 );
274
275
276                 if(!patron.survey_responses())
277                         patron.survey_responses([]);
278
279                 var val = getSelectorVal(selector);
280
281                 if( patron.isnew() && isTrue(survey.required()) ) {
282                         if(val)
283                                 removeCSSClass(selector, 'invalid_value');
284                         else 
285                                 addCSSClass(selector, 'invalid_value');
286                         uEditCheckErrors();
287                 }
288
289                 if(!val) return;
290
291                 var resp        = new asvr();
292                 resp.isnew(1);
293                 resp.survey(survey.id());
294                 resp.usr(patron.id());
295                 resp.question(row.getAttribute('question'));
296                 resp.answer(val);
297                 patron.survey_responses().push( resp );
298         }
299 }
300
301
302
303
304
305 /* -----------------------------------------------------------------------
306         Spit out the patron info to the summary display tables...
307         ----------------------------------------------------------------------- */
308
309 function uEditShowSummary() {
310         hideMe($('main_div_container'));
311         unHideMe($('summary_div_container'));
312
313         for( var f in dataFields ) {
314
315                 var field = dataFields[f];
316                 if( field.object == patron ) {
317
318                         var val = uEditNodeVal(field);
319
320                         if(     field.key == 'profile'          ||
321                                         field.key == 'home_ou'          ||
322                                         field.key == 'ident_type'       ||
323                                         field.key == 'ident_type2') {
324
325                                 val = getSelectorName($(field.widget.id));
326                         }
327
328                         var node = $('ue_summary_'+field.key);
329                         if(node) appendClear(node, text(val));
330                 }
331
332                 if( field.object == patron.card() )
333                         appendClear($('ue_summary_barcode'), text(uEditNodeVal(field)));
334
335         }
336
337         var table = $('ue_summary_table');
338         uEditFleshSummaryAddresses( table, patron );
339         uEditFleshSummaryStatCats( table, patron );
340         uEditFleshSummarySurveys( table, patron );
341 }
342
343
344
345 var uEditSummaryAddrRow;
346 function uEditFleshSummaryAddresses( table, patron ) {
347
348         var addrtbody = $n(table, 'ue_summary_addr_tbody');
349         if(!uEditSummaryAddrRow)
350                 uEditSummaryAddrRow = 
351                         addrtbody.removeChild($n(addrtbody, 'ue_summary_addr_row'));
352         var rowtmpl = uEditSummaryAddrRow;
353         removeChildren(addrtbody);
354
355         for( var a in patron.addresses() ) {
356                 var address = patron.addresses()[a];
357                 var row = rowtmpl.cloneNode(true);
358                 uEditFleshSummaryAddr( address, patron, row );
359                 addrtbody.appendChild(row);
360                 if(address.isdeleted()) addCSSClass(row, 'deleted');
361         }
362 }
363
364
365 function uEditFleshSummaryAddr( address, patron, row ) {
366         var yes = $('yes').innerHTML;
367         var no = $('no').innerHTML;
368
369         $n(row, 'label').appendChild(text(address.address_type()));
370         $n(row, 'street1').appendChild(text(address.street1()));
371         $n(row, 'street2').appendChild(text(address.street2()));
372         $n(row, 'city').appendChild(text(address.city()));
373         $n(row, 'county').appendChild(text(address.county()));
374         $n(row, 'state').appendChild(text(address.state()));
375         $n(row, 'country').appendChild(text(address.country()));
376         $n(row, 'zip').appendChild(text(address.post_code()));
377         $n(row, 'valid').appendChild(text( (isTrue(address.valid())) ? yes : no ));
378         $n(row, 'incorporated').appendChild(text( (isTrue(address.within_city_limits())) ? yes : no ));
379
380         var mid = patron.mailing_address();
381         if( typeof patron.mailing_address() == 'object' ) 
382                 mid = patron.mailing_address().id();
383         $n(row, 'mailing').appendChild(text((mid == address.id()) ? yes : no ));
384
385         var bid = patron.billing_address();
386         if( typeof patron.billing_address() == 'object' ) 
387                 bid = patron.billing_address().id();
388         $n(row, 'billing').appendChild(text((bid == address.id()) ? yes : no ));
389 }
390
391
392
393 var uEditSummaryStatCatRow;
394 function uEditFleshSummaryStatCats( table, patron ) {
395         var tbody = $n(table, 'ue_summary_stats_tbody');
396
397         if(!uEditSummaryStatCatRow)
398                 uEditSummaryStatCatRow = 
399                         tbody.removeChild($n(tbody, 'ue_summary_stats_row'));
400         var rowtmpl = uEditSummaryStatCatRow;
401         removeChildren(tbody);
402
403         for( var s in patron.stat_cat_entries() ) {
404                 row = rowtmpl.cloneNode(true);
405                 var entry = patron.stat_cat_entries()[s];
406                 var cat = statCatsCache[entry.stat_cat()];
407                 $n(row, 'ue_summary_stat_name').appendChild(text(cat.name()));
408                 $n(row, 'ue_summary_stat_value').appendChild(text(entry.stat_cat_entry()));
409                 row.setAttribute('statcat', entry.stat_cat());
410                 if( entry.isdeleted() ) addCSSClass(row, 'deleted'); 
411                 tbody.appendChild(row);
412         }
413
414         if( ! getElementsByTagNameFlat( tbody, 'tr' )[0] )
415                 hideMe(tbody.parentNode);
416         else
417                 unHideMe(tbody.parentNode);
418 }
419
420
421
422 var uEditSummarySurveyRow;
423 function uEditFleshSummarySurveys( table, patron ) {
424
425         var tbody       = $n(table, 'ue_summary_survey_tbody');
426         if(!uEditSummarySurveyRow)
427                 uEditSummarySurveyRow = 
428                         tbody.removeChild($n(tbody, 'ue_summary_survey_row'));
429         var rowtmpl = uEditSummarySurveyRow;
430
431         removeChildren(tbody);
432
433         for( var r in patron.survey_responses() ) {
434                 var row         = rowtmpl.cloneNode(true);
435                 var resp                = patron.survey_responses()[r];
436                 var survey      = surveysCache[resp.survey()];
437                 var quest       = surveyQuestionsCache[resp.question()];
438                 var answer      = surveyAnswersCache[resp.answer()];
439                 $n(row, 'ue_summary_survey_name').appendChild(text(survey.name()));
440                 $n(row, 'ue_summary_survey_question').appendChild(text(quest.question()));
441                 $n(row, 'ue_summary_survey_answer').appendChild(text(answer.answer()));
442                 tbody.appendChild(row);
443         }
444
445         if( ! getElementsByTagNameFlat(tbody, 'tr')[0])
446                 hideMe(tbody.parentNode);
447         else
448                 unHideMe(tbody.parentNode);
449 }
450
451
452 function uEditDrawNetLevels(netLevels) {
453         var sel = $('ue_net_level');
454         iterate( netLevels, 
455                 function(i) {
456                         insertSelectorVal( sel, -1, i.name(), i.id() );
457                 }
458         );
459 }
460
461
462
463