1 /* staff client integration functions */
3 // Browser staff client runs the TPAC within an iframe, whose onload
4 // is not called until after the page onload is called. window.onload
5 // actions are wrapped in timeouts (below) to ensure the wrapping page
6 // has a chance to insert the necessary xulG, etc. functions into the
9 function debug(msg){dump(msg+'\n')}
11 function attachEvt(scope, name, action) {
12 if(!eventCache[scope]) eventCache[scope] = {};
13 if(!eventCache[scope][name]) eventCache[scope][name] = [];
14 eventCache[scope][name].push(action);
16 function runEvt(scope, name) {
17 debug('running event '+scope+':'+name);
18 var args = Array.prototype.slice.call(arguments).slice(2);
19 if(eventCache[scope]) {
20 var evt = eventCache[scope][name];
21 for(var i in evt) {evt[i].apply(evt[i], args);}
24 function staff_hold_usr_input_disabler(input) {
25 document.getElementById("hold_usr_input").disabled =
26 Boolean(Number(input.value));
27 staff_hold_usr_barcode_changed();
30 var debounce_barcode_change = function() {
33 return function(event) {
34 clearTimeout(timeout);
35 document.getElementById('patron_usr_barcode_not_found').style.display = 'none';
37 if (event.which == '13') {
38 staff_hold_usr_barcode_changed();
42 var duration = event.type == 'paste' ? 0 : 500;
43 timeout = setTimeout(staff_hold_usr_barcode_changed, duration);
49 function no_hold_submit(event) {
50 if (event.which == 13) {
51 staff_hold_usr_barcode_changed();
57 function toggleMROptions(on) {
58 var anchor = document.getElementById("advanced_hold_link");
59 // Check for not equal to block so it works on first click.
62 anchor.style.display = "inline";
64 anchor.style.display = "none";
69 function maybeDisable (thing, value) {
70 var el = document.getElementById(thing);
71 if (el) el.disabled = value;
74 function toggleOnSubscription(isSub) {
75 toggleMROptions(!isSub);
76 maybeDisable("override_blocks_subscription",!isSub);
77 maybeDisable("pickup_lib",isSub);
78 maybeDisable("email_notify",isSub);
79 maybeDisable("phone_notify_checkbox",isSub);
80 maybeDisable("phone_notify",isSub);
81 maybeDisable("sms_notify_checkbox",isSub);
82 maybeDisable("sms_carrier",isSub);
83 maybeDisable("sms_notify",isSub);
86 function staff_hold_usr_barcode_changed(isload) {
88 if (!document.getElementById('place_hold_submit')) {
89 // in some cases, the submit button is not present.
90 // exit early to avoid needless JS errors
94 if (!window.xulG) return;
96 var sub_el = document.getElementById('hold_usr_is_subscription');
97 var adv_link = document.getElementById('advanced_hold_link');
99 adv_link.setAttribute('href', adv_link.getAttribute('href').replace(/&?is_requestor=[012]/,''));
100 var is_requestor = 0;
101 if (document.getElementById('hold_usr_is_requestor').checked) {
103 } else if (sub_el && sub_el.checked) {
106 adv_link.setAttribute('href', adv_link.getAttribute('href') + '&is_requestor=' + is_requestor.toString());
109 var cur_hold_barcode = undefined;
110 var barcode = isload;
111 if(!barcode || barcode === true) barcode = document.getElementById('staff_barcode').value;
112 var only_settings = true;
114 toggleOnSubscription(false);
115 if(sub_el && sub_el.checked) {
116 toggleOnSubscription(true);
118 only_settings = false;
120 } else if(!document.getElementById('hold_usr_is_requestor').checked) {
122 barcode = document.getElementById('hold_usr_input').value;
123 only_settings = false;
125 if(barcode && barcode != '' && !document.getElementById('hold_usr_is_requestor_not').checked)
126 document.getElementById('hold_usr_is_requestor_not').checked = 'checked';
129 if((barcode == undefined || barcode == '') && (!sub_el || !sub_el.checked)) {
130 document.getElementById('patron_name').innerHTML = '';
131 // No submitting on empty barcode, but empty barcode doesn't really count as "not found" either
132 document.getElementById('place_hold_submit').disabled = true;
133 document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
134 cur_hold_barcode = null;
137 if(barcode == cur_hold_barcode)
139 // No submitting until we think the barcode is valid
140 document.getElementById('place_hold_submit').disabled = true;
142 if (window.IAMBROWSER) {
143 // Browser client operates asynchronously
144 if (!xulG.get_barcode_and_settings_async) return;
145 xulG.get_barcode_and_settings_async(barcode, only_settings)
147 function(load_info) { // load succeeded
148 staff_hold_usr_barcode_changed2(
149 isload, only_settings, barcode, cur_hold_barcode, load_info);
152 // load failed (rejected). Call staff_hold_usr_barcode_changed2
153 // anyway, since it handles clearing the form
154 staff_hold_usr_barcode_changed2(
155 isload, only_settings, barcode, cur_hold_barcode, false);
159 // XUL version is synchronous
160 if (!xulG.get_barcode_and_settings) return;
161 var load_info = xulG.get_barcode_and_settings(window, barcode, only_settings);
162 staff_hold_usr_barcode_changed2(isload, only_settings, barcode, cur_hold_barcode, load_info);
166 function staff_hold_usr_barcode_changed2(
167 isload, only_settings, barcode, cur_hold_barcode, load_info) {
169 var sub_el = document.getElementById('hold_usr_is_subscription');
171 if(load_info == false || load_info == undefined) {
172 document.getElementById('patron_name').innerHTML = '';
173 document.getElementById("patron_usr_barcode_not_found").style.display = '';
174 cur_hold_barcode = null;
177 cur_hold_barcode = load_info.barcode;
178 if ((!only_settings || (isload && isload !== true)) && (!sub_el || !sub_el.checked)) {
179 // Safe at this point as we already set cur_hold_barcode
180 document.getElementById('hold_usr_input').value = load_info.barcode;
182 // Patron preferred pickup loc overrides the default pickup lib
183 // unless the default to workstation setting is enabled
184 document.getElementById('pickup_lib').value =
185 !load_info.settings['circ.staff_placed_holds_default_to_ws_ou'] &&
186 load_info.settings['opac.default_pickup_location'] ?
187 load_info.settings['opac.default_pickup_location'] : load_info.pickup_lib;
190 if (!load_info.settings['opac.default_sms_notify']){
191 load_info.settings['opac.default_sms_notify'] = '';
194 if (!load_info.settings['opac.default_sms_carrier']){
195 load_info.settings['opac.default_sms_carrier'] = '';
198 if (!sub_el || !sub_el.checked) {
199 if (load_info.settings['opac.hold_notify'] || load_info.settings['opac.hold_notify'] === '') {
200 var email = load_info.settings['opac.hold_notify'].indexOf('email') > -1;
201 var phone = load_info.settings['opac.hold_notify'].indexOf('phone') > -1;
202 var sms = load_info.settings['opac.hold_notify'].indexOf('sms') > -1;
203 var update_elements = document.getElementsByName('email_notify');
204 for(var i in update_elements) update_elements[i].checked = (email ? 'checked' : '');
205 update_elements = document.getElementsByName('phone_notify_checkbox');
206 for(var i in update_elements) update_elements[i].checked = (phone ? 'checked' : '');
207 update_elements = document.getElementsByName('sms_notify_checkbox');
208 for(var i in update_elements) update_elements[i].checked = (sms ? 'checked' : '');
211 update_elements = document.getElementsByName('phone_notify');
212 for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_phone']
213 ? load_info.settings['opac.default_phone'] : '';
214 update_elements = document.getElementsByName('sms_notify');
215 for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_notify'];
216 update_elements = document.getElementsByName('sms_carrier');
217 for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_carrier'];
218 update_elements = document.getElementsByName('email_notify');
219 for(var i in update_elements) {
220 update_elements[i].disabled = (load_info.user_email ? false : true);
221 if(update_elements[i].disabled) update_elements[i].checked = false;
223 update_elements = document.getElementsByName('email_address');
224 for(var i in update_elements) update_elements[i].textContent = load_info.user_email;
225 if(!document.getElementById('hold_usr_is_requestor').checked && document.getElementById('hold_usr_input').value) {
226 document.getElementById('patron_name').innerHTML = load_info.patron_name;
227 document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
230 // Ok, now we can allow submitting again, unless this is a "true" load, in which case we likely have a blank barcode box active
232 // update the advanced hold options link to propagate the patron
233 // barcode if clicked. This is needed when the patron barcode
234 // is manually entered (i.e. the staff client does not provide one).
235 var adv_link = document.getElementById('advanced_hold_link');
236 if (adv_link) { // not present on MR hold pages
237 var href = adv_link.getAttribute('href').replace(
238 /;usr_barcode=[^;\&]+|$/,
239 ';usr_barcode=' + encodeURIComponent(cur_hold_barcode));
240 adv_link.setAttribute('href', href);
244 document.getElementById('place_hold_submit').disabled = false;
246 window.onload = function() {
247 // record details page events
249 setTimeout(function() {
251 if (location.href.match(/is_requestor=[012]/)) {
252 var loc = location.href;
253 var is_req_match = new RegExp("is_requestor=[012]");
254 var is_req = is_req_match.exec(loc).toString();
255 is_req = is_req.replace(/is_requestor=/, '');
257 var sub_el = document.getElementById('hold_usr_is_subscription');
259 sub_el.checked = 'checked';
261 document.getElementById('hold_usr_input').disabled = true;
262 } else if (is_req == "1") {
263 document.getElementById('hold_usr_is_requestor').checked = 'checked';
264 document.getElementById('hold_usr_input').disabled = true;
266 document.getElementById('hold_usr_is_requestor_not').checked = 'checked';
267 document.getElementById('hold_usr_input').disabled = false;
271 var rec = location.href.match(/\/opac\/record\/(\d+)/);
273 runEvt('rdetail', 'recordRetrieved', rec[1]);
274 runEvt('rdetail', 'MFHDDrawn');
276 if(location.href.match(/place_hold/)) {
277 // patron barcode may come from XUL or a CGI param
278 var patron_barcode = xulG.patron_barcode ||
279 document.getElementById('hold_usr_input').value;
281 staff_hold_usr_barcode_changed(patron_barcode);
283 staff_hold_usr_barcode_changed(true);
289 function rdetail_next_prev_actions(index, count, prev, next, start, end, results) {
290 /* we mostly get the relative URL from the template: recid?query_args...
291 replace the recid and args on location.href to get the new URL */
292 function fullurl(url) {
293 if (url.match(/eg\/opac\/results/)) {
294 return location.href.replace(/\/eg\/opac\/.+$/, url);
296 return location.href.replace(/\/\d+\??.*/, '/' + url);
302 window.rdetailPrev = function() { location.href = fullurl(prev); }
304 window.rdetailStart = function() { location.href = fullurl(start); }
307 if (index < count - 1) {
309 window.rdetailNext = function() { location.href = fullurl(next); }
311 window.rdetailEnd = function() { location.href = fullurl(end); }
314 window.rdetailBackToResults = function() { location.href = fullurl(results); };
317 window.onload = function() {
319 setTimeout(function() {
320 runEvt('rdetail', 'nextPrevDrawn', Number(index), Number(count));