]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/staff.js
LP#1838995: Hold group buckets
[Evergreen.git] / Open-ILS / web / js / ui / default / opac / staff.js
1 /* staff client integration functions */
2
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
7 // window.
8
9 function debug(msg){dump(msg+'\n')}
10 var eventCache={};
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);
15 }
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);}
22     } 
23 }
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();
28 }
29
30 var debounce_barcode_change = function() {
31     var timeout;
32
33     return function(event) {
34         clearTimeout(timeout);
35         document.getElementById('patron_usr_barcode_not_found').style.display = 'none';
36
37         if (event.which == '13') {
38             staff_hold_usr_barcode_changed();
39             return false;
40         }
41
42         var duration = event.type == 'paste' ? 0 : 500;
43         timeout = setTimeout(staff_hold_usr_barcode_changed, duration);
44
45         return true;
46     };
47 }();
48
49 function no_hold_submit(event) {
50     if (event.which == 13) {
51         staff_hold_usr_barcode_changed();
52         return false;
53     }
54     return true;
55 }
56
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.
60     if (on) {
61         anchor.style.display = "inline";
62     } else {
63         anchor.style.display = "none";
64     }
65 }
66
67 function maybeDisable (thing, value) {
68     var el = document.getElementById(thing);
69     if (el) el.disabled = value;
70 }
71
72 function toggleOnSubscription(isSub) {
73     toggleMROptions(!isSub);
74     maybeDisable("override_blocks_subscription",!isSub);
75     maybeDisable("pickup_lib",isSub);
76     maybeDisable("email_notify",isSub);
77     maybeDisable("phone_notify_checkbox",isSub);
78     maybeDisable("phone_notify",isSub);
79     maybeDisable("sms_notify_checkbox",isSub);
80     maybeDisable("sms_carrier",isSub);
81     maybeDisable("sms_notify",isSub);
82 }
83
84 function staff_hold_usr_barcode_changed(isload) {
85
86     if (!document.getElementById('place_hold_submit')) {
87         // in some cases, the submit button is not present.
88         // exit early to avoid needless JS errors
89         return;
90     }
91
92     if (!window.xulG) return;
93  
94     var adv_link = document.getElementById('advanced_hold_link');
95     if (adv_link) {
96         adv_link.setAttribute('href', adv_link.getAttribute('href').replace(/&?is_requestor=[012]/,''));
97         var is_requestor = 0;
98         if (document.getElementById('hold_usr_is_requestor').checked) {
99             is_requestor = 1;
100         } else if (document.getElementById('hold_usr_is_subscription').checked) {
101             is_requestor = 2;
102         }
103         adv_link.setAttribute('href', adv_link.getAttribute('href') + '&is_requestor=' + is_requestor.toString());
104     }
105
106     var cur_hold_barcode = undefined;
107     var barcode = isload;
108     if(!barcode || barcode === true) barcode = document.getElementById('staff_barcode').value;
109     var only_settings = true;
110     var sub_el = document.getElementById('hold_usr_is_subscription');
111
112     toggleOnSubscription(false);
113     if(sub_el && sub_el.checked) {
114         toggleOnSubscription(true);
115         if(!isload) {
116             only_settings = false;
117         }
118     } else if(!document.getElementById('hold_usr_is_requestor').checked) {
119         if(!isload) {
120             barcode = document.getElementById('hold_usr_input').value;
121             only_settings = false;
122         }
123         if(barcode && barcode != '' && !document.getElementById('hold_usr_is_requestor_not').checked)
124             document.getElementById('hold_usr_is_requestor_not').checked = 'checked';
125     }
126
127     if((barcode == undefined || barcode == '') && (!sub_el || !sub_el.checked)) {
128         document.getElementById('patron_name').innerHTML = '';
129         // No submitting on empty barcode, but empty barcode doesn't really count as "not found" either
130         document.getElementById('place_hold_submit').disabled = true;
131         document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
132         cur_hold_barcode = null;
133         return;
134     }
135     if(barcode == cur_hold_barcode)
136         return;
137     // No submitting until we think the barcode is valid
138     document.getElementById('place_hold_submit').disabled = true;
139
140     if (window.IAMBROWSER) {
141         // Browser client operates asynchronously
142         if (!xulG.get_barcode_and_settings_async) return;
143         xulG.get_barcode_and_settings_async(barcode, only_settings)
144         .then(
145             function(load_info) { // load succeeded
146                 staff_hold_usr_barcode_changed2(
147                     isload, only_settings, barcode, cur_hold_barcode, load_info);
148             },
149             function() { 
150                 // load failed (rejected).  Call staff_hold_usr_barcode_changed2
151                 // anyway, since it handles clearing the form
152                 staff_hold_usr_barcode_changed2(
153                     isload, only_settings, barcode, cur_hold_barcode, false);
154             }
155         )
156     } else {
157         // XUL version is synchronous
158         if (!xulG.get_barcode_and_settings) return;
159         var load_info = xulG.get_barcode_and_settings(window, barcode, only_settings);
160         staff_hold_usr_barcode_changed2(isload, only_settings, barcode, cur_hold_barcode, load_info);
161     }
162 }
163
164 function staff_hold_usr_barcode_changed2(
165     isload, only_settings, barcode, cur_hold_barcode, load_info) {
166
167     var sub_el = document.getElementById('hold_usr_is_subscription');
168
169     if(load_info == false || load_info == undefined) {
170         document.getElementById('patron_name').innerHTML = '';
171         document.getElementById("patron_usr_barcode_not_found").style.display = '';
172         cur_hold_barcode = null;
173         return;
174     }
175     cur_hold_barcode = load_info.barcode;
176     if ((!only_settings || (isload && isload !== true)) && (sub_el && !sub_el.checked)) {
177         // Safe at this point as we already set cur_hold_barcode
178         document.getElementById('hold_usr_input').value = load_info.barcode;
179
180         // Patron preferred pickup loc always overrides the default pickup lib
181         document.getElementById('pickup_lib').value = 
182             load_info.settings['opac.default_pickup_location'] ?
183             load_info.settings['opac.default_pickup_location'] : load_info.pickup_lib;
184     }
185
186     if (!load_info.settings['opac.default_sms_notify']){
187         load_info.settings['opac.default_sms_notify'] = '';
188     }
189
190     if (!load_info.settings['opac.default_sms_carrier']){
191         load_info.settings['opac.default_sms_carrier'] = '';
192     }
193
194     if (!sub_el || !sub_el.checked) {
195         if (load_info.settings['opac.hold_notify'] || load_info.settings['opac.hold_notify'] === '') {
196             var email = load_info.settings['opac.hold_notify'].indexOf('email') > -1;
197             var phone = load_info.settings['opac.hold_notify'].indexOf('phone') > -1;
198             var sms = load_info.settings['opac.hold_notify'].indexOf('sms') > -1;
199             var update_elements = document.getElementsByName('email_notify');
200             for(var i in update_elements) update_elements[i].checked = (email ? 'checked' : '');
201             update_elements = document.getElementsByName('phone_notify_checkbox');
202             for(var i in update_elements) update_elements[i].checked = (phone ? 'checked' : '');
203             update_elements = document.getElementsByName('sms_notify_checkbox');
204             for(var i in update_elements) update_elements[i].checked = (sms ? 'checked' : '');
205         }
206     
207         update_elements = document.getElementsByName('phone_notify');
208         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_phone']
209             ? load_info.settings['opac.default_phone'] : '';
210         update_elements = document.getElementsByName('sms_notify');
211         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_notify'];
212         update_elements = document.getElementsByName('sms_carrier');
213         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_carrier'];
214         update_elements = document.getElementsByName('email_notify');
215         for(var i in update_elements) {
216             update_elements[i].disabled = (load_info.user_email ? false : true);
217             if(update_elements[i].disabled) update_elements[i].checked = false;
218         }
219         update_elements = document.getElementsByName('email_address');
220         for(var i in update_elements) update_elements[i].textContent = load_info.user_email;
221         if(!document.getElementById('hold_usr_is_requestor').checked && document.getElementById('hold_usr_input').value) {
222             document.getElementById('patron_name').innerHTML = load_info.patron_name;
223             document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
224         }
225     }
226     // Ok, now we can allow submitting again, unless this is a "true" load, in which case we likely have a blank barcode box active
227
228     // update the advanced hold options link to propagate the patron
229     // barcode if clicked.  This is needed when the patron barcode
230     // is manually entered (i.e. the staff client does not provide one).
231     var adv_link = document.getElementById('advanced_hold_link');
232     if (adv_link) { // not present on MR hold pages
233         var href = adv_link.getAttribute('href').replace(
234             /;usr_barcode=[^;\&]+|$/, 
235             ';usr_barcode=' + encodeURIComponent(cur_hold_barcode));
236         adv_link.setAttribute('href', href);
237     }
238
239     if (isload !== true)
240         document.getElementById('place_hold_submit').disabled = false;
241 }
242 window.onload = function() {
243     // record details page events
244
245     setTimeout(function() {
246
247         if (location.href.match(/is_requestor=[012]/)) {
248             var loc = location.href;
249             var is_req_match = new RegExp("is_requestor=[012]");
250             var is_req = is_req_match.exec(loc).toString();
251             is_req = is_req.replace(/is_requestor=/, '');
252             if (is_req == "2") {
253                 document.getElementById('hold_usr_is_subscription').checked = 'checked';
254                 document.getElementById('hold_usr_input').disabled = true;
255             } else if (is_req == "1") {
256                 document.getElementById('hold_usr_is_requestor').checked = 'checked';
257                 document.getElementById('hold_usr_input').disabled = true;
258             } else {
259                 document.getElementById('hold_usr_is_requestor_not').checked = 'checked';
260                 document.getElementById('hold_usr_input').disabled = false;
261             }
262         }
263
264         var rec = location.href.match(/\/opac\/record\/(\d+)/);
265         if(rec && rec[1]) { 
266             runEvt('rdetail', 'recordRetrieved', rec[1]); 
267             runEvt('rdetail', 'MFHDDrawn');
268         }
269         if(location.href.match(/place_hold/)) {
270             // patron barcode may come from XUL or a CGI param
271             var patron_barcode = xulG.patron_barcode ||
272                 document.getElementById('hold_usr_input').value;
273             if(patron_barcode) {
274                 staff_hold_usr_barcode_changed(patron_barcode);
275             } else {
276                 staff_hold_usr_barcode_changed(true);
277             }
278         }
279     });
280 }
281
282 function rdetail_next_prev_actions(index, count, prev, next, start, end, results) {
283     /*  we mostly get the relative URL from the template:  recid?query_args...
284         replace the recid and args on location.href to get the new URL  */
285     function fullurl(url) {
286         if (url.match(/eg\/opac\/results/)) {
287             return location.href.replace(/\/eg\/opac\/.+$/, url);
288         } else {
289             return location.href.replace(/\/\d+\??.*/, '/' + url);
290         }
291     }
292
293     if (index > 0) {
294         if(prev) 
295             window.rdetailPrev = function() { location.href = fullurl(prev); }
296         if(start) 
297             window.rdetailStart = function() { location.href = fullurl(start); }
298     }
299
300     if (index < count - 1) {
301         if(next) 
302             window.rdetailNext = function() { location.href = fullurl(next); }
303         if(end) 
304             window.rdetailEnd = function() { location.href = fullurl(end); }
305     }
306
307     window.rdetailBackToResults = function() { location.href = fullurl(results); };
308
309     ol = window.onload;
310     window.onload = function() {
311         if(ol) ol(); 
312         setTimeout(function() {
313             runEvt('rdetail', 'nextPrevDrawn', Number(index), Number(count)); 
314         });
315     };
316 }