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