]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/opac/staff.js
LP#1350042 Browser client templates/scripts (phase 1)
[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 cur_hold_barcode = undefined;
47     var barcode = isload;
48     if(!barcode || barcode === true) barcode = document.getElementById('staff_barcode').value;
49     var only_settings = true;
50     if(!document.getElementById('hold_usr_is_requestor').checked) {
51         if(!isload) {
52             barcode = document.getElementById('hold_usr_input').value;
53             only_settings = false;
54         }
55         if(barcode && barcode != '' && !document.getElementById('hold_usr_is_requestor_not').checked)
56             document.getElementById('hold_usr_is_requestor_not').checked = 'checked';
57     }
58     if(barcode == undefined || barcode == '') {
59         document.getElementById('patron_name').innerHTML = '';
60         // No submitting on empty barcode, but empty barcode doesn't really count as "not found" either
61         document.getElementById('place_hold_submit').disabled = true;
62         document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
63         cur_hold_barcode = null;
64         return;
65     }
66     if(barcode == cur_hold_barcode)
67         return;
68     // No submitting until we think the barcode is valid
69     document.getElementById('place_hold_submit').disabled = true;
70
71     if (window.IAMBROWSER) {
72         // Browser client operates asynchronously
73         if (!xulG.get_barcode_and_settings_async) return;
74         xulG.get_barcode_and_settings_async(barcode, only_settings)
75         .then(
76             function(load_info) { // load succeeded
77                 staff_hold_usr_barcode_changed2(
78                     isload, only_settings, barcode, cur_hold_barcode, load_info);
79             },
80             function() { 
81                 // load failed (rejected).  Call staff_hold_usr_barcode_changed2
82                 // anyway, since it handles clearing the form
83                 staff_hold_usr_barcode_changed2(
84                     isload, only_settings, barcode, cur_hold_barcode, false);
85             }
86         )
87     } else {
88         // XUL version is synchronous
89         if (!xulG.get_barcode_and_settings) return;
90         var load_info = xulG.get_barcode_and_settings(window, barcode, only_settings);
91         staff_hold_usr_barcode_changed2(isload, only_settings, barcode, cur_hold_barcode, load_info);
92     }
93 }
94
95 function staff_hold_usr_barcode_changed2(
96     isload, only_settings, barcode, cur_hold_barcode, load_info) {
97
98     if(load_info == false || load_info == undefined) {
99         document.getElementById('patron_name').innerHTML = '';
100         document.getElementById("patron_usr_barcode_not_found").style.display = '';
101         cur_hold_barcode = null;
102         return;
103     }
104     cur_hold_barcode = load_info.barcode;
105     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
106     if(load_info.settings['opac.default_pickup_location'])
107         document.getElementById('pickup_lib').value = load_info.settings['opac.default_pickup_location'];
108     if(!load_info.settings['opac.default_phone']) load_info.settings['opac.default_phone'] = '';
109     if(!load_info.settings['opac.default_sms_notify']) load_info.settings['opac.default_sms_notify'] = '';
110     if(!load_info.settings['opac.default_sms_carrier']) load_info.settings['opac.default_sms_carrier'] = '';
111     if(load_info.settings['opac.hold_notify'] || load_info.settings['opac.hold_notify'] === '') {
112         var email = load_info.settings['opac.hold_notify'].indexOf('email') > -1;
113         var phone = load_info.settings['opac.hold_notify'].indexOf('phone') > -1;
114         var sms = load_info.settings['opac.hold_notify'].indexOf('sms') > -1;
115         var update_elements = document.getElementsByName('email_notify');
116         for(var i in update_elements) update_elements[i].checked = (email ? 'checked' : '');
117         update_elements = document.getElementsByName('phone_notify_checkbox');
118         for(var i in update_elements) update_elements[i].checked = (phone ? 'checked' : '');
119         update_elements = document.getElementsByName('sms_notify_checkbox');
120         for(var i in update_elements) update_elements[i].checked = (sms ? 'checked' : '');
121     }
122     update_elements = document.getElementsByName('phone_notify');
123     for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_phone'];
124     update_elements = document.getElementsByName('sms_notify');
125     for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_notify'];
126     update_elements = document.getElementsByName('sms_carrier');
127     for(var i in update_elements) update_elements[i].value = load_info.settings['opac.default_sms_carrier'];
128     update_elements = document.getElementsByName('email_notify');
129     for(var i in update_elements) {
130         update_elements[i].disabled = (load_info.user_email ? false : true);
131         if(update_elements[i].disabled) update_elements[i].checked = false;
132     }
133     update_elements = document.getElementsByName('email_address');
134     for(var i in update_elements) update_elements[i].textContent = load_info.user_email;
135     if(!document.getElementById('hold_usr_is_requestor').checked && document.getElementById('hold_usr_input').value) {
136         document.getElementById('patron_name').innerHTML = load_info.patron_name;
137         document.getElementById("patron_usr_barcode_not_found").style.display = 'none';
138     }
139     // Ok, now we can allow submitting again, unless this is a "true" load, in which case we likely have a blank barcode box active
140
141     // update the advanced hold options link to propagate the patron
142     // barcode if clicked.  This is needed when the patron barcode
143     // is manually entered (i.e. the staff client does not provide one).
144     var adv_link = document.getElementById('advanced_hold_link');
145     if (adv_link) { // not present on MR hold pages
146         var href = adv_link.getAttribute('href').replace(
147             /;usr_barcode=[^;\&]+|$/, 
148             ';usr_barcode=' + encodeURIComponent(cur_hold_barcode));
149         adv_link.setAttribute('href', href);
150     }
151
152     if (isload !== true)
153         document.getElementById('place_hold_submit').disabled = false;
154 }
155 window.onload = function() {
156     // record details page events
157
158     setTimeout(function() {
159         var rec = location.href.match(/\/opac\/record\/(\d+)/);
160         if(rec && rec[1]) { 
161             runEvt('rdetail', 'recordRetrieved', rec[1]); 
162             runEvt('rdetail', 'MFHDDrawn');
163         }
164         if(location.href.match(/place_hold/)) {
165             // patron barcode may come from XUL or a CGI param
166             var patron_barcode = xulG.patron_barcode ||
167                 document.getElementById('hold_usr_input').value;
168             if(patron_barcode) {
169                 staff_hold_usr_barcode_changed(patron_barcode);
170             } else {
171                 staff_hold_usr_barcode_changed(true);
172             }
173         }
174     });
175 }
176
177 function rdetail_next_prev_actions(index, count, prev, next, start, end, results) {
178     /*  we mostly get the relative URL from the template:  recid?query_args...
179         replace the recid and args on location.href to get the new URL  */
180     function fullurl(url) {
181         if (url.match(/eg\/opac\/results/)) {
182             return location.href.replace(/\/eg\/opac\/.+$/, url);
183         } else {
184             return location.href.replace(/\/\d+\??.*/, '/' + url);
185         }
186     }
187
188     if (index > 0) {
189         if(prev) 
190             window.rdetailPrev = function() { location.href = fullurl(prev); }
191         if(start) 
192             window.rdetailStart = function() { location.href = fullurl(start); }
193     }
194
195     if (index < count - 1) {
196         if(next) 
197             window.rdetailNext = function() { location.href = fullurl(next); }
198         if(end) 
199             window.rdetailEnd = function() { location.href = fullurl(end); }
200     }
201
202     window.rdetailBackToResults = function() { location.href = fullurl(results); };
203
204     ol = window.onload;
205     window.onload = function() {
206         if(ol) ol(); 
207         setTimeout(function() {
208             runEvt('rdetail', 'nextPrevDrawn', Number(index), Number(count)); 
209         });
210     };
211 }