]> 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, '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 (callnumber == null) {
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, cn, orgid, depth) {
127
128         var div = cpdBuildPrintWindow( record, orgid);
129
130     var whole_cn_text = (cn[0] ? cn[0] + ' ' : '') + cn[1] + (cn[2] ? ' ' + cn[2] : '');
131         $n(div, 'cn').appendChild(text(whole_cn_text));
132
133         unHideMe($n(div, 'copy_header'));
134
135         var subtbody = $n(contextRow.nextSibling, 'copies_tbody');
136         var rows = subtbody.getElementsByTagName('tr');
137
138         for( var r = 0; r < rows.length; r++ ) {
139                 var row = rows[r];
140                 if(!row) continue;
141                 var name = row.getAttribute('name');
142                 if( name.match(/extras_row/) ) continue; /* hide the copy notes, stat-cats */
143                 var clone = row.cloneNode(true);
144                 var links = clone.getElementsByTagName('a');
145                 for( var i = 0; i < links.length; i++ ) 
146                         links[i].style.display = 'none';
147
148                 $n(div, 'tbody').appendChild(clone);
149         }
150
151         cpdStylePopupWindow(div);
152         openWindow( div.innerHTML);
153 }
154
155
156
157 /* hide any open tables and if we've already 
158         fleshed this cn, just unhide it */
159 function cpdCheckExisting( contextRow ) {
160
161         var existingid;
162         var next = contextRow.nextSibling;
163
164         if( next && next.getAttribute('templateRow') ) {
165                 var obj = cpdNodes[next.id];
166                 if(obj.templateRow.className.match(/hide_me/)) 
167                         unHideMe(obj.templateRow);
168                 else hideMe(obj.templateRow);
169                 existingid = next.id;
170         }
171
172         if(existingid) _debug('row exists with id ' + existingid);
173
174         for( var o in cpdNodes ) {
175                 var node = cpdNodes[o];
176                 if( existingid && o == existingid ) continue;
177                 hideMe(node.templateRow);
178                 removeCSSClass(node.templateRow.previousSibling, 'rdetail_context_row');
179         }
180
181         addCSSClass(contextRow, 'rdetail_context_row');
182         if(existingid) return existingid;
183         return null;
184 }
185
186 /*
187 function cpdFetchCopies(r) {
188         var args = r.args;
189         args.cn = r.getResultObject();
190         var req = new Request(FETCH_COPIES_FROM_VOLUME, args.cn.id());
191         req.request.args = args;
192         req.callback(cpdDrawCopies);
193         req.send();
194 }
195 */
196
197 function cpdDrawCopies(r) {
198
199         var copies              = r.getResultObject();
200         var args                        = r.args;
201         var copytbody   = $n(args.templateRow, 'copies_tbody');
202         var copyrow             = copytbody.removeChild($n(copytbody, 'copies_row'));
203
204         if(isXUL()) {
205                 /* unhide before we unhide/clone the parent */
206                 unHideMe($n(copyrow, 'age_protect_value'));
207                 unHideMe($n(copyrow, 'create_date_value'));
208                 unHideMe($n(copyrow, 'copy_holdable_td'));
209         }
210
211         if(isXUL() || showDueDate) {
212                 unHideMe($n(copyrow, 'copy_due_date_td'));
213         }
214
215         for( var i = 0; i < copies.length; i++ ) {
216                 var row = copyrow.cloneNode(true);
217                 var copyid = copies[i];
218         var pt; if (args.peer_types) pt = args.peer_types[i];
219         if (typeof copyid != 'object') {
220             var req = new Request(FETCH_FLESHED_COPY, copyid);
221             req.callback(cpdDrawCopy);
222             req.request.args = r.args;
223             req.request.row = row;
224             req.send();
225         } else {
226             setTimeout(
227                 function(copy,row,pt) {
228                     return function() {
229                         cpdDrawCopy({
230                             'getResultObject' : function() { return copy; },
231                             'args' : r.args,
232                             'peer_type' : pt,
233                             'row' : row
234                         });
235                     };
236                 }(copies[i],row,pt), 0
237             );
238         }
239                 copytbody.appendChild(row);
240         }
241 }
242
243 function cpdDrawCopy(r) {
244         var copy = r.getResultObject();
245         var row  = r.row;
246         var pt   = r.peer_type;
247     var trow = r.args.templateRow;
248
249     if (r.args.copy_location && copy.location().name() != r.args.copy_location) {
250         hideMe(row);
251         return;
252     }
253
254     // Make barcode more useful for staff client usage
255     if(isXUL()) {
256         var my_a = document.createElement('a');
257         my_a.appendChild(text(copy.barcode()));
258         my_a.setAttribute("href","javascript:void(0);");
259         my_a.onclick = function() {
260             xulG.new_tab(xulG.urls.XUL_COPY_STATUS, {}, {'from_item_details_new': true, 'barcodes': [copy.barcode()]});
261                 };
262         $n(row, 'barcode').appendChild(my_a);
263     }
264     else {
265         $n(row, 'barcode').appendChild(text(copy.barcode()));
266     }
267
268     /* show the peer type*/
269     if (pt) {
270         $n(row, 'barcode').appendChild(text(' :: ' + pt));
271     }
272
273         $n(row, 'location').appendChild(text(copy.location().name()));
274         $n(row, 'status').appendChild(text(copy.status().name()));
275
276     // append comma-separated list of part this copy is linked to
277     if(copy.parts() && copy.parts().length) {
278         unHideMe($n(trow, 'copy_part_label'));
279         unHideMe($n(row, 'copy_part'));
280         for(var i = 0; i < copy.parts().length; i++) {
281             var part = copy.parts()[i];
282             var node = $n(row, 'copy_part');
283             if(i > 0) node.appendChild(text(','));
284             node.appendChild(text(part.label()));
285         }
286     }
287
288     /* show the other bibs link */
289     if (copy.peer_record_maps().length > 0) {
290         var l = $n(row, 'copy_multi_home');
291         unHideMe(l);
292         var link_args = {};
293         link_args.page = RRESULT;
294         link_args[PARAM_RTYPE] = RTYPE_LIST;
295         link_args[PARAM_RLIST] = new Array();
296         for (var i = 0; i < copy.peer_record_maps().length; i++) {
297             link_args[PARAM_RLIST].push( copy.peer_record_maps()[i].peer_record() );
298         }
299         l.setAttribute('href',buildOPACLink(link_args));
300     }
301
302         if(isXUL()) {
303                 /* show the hold link */
304                 var l = $n(row, 'copy_hold_link');
305                 unHideMe(l);
306                 l.onclick = function() {
307                         holdsDrawEditor( 
308                                 { 
309                                         type                    : 'C',
310                                         copyObject      : copy,
311                                         onComplete      : function(){}
312                                 }
313                         );
314                 }
315
316                 /* show the book now link */
317                 l = $n(row, 'copy_reserve_link');
318                 unHideMe(l);
319                 l.onclick = function() {
320                         // XXX FIXME this duplicates things in cat/util.js
321                         // Also needs i18n
322                         dojo.require("fieldmapper.Fieldmapper");
323                         var r = fieldmapper.standardRequest(
324                                 ["open-ils.booking",
325                                         "open-ils.booking.resources.create_from_copies"],
326                                 [G.user.session, [copy.id()]]
327                         );
328                         if (!r) {
329                                 alert("No response from server!");
330                         } else if (r.ilsevent != undefined) {
331                                 alert("Error from server:\n" + js2JSON(r));
332                         } else {
333                                 xulG.auth = {"session": {"key": G.user.session}};
334                                 xulG.bresv_interface_opts = {"booking_results": r};
335                                 location.href = "/eg/booking/reservation";
336                         }
337                 }
338
339                 if( copy.age_protect() ) 
340                         appendClear($n(row, 'age_protect_value'), text(copy.age_protect().name()));
341
342                 var cd = copy.create_date();
343                 cd = cd.replace(/T.*/, '');
344                 $n(row, 'create_date_value').appendChild(text(cd));
345
346                 var yes = $('rdetail.yes').innerHTML;
347                 var no = $('rdetail.no').innerHTML;
348
349                 if( isTrue(copy.holdable()) &&
350                                 isTrue(copy.location().holdable()) &&
351                                 isTrue(copy.status().holdable()) ) {
352                         $n(row, 'copy_is_holdable').appendChild(text(yes));     
353                 } else {
354                         $n(row, 'copy_is_holdable').appendChild(text(no));      
355                 }
356         }
357
358         if (isXUL() || showDueDate) {
359                 var circ;
360                 if( copy.circulations() ) {
361                         circ = copy.circulations()[0];
362                         if( circ ) {
363                                 var due_time = dojo.date.stamp.fromISOString(circ.due_date());
364                                 if( showDueTime ) {
365                                         $n(row, 'copy_due_date').appendChild(text(dojo.date.locale.format(due_time, {"formatLength": "medium"})));
366                                 } else {
367                                         $n(row, 'copy_due_date').appendChild(text(dojo.date.locale.format(due_time, {"selector": "date", "formatLength": "medium"})));
368                                 }
369                         }
370                 }
371         }
372
373         r.args.copy = copy;
374         r.args.copyrow = row;
375         cpdShowNotes(copy, r.args)
376         cpdShowStats(copy, r.args);
377
378 }
379
380 function _cpdExtrasInit(args) {
381
382         var newrid      = 'extras_row_' + args.copy.barcode();
383         var newrow      = $(newrid);
384         if(!newrow) newrow = args.extrasRow.cloneNode(true);
385         var tbody       = $n(newrow, 'extras_tbody');
386         var rowt                = $n(tbody, 'extras_row');
387         newrow.id       = newrid;
388
389         var cr = args.copyrow;
390         var nr = cr.nextSibling;
391         var np = args.mainTbody;
392
393         /* insert the extras row into the main table */
394         if(nr) np.insertBefore( newrow, nr );
395         else np.appendChild(newrow);
396
397         var link = $n(args.copyrow, 'details_link');
398         var link2 = $n(args.copyrow, 'less_details_link');
399         var id = newrow.id;
400         link.id = id + '_morelink';
401         link2.id = id + '_lesslink';
402         unHideMe(link);
403         hideMe(link2);
404
405         link.setAttribute('href', 
406                         'javascript:unHideMe($("'+link2.id+'")); hideMe($("'+link.id+'"));unHideMe($("'+newrow.id+'"));');
407
408         link2.setAttribute('href', 
409                         'javascript:unHideMe($("'+link.id+'")); hideMe($("'+link2.id+'"));hideMe($("'+newrow.id+'"));');
410
411         return [ tbody, rowt ];
412 }
413
414 function cpdShowNotes(copy, args) {
415         var notes = copy.notes();
416         if(!notes || notes.length == 0) return;
417
418         var a = _cpdExtrasInit(args);
419         var tbody = a[0];
420         var rowt = a[1];
421
422         for( var n in notes ) {
423                 var note = notes[n];
424                 if(!isTrue(note.pub())) continue;
425                 var row = rowt.cloneNode(true);
426                 $n(row, 'key').appendChild(text(note.title()));
427                 $n(row, 'value').appendChild(text(note.value()));
428                 unHideMe($n(row, 'note'));
429                 unHideMe(row);
430                 tbody.appendChild(row);
431         }
432 }
433
434
435 function cpdShowStats(copy, args) {
436         var entries = copy.stat_cat_entry_copy_maps();
437         if(!entries || entries.length == 0) return;
438
439         var visibleStatCat = false;
440
441         /*
442                 check all copy stat cats; if we find one that's OPAC visible,
443                 set the flag and break the loop. If we've found one, or we're
444                 in the staff client, build the table. if not, we return doing
445                 nothing, as though the stat_cat_entry_copy_map was empty or null
446         */
447
448         for( var n in entries )
449         {
450                         var entry = entries[n];
451                         if(isTrue(entry.stat_cat().opac_visible()))
452                         {
453                                 visibleStatCat = true;
454                                 break;
455                         }
456         }
457
458         if(!(isXUL() || visibleStatCat)) return;
459
460         var a = _cpdExtrasInit(args);
461         var tbody = a[0];
462         var rowt = a[1];
463
464         for( var n in entries ) {
465                 var entry = entries[n];
466                 if(!(isXUL() || isTrue(entry.stat_cat().opac_visible()))) continue;
467                 var row = rowt.cloneNode(true);
468                 $n(row, 'key').appendChild(text(entry.stat_cat().name()));
469                 $n(row, 'value').appendChild(text(entry.stat_cat_entry().value()));
470                 unHideMe($n(row, 'cat'));
471                 unHideMe(row);
472                 tbody.appendChild(row);
473         }
474 }
475