]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/non_cat_types.js
more coming together of the admin editor code
[Evergreen.git] / Open-ILS / xul / staff_client / server / admin / non_cat_types.js
1 var FETCH_NON_CAT_TYPES = "open-ils.circ:open-ils.circ.non_cat_types.retrieve.all";
2 var CREATE_NON_CAT_TYPE = "open-ils.circ:open-ils.circ.non_cat_type.create";
3 var UPDATE_NON_CAT_TYPE = "open-ils.circ:open-ils.circ.non_cat_type.update";
4 var myPerms = [ 
5         'CREATE_NON_CAT_TYPE', 
6         'UPDATE_NON_CAT_TYPE',
7         'DELETE_NON_CAT_TYPE' ];
8
9 function ncEditorInit() {
10         fetchUser();
11         $('nc_user').appendChild(text(USER.usrname()));
12         setTimeout( function() { 
13                 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
14                 ncBuildNew();
15                 ncFetchTypes(); }, 20 );
16 }
17
18 function ncBuildNew() {
19
20         var name = $('nc_new_name');
21         name.focus();
22         setEnterFunc(name, ncCreateNew );
23
24         var org = findOrgUnit(PERMS['CREATE_NON_CAT_TYPE']);
25         var mydepth = findOrgDepth(org);
26         if( mydepth == -1 ) return;
27
28         var selector = $('nc_new_owner');
29         buildOrgSel(selector, org, mydepth );
30         if(org.children() && org.children()[0]) 
31                 selector.disabled = false;
32
33         $('nc_new_submit').disabled = false;
34         $('nc_new_submit').onclick = ncCreateNew;
35 }
36
37
38 function ncFetchTypes() {
39         var req = new Request( FETCH_NON_CAT_TYPES, USER.home_ou() );   
40         req.callback(ncDisplayTypes);
41         req.send();
42 }
43
44 function ncCreateNew() {
45         var name = $('nc_new_name').value;
46         if(!name) return;
47         var org = getSelectorVal($('nc_new_owner'));
48         var req = new Request(CREATE_NON_CAT_TYPE, SESSION, name, org );
49         req.send(true);
50         var res = req.result();
51         if(checkILSEvent(res)) throw res;
52         ncFetchTypes();
53 }
54
55
56 var rowTemplate;
57 function ncDisplayTypes(r) {
58
59         var types = r.getResultObject();
60         var tbody = $('nc_tbody');
61         if(!rowTemplate) 
62                 rowTemplate = tbody.removeChild($('nc_row_template'));
63
64         removeChildren(tbody);
65         types = types.sort( 
66                 function(a,b) {
67                         if( a.name().toLowerCase() > b.name().toLowerCase() ) return 1; 
68                         if( a.name().toLowerCase() < b.name().toLowerCase() ) return -1;        
69                         return 0;
70                 });
71
72
73         for( var idx = 0; idx != types.length; idx++ ) {
74                 var type = types[idx];
75                 var org = findOrgUnit( type.owning_lib() );
76                 var row = rowTemplate.cloneNode(true);
77                 row.id = 'nc_row_' + type.id();
78                 $n(row, 'nc_name').appendChild(text(type.name()));
79                 $n(row, 'nc_owner').appendChild( text( org.name() ));
80                 ncSetRowCallbacks( type, org, tbody, row );
81                 tbody.appendChild(row);
82         }
83 }
84
85 function ncSetRowCallbacks( type, owner, tbody, row ) {
86         var tdepth = findOrgDepth( owner );
87         var mydepth = findOrgDepth( PERMS['UPDATE_NON_CAT_TYPE'] );
88         if( mydepth != -1 && mydepth <= tdepth ) $n(row, 'nc_edit').disabled = false;
89
90         mydepth = findOrgDepth( PERMS['DELETE_NON_CAT_TYPE'] );
91         if( mydepth != -1 && mydepth <= tdepth ) $n(row, 'nc_delete').disabled = false;
92
93         $n(row, 'nc_edit').onclick = 
94                 function() { ncEditType( tbody, row, type ); };
95
96         $n(row, 'nc_delete').onclick = 
97                 function() { ncDeleteType( tbody, row, type ); };
98 }
99
100 function ncEditType( tbody, row, type ) {
101         cleanTbody(row.parentNode, 'edit');
102         var row = $('nc_edit_row_temaplate').cloneNode(true);
103
104         var name = $n(row, 'nc_edit_name');
105         name.value = type.name();
106         name.focus();
107         name.select();
108
109         $n(row, 'nc_edit_submit').onclick = function() { 
110                 var name = $n(row, 'nc_edit_name').value;
111                 ncEditSubmit( type, name );
112         };
113
114         $n(row, 'nc_edit_cancel').onclick = 
115                 function(){cleanTbody(row.parentNode, 'edit'); }
116
117         var r = $('nc_row_' + type.id());
118         if(r.nextSibling) tbody.insertBefore( row, r.nextSibling );
119         else{ tbody.appendChild(row); }
120 }
121
122 function ncEditSubmit( type, name ) {
123         if(!name) return;
124         type.name(name);
125         var req = new Request( UPDATE_NON_CAT_TYPE, SESSION, type );
126         req.send(true);
127         var res = req.result();
128         if(checkILSEvent(res)) throw res;
129         ncFetchTypes();
130 }
131
132 function ncDeleteType( tbody, row, type ) {
133         alert("Deleting type " + type.id() );
134 }
135
136