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