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