moved some JS to an external lib file for ease of sharing
[Evergreen.git] / Open-ILS / xul / staff_client / server / admin / stat_cat_editor.js
1 var SC_FETCH_ALL                = 'open-ils.circ:open-ils.circ.stat_cat.TYPE.retrieve.all';
2 var SC_CREATE                   = 'open-ils.circ:open-ils.circ.stat_cat.TYPE.create';
3 var SC_UPDATE                   = 'open-ils.circ:open-ils.circ.stat_cat.TYPE.update';
4 var SC_DELETE                   = 'open-ils.circ:open-ils.circ.stat_cat.TYPE.delete';
5 var SC_ENTRY_CREATE     = 'open-ils.circ:open-ils.circ.stat_cat.TYPE.entry.create';
6 var SC_ENTRY_UPDATE     = 'open-ils.circ:open-ils.circ.stat_cat.TYPE.entry.update';
7 var SC_ENTRY_DELETE     = 'open-ils.circ:open-ils.circ.stat_cat.TYPE.entry.delete';
8
9 var ACTOR                               = 'actor';
10 var ASSET                               = 'asset';
11 var session                             = null;
12 var user                                        = null;
13
14 var scCache                             = {};
15 var PERMS                               = {};
16 PERMS[ACTOR]                    = {};
17 PERMS[ASSET]                    = {};
18
19 var currentlyVisible;
20 var opacVisible         = false;
21 var cgi;
22
23
24 function scEditorInit() {
25         cgi = new CGI();
26         session = cgi.param('ses');
27         if(!session) throw "User session is not defined";
28         user = fetchUser(session);
29         setTimeout( function() { scFetchPerms(); scGo(); }, 20 );
30 }
31
32 function scGo() {
33
34         var show = cgi.param('show');
35         if(!show) show = ASSET;
36         scShow(show);
37         scBuildNew();
38         $('sc_user').appendChild(text(user.usrname()));
39 }
40
41 function scFetchPerms() {
42
43         var orgs = fetchHighestPermOrgs( session, user.id(), 
44                 [       'CREATE_PATRON_STAT_CAT',
45                         'UPDATE_PATRON_STAT_CAT',
46                         'DELETE_PATRON_STAT_CAT',
47                         'CREATE_PATRON_STAT_CAT_ENTRY',
48                         'UPDATE_PATRON_STAT_CAT_ENTRY',
49                         'DELETE_PATRON_STAT_CAT_ENTRY',
50         
51                         'CREATE_COPY_STAT_CAT',
52                         'UPDATE_COPY_STAT_CAT',
53                         'DELETE_COPY_STAT_CAT',
54                         'CREATE_COPY_STAT_CAT_ENTRY',
55                         'UPDATE_COPY_STAT_CAT_ENTRY',
56                         'DELETE_COPY_STAT_CAT_ENTRY' ] );
57
58         PERMS[ACTOR].create_stat_cat = orgs[0];
59         PERMS[ACTOR].update_stat_cat = orgs[1];
60         PERMS[ACTOR].delete_stat_cat = orgs[2];
61         PERMS[ACTOR].create_stat_cat_entry = orgs[3];
62         PERMS[ACTOR].update_stat_cat_entry = orgs[4];
63         PERMS[ACTOR].delete_stat_cat_entry = orgs[5];
64
65         PERMS[ASSET].create_stat_cat = orgs[6];
66         PERMS[ASSET].update_stat_cat = orgs[7];
67         PERMS[ASSET].delete_stat_cat = orgs[8];
68         PERMS[ASSET].create_stat_cat_entry =  orgs[9];
69         PERMS[ASSET].update_stat_cat_entry =  orgs[10];
70         PERMS[ASSET].delete_stat_cat_entry =  orgs[11];
71 }
72
73 function scFetchPerm(perm) {
74         var req = new RemoteRequest(
75                 'open-ils.actor',
76                 'open-ils.actor.user.perm.highest_org', session, user.id(), perm );
77         req.send(true);
78         return req.getResultObject();
79         PERMS.create_stat = req.getResultObjecdt();
80 }
81
82
83 function _cleanTbody(tbody) {
84         for( var c  = 0; c < tbody.childNodes.length; c++ ) {
85                 var child = tbody.childNodes[c];
86                 if(child && child.getAttribute('edit')) tbody.removeChild(child); 
87         }
88 }
89
90
91 function scFetchAll( session, type, orgid, callback, args ) {
92         var req = new Request( 
93                 SC_FETCH_ALL.replace(/TYPE/, type) , session, orgid );
94         req.send(true);
95         return req.result();
96 }
97
98 function scShow(type) { 
99
100         currentlyVisible = type;
101
102         if( type == ASSET ) {
103                 addCSSClass($('sc_show_copy'), 'has_color');
104                 removeCSSClass($('sc_show_actor'), 'has_color');
105
106         } else if( type == ACTOR ) {
107                 addCSSClass($('sc_show_actor'), 'has_color');
108                 removeCSSClass($('sc_show_copy'), 'has_color');
109         }
110
111         scCache[type] = scFetchAll( session, type, user.home_ou() );  
112         scDraw( type, scCache[type] );
113 }
114
115 var scRow; var scCounter;
116 function scDraw( type, cats ) {
117
118         hideMe($('loading'));
119         if(!cats || cats.length == 0) return unHideMe($('sc_none'));
120         var tbody = $('sc_tbody');
121
122         if(!scRow) scRow = tbody.removeChild($('sc_tr'));
123         removeChildren(tbody);
124         unHideMe($('sc_table'));
125
126         scCounter = 0;
127         for( var c in cats ) scInsertCat( tbody, cats[c], type );
128 }
129
130
131 var scEntryCounter;
132 function scInsertCat( tbody, cat, type ) {
133
134         var row = scRow.cloneNode(true);
135         row.id = 'sc_tr_' + cat.id();
136         var name_td = $n(row, 'sc_name');
137         name_td.appendChild( text(cat.name()) );
138         if(scCounter++ % 2) addCSSClass(row, 'has_color');
139
140         $n(row, 'sc_new_entry').onclick = function() { scNewEntry(type, cat, tbody); }
141         $n(row, 'sc_edit').onclick = function(){ scEdit(tbody, type, cat); };
142         $n(row, 'sc_owning_lib').appendChild( text( findOrgUnit(cat.owner()).name() ));
143
144         if( cat.opac_visible() ) unHideMe($n(row, 'sc_opac_visible'));
145         else unHideMe($n(row, 'sc_opac_invisible'));
146
147         tbody.appendChild(row);
148         scEntryCounter = 0;
149
150         cat.entries().sort(  /* sort the entries by value */
151                 function( a, b ) { 
152                         if( a.value() > b.value()) return 1;
153                         if( a.value() < b.value()) return -1;
154                         return 0;
155                 }
156         );
157
158         for( var e in cat.entries() ) 
159                 scInsertEntry( cat, cat.entries()[e], $n(row, 'sc_entries_selector'), tbody, type );
160 }
161
162
163 function scInsertEntry( cat, entry, selector, tbody, type ) {
164         setSelectorVal( selector, scEntryCounter++, entry.value(), entry.id(), 
165                         function(){ scUpdateEntry( cat, entry, tbody, type );} );
166 }
167
168
169
170 function scDelete(type, id) {
171         if(!confirm($('sc_delete_confirm').innerHTML)) return;
172         var req = new Request( SC_DELETE.replace(/TYPE/,type), session, id );
173         req.send(true);
174         var res = req.result();
175         if(checkILSEvent(res)) throw res;
176         scShow(type);
177 }
178
179 function scCreateEntry( type, id, row ) {
180         var value = $n(row, 'sc_new_entry_name').value;
181         if(!value) return;
182         var entry;
183         if( type == ACTOR ) entry = new actsce();
184         if( type == ASSET ) entry = new asce();
185
186         entry.isnew(1);
187         entry.stat_cat(id);
188         entry.owner(user.home_ou());
189         entry.value(value);
190
191         var req = new Request( SC_ENTRY_CREATE.replace(/TYPE/, type), session, entry );
192         req.send(true);
193         var res = req.result();
194         if(checkILSEvent(res)) throw res;
195         scShow(type);
196 }
197
198 function scNewEntry( type, cat, tbody ) {
199         _cleanTbody(tbody);
200         var row = $('sc_new_entry_row').cloneNode(true);
201         row.setAttribute('edit', '1');
202
203         var r = $('sc_tr_' + cat.id());
204         if(r.nextSibling) tbody.insertBefore( row, r.nextSibling );
205         else{ tbody.appendChild(row); }
206
207         $n(row, 'sc_new_entry_create').onclick = 
208                 function() {
209                         if( scCreateEntry( type, cat.id(), row ) )
210                                 tbody.removeChild(row); };
211         $n(row, 'sc_new_entry_cancel').onclick = function(){tbody.removeChild(row);}
212
213         var c_org = PERMS[type].create_stat_cat_entry;
214         var max_c_depth = (c_org != null) ? findOrgDepth(c_org) : -1;
215         
216         if( max_c_depth == -1 ) {
217                 $n(row, 'sc_new_entry_create').disabled = true;
218                 $n(row, 'sc_new_entry_lib').disabled = true;
219                 return;
220         }
221
222         var org = findOrgUnit(cat.owner());
223         var depth = findOrgDepth(org);
224
225         if( depth < max_c_depth ) {
226                 depth = max_c_depth;
227                 org = findOrgUnit(c_org);
228         }
229         
230         _scBuildOrgSelector( $n(row, 'sc_new_entry_lib'), org, depth );
231         $n(row, 'sc_new_entry_name').focus();
232 }
233
234
235 function scBuildNew() {
236
237         var c_org = PERMS[ASSET].create_stat_cat;
238         var max_c_depth = (c_org != null) ? findOrgDepth(c_org) : -1;
239
240         var ac_org = PERMS[ACTOR].create_stat_cat;
241         var max_ac_depth = (ac_org != null) ? findOrgDepth(ac_org) : -1;
242
243         var depth = max_c_depth;
244         var org = c_org;
245
246         var selector = $('sc_owning_lib_selector');
247
248         if( depth == -1 ) {
249                 depth = max_ac_depth;
250                 org = ac_org;
251                 if( depth == -1 ) {
252                         $('sc_new').disabled = true;
253                         $('sc_type_selector').disabled = true;
254                         selector.disabled = true;
255                         return;
256                 }
257         }
258
259         org = findOrgUnit( org );
260         _scBuildOrgSelector( selector, org, depth );
261 }
262
263 function _scBuildOrgSelector(selector, org, offset) {
264         insertSelectorVal( selector, -1, 
265                 org.name(), org.id(), null, findOrgDepth(org) - offset );
266         for( var c in org.children() )
267                 _scBuildOrgSelector( selector, org.children()[c], offset);
268 }
269
270 function scNew() {
271
272         var name = $('sc_new_name').value;
273         var type = getSelectorVal($('sc_type_selector'));
274
275         var visible = 0;
276         if( $('sc_make_opac_visible').checked) visible = 1;
277
278         var cat;
279         if( type == ACTOR ) cat = new actsc();
280         if( type == ASSET ) cat = new asc();
281
282         cat.opac_visible(visible);
283         cat.name(name);
284         cat.owner(getSelectorVal($('sc_owning_lib_selector')));
285         cat.isnew(1);
286
287         var req = new Request( SC_CREATE.replace(/TYPE/, type), session, cat );
288
289         req.send(true);
290         var res = req.result();
291         if(checkILSEvent(res)) throw res;
292
293         scShow(type);
294 }
295
296 function scEdit( tbody, type, cat ) {
297
298         _cleanTbody(tbody);
299         var row = $('sc_edit_row').cloneNode(true);
300         row.setAttribute('edit', '1');
301
302         var r = $('sc_tr_' + cat.id());
303         if(r.nextSibling) { tbody.insertBefore( row, r.nextSibling ); }
304         else{ tbody.appendChild(row); }
305
306         $n(row, 'sc_edit_name').value = cat.name();
307
308         var name = $n(row, 'sc_edit_cancel');
309         name.onclick = function() { tbody.removeChild(row); };
310
311         var show = $n(row, 'sc_edit_show_owning_lib');
312         
313         var myorg = findOrgUnit(user.home_ou());
314         var ownerorg = findOrgUnit(cat.owner());
315         show.appendChild(text(ownerorg.name()));
316
317         var selector = null;
318         if( myorg.children() && myorg.children().length > 0 ) {
319                 selector = $n(row, 'sc_edit_owning_lib');
320                 _scBuildOrgSelector( selector, myorg, findOrgDepth(myorg) );
321                 setSelector( selector, cat.owner() );
322                 unHideMe(selector);
323
324         } else { unHideMe(show); }
325
326         name.focus();
327         name.select();
328
329         if( cat.opac_visible() ) {
330                 $n( $n(row, 'sc_edit_opac_vis'), 
331                         'sc_edit_opac_visibility').checked = true;
332         }
333         else 
334                 $n( $n(row, 'sc_edit_opac_invis'), 
335                         'sc_edit_opac_visibility').checked = true;
336
337         $n(row, 'sc_edit_submit').onclick = 
338                 function() { scEditGo( type, cat, row, selector ); };
339
340         $n(row, 'sc_edit_delete').onclick = 
341                 function(){ scDelete(type, cat.id()); };
342
343         var o_depth = findOrgDepth(findOrgUnit(cat.owner()));
344         /*var m_depth = findOrgDepth(findOrgUnit(user.home_ou()));*/
345         var e_org = PERMS[type].update_stat_cat;
346         var d_org = PERMS[type].delete_stat_cat;
347         var max_e_depth = (e_org != null) ? findOrgDepth(e_org) : -1;
348         var max_d_depth = (d_org != null) ? findOrgDepth(d_org) : -1;
349
350         if( max_e_depth == -1 || o_depth < max_e_depth )
351                 $n(row,'sc_edit_submit').disabled = true;
352
353         if( max_d_depth == -1 || o_depth < max_d_depth )
354                 $n(row,'sc_edit_delete').disabled = true;
355 }
356
357 function scEditGo( type, cat, row, selector ) {
358         var name = $n(row, 'sc_edit_name').value;
359         var visible = 
360                 $n( $n(row, 'sc_edit_opac_vis'), 'sc_edit_opac_visibility').checked;
361
362         var newlib = cat.owner();
363         if(selector) newlib = getSelectorVal( selector );
364
365         if(!name) return false;
366
367         var isvisible = false;
368         if( cat.opac_visible() ) isvisible = true;
369
370         if( (name == cat.name()) && (visible == isvisible) 
371                 && (newlib == cat.owner()) ) { return true; }
372
373         cat.name( name );
374         cat.owner( newlib );
375         cat.entries(null);
376         cat.opac_visible(0);
377         if( visible ) cat.opac_visible(1);
378
379         var req = new Request( SC_UPDATE.replace(/TYPE/,type), session, cat );
380         req.send(true);
381         var res = req.result();
382         if(checkILSEvent(res)) throw res;
383         scShow(type);
384
385         return true;
386 }
387
388 function scUpdateEntry( cat, entry, tbody, type ) {
389         _cleanTbody(tbody);
390         var row = $('sc_edit_entry_row').cloneNode(true);
391         row.setAttribute('edit', '1');
392
393         var r = $('sc_tr_' + cat.id());
394         if(r.nextSibling) tbody.insertBefore( row, r.nextSibling );
395         else{ tbody.appendChild(row); }
396
397         $n(row, 'sc_edit_entry_owner').appendChild(text(findOrgUnit(entry.owner()).name()));
398
399         var name = $n(row, 'sc_edit_entry_name');
400         name.value = entry.value();
401         name.focus();
402         name.select();
403
404         $n(row,'sc_edit_entry_name_submit').onclick = 
405                 function(){
406                         if( scEditEntry(cat, entry, name.value, type ) )
407                                 tbody.removeChild(row);
408                         };
409
410         $n(row,'sc_edit_entry_cancel').onclick = function(){tbody.removeChild(row);};
411         $n(row,'sc_edit_entry_delete').onclick = 
412                 function(){ scEntryDelete( cat, entry, type ); }
413
414         var o_depth = findOrgDepth( findOrgUnit(entry.owner()) );
415         /*var m_depth = findOrgDepth(findOrgUnit(user.home_ou()));*/
416
417         var e_org = PERMS[type].update_stat_cat_entry;
418         var d_org = PERMS[type].delete_stat_cat_entry;
419         var max_e_depth = (e_org != null) ? findOrgDepth(e_org) : -1;
420         var max_d_depth = (d_org != null) ? findOrgDepth(d_org) : -1;
421
422         if( max_e_depth == -1 || o_depth < max_e_depth )
423                 $n(row,'sc_edit_entry_name_submit').disabled = true;
424
425         if( max_d_depth == -1 || o_depth < max_d_depth )
426                 $n(row,'sc_edit_entry_delete').disabled = true;
427 }
428
429 function scEntryDelete( cat, entry, type ) {
430         if(!confirm($('sc_entry_delete_confirm').innerHTML)) return;
431         var req = new Request( SC_ENTRY_DELETE.replace(/TYPE/,type), session, entry.id() );
432         req.send(true);
433         var res = req.result();
434         if(checkILSEvent(res)) throw res;
435         scShow(type);
436 }
437
438 function scEditEntry( cat, entry, newvalue, type ) {
439         if(entry.value() == newvalue) return;
440         entry.value( newvalue );
441         var req = new Request( 
442                 SC_ENTRY_UPDATE.replace(/TYPE/, type), session, entry );
443         req.send(true);
444         var res = req.result();
445         if(checkILSEvent(res)) throw res;
446         scShow(type);
447 }
448