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