]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/copy_details.js
Merge branch 'master' of git://git.evergreen-ils.org/Evergreen into ttopac
[working/Evergreen.git] / Open-ILS / web / opac / skin / default / js / copy_details.js
1 var cpdTemplate;
2 var cpdCounter = 0;
3 var cpdNodes = {};
4
5 /* showDueDate will show the due date in the OPAC */
6 var showDueDate = false;
7 /* showDueTime will show the due time (hours and minutes) in the OPAC;
8    if showDueDate is false, then showDueTime has no effect
9 */
10 var showDueTime = false;
11
12 function cpdBuild( contextTbody, contextRow, record, callnumber, orgid, depth, copy_location, already_fetched_copies, peer_types ) {
13         var i = cpdCheckExisting(contextRow);
14         if(i) return i;
15
16         var counter = cpdCounter++;
17
18         /* yank out all of the template rows */
19         if(!cpdTemplate) cpdTemplate = $('rdetail_volume_details_row');
20         var templateRow = cpdTemplate.cloneNode(true);
21         templateRow.id = 'cpd_row_' + counter;
22
23         /* shove a dummy a tag in before the context previous sibling */
24         /*
25         contextTbody.insertBefore( 
26                 elem('a',{name:'slot_'+templateRow.id}), contextRow.previousSibling);
27         goTo('#slot_'+templateRow.id);
28         */
29
30         if(isXUL()) {
31                 /* unhide before we unhide/clone the parent */
32                 unHideMe($n(templateRow, 'age_protect_label'));
33                 unHideMe($n(templateRow, 'create_date_label'));
34         unHideMe($n(templateRow, 'active_date_label'));
35                 unHideMe($n(templateRow, 'holdable_label'));
36         }
37
38         if (isXUL() || showDueDate) {
39                 unHideMe($n(templateRow, 'due_date_label'));
40         }
41
42         unHideMe(templateRow);
43
44         var print = $n(templateRow,'print');
45         print.onclick = function() { cpdBuildPrintPane(
46                 contextRow, record, callnumber, orgid, depth) };
47     if (callnumber == null) {
48         addCSSClass(print,'hide_me');
49     }
50
51         var mainTbody = $n(templateRow, 'copies_tbody');
52         var extrasRow = mainTbody.removeChild($n(mainTbody, 'copy_extras_row'));
53
54     var request_args = {
55         peer_types      : peer_types, /* indexed the same as already_fetched_copies */
56         contextTbody    : contextTbody, /* tbody that holds the contextrow */
57         contextRow              : contextRow, /* the row our new row will be inserted after */
58         record                  : record,
59         callnumber              : callnumber, 
60         orgid                           : orgid,
61         depth                           : depth,
62         templateRow             : templateRow, /* contains everything */
63         copy_location           : copy_location,
64         mainTbody               : mainTbody, /* holds the copy rows */
65         extrasRow               : extrasRow, /* wrapper row for all extras */
66         counter                 : counter
67     }
68
69     if (! already_fetched_copies) {
70         var req = new Request(FETCH_COPIES_FROM_VOLUME, record.doc_id(), callnumber, orgid);
71         req.callback(cpdDrawCopies);
72
73         req.request.args = request_args;
74
75         req.send();
76     } else {
77         setTimeout(
78             function() {
79                 delete request_args['copy_location'];
80                 cpdDrawCopies({
81                     'args' : request_args,
82                     'getResultObject' : function() { return already_fetched_copies; }
83                 });
84             }, 0
85         );
86     }
87
88         if( contextRow.nextSibling ) 
89                 contextTbody.insertBefore( templateRow, contextRow.nextSibling );
90         else
91                 contextTbody.appendChild( templateRow );
92         _debug('creating new details row with id ' + templateRow.id);
93         cpdNodes[templateRow.id] = { templateRow : templateRow };
94         return templateRow.id;
95 }
96
97
98 function cpdBuildPrintWindow(record, orgid) {
99         var div = $('rdetail_print_details').cloneNode(true);
100         div.id = "";
101
102         $n(div, 'lib').appendChild(text(findOrgUnit(orgid).name()));
103         $n(div, 'title').appendChild(text(record.title()));
104         $n(div, 'author').appendChild(text(record.author()));
105         $n(div, 'edition').appendChild(text(record.edition()));
106         $n(div, 'pubdate').appendChild(text(record.pubdate()));
107         $n(div, 'publisher').appendChild(text(record.publisher()));
108         $n(div, 'phys').appendChild(text(record.physical_description()));
109
110         return div;
111 }
112
113 function cpdStylePopupWindow(div) {
114         var tds = div.getElementsByTagName('td');
115         for( var i = 0; i < tds.length ; i++ ) {
116                 var td = tds[i];
117                 var sty = td.getAttribute('style');
118                 if(!sty) sty = "";
119                 td.setAttribute('style', sty + 'padding: 2px; border: 1px solid #F0F0E0;');
120                 if( td.className && td.className.match(/hide_me/) ) 
121                         td.parentNode.removeChild(td);
122         }
123 }
124
125
126 /* builds a friendly print window for this CNs data */
127 function cpdBuildPrintPane(contextRow, record, cn, orgid, depth) {
128
129         var div = cpdBuildPrintWindow( record, orgid);
130
131     var whole_cn_text = (cn[0] ? cn[0] + ' ' : '') + cn[1] + (cn[2] ? ' ' + cn[2] : '');
132         $n(div, 'cn').appendChild(text(whole_cn_text));
133
134         unHideMe($n(div, 'copy_header'));
135
136         var subtbody = $n(contextRow.nextSibling, 'copies_tbody');
137         var rows = subtbody.getElementsByTagName('tr');
138
139         for( var r = 0; r < rows.length; r++ ) {
140                 var row = rows[r];
141                 if(!row) continue;
142                 var name = row.getAttribute('name');
143                 if( name.match(/extras_row/) ) continue; /* hide the copy notes, stat-cats */
144                 var clone = row.cloneNode(true);
145                 var links = clone.getElementsByTagName('a');
146                 for( var i = 0; i < links.length; i++ ) 
147                         links[i].style.display = 'none';
148
149                 $n(div, 'tbody').appendChild(clone);
150         }
151
152         cpdStylePopupWindow(div);
153         openWindow( div.innerHTML);
154 }
155
156
157
158 /* hide any open tables and if we've already 
159         fleshed this cn, just unhide it */
160 function cpdCheckExisting( contextRow ) {
161
162         var existingid;
163         var next = contextRow.nextSibling;
164
165         if( next && next.getAttribute('templateRow') ) {
166                 var obj = cpdNodes[next.id];
167                 if(obj.templateRow.className.match(/hide_me/)) 
168                         unHideMe(obj.templateRow);
169                 else hideMe(obj.templateRow);
170                 existingid = next.id;
171         }
172
173         if(existingid) _debug('row exists with id ' + existingid);
174
175         for( var o in cpdNodes ) {
176                 var node = cpdNodes[o];
177                 if( existingid && o == existingid ) continue;
178                 hideMe(node.templateRow);
179                 removeCSSClass(node.templateRow.previousSibling, 'rdetail_context_row');
180         }
181
182         addCSSClass(contextRow, 'rdetail_context_row');
183         if(existingid) return existingid;
184         return null;
185 }
186
187 /*
188 function cpdFetchCopies(r) {
189         var args = r.args;
190         args.cn = r.getResultObject();
191         var req = new Request(FETCH_COPIES_FROM_VOLUME, args.cn.id());
192         req.request.args = args;
193         req.callback(cpdDrawCopies);
194         req.send();
195 }
196 */
197
198 function cpdDrawCopies(r) {
199
200         var copies              = r.getResultObject();
201         var args                        = r.args;
202         var copytbody   = $n(args.templateRow, 'copies_tbody');
203         var copyrow             = copytbody.removeChild($n(copytbody, 'copies_row'));
204
205         if(isXUL()) {
206                 /* unhide before we unhide/clone the parent */
207                 unHideMe($n(copyrow, 'age_protect_value'));
208                 unHideMe($n(copyrow, 'create_date_value'));
209         unHideMe($n(copyrow, 'active_date_value'));
210                 unHideMe($n(copyrow, 'copy_holdable_td'));
211         }
212
213         if(isXUL() || showDueDate) {
214                 unHideMe($n(copyrow, 'copy_due_date_td'));
215         }
216
217         for( var i = 0; i < copies.length; i++ ) {
218                 var row = copyrow.cloneNode(true);
219                 var copyid = copies[i];
220         var pt; if (args.peer_types) pt = args.peer_types[i];
221         if (typeof copyid != 'object') {
222             var req = new Request(FETCH_FLESHED_COPY, copyid);
223             req.callback(cpdDrawCopy);
224             req.request.args = r.args;
225             req.request.row = row;
226             req.send();
227         } else {
228             setTimeout(
229                 function(copy,row,pt) {
230                     return function() {
231                         cpdDrawCopy({
232                             'getResultObject' : function() { return copy; },
233                             'args' : r.args,
234                             'peer_type' : pt,
235                             'row' : row
236                         });
237                     };
238                 }(copies[i],row,pt), 0
239             );
240         }
241                 copytbody.appendChild(row);
242         }
243 }
244
245 function cpdDrawCopy(r) {
246         var copy = r.getResultObject();
247         var row  = r.row;
248         var pt   = r.peer_type;
249     var trow = r.args.templateRow;
250
251     if (r.args.copy_location && copy.location().name() != r.args.copy_location) {
252         hideMe(row);
253         return;
254     }
255
256     // Make barcode more useful for staff client usage
257     if(isXUL()) {
258         var my_a = document.createElement('a');
259         my_a.appendChild(text(copy.barcode()));
260         my_a.setAttribute("href","javascript:void(0);");
261         my_a.onclick = function() {
262             xulG.new_tab(xulG.urls.XUL_COPY_STATUS, {}, {'from_item_details_new': true, 'barcodes': [copy.barcode()]});
263                 };
264         $n(row, 'barcode').appendChild(my_a);
265     }
266     else {
267         $n(row, 'barcode').appendChild(text(copy.barcode()));
268     }
269
270     /* show the peer type*/
271     if (pt) {
272         $n(row, 'barcode').appendChild(text(' :: ' + pt));
273     }
274
275         $n(row, 'location').appendChild(text(copy.location().name()));
276         $n(row, 'status').appendChild(text(copy.status().name()));
277
278     // append comma-separated list of part this copy is linked to
279     if(copy.parts() && copy.parts().length) {
280         unHideMe($n(trow, 'copy_part_label'));
281         unHideMe($n(row, 'copy_part'));
282         for(var i = 0; i < copy.parts().length; i++) {
283             var part = copy.parts()[i];
284             var node = $n(row, 'copy_part');
285             if(i > 0) node.appendChild(text(','));
286             node.appendChild(text(part.label()));
287         }
288     }
289
290     /* show the other bibs link */
291     if (copy.peer_record_maps().length > 0) {
292         var l = $n(row, 'copy_multi_home');
293         unHideMe(l);
294         var link_args = {};
295         link_args.page = RRESULT;
296         link_args[PARAM_RTYPE] = RTYPE_LIST;
297         link_args[PARAM_RLIST] = new Array();
298         for (var i = 0; i < copy.peer_record_maps().length; i++) {
299             link_args[PARAM_RLIST].push( copy.peer_record_maps()[i].peer_record() );
300         }
301         l.setAttribute('href',buildOPACLink(link_args));
302     }
303
304         if(isXUL()) {
305                 /* show the hold link */
306                 var l = $n(row, 'copy_hold_link');
307                 unHideMe(l);
308                 l.onclick = function() {
309                         holdsDrawEditor( 
310                                 { 
311                                         type                    : 'C',
312                                         copyObject      : copy,
313                                         onComplete      : function(){}
314                                 }
315                         );
316                 }
317
318                 /* show the book now link */
319                 l = $n(row, 'copy_reserve_link');
320                 unHideMe(l);
321                 l.onclick = function() {
322                         // XXX FIXME this duplicates things in cat/util.js
323                         // Also needs i18n
324                         dojo.require("fieldmapper.Fieldmapper");
325                         var r = fieldmapper.standardRequest(
326                                 ["open-ils.booking",
327                                         "open-ils.booking.resources.create_from_copies"],
328                                 [G.user.session, [copy.id()]]
329                         );
330                         if (!r) {
331                                 alert("No response from server!");
332                         } else if (r.ilsevent != undefined) {
333                                 alert("Error from server:\n" + js2JSON(r));
334                         } else {
335                                 xulG.auth = {"session": {"key": G.user.session}};
336                                 xulG.bresv_interface_opts = {"booking_results": r};
337                                 location.href = "/eg/booking/reservation";
338                         }
339                 }
340
341                 if( copy.age_protect() ) 
342                         appendClear($n(row, 'age_protect_value'), text(copy.age_protect().name()));
343
344                 var cd = copy.create_date();
345                 cd = cd.replace(/T.*/, '');
346                 $n(row, 'create_date_value').appendChild(text(cd));
347
348         var ad = copy.active_date();
349         if(ad) {
350             ad = ad.replace(/T.*/, '');
351             $n(row, 'active_date_value').appendChild(text(ad));
352         }
353
354                 var yes = $('rdetail.yes').innerHTML;
355                 var no = $('rdetail.no').innerHTML;
356
357                 if( isTrue(copy.holdable()) &&
358                                 isTrue(copy.location().holdable()) &&
359                                 isTrue(copy.status().holdable()) ) {
360                         $n(row, 'copy_is_holdable').appendChild(text(yes));     
361                 } else {
362                         $n(row, 'copy_is_holdable').appendChild(text(no));      
363                 }
364         }
365
366         if (isXUL() || showDueDate) {
367                 var circ;
368                 if( copy.circulations() ) {
369                         circ = copy.circulations()[0];
370                         if( circ ) {
371                                 var due_time = dojo.date.stamp.fromISOString(circ.due_date());
372                                 if( showDueTime ) {
373                                         $n(row, 'copy_due_date').appendChild(text(dojo.date.locale.format(due_time, {"formatLength": "medium"})));
374                                 } else {
375                                         $n(row, 'copy_due_date').appendChild(text(dojo.date.locale.format(due_time, {"selector": "date", "formatLength": "medium"})));
376                                 }
377                         }
378                 }
379         }
380
381         r.args.copy = copy;
382         r.args.copyrow = row;
383         cpdShowNotes(copy, r.args)
384         cpdShowStats(copy, r.args);
385
386 }
387
388 function _cpdExtrasInit(args) {
389
390         var newrid      = 'extras_row_' + args.copy.barcode();
391         var newrow      = $(newrid);
392         if(!newrow) newrow = args.extrasRow.cloneNode(true);
393         var tbody       = $n(newrow, 'extras_tbody');
394         var rowt                = $n(tbody, 'extras_row');
395         newrow.id       = newrid;
396
397         var cr = args.copyrow;
398         var nr = cr.nextSibling;
399         var np = args.mainTbody;
400
401         /* insert the extras row into the main table */
402         if(nr) np.insertBefore( newrow, nr );
403         else np.appendChild(newrow);
404
405         var link = $n(args.copyrow, 'details_link');
406         var link2 = $n(args.copyrow, 'less_details_link');
407         var id = newrow.id;
408         link.id = id + '_morelink';
409         link2.id = id + '_lesslink';
410         unHideMe(link);
411         hideMe(link2);
412
413         link.setAttribute('href', 
414                         'javascript:unHideMe($("'+link2.id+'")); hideMe($("'+link.id+'"));unHideMe($("'+newrow.id+'"));');
415
416         link2.setAttribute('href', 
417                         'javascript:unHideMe($("'+link.id+'")); hideMe($("'+link2.id+'"));hideMe($("'+newrow.id+'"));');
418
419         return [ tbody, rowt ];
420 }
421
422 function cpdShowNotes(copy, args) {
423         var notes = copy.notes();
424         if(!notes || notes.length == 0) return;
425
426         var a = _cpdExtrasInit(args);
427         var tbody = a[0];
428         var rowt = a[1];
429
430         for( var n in notes ) {
431                 var note = notes[n];
432                 if(!isTrue(note.pub())) continue;
433                 var row = rowt.cloneNode(true);
434                 $n(row, 'key').appendChild(text(note.title()));
435                 $n(row, 'value').appendChild(text(note.value()));
436                 unHideMe($n(row, 'note'));
437                 unHideMe(row);
438                 tbody.appendChild(row);
439         }
440 }
441
442
443 function cpdShowStats(copy, args) {
444         var entries = copy.stat_cat_entry_copy_maps();
445         if(!entries || entries.length == 0) return;
446
447         var visibleStatCat = false;
448
449         /*
450                 check all copy stat cats; if we find one that's OPAC visible,
451                 set the flag and break the loop. If we've found one, or we're
452                 in the staff client, build the table. if not, we return doing
453                 nothing, as though the stat_cat_entry_copy_map was empty or null
454         */
455
456         for( var n in entries )
457         {
458                         var entry = entries[n];
459                         if(isTrue(entry.stat_cat().opac_visible()))
460                         {
461                                 visibleStatCat = true;
462                                 break;
463                         }
464         }
465
466         if(!(isXUL() || visibleStatCat)) return;
467
468         var a = _cpdExtrasInit(args);
469         var tbody = a[0];
470         var rowt = a[1];
471
472         for( var n in entries ) {
473                 var entry = entries[n];
474                 if(!(isXUL() || isTrue(entry.stat_cat().opac_visible()))) continue;
475                 var row = rowt.cloneNode(true);
476                 $n(row, 'key').appendChild(text(entry.stat_cat().name()));
477                 $n(row, 'value').appendChild(text(entry.stat_cat_entry().value()));
478                 unHideMe($n(row, 'cat'));
479                 unHideMe(row);
480                 tbody.appendChild(row);
481         }
482 }
483