]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/staff.js
LP#1294156 TPAC activate hold Submit on load w/ barcode
[working/Evergreen.git] / Open-ILS / web / js / ui / default / opac / staff.js
1 /* staff client integration functions */
2 function debug(msg){dump(msg+'\n')}
3 var eventCache={};
4 function attachEvt(scope, name, action) {
5     if(!eventCache[scope]) eventCache[scope] = {};
6     if(!eventCache[scope][name]) eventCache[scope][name] = [];
7     eventCache[scope][name].push(action);
8 }
9 function runEvt(scope, name) {
10     debug('running event '+scope+':'+name);
11     var args = Array.prototype.slice.call(arguments).slice(2);
12     if(eventCache[scope]) {
13         var evt = eventCache[scope][name];
14         for(var i in evt) {evt[i].apply(evt[i], args);}
15     } 
16 }
17 function staff_hold_usr_input_disabler(input) {
18     document.getElementById("hold_usr_input").disabled =
19         Boolean(Number(input.value));
20     staff_hold_usr_barcode_changed();
21 }
22 function no_hold_submit(event) {
23     if (event.which == 13) {
24         staff_hold_usr_barcode_changed();
25         return false;
26     }
27     return true;
28 }
29 function staff_hold_usr_barcode_changed(isload) {
30
31     if (!document.getElementById('place_hold_submit')) {
32         // in some cases, the submit button is not present.
33         // exit early to avoid needless JS errors
34         return;
35     }
36
37     if(typeof xulG != 'undefined' && xulG.get_barcode_and_settings) {
38         var cur_hold_barcode = undefined;
39         var barcode = isload;
40         if(!barcode || barcode === true) barcode = document.getElementById('staff_barcode').value;
41         var only_settings = true;
42         if(!document.getElementById('hold_usr_is_requestor').checked) {
43             if(!isload) {
44                 barcode = document.getElementById('hold_usr_input').value;
45                 only_settings = false;
46             }
47             if(barcode && barcode != '' && !document.getElementById('hold_usr_is_requestor_not').checked)
48                 document.getElementById('hold_usr_is_requestor_not').checked = 'checked';
49         }
50         if(barcode == undefined || barcode == '') {
51             document.getElementById('patron_name').innerHTML = '';
52             // No submitting on empty barcode, but empty barcode doesn't really count as "not found" either
53             document.getElementById('place_hold_submit').disabled = true;
54             document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
55             cur_hold_barcode = null;
56             return;
57         }
58         if(barcode == cur_hold_barcode)
59             return;
60         // No submitting until we think the barcode is valid
61         document.getElementById('place_hold_submit').disabled = true;
62         var load_info = xulG.get_barcode_and_settings(window, barcode, only_settings);
63         if(load_info == false || load_info == undefined) {
64             document.getElementById('patron_name').innerHTML = '';
65             document.getElementById("patron_usr_barcode_not_found").style.display = '';
66             cur_hold_barcode = null;
67             return;
68         }
69         cur_hold_barcode = load_info.barcode;
70         if(!only_settings || (isload && isload !== true)) document.getElementById('hold_usr_input').value = load_info.barcode; // Safe at this point as we already set cur_hold_barcode
71         if(load_info.settings['opac.default_pickup_location'])
72             document.getElementById('pickup_lib').value = load_info.settings['opac.default_pickup_location'];
73         if(!load_info.settings['opac.default_phone']) load_info.settings['opac.default_phone'] = '';
74         if(!load_info.settings['opac.default_sms_notify']) load_info.settings['opac.default_sms_notify'] = '';
75         if(!load_info.settings['opac.default_sms_carrier']) load_info.settings['opac.default_sms_carrier'] = '';
76         if(load_info.settings['opac.hold_notify'] || load_info.settings['opac.hold_notify'] === '') {
77             var email = load_info.settings['opac.hold_notify'].indexOf('email') > -1;
78             var phone = load_info.settings['opac.hold_notify'].indexOf('phone') > -1;
79             var sms = load_info.settings['opac.hold_notify'].indexOf('sms') > -1;
80             var update_elements = document.getElementsByName('email_notify');
81             for(var i in update_elements) update_elements[i].checked = (email ? 'checked' : '');
82             update_elements = document.getElementsByName('phone_notify_checkbox');
83             for(var i in update_elements) update_elements[i].checked = (phone ? 'checked' : '');
84             update_elements = document.getElementsByName('sms_notify_checkbox');
85             for(var i in update_elements) update_elements[i].checked = (sms ? 'checked' : '');
86         }
87         update_elements = document.getElementsByName('phone_notify');
88         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_phone'];
89         update_elements = document.getElementsByName('sms_notify');
90         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_notify'];
91         update_elements = document.getElementsByName('sms_carrier');
92         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_carrier'];
93         update_elements = document.getElementsByName('email_notify');
94         for(var i in update_elements) {
95             update_elements[i].disabled = (load_info.user_email ? false : true);
96             if(update_elements[i].disabled) update_elements[i].checked = false;
97         }
98         update_elements = document.getElementsByName('email_address');
99         for(var i in update_elements) update_elements[i].textContent = load_info.user_email;
100         if(!document.getElementById('hold_usr_is_requestor').checked && document.getElementById('hold_usr_input').value) {
101             document.getElementById('patron_name').innerHTML = load_info.patron_name;
102             document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
103         }
104         // Ok, now we can allow submitting again, unless this is a "true" load, in which case we likely have a blank barcode box active
105
106         // update the advanced hold options link to propagate the patron
107         // barcode if clicked.  This is needed when the patron barcode
108         // is manually entered (i.e. the staff client does not provide one).
109         var adv_link = document.getElementById('advanced_hold_link');
110         if (adv_link) { // not present on MR hold pages
111             var href = adv_link.getAttribute('href').replace(
112                 /;usr_barcode=[^;\&]+|$/, 
113                 ';usr_barcode=' + encodeURIComponent(cur_hold_barcode));
114             adv_link.setAttribute('href', href);
115         }
116
117         if (isload !== true)
118             document.getElementById('place_hold_submit').disabled = false;
119     }
120 }
121 window.onload = function() {
122     // record details page events
123     var rec = location.href.match(/\/opac\/record\/(\d+)/);
124     if(rec && rec[1]) { 
125         runEvt('rdetail', 'recordRetrieved', rec[1]); 
126         runEvt('rdetail', 'MFHDDrawn');
127     }
128     if(location.href.match(/place_hold/)) {
129         // patron barcode may come from XUL or a CGI param
130         var patron_barcode = xulG.patron_barcode ||
131             document.getElementById('hold_usr_input').value;
132         if(patron_barcode) {
133             staff_hold_usr_barcode_changed(patron_barcode);
134         } else {
135             staff_hold_usr_barcode_changed(true);
136         }
137     }
138 }
139
140 function rdetail_next_prev_actions(index, count, prev, next, start, end, results) {
141     /*  we mostly get the relative URL from the template:  recid?query_args...
142         replace the recid and args on location.href to get the new URL  */
143     function fullurl(url) {
144         if (url.match(/eg\/opac\/results/)) {
145             return location.href.replace(/\/eg\/opac\/.+$/, url);
146         } else {
147             return location.href.replace(/\/\d+\??.*/, '/' + url);
148         }
149     }
150
151     if (index > 0) {
152         if(prev) 
153             window.rdetailPrev = function() { location.href = fullurl(prev); }
154         if(start) 
155             window.rdetailStart = function() { location.href = fullurl(start); }
156     }
157
158     if (index < count - 1) {
159         if(next) 
160             window.rdetailNext = function() { location.href = fullurl(next); }
161         if(end) 
162             window.rdetailEnd = function() { location.href = fullurl(end); }
163     }
164
165     window.rdetailBackToResults = function() { location.href = fullurl(results); };
166
167     ol = window.onload;
168     window.onload = function() {
169         if(ol) ol(); 
170         runEvt('rdetail', 'nextPrevDrawn', Number(index), Number(count)); 
171     };
172 }