]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/non_cat_types.js
Removed the submit-on-enter code when creating new non-cat types. Allowing
[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
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         setTimeout(function(){req.send();}, 500);
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 time = $('nc_new_interval_count').value;
50         var type = getSelectorVal($('nc_new_interval_type'));
51         var inh = $('nc_new_inhouse').checked ? 1 : null;
52
53         var req = new Request(CREATE_NON_CAT_TYPE, SESSION, name, org, time + ' ' + type, inh );
54         req.send(true);
55         var res = req.result();
56         if(checkILSEvent(res)) throw res;
57         alertId('nc_update_success');
58         ncFetchTypes();
59 }
60
61
62 var rowTemplate;
63 function ncDisplayTypes(r) {
64
65         var types = r.getResultObject();
66         var tbody = $('nc_tbody');
67         if(!rowTemplate) 
68                 rowTemplate = tbody.removeChild($('nc_row_template'));
69
70         removeChildren(tbody);
71         types = types.sort( 
72                 function(a,b) {
73                         try {
74                                 if( a.name()+''.toLowerCase() > b.name()+''.toLowerCase() ) return 1;   
75                                 if( a.name()+''.toLowerCase() < b.name()+''.toLowerCase() ) return -1;  
76                         } catch(e) {}
77                         return 0;
78                 });
79
80         for( var idx = 0; idx != types.length; idx++ ) {
81
82                 var type = types[idx];
83                 var org = findOrgUnit( type.owning_lib() );
84                 var row = rowTemplate.cloneNode(true);
85
86
87                 row.id = 'nc_row_' + type.id();
88                 $n(row, 'nc_name').appendChild(text(type.name()));
89                 $n(row, 'nc_owner').appendChild(text(org.name()));
90                 $n(row, 'nc_inhouse').checked = isTrue(type.in_house());
91
92                 var idata = _splitInterval(type.circ_duration());
93                 $n(row, 'nc_interval_count').value = idata[0];
94                 setSelector( $n(row, 'nc_interval_type'), idata[1]);
95
96                 ncSetRowCallbacks( type, org, tbody, row );
97                 tbody.appendChild(row);
98         }
99 }
100
101 /* this is a kind of brittle, but works with the data we create */
102 function _splitInterval( interval ) {
103         interval = interval.split(/ /);
104         var time = interval[0];
105         var type = interval[1];
106          
107         if( time.match(/:/) ) {
108                 var d = time.split(/:/);
109                 if(d[0] == '00') return [ d[1], 'minutes' ];
110                 if(d[0] != '00' && d[1] != '00')
111                         return [ parseInt(d[1]) + (d[0]*60), 'minutes' ];
112                 return [ d[0], 'hours' ]
113         }
114
115         if( type.match(/mi/i) ) return [ time, 'minutes' ];
116         if( type.match(/h/i) ) return [ time, 'hours' ];
117         if( type.match(/d/i) ) return [ time, 'days' ];
118         if( type.match(/w/i) ) return [ time, 'weeks' ];
119         if( type.match(/mo/i) ) return [ time, 'months' ];
120 }
121
122 function ncSetRowCallbacks( type, owner, tbody, row ) {
123
124         checkDisabled( $n(row, 'nc_edit'), owner, 'UPDATE_NON_CAT_TYPE');
125
126         checkDisabled( $n(row, 'nc_delete'), owner, 'DELETE_NON_CAT_TYPE' );
127
128         $n(row, 'nc_edit').onclick = 
129                 function() { ncEditType( tbody, row, type ); };
130
131         $n(row, 'nc_delete').onclick = 
132                 function() { ncDeleteType( tbody, row, type ); };
133 }
134
135 function ncEditType( tbody, row, type ) {
136         cleanTbody(row.parentNode, 'edit');
137         var row = $('nc_edit_row_temaplate').cloneNode(true);
138
139         var name = $n(row, 'nc_edit_name');
140         name.value = type.name();
141
142         var idata = _splitInterval(type.circ_duration());
143         $n(row, 'nc_edit_interval_count').value = idata[0];
144         setSelector( $n(row, 'nc_edit_interval_type'), idata[1]);
145
146         $n(row, 'nc_edit_inhouse').checked = isTrue(type.in_house());
147         $n(row, 'nc_edit_owner').appendChild(text( findOrgUnit(type.owning_lib()).name() ));
148
149         $n(row, 'nc_edit_submit').onclick = function() { 
150                 var name = $n(row, 'nc_edit_name').value;
151                 var time = $n(row, 'nc_edit_interval_count').value;
152                 var tp = getSelectorVal($n(row, 'nc_edit_interval_type'));
153                 var inh = $n(row, 'nc_edit_inhouse').checked ? 't' : 'f';
154                 ncEditSubmit( type, name, time + ' ' + tp, inh );
155         };
156
157         $n(row, 'nc_edit_cancel').onclick = 
158                 function(){cleanTbody(row.parentNode, 'edit'); }
159
160         var r = $('nc_row_' + type.id());
161         if(r.nextSibling) tbody.insertBefore( row, r.nextSibling );
162         else{ tbody.appendChild(row); }
163
164         name.focus();
165         name.select();
166 }
167
168 function ncEditSubmit( type, name, interval, inhouse ) {
169         if(!name) return;
170         type.name(name);
171         type.circ_duration(interval);
172         type.in_house(inhouse);
173         var req = new Request( UPDATE_NON_CAT_TYPE, SESSION, type );
174         req.send(true);
175         var res = req.result();
176         if(checkILSEvent(res)) throw res;
177         alertId('nc_update_success');
178         ncFetchTypes();
179 }
180
181 function ncDeleteType( tbody, row, type ) {
182         if( ! confirm($('nc_delete_confirm').innerHTML) ) return;
183         var req = new Request(DELETE_NON_CAT_TYPE, SESSION, type.id());
184         req.callback( 
185                 function(r) {
186                         var res = r.getResultObject();
187                         if(checkILSEvent(res)) alertILSEvent(res);
188                         alertId('nc_update_success');
189                         ncFetchTypes();
190                 }
191         );
192         req.send();
193 }
194
195
196
197