]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/non_cat_types.js
added some status popups
[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 DELETE_NON_CAT_TYPE = 'open-ils.circ:open-ils.circ.non_cataloged_type.delete';
5 var myPerms = [ 
6         'CREATE_NON_CAT_TYPE', 
7         'UPDATE_NON_CAT_TYPE',
8         'DELETE_NON_CAT_TYPE' ];
9
10 function ncEditorInit() {
11         fetchUser();
12         $('nc_user').appendChild(text(USER.usrname()));
13         setTimeout( function() { 
14                 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
15                 ncBuildNew();
16                 ncFetchTypes(); }, 20 );
17 }
18
19
20 function ncBuildNew() {
21
22         var name = $('nc_new_name');
23         name.focus();
24         setEnterFunc(name, ncCreateNew );
25
26         var org = findOrgUnit(PERMS['CREATE_NON_CAT_TYPE']);
27         var mydepth = findOrgDepth(org);
28         if( mydepth == -1 ) return;
29
30         var selector = $('nc_new_owner');
31         buildOrgSel(selector, org, mydepth );
32         if(org.children() && org.children()[0]) 
33                 selector.disabled = false;
34
35         $('nc_new_submit').disabled = false;
36         $('nc_new_submit').onclick = ncCreateNew;
37 }
38
39
40 function ncFetchTypes() {
41         var req = new Request( FETCH_NON_CAT_TYPES, USER.home_ou() );   
42         req.callback(ncDisplayTypes);
43         setTimeout(function(){req.send();}, 500);
44 }
45
46 function ncCreateNew() {
47         var name = $('nc_new_name').value;
48         if(!name) return;
49         var org = getSelectorVal($('nc_new_owner'));
50         var time = $('nc_new_interval_count').value;
51         var type = getSelectorVal($('nc_new_interval_type'));
52         var inh = $('nc_new_inhouse').checked ? 1 : null;
53
54         var req = new Request(CREATE_NON_CAT_TYPE, SESSION, name, org, time + ' ' + type, inh );
55         req.send(true);
56         var res = req.result();
57         if(checkILSEvent(res)) throw res;
58         alertId('nc_update_success');
59         ncFetchTypes();
60 }
61
62
63 var rowTemplate;
64 function ncDisplayTypes(r) {
65
66         var types = r.getResultObject();
67         var tbody = $('nc_tbody');
68         if(!rowTemplate) 
69                 rowTemplate = tbody.removeChild($('nc_row_template'));
70
71         removeChildren(tbody);
72         types = types.sort( 
73                 function(a,b) {
74                         try {
75                                 if( a.name()+''.toLowerCase() > b.name()+''.toLowerCase() ) return 1;   
76                                 if( a.name()+''.toLowerCase() < b.name()+''.toLowerCase() ) return -1;  
77                         } catch(e) {}
78                         return 0;
79                 });
80
81         for( var idx = 0; idx != types.length; idx++ ) {
82
83                 var type = types[idx];
84                 var org = findOrgUnit( type.owning_lib() );
85                 var row = rowTemplate.cloneNode(true);
86
87
88                 row.id = 'nc_row_' + type.id();
89                 $n(row, 'nc_name').appendChild(text(type.name()));
90                 $n(row, 'nc_owner').appendChild(text(org.name()));
91                 $n(row, 'nc_inhouse').checked = isTrue(type.in_house());
92
93                 var idata = _splitInterval(type.circ_duration());
94                 $n(row, 'nc_interval_count').value = idata[0];
95                 setSelector( $n(row, 'nc_interval_type'), idata[1]);
96
97                 ncSetRowCallbacks( type, org, tbody, row );
98                 tbody.appendChild(row);
99         }
100 }
101
102 /* this is a kind of brittle, but works with the data we create */
103 function _splitInterval( interval ) {
104         interval = interval.split(/ /);
105         var time = interval[0];
106         var type = interval[1];
107          
108         if( time.match(/:/) ) {
109                 var d = time.split(/:/);
110                 if(d[0] == '00') return [ d[1], 'minutes' ];
111                 if(d[0] != '00' && d[1] != '00')
112                         return [ parseInt(d[1]) + (d[0]*60), 'minutes' ];
113                 return [ d[0], 'hours' ]
114         }
115
116         if( type.match(/mi/i) ) return [ time, 'minutes' ];
117         if( type.match(/h/i) ) return [ time, 'hours' ];
118         if( type.match(/d/i) ) return [ time, 'days' ];
119         if( type.match(/w/i) ) return [ time, 'weeks' ];
120         if( type.match(/mo/i) ) return [ time, 'months' ];
121 }
122
123 function ncSetRowCallbacks( type, owner, tbody, row ) {
124
125         checkDisabled( $n(row, 'nc_edit'), owner, 'UPDATE_NON_CAT_TYPE');
126
127         checkDisabled( $n(row, 'nc_delete'), owner, 'DELETE_NON_CAT_TYPE' );
128
129         $n(row, 'nc_edit').onclick = 
130                 function() { ncEditType( tbody, row, type ); };
131
132         $n(row, 'nc_delete').onclick = 
133                 function() { ncDeleteType( tbody, row, type ); };
134 }
135
136 function ncEditType( tbody, row, type ) {
137         cleanTbody(row.parentNode, 'edit');
138         var row = $('nc_edit_row_temaplate').cloneNode(true);
139
140         var name = $n(row, 'nc_edit_name');
141         name.value = type.name();
142
143         var idata = _splitInterval(type.circ_duration());
144         $n(row, 'nc_edit_interval_count').value = idata[0];
145         setSelector( $n(row, 'nc_edit_interval_type'), idata[1]);
146
147         $n(row, 'nc_edit_inhouse').checked = isTrue(type.in_house());
148         $n(row, 'nc_edit_owner').appendChild(text( findOrgUnit(type.owning_lib()).name() ));
149
150         $n(row, 'nc_edit_submit').onclick = function() { 
151                 var name = $n(row, 'nc_edit_name').value;
152                 var time = $n(row, 'nc_edit_interval_count').value;
153                 var tp = getSelectorVal($n(row, 'nc_edit_interval_type'));
154                 var inh = $n(row, 'nc_edit_inhouse').checked ? 't' : 'f';
155                 ncEditSubmit( type, name, time + ' ' + tp, inh );
156         };
157
158         $n(row, 'nc_edit_cancel').onclick = 
159                 function(){cleanTbody(row.parentNode, 'edit'); }
160
161         var r = $('nc_row_' + type.id());
162         if(r.nextSibling) tbody.insertBefore( row, r.nextSibling );
163         else{ tbody.appendChild(row); }
164
165         name.focus();
166         name.select();
167 }
168
169 function ncEditSubmit( type, name, interval, inhouse ) {
170         if(!name) return;
171         type.name(name);
172         type.circ_duration(interval);
173         type.in_house(inhouse);
174         var req = new Request( UPDATE_NON_CAT_TYPE, SESSION, type );
175         req.send(true);
176         var res = req.result();
177         if(checkILSEvent(res)) throw res;
178         alertId('nc_update_success');
179         ncFetchTypes();
180 }
181
182 function ncDeleteType( tbody, row, type ) {
183         if( ! confirm($('nc_delete_confirm').innerHTML) ) return;
184         var req = new Request(DELETE_NON_CAT_TYPE, SESSION, type.id());
185         req.callback( 
186                 function(r) {
187                         var res = r.getResultObject();
188                         if(checkILSEvent(res)) alertILSEvent(res);
189                         alertId('nc_update_success');
190                         ncFetchTypes();
191                 }
192         );
193         req.send();
194 }
195
196
197
198