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';
8 'DELETE_NON_CAT_TYPE' ];
10 function ncEditorInit() {
12 $('nc_user').appendChild(text(USER.usrname()));
13 setTimeout( function() {
14 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
16 ncFetchTypes(); }, 20 );
20 function ncBuildNew() {
22 var name = $('nc_new_name');
24 setEnterFunc(name, ncCreateNew );
26 var org = findOrgUnit(PERMS['CREATE_NON_CAT_TYPE']);
27 var mydepth = findOrgDepth(org);
28 if( mydepth == -1 ) return;
30 var selector = $('nc_new_owner');
31 buildOrgSel(selector, org, mydepth );
32 if(org.children() && org.children()[0])
33 selector.disabled = false;
35 $('nc_new_submit').disabled = false;
36 $('nc_new_submit').onclick = ncCreateNew;
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);
46 function ncCreateNew() {
47 var name = $('nc_new_name').value;
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;
54 var req = new Request(CREATE_NON_CAT_TYPE, SESSION, name, org, time + ' ' + type, inh );
56 var res = req.result();
57 if(checkILSEvent(res)) throw res;
58 alertId('nc_update_success');
64 function ncDisplayTypes(r) {
66 var types = r.getResultObject();
67 var tbody = $('nc_tbody');
69 rowTemplate = tbody.removeChild($('nc_row_template'));
71 removeChildren(tbody);
75 if( a.name()+''.toLowerCase() > b.name()+''.toLowerCase() ) return 1;
76 if( a.name()+''.toLowerCase() < b.name()+''.toLowerCase() ) return -1;
81 for( var idx = 0; idx != types.length; idx++ ) {
83 var type = types[idx];
84 var org = findOrgUnit( type.owning_lib() );
85 var row = rowTemplate.cloneNode(true);
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());
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]);
97 ncSetRowCallbacks( type, org, tbody, row );
98 tbody.appendChild(row);
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];
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' ]
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' ];
123 function ncSetRowCallbacks( type, owner, tbody, row ) {
125 checkDisabled( $n(row, 'nc_edit'), owner, 'UPDATE_NON_CAT_TYPE');
127 checkDisabled( $n(row, 'nc_delete'), owner, 'DELETE_NON_CAT_TYPE' );
129 $n(row, 'nc_edit').onclick =
130 function() { ncEditType( tbody, row, type ); };
132 $n(row, 'nc_delete').onclick =
133 function() { ncDeleteType( tbody, row, type ); };
136 function ncEditType( tbody, row, type ) {
137 cleanTbody(row.parentNode, 'edit');
138 var row = $('nc_edit_row_temaplate').cloneNode(true);
140 var name = $n(row, 'nc_edit_name');
141 name.value = type.name();
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]);
147 $n(row, 'nc_edit_inhouse').checked = isTrue(type.in_house());
148 $n(row, 'nc_edit_owner').appendChild(text( findOrgUnit(type.owning_lib()).name() ));
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 );
158 $n(row, 'nc_edit_cancel').onclick =
159 function(){cleanTbody(row.parentNode, 'edit'); }
161 var r = $('nc_row_' + type.id());
162 if(r.nextSibling) tbody.insertBefore( row, r.nextSibling );
163 else{ tbody.appendChild(row); }
169 function ncEditSubmit( type, name, interval, inhouse ) {
172 type.circ_duration(interval);
173 type.in_house(inhouse);
174 var req = new Request( UPDATE_NON_CAT_TYPE, SESSION, type );
176 var res = req.result();
177 if(checkILSEvent(res)) throw res;
178 alertId('nc_update_success');
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());
187 var res = r.getResultObject();
188 if(checkILSEvent(res)) alertILSEvent(res);
189 alertId('nc_update_success');