]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
Enhance RefWorks support on a per-skin basis:
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / rdetail.js
1 /* */
2
3 detachAllEvt('common', 'run');
4 attachEvt("common", "run", rdetailDraw);
5 attachEvt("rdetail", "recordDrawn", rdetailBuildStatusColumns);
6 attachEvt("rdetail", "recordDrawn", rdetailBuildInfoRows);
7 attachEvt("rdetail", "recordDrawn", rdetailGetPageIds);
8
9 /* Per-skin configuration settings */
10 var rdetailLocalOnly = true;
11 var rdetailShowLocal = true;
12 var rdetailShowCopyLocation = true;
13 var rdetailGoogleBookPreview = true;
14 var rdetailDisplaySerialHoldings = true;
15 var rdetailEnableRefWorks = true;
16 var rdetailRefWorksHost = 'http://www.refworks.com';
17
18 /* vars vars vars */
19 var record = null;
20 var cp_statuses = null;
21 var recordsCache = [];
22
23 var copyRowParent = null;
24 var copyRow = null;
25 var statusRow = null;
26 var numStatuses = null;
27 var defaultCN;
28 var callnumberCache = {};
29 var globalCNCache = {};
30 var localTOC;
31 var cachedRecords;
32 var _statusPositions = {};
33 var opac_strings;
34
35 var nextContainerIndex;
36
37 var nextRecord;
38 var prevRecord;
39
40 var rdetailPrev = null;
41 var rdetailNext = null;
42 var rdetailStart = null;
43 var rdetailEnd = null;
44
45 /* serials are currently the only use of Dojo strings in the OPAC */
46 if (rdetailDisplaySerialHoldings) {
47         dojo.requireLocalization("openils.opac", "opac");
48         opac_strings = dojo.i18n.getLocalization("openils.opac", "opac");
49 }
50
51 function rdetailReload() {
52         var args = {};
53         args[PARAM_LOCATION] = getNewSearchLocation();
54         args[PARAM_DEPTH] = depthSelGetDepth();
55         goTo(buildOPACLink(args));
56 }
57
58 /* looks to see if we have a next and/or previous record in the
59    record cache, if so, set up the nav links */
60 function rdetailSetPaging(ids) {
61
62         cachedRecords = {};
63         cachedRecords.ids = ids;
64
65         for( var i = 0; i < cachedRecords.ids.length; i++ ) {
66                 var rec = cachedRecords.ids[i];
67                 if( rec == getRid() ) {
68                         if( i > 0 ) prevRecord = cachedRecords.ids[i-1];
69                         if( i < cachedRecords.ids.length - 1 )
70                                 nextRecord = cachedRecords.ids[i+1];
71                         break;
72                 }
73         }
74
75         $('np_offset').appendChild(text(i + 1));
76         $('np_count').appendChild(text(getHitCount()));
77
78         if(prevRecord) {
79                 unHideMe($('np_table'));
80                 unHideMe($('np_prev'));
81                 unHideMe($('np_start'));
82                 rdetailPrev = function() { _rdetailNav(prevRecord); };
83                 rdetailStart = function() { _rdetailNav(cachedRecords.ids[0]); };
84         }
85
86         if(nextRecord) {
87                 unHideMe($('np_table'));
88                 unHideMe($('np_next'));
89                 unHideMe($('np_end'));
90                 rdetailNext = function() { _rdetailNav(nextRecord); };
91                 rdetailEnd = function() { _rdetailNav(cachedRecords.ids[cachedRecords.ids.length-1]); };
92         }
93
94         runEvt('rdetail', 'nextPrevDrawn', i, cachedRecords.ids.length);
95 }
96
97
98 function _rdetailNav(id, offset) {
99         var args = {};
100         args[PARAM_RID] = id;
101         goTo(buildOPACLink(args));
102 }
103
104 function rdetailDraw() {
105
106         detachAllEvt('common','depthChanged');
107         detachAllEvt('common','locationUpdated');
108         attachEvt('common','depthChanged', rdetailReload);
109         attachEvt('common','locationUpdated', rdetailReload);
110         attachEvt('common','holdUpdated', rdetailReload);
111         attachEvt('common','holdUpdateCanceled', rdetailReload);
112
113         copyRowParent = G.ui.rdetail.cp_info_row.parentNode;
114         copyRow = copyRowParent.removeChild(G.ui.rdetail.cp_info_row);
115         statusRow = G.ui.rdetail.cp_status.parentNode;
116         statusRow.id = '__rdsrow';
117
118         G.ui.rdetail.cp_info_local.onclick = rdetailShowLocalCopies;
119         G.ui.rdetail.cp_info_all.onclick = rdetailShowAllCopies;
120
121         if(getLocation() == globalOrgTree.id())
122                 hideMe(G.ui.rdetail.cp_info_all);
123
124         var req = new Request(FETCH_RMODS, getRid());
125         req.callback(_rdetailDraw);
126         req.send();
127
128         if (rdetailDisplaySerialHoldings) {
129                 var req = new Request(FETCH_MFHD_SUMMARY, getRid());
130                 req.callback(_holdingsDraw);
131                 req.send();
132         }
133
134         detachAllEvt("result", "idsReceived");
135         G.evt.result.hitCountReceived = [];
136         G.evt.result.recordReceived = [];
137         G.evt.result.copyCountsReceived = [];
138         G.evt.result.allRecordsReceived = [];
139 }
140
141 function rdetailGetPageIds() {
142         attachEvt("result", "idsReceived", rdetailSetPaging );
143         resultFetchAllRecords = true;
144         rresultCollectIds(true);
145 }
146
147
148 function buildunAPISpan (span, type, id) {
149         var cgi = new CGI();
150         var d = new Date();
151
152         addCSSClass(span,'unapi-id');
153
154         span.setAttribute(
155                         'title', 'tag:' + cgi.server_name + ',' +
156                         d.getFullYear() + ':' + type + '/' + id
157                         );
158 }
159
160 function rdetailViewMarc(r,id) {
161         hideMe($('rdetail_extras_loading'));
162         $('rdetail_view_marc_box').innerHTML = r.getResultObject();
163
164         var div = elem('div', { "class" : 'hide_me' });
165         var span = div.appendChild( elem('abbr') );
166
167         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
168
169         $('rdetail_view_marc_box').insertBefore(span, $('rdetail_view_marc_box').firstChild);
170 }
171
172
173 function rdetailShowLocalCopies() {
174         rdetailShowLocal = true;
175         rdetailBuildInfoRows();
176         hideMe(G.ui.rdetail.cp_info_local);
177         unHideMe(G.ui.rdetail.cp_info_all);
178         hideMe(G.ui.rdetail.cp_info_none); 
179 }
180
181 function rdetailShowAllCopies() {
182
183         rdetailShowLocal = false;
184         rdetailBuildInfoRows();
185         hideMe(G.ui.rdetail.cp_info_all);
186         unHideMe(G.ui.rdetail.cp_info_local);
187         hideMe(G.ui.rdetail.cp_info_none); 
188 }
189
190 function OpenMarcEditWindow(pcrud, rec) {
191         /*
192            To run in Firefox directly, must set signed.applets.codebase_principal_support
193            to true in about:config
194          */
195         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
196         win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
197         dojo.require('openils.PermaCrud');
198
199         win.xulG = {
200                 "record": {"marc": rec.marc()},
201                 "save": {
202                         "label": opac_strings.SAVE_MFHD_LABEL,
203                         "func": function(xmlString) {
204                                 rec.marc(xmlString);
205                                 rec.ischanged(true);
206                                 pcrud.update(rec);
207                         }
208                 }
209         };
210 }
211
212 function loadMarcEditor(recId) {
213         var pcrud = new openils.PermaCrud({"authtoken": G.user.session});
214         var rec = pcrud.retrieve("sre", recId);
215         if (rec) {
216                 OpenMarcEditWindow(pcrud, rec);
217         }
218 }
219
220 /*
221  * This function could be written much more intelligently
222  * Limited brain power means that I'm brute-forcing it for now
223  */
224 function _holdingsDraw(h) {
225         holdings = h.getResultObject();
226         if (!holdings) { return null; }
227
228         dojo.forEach(holdings, _holdingsDrawMFHD);
229 }
230
231 function _holdingsDrawMFHD(holdings, entryNum) {
232         var here = findOrgUnit(getLocation());
233         if (getDepth() > 0 || getDepth === 0 ) {
234                 while (getDepth() < findOrgDepth(here))
235                 here = findOrgUnit( here.parent_ou() );
236                 if (!orgIsMine(findOrgUnit(here), findOrgUnit(holdings.owning_lib()))) {
237                         return null;
238                 }
239         }
240
241         var hh = holdings.holdings();
242         var hch = holdings.current_holdings();
243         var hs = holdings.supplements();
244         var hcs = holdings.current_supplements();
245         var hi = holdings.indexes();
246         var hci = holdings.current_indexes();
247         var ho = holdings.online();
248         var hm = holdings.missing();
249         var hinc = holdings.incomplete();
250         var hloc = holdings.location() || 'MFHD';
251
252         if (    hh.length == 0 && hch.length == 0 && hs.length == 0 &&
253                 hcs.length == 0 && hi.length == 0 && hci.length == 0 &&
254                 ho.length == 0 && hm.length == 0 && hinc.length == 0 && !isXUL()
255         ) {
256                 return null;
257         }
258
259         dojo.place("<table style='width: 100%;'><caption id='mfhdHoldingsCaption' class='rdetail_header color_1'>" +
260                 dojo.string.substitute(opac_strings.HOLDINGS_TABLE_CAPTION, [hloc]) +
261                 "</caption><tbody id='rdetail_holdings_tbody_" + entryNum +
262                 "'></tbody></table>", "rdetail_details_table", "after"
263         );
264         if (hh.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.HOLDINGS, hh); }
265         if (hch.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.CURRENT_HOLDINGS, hch); }
266         if (hs.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.SUPPLEMENTS, hs); }
267         if (hcs.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.CURRENT_SUPPLEMENTS, hcs); }
268         if (hi.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.INDEXES, hi); }
269         if (hci.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.CURRENT_INDEXES, hci); }
270         if (ho.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.ONLINE_VOLUMES, ho); }
271         if (hm.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.MISSING_VOLUMES, hm); }
272         if (hinc.length > 0) { _holdingsDrawMFHDEntry(entryNum, opac_strings.INCOMPLETE_VOLUMES, hinc); }
273
274         if (isXUL()) {
275                 dojo.require('openils.Event');
276                 dojo.require('openils.PermaCrud');
277                 dojo.place("<span> - </span><a class='classic_link' href='javascript:loadMarcEditor(" +
278                         holdings.id() + ")'>" + opac_strings.EDIT_LABEL + "</a>", 
279                         "mfhdHoldingsCaption", "last"
280                 );
281         }
282 }
283
284 function _holdingsDrawMFHDEntry(entryNum, entryName, entry) {
285         var flatEntry = entry.toString().replace(/,/g, ', ');
286         dojo.place("<tr><td> </td><td nowrap='nowrap' class='rdetail_desc'>" + entryName + "</td><td class='rdetail_item'>" + flatEntry + "</td></tr>", "rdetail_holdings_tbody_" + entryNum, "last");
287 }
288
289 function _rdetailDraw(r) {
290         record = r.getResultObject();
291
292         runEvt('rdetail', 'recordRetrieved', record.doc_id());
293
294         G.ui.rdetail.title.appendChild(text(record.title()));
295         buildSearchLink(STYPE_AUTHOR, record.author(), G.ui.rdetail.author);
296         G.ui.rdetail.isbn.appendChild(text(cleanISBN(record.isbn())));
297         G.ui.rdetail.edition.appendChild(text(record.edition()));
298         G.ui.rdetail.pubdate.appendChild(text(record.pubdate()));
299         G.ui.rdetail.publisher.appendChild(text(record.publisher()));
300         $('rdetail_physical_desc').appendChild(text(record.physical_description()));
301         r = record.types_of_resource();
302         if(r) {
303                 G.ui.rdetail.tor.appendChild(text(r[0]));
304                 setResourcePic( G.ui.rdetail.tor_pic, r[0]);
305         }
306         G.ui.rdetail.abstr.appendChild(text(record.synopsis()));
307
308         try{
309                 if(record.isbn()) {
310                         if(ENABLE_ADDED_CONTENT_ATTRIB_LINKS) {
311                                 unHideMe($('rdetail.jacket_attrib_div'));
312                                 var href = $('rdetail.jacket_attrib_link').getAttribute('href') +cleanISBN(record.isbn());
313                                 $('rdetail.jacket_attrib_link').setAttribute('href', href);
314                         }
315                         rdetailCheckForGBPreview();
316
317                 } else {
318                         hideMe($("rdetail.jacket_attrib_div"));
319                         hideMe($("rdetail_img_link"));
320                 }
321         } catch(E) {}
322
323
324         // see if the record has any external links 
325         var links = record.online_loc();
326         for( var i = 0; links && links.length > 0 && i < links.length; i = i + 3 ) {
327                 var href = links[i];
328                 // avoid matching "HTTP: The Complete Reference"
329                 if( href.match(/https?:\/|ftps?:\/|mailto:/i) ) {
330                         unHideMe($('rdetail_online_row'));
331                         // MODS can contain a display label (used for the text of the link)
332                         // as well as a note about the URL; many legacy systems conflate the
333                         // two and generate MARC records that expect the note to be used as
334                         // the text of the link, with no display label; here's the canonical
335                         // format:
336                         //
337                         // 856 40 $uhttp://localhost$yDisplay label$zPublic note
338                         //
339                         // Note that the MARC21slim2MODS XSL concatenates $3 and $y together
340                         // (as $y was defined later in MARC21's life as the display label)
341                         var displayLabel = '' + links[i+1];
342                         var note = '' + links[i+2];
343                         if(!displayLabel || displayLabel.match(/https?:\/|ftps?:\/|mailto:/i)) {
344                                 if(!note || note.match(/https?:\/|ftps?:\/|mailto:/i)) {
345                                         displayLabel = href;
346                                 } else {
347                                         displayLabel = note;
348                                 }
349                         }
350                         $('rdetail_online').appendChild(elem('a', {href:href,'class':'classic_link'}, displayLabel));
351                         if (note && note != displayLabel) {
352                                 $('rdetail_online').appendChild(elem('span', {'class':'url_note'}, ' - ' + note));
353                         }
354                         $('rdetail_online').appendChild(elem('br'));
355                 }
356         }
357
358         // Fill in our unAPI ID, if anyone cares
359         var abbrs = document.getElementsByTagName('abbr');
360         var span;
361         for (var i = 0; i < abbrs.length; i = i + 1) {
362                 if (abbrs[i].getAttribute('name') == 'unapi') {
363                         span = abbrs[i];
364                         break;
365                 }
366         }
367         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
368
369         $('rdetail_place_hold').setAttribute(
370                         'href','javascript:holdsDrawEditor({record:"'+record.doc_id()+'",type:"T"});');
371
372         var RW = $('rdetail_exp_refworks');
373         if (RW && rdetailEnableRefWorks) {
374
375                 var here = (findOrgUnit(getLocation())).name();
376                 var org_name = here.replace(" ", "+");
377                 var cgi = new CGI();
378
379                 RW.setAttribute(
380                         'href',
381                         rdetailRefWorksHost + '/express/expressimport.asp?vendor='
382                         + org_name
383                         + '&filter=MARC+Format&database=All+MARC+Formats&encoding=65001&url=http%3A%2F%2F'
384                         + cgi.server_name + '/opac/extras/supercat/marctxt/record/'
385                         + record.doc_id()
386                );
387
388                 RW.setAttribute('target', 'RefWorksMain');
389
390                 unHideMe($('rdetail_exp_refworks_span'));
391         }
392
393         $('rdetail_img_link').setAttribute('href', buildISBNSrc(cleanISBN(record.isbn()), 'large'));
394         G.ui.rdetail.image.setAttribute("src", buildISBNSrc(cleanISBN(record.isbn())));
395         runEvt("rdetail", "recordDrawn");
396         recordsCache.push(record);
397
398         rdetailSetExtrasSelector();
399
400         var breq = new Request(FETCH_BRE, [getRid()]);
401         breq.callback( rdetailCheckDeleted );
402         breq.send();
403
404         resultBuildCaches( [ record ] );
405         resultDrawSubjects();
406         resultDrawSeries();
407
408         // grab added content 
409         acCollectData(cleanISBN(record.isbn()), rdetailhandleAC);
410 }
411
412
413
414 function rdetailCheckDeleted(r) {
415         var br = r.getResultObject()[0];
416         if( isTrue(br.deleted()) ) {
417                 hideMe($('rdetail_place_hold'));
418                 $('rdetail_more_actions_selector').disabled = true;
419                 unHideMe($('rdetail_deleted_exp'));
420         }
421 }
422
423 function rdetailSetExtrasSelector() {
424         if(!grabUser()) return;
425         unHideMe($('rdetail_more_actions'));
426
427         var req = new Request( 
428                         FETCH_CONTAINERS, G.user.session, G.user.id(), 'biblio', 'bookbag' );
429         req.callback(rdetailAddBookbags);
430         req.send();
431 }
432
433 function rdetailAddBookbags(r) {
434
435         var containers = r.getResultObject();
436         var selector = $('rdetail_more_actions_selector');
437         var found = false;
438         var index = 3;
439         doSelectorActions(selector);
440
441         for( var i = 0; i != containers.length; i++ ) {
442                 found = true;
443                 var container = containers[i];
444                 insertSelectorVal( selector, index++, container.name(), 
445                                 "container_" + container.id(), rdetailAddToBookbag,  1 );
446         }
447
448         nextContainerIndex = index;
449 }
450
451 var _actions = {};
452 function rdetailNewBookbag() {
453         var name = prompt($('rdetail_bb_new').innerHTML,"");
454         if(!name) return;
455
456         var id;
457         if( id = containerCreate( name ) ) {
458                 alert($('rdetail_bb_success').innerHTML);
459                 var selector = $('rdetail_more_actions_selector');
460                 insertSelectorVal( selector, nextContainerIndex++, name, 
461                                 "container_" + id, rdetailAddToBookbag, 1 );
462                 setSelector( selector, 'start' );
463         }
464 }
465
466
467 function rdetailAddToBookbag() {
468         var selector = $('rdetail_more_actions_selector');
469         var id = selector.options[selector.selectedIndex].value;
470         setSelector( selector, 'start' );
471
472         if( containerCreateItem( id.substring(10), record.doc_id() )) {
473                 alert($('rdetail_bb_item_success').innerHTML);
474         }
475 }
476
477
478 var rdetailMarcFetched = false;
479 function rdetailShowExtra(type, args) {
480
481         hideMe($('rdetail_copy_info_div'));
482         hideMe($('rdetail_reviews_div'));
483         hideMe($('rdetail_toc_div'));
484         hideMe($('rdetail_anotes_div'));
485         hideMe($('rdetail_excerpt_div'));
486         hideMe($('rdetail_preview_div'));
487         hideMe($('rdetail_marc_div'));
488         hideMe($('cn_browse'));
489         hideMe($('rdetail_cn_browse_div'));
490         hideMe($('rdetail_notes_div'));
491
492         removeCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
493         removeCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
494         removeCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
495         removeCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
496         removeCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
497         removeCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
498         removeCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
499         removeCSSClass($('rdetail_annotation_link'), 'rdetail_extras_selected');
500         removeCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
501
502         switch(type) {
503
504                 case "copyinfo": 
505                         unHideMe($('rdetail_copy_info_div')); 
506                         addCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
507                         break;
508
509                 case "reviews": 
510                         addCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
511                         unHideMe($('rdetail_reviews_div')); 
512                         break;
513
514                 case "excerpt": 
515                         addCSSClass($('rdetail_excerpt_link'), 'rdetail_extras_selected');
516                         unHideMe($('rdetail_excerpt_div'));
517                         break;
518
519                 case "preview": 
520                         addCSSClass($('rdetail_preview_link'), 'rdetail_extras_selected');
521                         unHideMe($('rdetail_preview_div'));
522                         rdetailDisplayGBPreview();
523                         break;
524
525                 case "anotes": 
526                         addCSSClass($('rdetail_anotes_link'), 'rdetail_extras_selected');
527                         unHideMe($('rdetail_anotes_div'));
528                         break;
529
530                 case "toc": 
531                         addCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
532                         unHideMe($('rdetail_toc_div'));
533                         break;
534
535                 case "marc": 
536                         addCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
537                         unHideMe($('rdetail_marc_div')); 
538                         if(rdetailMarcFetched) return;
539                         unHideMe($('rdetail_extras_loading'));
540                         rdetailMarcFetched = true;
541                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
542                         req.callback(rdetailViewMarc); 
543                         req.send();
544                         break;
545
546                 case 'cn':
547                         addCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
548                         unHideMe($('rdetail_cn_browse_div'));
549                         rdetailShowCNBrowse(defaultCN, getLocation(), null, true);
550                         break;
551
552         }
553 }
554
555 function rdetailVolumeDetails(args) {
556         var row = $(args.rowid);
557         var tbody = row.parentNode;
558         cpdBuild( tbody, row, record, args.cn, args.org, args.depth, args.copy_location );
559         return;
560 }
561
562 function rdetailBuildCNList() {
563
564         var select = $('cn_browse_selector');
565         var index = 0;
566         var arr = [];
567         for( var cn in callnumberCache ) arr.push( cn );
568         arr.sort();
569
570         if( arr.length == 0 ) {
571                 hideMe($('rdetail_cn_browse_select_div'));
572                 return;
573         }
574
575         for( var i in arr ) {
576                 var cn = arr[i];
577                 var opt = new Option(cn);
578                 select.options[index++] = opt;
579         }
580         select.onchange = rdetailGatherCN;
581 }
582
583 function rdetailGatherCN() {
584         var cn = getSelectorVal($('cn_browse_selector'));
585         rdetailShowCNBrowse( cn, getLocation(), getDepth(), true );
586         setSelector( $('cn_browse_selector'), cn );
587 }
588
589
590 function rdetailShowCNBrowse( cn, loc, depth, fromOnclick ) {
591
592         if(!cn) {
593                 unHideMe($('cn_browse_none'));
594                 hideMe($('rdetail_cn_browse_select_div'));
595                 return;
596         }
597
598         unHideMe($('rdetail_cn_browse_select_div'));
599         rdetailBuildCNList();
600         setSelector( $('cn_browse_selector'), cn );
601         hideMe($('rdetail_copy_info_div'));
602         hideMe($('rdetail_reviews_div'));
603         hideMe($('rdetail_toc_div'));
604         hideMe($('rdetail_marc_div'));
605         unHideMe($('rdetail_cn_browse_div'));
606         unHideMe($('cn_browse'));
607         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
608         cnBrowseGo(cn, loc, depth);
609 }
610
611 function rdetailhandleAC(data) {
612
613         if( data.reviews.html ) {
614                 $('rdetail_review_container').innerHTML = data.reviews.html;
615                 unHideMe($('rdetail_reviews_link'));
616         }
617
618         if( data.toc.html ) {
619                 $('rdetail_toc_div').innerHTML = data.toc.html;
620                 unHideMe($('rdetail_toc_link'));
621         }
622
623         if( data.excerpt.html ) {
624                 $('rdetail_excerpt_div').innerHTML = data.excerpt.html;
625                 unHideMe($('rdetail_excerpt_link'));
626         }
627
628         if( data.anotes.html ) {
629                 $('rdetail_anotes_div').innerHTML = data.anotes.html;
630                 unHideMe($('rdetail_anotes_link'));
631         }
632 }
633
634 function rdetailShowReviews(r) {
635         hideMe($('rdetail_extras_loading'));
636         var res = r.getResultObject();
637         var par = $('rdetail_reviews_div');
638         var template = par.removeChild($('rdetail_review_template'));
639         if( res && res.length > 0 ) {
640                 unHideMe($('rdetail_reviews_link'));
641                 for( var i = 0; i != res.length; i++ ) {
642                         var rev = res[i];       
643                         if( rev.text && rev.info ) {
644                                 var node = template.cloneNode(true);
645                                 $n(node, 'review_header').appendChild(text(rev.info));
646                                 $n(node, 'review_text').appendChild(text(rev.text));
647                                 par.appendChild(node);
648                         }
649                 }
650         }
651 }
652
653
654 function rdetailShowTOC(r) {
655         hideMe($('rdetail_extras_loading'));
656         var resp = r.getResultObject();
657         if(resp) {
658                 unHideMe($('rdetail_toc_link'));
659                 $('rdetail_toc_div').innerHTML = resp;
660         }
661 }
662
663 function rdetailBuildInfoRows() {
664         var req;
665         var method = FETCH_COPY_COUNTS_SUMMARY;
666         if (rdetailShowCopyLocation)
667                 method = FETCH_COPY_LOCATION_COUNTS_SUMMARY;
668
669         if( rdetailShowLocal ) 
670                 req = new Request(method, record.doc_id(), getLocation(), getDepth())
671         else
672                 req = new Request(method, record.doc_id());
673         req.callback(_rdetailBuildInfoRows);
674         req.send();
675 }
676
677 function _rdetailRows(node) {
678
679         if( rdetailShowLocal && getLocation() != globalOrgTree.id() ) {
680                 var loc = findOrgUnit(getLocation());
681                 if( node ) {
682                         if( !orgIsMine(node, loc) && !orgIsMine(loc,node) ) return;
683                 } else {
684                         for( var i = 0; i < globalOrgTree.children().length; i++ ) {
685                                 var org = findOrgUnit(globalOrgTree.children()[i]);
686                                 if( orgIsMine(org, loc) ) {
687                                         node = org;
688                                         break;
689                                 }
690                         }
691                 } 
692         }
693
694         if(!node && findOrgType(globalOrgTree.ou_type()).can_have_vols())
695                 node = globalOrgTree;
696
697
698         /* don't show hidden orgs */
699
700         if(node) {
701
702                 if(!isXUL() && !isTrue(node.opac_visible())) return;
703
704                 var row = copyRow.cloneNode(true);
705                 row.id = "cp_info_" + node.id();
706
707                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
708                 var cntd  = findNodeByName( row, config.names.rdetail.cn_cell );
709                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
710                 var actions = $n(row, 'rdetail_actions_cell');
711
712                 var p = libtd.getElementsByTagName('a')[0];
713                 libtd.insertBefore(text(node.name()), p);
714                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
715
716                 if(!findOrgType(node.ou_type()).can_have_vols()) {
717
718                         row.removeChild(cntd);
719                         row.removeChild(cpctd);
720                         row.removeChild(actions);
721                         row.setAttribute('novols', '1');
722
723                         libtd.setAttribute("colspan", numStatuses + 3 );
724                         libtd.colSpan = numStatuses + 3;
725                         addCSSClass(row, 'copy_info_region_row');
726                 } 
727
728                 copyRowParent.appendChild(row);
729
730         } else { node = globalOrgTree; }
731
732         for( var c in node.children() ) 
733                 _rdetailRows(node.children()[c]);
734 }
735
736 function rdetailCNPrint(orgid, cn) {
737         var div = cpdBuildPrintWindow( record, orgid);
738         var template = div.removeChild($n(div, 'cnrow'));
739         var rowNode = $("cp_info_" + orgid);
740         cpdStylePopupWindow(div);
741         openWindow(div.innerHTML);
742 }
743
744 var localCNFound = false;
745 var ctr = 0;
746 function _rdetailBuildInfoRows(r) {
747
748         if (rdetailShowCopyLocation)
749                 unHideMe( $n( $('rdetail_copy_info_table'), 'rdetail_copylocation_header' ) );
750
751         removeChildren(copyRowParent);
752
753         _rdetailRows();
754
755         var summary = r.getResultObject();
756         if(!summary) return;
757
758         var found = false;
759         for( var i = 0; i < summary.length; i++ ) {
760
761                 var arr = summary[i];
762                 globalCNCache[arr[1]] = 1;
763                 var thisOrg = findOrgUnit(arr[0]);
764                 var rowNode = $("cp_info_" + thisOrg.id());
765                 if(!rowNode) continue;
766
767                 if(rowNode.getAttribute("used")) {
768
769                         if( rowNode.nextSibling ) {
770                                 sib = rowNode.nextSibling;
771                                 o ='cp_info_'+thisOrg.id()+'_';
772                                 /* push the new row on as the last row for this org unit */
773                                 while( sib && sib.id.match(o) ) {
774                                         sib = sib.nextSibling;
775                                 }
776                                 if(sib)
777                                         rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), sib);
778                                 else
779                                         rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
780                         } else {
781                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
782                         }
783
784                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
785                         n.appendChild(text(thisOrg.name()));
786                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
787                         rowNode.id = "cp_info_" + thisOrg.id() + '_' + (++ctr); 
788
789                 } else {
790                         rowNode.setAttribute("used", "1");
791                 }
792
793                 var cpc_temp = rowNode.removeChild(
794                                 findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
795
796                 var statuses = arr[2];
797                 var cl = '';
798                 if (rdetailShowCopyLocation) {
799                         cl = arr[2];
800                         statuses = arr[3];
801                 }
802
803
804                 rdetailApplyStatuses(rowNode, cpc_temp, statuses);
805
806                 var isLocal = false;
807                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
808                         found = true; 
809                         isLocal = true; 
810                         if(!localCNFound) {
811                                 localCNFound = true;
812                                 defaultCN = arr[1];
813                         }
814                 }
815
816                 //if(isLocal) unHideMe(rowNode);
817                 unHideMe(rowNode);
818
819                 rdetailSetPath( thisOrg, isLocal );
820                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal, thisOrg, cl );
821
822                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
823         }
824
825         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
826 }
827
828 function rdetailBuildBrowseInfo(row, cn, local, orgNode, cl) {
829
830         if(local) {
831                 var cache = callnumberCache[cn];
832                 if( cache ) cache.count++;
833                 else callnumberCache[cn] = { count : 1 };
834         }
835
836         var depth = getDepth();
837         if( !local ) depth = findOrgDepth(globalOrgTree);
838
839         $n(row, 'rdetail_callnumber_cell').appendChild(text(cn));
840
841         if (rdetailShowCopyLocation) {
842                 var cl_cell = $n(row, 'rdetail_copylocation_cell');
843                 cl_cell.appendChild(text(cl));
844                 unHideMe(cl_cell);
845         }
846
847         _debug('setting action clicks for cn ' + cn);
848
849         var dHref = 'javascript:rdetailVolumeDetails('+
850                         '{copy_location : "'+cl+'", rowid : "'+row.id+'", cn :"'+cn+'", depth:"'+depth+'", org:"'+orgNode.id()+'", local: '+local+'});';
851
852         var bHref = 'javascript:rdetailShowCNBrowse("' + cn + '", '+orgNode.id()+', "'+depth+'");'; 
853
854         unHideMe( $n(row, 'details') )
855                 $n(row, 'details').setAttribute('href', dHref);
856         unHideMe( $n(row, 'browse') )
857                 $n(row, 'browse').setAttribute('href', bHref);
858
859         if(isXUL()) {
860                 unHideMe($n(row, 'hold_div'));
861                 $n(row, 'hold').onclick = function() {
862                         var req = new Request(FETCH_VOLUME_BY_INFO, cn, record.doc_id(), orgNode.id());
863                         req.callback(
864                                         function(r) {
865                                         var vol = r.getResultObject();
866                                         holdsDrawEditor({type: 'V', volumeObject : vol});
867                                         }
868                                     );
869                         req.send();
870                 };
871         }
872 }
873
874 // sets the path to org as 'active' and displays the path if it's local 
875 function rdetailSetPath(org, local) {
876         if( findOrgDepth(org) == 0 ) return;
877         var row = $("cp_info_" + org.id());
878         row.setAttribute("hasinfo", "1");
879         unHideMe(row);
880         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
881 }
882
883 //Append all the statuses for a given summary to the 
884 //copy summary table 
885 function rdetailApplyStatuses( row, template, statuses ) {
886         for( var j in _statusPositions ) {
887                 var stat = _statusPositions[j];
888                 var val = statuses[stat.id()];
889                 var nn = template.cloneNode(true);
890                 if(val) nn.appendChild(text(val));
891                 else nn.appendChild(text(0));
892                 row.appendChild(nn);
893         }
894 }
895
896 //Add one td (creating a new column) to the copy summary
897 //table for each opac_visible copy status
898 function rdetailBuildStatusColumns() {
899
900         rdetailGrabCopyStatuses();
901         var parent = statusRow;
902         var template = parent.removeChild(G.ui.rdetail.cp_status);
903
904         var i = 0;
905         for( i = 0; i < cp_statuses.length; i++ ) {
906
907                 var c = cp_statuses[i];
908                 if( c && isTrue(c.opac_visible()) ) {
909                         var name = c.name();
910                         _statusPositions[i] = c;
911                         var node = template.cloneNode(true);
912                         var data = findNodeByName( node, config.names.rdetail.cp_status);
913
914                         data.appendChild(text(name));
915                         parent.appendChild(node);
916                 }
917         }       
918
919         numStatuses = 0;
920         for(x in _statusPositions) numStatuses++; 
921 }
922
923 function rdetailGrabCopyStatuses() {
924         if(cp_statuses) return cp_statuses;
925         var req = new Request(FETCH_COPY_STATUSES);
926         req.send(true);
927         cp_statuses = req.result();
928         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
929 }
930
931 function _rdetailSortStatuses(a, b) {
932         return parseInt(a.id()) - parseInt(b.id());
933 }
934
935 /**
936  * Check for a Google Book preview 
937  */
938 function rdetailCheckForGBPreview() {
939         if (!rdetailGoogleBookPreview) return;
940         searchForGBPreview( cleanISBN(record.isbn()) );
941 }
942
943 /**
944  *
945  * @param {DOM object} query The form element containing the
946  *                     input parameters "isbns"
947  */
948 function searchForGBPreview( isbn ) {
949
950         // Delete any previous Google Booksearch JSON queries.
951         var GBPJsonScript = document.getElementById("GBPJsonScript");
952         if (GBPJsonScript) {
953                 GBPJsonScript.parentNode.removeChild(GBPJsonScript);
954         }
955
956         // Add a script element with the src as the user's Google Booksearch query. 
957         // JSON output is specified by including the alt=json-in-script argument
958         // and the callback function is also specified as a URI argument.
959         var GBPScriptElement = document.createElement("script");
960
961         GBPScriptElement.setAttribute("id", "GBPJsonScript");
962         GBPScriptElement.setAttribute("src",
963                         "http://books.google.com/books?bibkeys=" + 
964                         isbn + "&jscmd=viewapi&callback=GBPreviewCallback");
965         GBPScriptElement.setAttribute("type", "text/javascript");
966
967         // make the request to Google booksearch
968         document.documentElement.firstChild.appendChild(GBPScriptElement);
969 }
970
971 /**
972  * This function is the call-back function for the JSON scripts which 
973  * executes a Google book search response.
974  *
975  * XXX I18N of text needed
976  *
977  * @param {JSON} booksInfo is the JSON object pulled from the Google books service.
978  */
979 function GBPreviewCallback(GBPBookInfo) {
980         var GBPreviewDiv = document.getElementById("rdetail_preview_div");
981         var GBPBook;
982
983         for ( i in GBPBookInfo ) {
984                 GBPBook = GBPBookInfo[i];
985         }
986
987         if ( !GBPBook ) {
988                 return;
989         }
990
991         if ( GBPBook.preview != "noview" ) {
992                 if ( GBPBook.preview == 'full' ) {
993                         setText( $('rdetail_preview_link'), $('rdetail_preview_full_text').innerHTML );
994                         $('rdetail_preview_link_a').title = $('rdetail_preview_title').innerHTML;      
995                 }
996
997                 // Add a button below the book cover image to load the preview.
998                 GBPBadge = document.createElement( 'img' );
999                 GBPBadge.src = 'http://books.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif';
1000                 GBPBadge.title = $('rdetail_preview_badge').innerHTML;
1001                 GBPBadge.style.border = 0;
1002                 GBPBadgelink = document.createElement( 'a' );
1003                 GBPBadgelink.href = 'javascript:rdetailShowExtra("preview");';
1004                 GBPBadgelink.appendChild( GBPBadge );
1005                 $('rdetail_image_cell').appendChild( GBPBadgelink );
1006                 $('rdetail_preview_div').style.height = 600;
1007
1008                 /* Display the "Preview" tab in the Extras section */
1009                 unHideMe( $('rdetail_preview_link' ) );
1010         }
1011 }
1012
1013 /**
1014  *  This is called when the user clicks on the 'Preview' link.  We assume
1015  *  a preview is available from Google if this link was made visible.
1016  *
1017  * XXX I18N of Google Book Preview language attribute needed
1018  */
1019 function rdetailDisplayGBPreview() {
1020         unHideMe($('rdetail_extras_loading'));
1021         GBPreviewPane = $('rdetail_preview_div');
1022         if ( GBPreviewPane.getAttribute('loaded') == null ||
1023                 GBPreviewPane.getAttribute('loaded') == "false" ) {
1024                 google.load("books", "0", {"callback" : rdetailGBPViewerLoadCallback, "language": "en"} );
1025                 GBPreviewPane.setAttribute('loaded', 'true');
1026         }
1027 }
1028
1029 function rdetailGBPViewerLoadCallback() {
1030         hideMe($('rdetail_extras_loading'));
1031         var GBPViewer = new google.books.DefaultViewer(document.getElementById('rdetail_preview_div'));
1032         GBPViewer.load('ISBN:' + cleanISBN(record.isbn()) );
1033
1034 }