]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/staff.js
8bae62f732af1578feacdfbb5c50ef7f7d758b6d
[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         var bc_from_cgi = false; // user_barcode passed via cgi
43         if(!document.getElementById('hold_usr_is_requestor').checked) {
44             if (isload && document.getElementById('hold_usr_input').value)
45                 bc_from_cgi = true;
46             if(!isload || bc_from_cgi) {
47                 barcode = document.getElementById('hold_usr_input').value;
48                 only_settings = false;
49             }
50             if(barcode && barcode != '' && !document.getElementById('hold_usr_is_requestor_not').checked)
51                 document.getElementById('hold_usr_is_requestor_not').checked = 'checked';
52         }
53         if(barcode == undefined || barcode == '') {
54             document.getElementById('patron_name').innerHTML = '';
55             // No submitting on empty barcode, but empty barcode doesn't really count as "not found" either
56             document.getElementById('place_hold_submit').disabled = true;
57             document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
58             cur_hold_barcode = null;
59             return;
60         }
61         if(barcode == cur_hold_barcode)
62             return;
63         // No submitting until we think the barcode is valid
64         document.getElementById('place_hold_submit').disabled = true;
65         var load_info = xulG.get_barcode_and_settings(window, barcode, only_settings);
66         if(load_info == false || load_info == undefined) {
67             document.getElementById('patron_name').innerHTML = '';
68             document.getElementById("patron_usr_barcode_not_found").style.display = '';
69             cur_hold_barcode = null;
70             return;
71         }
72         cur_hold_barcode = load_info.barcode;
73         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
74         if(load_info.settings['opac.default_pickup_location'])
75             document.getElementById('pickup_lib').value = load_info.settings['opac.default_pickup_location'];
76         if(!load_info.settings['opac.default_phone']) load_info.settings['opac.default_phone'] = '';
77         if(!load_info.settings['opac.default_sms_notify']) load_info.settings['opac.default_sms_notify'] = '';
78         if(!load_info.settings['opac.default_sms_carrier']) load_info.settings['opac.default_sms_carrier'] = '';
79         if(load_info.settings['opac.hold_notify'] || load_info.settings['opac.hold_notify'] === '') {
80             var email = load_info.settings['opac.hold_notify'].indexOf('email') > -1;
81             var phone = load_info.settings['opac.hold_notify'].indexOf('phone') > -1;
82             var sms = load_info.settings['opac.hold_notify'].indexOf('sms') > -1;
83             var update_elements = document.getElementsByName('email_notify');
84             for(var i in update_elements) update_elements[i].checked = (email ? 'checked' : '');
85             update_elements = document.getElementsByName('phone_notify_checkbox');
86             for(var i in update_elements) update_elements[i].checked = (phone ? 'checked' : '');
87             update_elements = document.getElementsByName('sms_notify_checkbox');
88             for(var i in update_elements) update_elements[i].checked = (sms ? 'checked' : '');
89         }
90         update_elements = document.getElementsByName('phone_notify');
91         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_phone'];
92         update_elements = document.getElementsByName('sms_notify');
93         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_notify'];
94         update_elements = document.getElementsByName('sms_carrier');
95         for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_carrier'];
96         update_elements = document.getElementsByName('email_notify');
97         for(var i in update_elements) {
98             update_elements[i].disabled = (load_info.user_email ? false : true);
99             if(update_elements[i].disabled) update_elements[i].checked = false;
100         }
101         update_elements = document.getElementsByName('email_address');
102         for(var i in update_elements) update_elements[i].textContent = load_info.user_email;
103         if(!document.getElementById('hold_usr_is_requestor').checked && document.getElementById('hold_usr_input').value) {
104             document.getElementById('patron_name').innerHTML = load_info.patron_name;
105             document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
106         }
107         
108         // update the advanced hold options link to propagate the patron
109         // barcode if clicked.  This is needed when the patron barcode
110         // is manually entered (i.e. the staff client does not provide one).
111         var adv_link = document.getElementById('advanced_hold_link');
112         if (adv_link) { // not present on MR hold pages
113             var href = adv_link.getAttribute('href').replace(
114                 /;usr_barcode=[^;\&]+|$/, 
115                 ';usr_barcode=' + encodeURIComponent(cur_hold_barcode));
116             adv_link.setAttribute('href', href);
117         }
118
119         // if we're here, we have a valid barcode.  activate the sumbmit option
120         if(!isload || bc_from_cgi) 
121             document.getElementById('place_hold_submit').disabled = false;
122     }
123 }
124 window.onload = function() {
125     // record details page events
126     var rec = location.href.match(/\/opac\/record\/(\d+)/);
127     if(rec && rec[1]) { 
128         runEvt('rdetail', 'recordRetrieved', rec[1]); 
129         runEvt('rdetail', 'MFHDDrawn');
130     }
131     if(location.href.match(/place_hold/)) {
132         if(xulG.patron_barcode) {
133             staff_hold_usr_barcode_changed(xulG.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 }