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