]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/opac/HoldsWindow.js
our little opac is growing up
[Evergreen.git] / Open-ILS / src / javascript / opac / HoldsWindow.js
1
2 /* 
3         @param record the id of the target item 
4         @param type is the hold level (M, T, V, C) for metarecord, title,
5                 volume, and copy respectively.
6         @param requestor is the user object (fieldmapper) for the requestor
7         @param recipient is the user object (fieldmapper) for the hold recipient
8                 role in the holds process 
9         @param requestor_login is the login session of the hold requestor
10         */
11 function HoldsWindow(record, type, requestor, recipient, requestor_login) {
12
13         this.record = record;
14         this.div = elem("div");
15         this.requestor = requestor;
16         this.recipient  = recipient;
17         this.type = type;
18         this.session = requestor_login;
19         this.built = false;
20
21         add_css_class(this.div, "holds_window");
22         add_css_class(this.div, "hide_me");
23         getDocument().body.appendChild(this.div);
24 }
25
26
27 HoldsWindow.prototype.process = function() {
28
29         /* collect the checked items */
30         var formats = "";
31         if(this.type == "M") {
32                 for(var idx in resourceFormats) {
33                         var form = resourceFormats[idx];
34                         var item = getById( form + "_hold_checkbox");
35                         if(item.checked) {
36                                 formats += modsFormatToMARC(form);
37                         }
38                 }
39                 
40                 if(formats.length == 0) {
41                         alert("Please select at least one item format");
42                         this.toggle();
43                 }
44         } 
45
46         var sel = getById("holds_org_selector");
47         var orgNode = sel.options[sel.selectedIndex];
48         var org = findOrgUnit(orgNode.value);
49
50         /* for now... */
51         var email = this.recipient.email();
52         var phone = this.recipient.day_phone();
53
54         this.sendHoldsRequest(formats, org, email, phone);
55 }
56
57 /* formats is a string of format characters, org is the 
58         org unit used for delivery */
59 HoldsWindow.prototype.sendHoldsRequest = function(formats, org, email, phone) {
60
61         if(isXUL()) { /* staff member entered barcode, now fetch the user */
62                 var recip_barcode = getById("recipient_barcode").value;
63                 if(!recip_barcode || recip_barcode == "") {
64                         alert("Please enter the user's barcode");
65                 }
66                 try {
67                         this.recipient = grabUserByBarcode(recip_barcode);
68                 } catch(E) { alert(E.err_msg()); }
69         }
70
71         var hold = new ahr();
72         hold.pickup_lib(org.id());
73         hold.requestor(this.requestor.id());
74         hold.usr(this.recipient.id());
75         hold.hold_type(this.type);
76         hold.email_notify(email);
77         hold.phone_notify(phone);
78
79         if(this.type == "M") hold.holdable_formats(formats);
80         hold.target(this.record);
81
82         var req = new RemoteRequest(
83                 "open-ils.circ",
84                 "open-ils.circ.holds.create",
85                 this.session, hold );
86
87         req.send(true);
88         var res = req.getResultObject();
89         if(res == 1) 
90                 alert("Hold request was succesfully submitted");
91
92 }
93
94 /* returns false if we don't have a recipient for the hold */
95 HoldsWindow.prototype.buildWindow = function(node) {
96
97         if(this.built) return true;
98         var d = elem("div");
99         var id = this.record;
100
101         var usr = this.recipient;
102         var barcodebox = null;
103
104         if(isXUL()) {
105                 /* used by xul to enter the recipient barcode */
106                 barcodebox = elem("input",{type:"text",id:"recipient_barcode"});
107         } else {
108
109                 if(!usr) {
110                         var obj = this;
111                         var func = function(usr) { 
112                                 obj.recipient = usr.userObject;
113                                 obj.requestor = usr.userObject;
114                                 obj.session = usr.session_id;
115                                 obj.toggle();
116                         }
117                         var diag = new LoginDialog(func);
118                         diag.display(node);
119                         return false;
120                 }
121         }
122
123
124
125         var org = usr.home_ou();
126         d.appendChild(this.buildPickuplibSelector(org));
127
128         if(this.type == "M")
129                 d.appendChild(this.buildResourceSelector());
130
131         if(barcodebox)
132                 d.appendChild(barcodebox);
133
134         d.appendChild(this.buildSubmit());
135
136         this.div.appendChild(d);
137         this.built = true;
138         return true;
139 }
140
141 HoldsWindow.prototype.toggle = function(node) {
142
143         debug("Building window with node " + node);
144         if(!this.built) 
145                 if(!this.buildWindow(node)) return;
146
147         swapClass( this.div, "hide_me", "show_me" );
148
149
150         /* workaround for IE select box widget bleed through, blegh... */
151         if(IE) {
152
153                 var sels = getDocument().getElementsByTagName("select");
154                 if(sels.length == 0) return true;
155
156                 if(this.div.className.indexOf("hide_me") != -1)  {
157                         for(var i = 0; i!= sels.length; i++) {
158                                 var s = sels[i];
159                                 if(s && s.id != "holds_org_selector") {
160                                         remove_css_class(s, "invisible");
161                                         add_css_class(s, "visible");
162                                 }
163                         }
164                 }
165         
166                 if(this.div.className.indexOf("show_me") != -1)  {
167                         for(var i = 0; i!= sels.length; i++) {
168                                 var s = sels[i];
169                                 if(s && s.id != "holds_org_selector") {
170                                         remove_css_class(s, "visible");
171                                         add_css_class(s, "invisible");
172                                 }
173                         }
174                 }
175         }
176
177         return true;
178 }
179
180
181
182 /*
183 HoldsWindow.prototype.buildHoldsWindowCallback = function(type) {
184
185         var hwindow = this;
186         var func = function() {
187
188                 var wrapper = elem("div");
189                 var id = hwindow.record.doc_id();
190
191                 var org = UserSession.instance().userObject.home_ou();
192                 wrapper.appendChild(hwindow.buildPickuplibSelector(org));
193                 if(type == "M")
194                         wrapper.appendChild(hwindow.buildResourceSelector());
195                 wrapper.appendChild(hwindow.buildSubmit());
196
197                 hwindow.win = window.open(null,"PLACE_HOLD_" + id,
198                         "location=0,menubar=0,status=0,resizeable,resize," +
199                         "outerHeight=500,outerWidth=500,height=500," +
200                         "width=500,scrollbars=1," +
201                         "alwaysraised, chrome" )
202         
203                 hwindow.win.document.write("<html>" + wrapper.innerHTML + "</html>");
204                 hwindow.win.document.close();
205                 hwindow.win.document.title = "View MARC";
206                 hwindow.win.focus();
207         }
208
209         return func;
210 }
211 */
212
213 HoldsWindow.prototype.buildSubmit = function() {
214         var div = elem("div");
215
216
217         /*
218         var bdiv = elem("div",  
219                         {style: 
220                                 "border-top: 1px solid lightgrey;" +
221                                 "border-bottom: 1px solid lightgrey;" +
222                                 "width:100%;text-align:center;"});
223                                 */
224
225         var bdiv = elem("div");  
226         add_css_class(bdiv, "holds_window_buttons");
227
228         var button = elem("input", 
229                 {type:"submit", style:"margin-right: 10px;", value:"Place Hold"});
230
231         var cancel = elem("input", 
232                 {type:"submit", style:"margin-left: 10px;",value:"Cancel"});
233         var obj = this;
234
235         cancel.onclick = function() { obj.toggle(); }
236         button.onclick = function() { obj.toggle(); obj.process(); }
237
238         div.appendChild(elem("br"));
239         bdiv.appendChild(button);
240         bdiv.appendChild(cancel);
241         div.appendChild(bdiv);
242
243         return div;
244 }
245
246 /* builds a selecor where the client can select the location to which
247         the item is sent */
248 HoldsWindow.prototype.buildPickuplibSelector = function(selected_id) {
249
250         var div = elem("div");
251         var tdiv = elem("div",{style:"margin-left:10px"}, null,
252                 "1. Select the location where the item(s) shall be delivered");
253
254         var sdiv = elem("div");
255         var selector = elem("select",{id:"holds_org_selector"});
256
257         /* this is not copied over... XXX fix me */
258         selector.onchange = function() {
259                 var idx = selector.selectedIndex;
260                 var option = selector.options[idx];
261                 var org = findOrgUnit(option.value);
262
263                 var d = getById("selector_error_div");
264                 if(d) div.removeChild(d);
265
266                 if(parseInt(findOrgType(org.ou_type()).depth()) < 2) {
267                         var err = elem("div",
268                                 {id:"selector_error_div", style:"color:red"},null, 
269                                 org.name() + " is a library system, please select a single branch");
270                         div.appendChild(err);
271                 } else {
272                 }
273         }
274
275         var center = elem("center");
276         center.appendChild(selector);
277         sdiv.appendChild(center);
278         _buildOrgList(selector, selected_id, null);
279
280         div.appendChild(elem("br"));
281         div.appendChild(tdiv);
282         div.appendChild(elem("br"));
283         div.appendChild(sdiv);
284         return div;
285 }
286
287 /* utility function for building a org list selector object */
288 function _buildOrgList(selector, selected_id, org) {
289
290         if(selected_id == null) selected_id = -1;
291
292         if(org == null) {
293                 org = globalOrgTree;
294
295         } else { /* add the org to the list */
296                 var index = selector.options.length;
297                 if(IE) {
298                         var node = elem("pre");
299                         for(var x=2; x <= findOrgType(org.ou_type()).depth(); x++) {
300                                 node.appendChild(mktext("    "));
301                         }
302                         node.appendChild(mktext(org.name()));
303                         var select = new Option("", org.id());
304                         selector.options[index] = select;
305                         select.appendChild(node);
306         
307                 } else {
308                         var pad = (findOrgType(org.ou_type()).depth() - 1) * 12;
309                         var select = new Option(org.name(), org.id());
310                         select.setAttribute("style", "padding-left: " + pad);
311                         selector.options[index] = select;
312                 }
313
314                 if(parseInt(org.id()) == parseInt(selected_id)) {
315                         selector.selectedIndex = index;
316                         selector.options[index].selected = true;
317                 }
318         }
319
320         for(var idx in org.children()) 
321                 _buildOrgList(selector, selected_id, org.children()[idx]);
322 }
323
324 HoldsWindow.prototype.buildResourceSelector = function() {
325
326         /* useful message */
327         var big_div = elem('div', {style:"margin-left: 10px;"});
328
329         var desc_div = elem("div",null, null, 
330                 "2. Select all acceptible item formats");
331
332         
333         var table = elem("table");      
334
335         for( var idx in resourceFormats ) {
336                 var row = table.insertRow(table.rows.length)
337         
338                 var pic_cell = row.insertCell(0);
339                 var name_cell = row.insertCell(1);
340                 var box_cell = row.insertCell(2);
341                 var box = elem("input", 
342                         {type:"checkbox", id: resourceFormats[idx] + "_hold_checkbox"}, null);
343
344                 if(idx == 0) { /* select text by default */
345                         box.setAttribute("checked","checked");
346                         box.checked = true;
347                 }
348                 
349                 pic_cell.appendChild(mkResourceImage(resourceFormats[idx]));
350                 name_cell.appendChild(mktext(resourceFormats[idx]));
351                 box_cell.appendChild(mktext(" "));
352                 box_cell.appendChild(box);
353         }
354
355         big_div.appendChild(elem("br"));
356         big_div.appendChild(desc_div);
357         big_div.appendChild(elem("br"));
358         big_div.appendChild(table);
359         return big_div;
360
361 }
362
363
364
365