]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
fixing "opac" format for supercat and unapi
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / rdetail.js
1 /* */
2
3 attachEvt("common", "run", rdetailDraw);
4 attachEvt("rdetail", "recordDrawn", rdetailBuildStatusColumns);
5 attachEvt("rdetail", "recordDrawn", rdetailBuildInfoRows);
6
7 var record = null;
8 var cp_statuses = null;
9 var recordsCache = [];
10
11 var copyRowParent = null;
12 var copyRow = null;
13 var statusRow = null;
14 var numStatuses = null;
15 var defaultCN;
16 var callnumberCache = {};
17 var rdetailLocalOnly = true;
18 var globalCNCache       = {};
19 var localTOC;
20
21
22
23 var nextContainerIndex;
24
25 function rdetailReload() {
26         var args = {};
27         args[PARAM_LOCATION] = getNewSearchLocation();
28         args[PARAM_DEPTH] = depthSelGetDepth();
29         goTo(buildOPACLink(args));
30 }
31
32
33 function rdetailDraw() {
34
35         detachAllEvt('common','depthChanged');
36         detachAllEvt('common','locationUpdated');
37         attachEvt('common','depthChanged', rdetailReload);
38         attachEvt('common','locationUpdated', rdetailReload);
39         attachEvt('common','holdUpdated', rdetailReload);
40         attachEvt('common','holdUpdateCanceled', rdetailReload);
41
42         copyRowParent = G.ui.rdetail.cp_info_row.parentNode;
43         copyRow = copyRowParent.removeChild(G.ui.rdetail.cp_info_row);
44         statusRow = G.ui.rdetail.cp_status.parentNode;
45         statusRow.id = '__rdsrow';
46
47         G.ui.rdetail.cp_info_local.onclick = rdetailShowLocalCopies;
48         G.ui.rdetail.cp_info_all.onclick = rdetailShowAllCopies;
49
50         if(getLocation() == globalOrgTree.id())
51                 hideMe(G.ui.rdetail.cp_info_all);
52
53         var req = new Request(FETCH_RMODS, getRid());
54         req.callback(_rdetailDraw);
55         req.send();
56 }
57
58 function buildunAPISpan (span, type, id) {
59         var cgi = new CGI();
60         var d = new Date();
61
62         addCSSClass(span,'unapi-id');
63
64         span.setAttribute(
65                 'title',
66                 'tag:' + cgi.server_name + ',' +
67                         d.getFullYear() +
68                         ':' + type + '/' + id
69         );
70 }
71
72 var rdeatilMarcFetched = false;
73 function rdetailViewMarc(r,id) {
74         hideMe($('rdetail_extras_loading'));
75         $('rdetail_view_marc_box').innerHTML = r.getResultObject();
76
77         var d = new Date();
78
79         var span = div.appendChild( elem('abbr') );
80
81         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
82
83         /* add the unapi span inside a hidden div */
84         $('rdetail_view_marc_box').insertBefore(span, $('rdetail_view_marc_box').firstChild);
85 }
86
87
88 function rdetailShowLocalCopies() {
89
90         var found = false;
91         var rows = copyRowParent.getElementsByTagName("tr");
92         for( var r in rows ) {
93                 var row = rows[r];
94                 if( row.parentNode != copyRowParent ) continue; /* don't hide copy details sub-rows */
95                 if(row.id == "__rdsrow") continue;
96                 if(row.getAttribute && row.getAttribute("local")) {
97                         unHideMe(row);
98                         found = true;
99                 } else {
100                         hideMe(row);
101                 }
102         }
103
104         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
105         hideMe(G.ui.rdetail.cp_info_local);
106         unHideMe(G.ui.rdetail.cp_info_all);
107         rdetailLocalOnly = true;
108 }
109
110 function rdetailShowAllCopies() {
111         var rows = copyRowParent.getElementsByTagName("tr");
112         for( var r in rows ) 
113                 if(rows[r].getAttribute && rows[r].getAttribute("hasinfo"))
114                         unHideMe(rows[r]);
115
116         hideMe(G.ui.rdetail.cp_info_all);
117         unHideMe(G.ui.rdetail.cp_info_local);
118         hideMe(G.ui.rdetail.cp_info_none);
119         rdetailLocalOnly = false;
120 }
121
122
123 function _rdetailDraw(r) {
124         record = r.getResultObject();
125
126         runEvt('rdetail', 'recordRetrieved', record.doc_id());
127
128         G.ui.rdetail.title.appendChild(text(record.title()));
129         buildSearchLink(STYPE_AUTHOR, record.author(), G.ui.rdetail.author);
130         G.ui.rdetail.isbn.appendChild(text(cleanISBN(record.isbn())));
131         G.ui.rdetail.edition.appendChild(text(record.edition()));
132         G.ui.rdetail.pubdate.appendChild(text(record.pubdate()));
133         G.ui.rdetail.publisher.appendChild(text(record.publisher()));
134         $('rdetail_physical_desc').appendChild(text(record.physical_description()));
135         G.ui.rdetail.tor.appendChild(text(record.types_of_resource()[0]));
136         setResourcePic( G.ui.rdetail.tor_pic, record.types_of_resource()[0]);
137         G.ui.rdetail.abstr.appendChild(text(record.synopsis()));
138
139         $('rdetail_place_hold').setAttribute(
140                 'href','javascript:holdsDrawWindow("'+record.doc_id()+'");');
141
142         G.ui.rdetail.image.setAttribute("src", buildISBNSrc(cleanISBN(record.isbn())));
143         runEvt("rdetail", "recordDrawn");
144         recordsCache.push(record);
145
146         rdetailSetExtrasSelector();
147
148         var breq = new Request(FETCH_BRE, [getRid()]);
149         breq.callback( rdetailCheckDeleted );
150         breq.send();
151
152         var req = new Request(FETCH_ACONT_SUMMARY, cleanISBN(record.isbn()));
153         req.callback(rdetailHandleAddedContent);
154         req.send();
155
156         resultBuildCaches( [ record ] );
157         resultDrawSubjects();
158         resultDrawSeries();
159 }
160
161 function rdetailCheckDeleted(r) {
162         var br = r.getResultObject()[0];
163         if( br.deleted() == 1 ) {
164                 hideMe($('rdetail_place_hold'));
165                 $('rdetail_more_actions_selector').disabled = true;
166                 unHideMe($('rdetail_deleted_exp'));
167         }
168 }
169
170 function rdetailSetExtrasSelector() {
171         if(!grabUser()) return;
172         unHideMe($('rdetail_more_actions'));
173
174         var req = new Request( 
175                 FETCH_CONTAINERS, G.user.session, G.user.id(), 'biblio', 'bookbag' );
176         req.callback(rdetailAddBookbags);
177         req.send();
178 }
179
180 function rdetailAddBookbags(r) {
181
182         var containers = r.getResultObject();
183         var selector = $('rdetail_more_actions_selector');
184         var found = false;
185         var index = 3;
186         doSelectorActions(selector);
187
188         for( var i = 0; i != containers.length; i++ ) {
189                 found = true;
190                 var container = containers[i];
191                 insertSelectorVal( selector, index++, container.name(), 
192                         "container_" + container.id(), rdetailAddToBookbag,  1 );
193         }
194
195         nextContainerIndex = index;
196 }
197
198 var _actions = {};
199 function rdetailNewBookbag() {
200         var name = prompt($('rdetail_bb_new').innerHTML,"");
201         if(!name) return;
202
203         var id;
204         if( id = containerCreate( name ) ) {
205                 alert($('rdetail_bb_success').innerHTML);
206                 var selector = $('rdetail_more_actions_selector');
207                 insertSelectorVal( selector, nextContainerIndex++, name, 
208                         "container_" + id, rdetailAddToBookbag, 1 );
209                 setSelector( selector, 'start' );
210         }
211 }
212
213
214 function rdetailAddToBookbag() {
215         var selector = $('rdetail_more_actions_selector');
216         var id = selector.options[selector.selectedIndex].value;
217         setSelector( selector, 'start' );
218
219         if( containerCreateItem( id.substring(10), record.doc_id() )) {
220                 alert($('rdetail_bb_item_success').innerHTML);
221         }
222 }
223
224
225
226 var rdetailTocFetched           = false;
227 var rdetailReviewFetched        = false;
228 var rdetailMarcFetched          = false;
229
230 function rdetailShowExtra(type, args) {
231
232         hideMe($('rdetail_copy_info_div'));
233         hideMe($('rdetail_reviews_div'));
234         hideMe($('rdetail_toc_div'));
235         hideMe($('rdetail_marc_div'));
236         hideMe($('cn_browse'));
237         hideMe($('rdetail_cn_browse_div'));
238         hideMe($('rdetail_notes_div'));
239
240         removeCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
241         removeCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
242         removeCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
243         removeCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
244         removeCSSClass($('rdetail_annotation_link'), 'rdetail_extras_selected');
245         removeCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
246
247         var req;
248         switch(type) {
249
250                 case "copyinfo": 
251                         unHideMe($('rdetail_copy_info_div')); 
252                         addCSSClass($('rdetail_copy_info_link'), 'rdetail_extras_selected');
253                         break;
254
255                 case "reviews": 
256                         addCSSClass($('rdetail_reviews_link'), 'rdetail_extras_selected');
257                         unHideMe($('rdetail_reviews_div')); 
258                         if(rdetailReviewFetched) break;
259                         unHideMe($('rdetail_extras_loading'));
260                         rdetailReviewFetched = true;
261                         req = new Request(FETCH_REVIEWS, cleanISBN(record.isbn()));
262                         req.callback(rdetailShowReviews);
263                         req.send();
264                         break;
265
266                 case "toc": 
267                         addCSSClass($('rdetail_toc_link'), 'rdetail_extras_selected');
268                         unHideMe($('rdetail_toc_div'));
269                         if(rdetailTocFetched) break;
270                         unHideMe($('rdetail_extras_loading'));
271                         rdetailTocFetched = true;
272                         if(localTOC) {
273                                 hideMe($('rdetail_extras_loading'));
274                                 $('rdetail_toc_div').innerHTML = record.toc().replace(/--/g, "<br/>");
275                         } else {
276                                 req = new Request(FETCH_TOC, cleanISBN(record.isbn()));
277                                 req.callback(rdetailShowTOC);
278                                 req.send();
279                         }
280                         break;
281
282                 case "marc": 
283                         addCSSClass($('rdetail_viewmarc_link'), 'rdetail_extras_selected');
284                         unHideMe($('rdetail_marc_div')); 
285                         if(rdetailMarcFetched) return;
286                         unHideMe($('rdetail_extras_loading'));
287                         rdetailMarcFetched = true;
288                         req = new Request( FETCH_MARC_HTML, record.doc_id() );
289                         req.callback(rdetailViewMarc); 
290                         req.send();
291                         break;
292
293                 case 'cn':
294                         addCSSClass($('rdetail_viewcn_link'), 'rdetail_extras_selected');
295                         unHideMe($('rdetail_cn_browse_div'));
296                         rdetailShowCNBrowse(defaultCN, getLocation(), null, true);
297                         break;
298
299         }
300 }
301
302 function rdetailVolumeDetails(args) {
303         var row = $(args.rowid);
304         var tbody = row.parentNode;
305         cpdBuild( tbody, row, record, args.cn, args.org, args.depth );
306         return;
307 }
308
309
310 function rdetailBuildCNList() {
311
312         var select = $('cn_browse_selector');
313         var index = 0;
314         var arr = [];
315         for( var cn in callnumberCache ) arr.push( cn );
316         arr.sort();
317
318         if( arr.length == 0 ) {
319                 hideMe($('rdetail_cn_browse_select_div'));
320                 return;
321         }
322
323         for( var i in arr ) {
324                 var cn = arr[i];
325                 var opt = new Option(cn);
326                 select.options[index++] = opt;
327         }
328         select.onchange = rdetailGatherCN;
329 }
330
331 function rdetailGatherCN() {
332         var cn = getSelectorVal($('cn_browse_selector'));
333         rdetailShowCNBrowse( cn, getLocation(), getDepth(), true );
334         setSelector( $('cn_browse_selector'), cn );
335 }
336
337
338 function rdetailShowCNBrowse( cn, loc, depth, fromOnclick ) {
339         if(!cn) return;
340         rdetailBuildCNList();
341         setSelector( $('cn_browse_selector'), cn );
342         hideMe($('rdetail_copy_info_div'));
343         hideMe($('rdetail_reviews_div'));
344         hideMe($('rdetail_toc_div'));
345         hideMe($('rdetail_marc_div'));
346         unHideMe($('rdetail_cn_browse_div'));
347         unHideMe($('cn_browse'));
348         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
349         cnBrowseGo(cn, loc, depth);
350 }
351
352 function rdetailHandleAddedContent(r) {
353         var resp = r.getResultObject();
354         if( resp.Review == 'true' ) unHideMe($('rdetail_reviews_link'));
355         if( resp.TOC == 'true' ) {
356                 unHideMe($('rdetail_toc_link'));
357         } else {
358                 if( record.toc() ) {
359                         localTOC = true;
360                         unHideMe($('rdetail_toc_link'));
361                 }
362         }
363 }
364
365
366 function rdetailShowReviews(r) {
367         hideMe($('rdetail_extras_loading'));
368         var res = r.getResultObject();
369         var par = $('rdetail_reviews_div');
370         var template = par.removeChild($('rdetail_review_template'));
371         if( res && res.length > 0 ) {
372                 unHideMe($('rdetail_reviews_link'));
373                 for( var i = 0; i != res.length; i++ ) {
374                         var rev = res[i];       
375                         if( rev.text && rev.info ) {
376                                 var node = template.cloneNode(true);
377                                 $n(node, 'review_header').appendChild(text(rev.info));
378                                 $n(node, 'review_text').appendChild(text(rev.text));
379                                 par.appendChild(node);
380                         }
381                 }
382         }
383 }
384
385 function rdetailShowTOC(r) {
386         hideMe($('rdetail_extras_loading'));
387         var resp = r.getResultObject();
388         if(resp) {
389                 unHideMe($('rdetail_toc_link'));
390                 $('rdetail_toc_div').innerHTML = resp;
391         }
392 }
393
394
395 function rdetailBuildInfoRows() {
396         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
397         req.callback(_rdetailBuildInfoRows);
398         req.send();
399 }
400
401 /* pre-allocate the copy info table with all org units in correct order */
402 function _rdetailRows(node) {
403
404         if(node) {
405
406                 var row = copyRow.cloneNode(true);
407                 row.id = "cp_info_" + node.id();
408
409                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
410                 var cntd  = findNodeByName( row, config.names.rdetail.cn_cell );
411                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
412                 var actions = $n(row, 'rdetail_actions_cell');
413         
414                 var p = libtd.getElementsByTagName('a')[0];
415                 libtd.insertBefore(text(node.name()), p);
416                 //libtd.appendChild(text(node.name()));
417                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
418         
419                 if(!findOrgType(node.ou_type()).can_have_vols()) {
420
421                         row.removeChild(cntd);
422                         row.removeChild(cpctd);
423                         row.removeChild(actions);
424                         row.setAttribute('novols', '1');
425
426                         libtd.setAttribute("colspan", numStatuses + 3 );
427                         libtd.colSpan = numStatuses + 3;
428                         addCSSClass(row, 'copy_info_region_row');
429                 } 
430         
431                 copyRowParent.appendChild(row);
432
433         } else { node = globalOrgTree; }
434
435         for( var c in node.children() ) 
436                 _rdetailRows(node.children()[c]);
437 }
438
439 function rdetailCNPrint(orgid, cn) {
440         /*
441         var arr = cpdBuildPrintWindow( record, orgid);
442         var win = arr[0];
443         var div = arr[1];
444         */
445         var div = cpdBuildPrintWindow( record, orgid);
446         var template = div.removeChild($n(div, 'cnrow'));
447         var rowNode = $("cp_info_" + orgid);
448         cpdStylePopupWindow(div);
449         openWindow(div.innerHTML);
450         /*
451         win.document.body.innerHTML = div.innerHTML;
452         */
453 }
454
455 /* walk through the copy info and build rows where necessary */
456 var localCNFound = false;
457 var ctr = 0;
458 function _rdetailBuildInfoRows(r) {
459
460         _rdetailRows();
461
462         var summary = r.getResultObject();
463         if(!summary) return;
464
465         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
466                 G.ui.rdetail.cp_info_loading);
467
468         var found = false;
469         for( var i = 0; i < summary.length; i++ ) {
470
471                 var arr = summary[i];
472                 globalCNCache[arr[1]] = 1;
473                 var thisOrg = findOrgUnit(arr[0]);
474                 var rowNode = $("cp_info_" + thisOrg.id());
475                 if(!rowNode) continue;
476
477                 /* set up the print link */
478                 var pn = rowNode.previousSibling;
479                 if( pn && pn.getAttribute('novols') ) {
480                         var a = $n(pn, 'lib_print_link');
481                         a.setAttribute('href',
482                                 'javascript:rdetailCNPrint('+thisOrg.id()+',"'+arr[1]+'");');
483                         /*unHideMe(a);*/
484                         /* make this more smarter */
485                 }
486
487                 if(rowNode.getAttribute("used")) {
488
489                         if( rowNode.nextSibling )
490                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
491                         else
492                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
493                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
494                         n.appendChild(text(thisOrg.name()));
495                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
496                         rowNode.id = "cp_info_" + thisOrg.id() + '_' + (++ctr); /* ensure a unique id */
497
498                 } else {
499                         rowNode.setAttribute("used", "1");
500                 }
501
502                 var cpc_temp = rowNode.removeChild(
503                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
504
505                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
506
507                 var isLocal = false;
508                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
509                         found = true; 
510                         isLocal = true; 
511                         if(!localCNFound) {
512                                 localCNFound = true;
513                                 defaultCN = arr[1];
514                         }
515                 }
516
517                 if(isLocal) unHideMe(rowNode);
518
519                 rdetailSetPath( thisOrg, isLocal );
520                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal, thisOrg );
521
522                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
523         }
524
525         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
526 }
527
528
529 function rdetailBuildBrowseInfo(row, cn, local, orgNode) {
530
531         /* used for building the shelf browser */
532         if(local) {
533                 var cache = callnumberCache[cn];
534                 if( cache ) cache.count++;
535                 else callnumberCache[cn] = { count : 1 };
536         }
537
538         var depth = getDepth();
539         if( !local ) depth = findOrgDepth(globalOrgTree);
540
541         $n(row, 'rdetail_callnumber_cell').appendChild(text(cn));
542
543         _debug('setting action clicks for cn ' + cn);
544
545         var dHref = 'javascript:rdetailVolumeDetails('+
546                 '{rowid : "'+row.id+'", cn :"'+cn+'", depth:"'+depth+'", org:"'+orgNode.id()+'", local: '+local+'});';
547
548         var bHref = 'javascript:rdetailShowCNBrowse("' + cn + '", '+orgNode.id()+', "'+depth+'");'; 
549
550         $n(row, 'details').setAttribute('href', dHref);
551         $n(row, 'browse').setAttribute('href', bHref);
552 }
553
554
555 /* sets the path to org as 'active' and displays the 
556         path if it's local */
557 function rdetailSetPath(org, local) {
558         if( findOrgDepth(org) == 0 ) return;
559         var row = $("cp_info_" + org.id());
560         row.setAttribute("hasinfo", "1");
561         if(local) {
562                 unHideMe(row);
563                 row.setAttribute("local", "1");
564         }
565         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
566 }
567
568
569
570 /**
571  * Append all the statuses for a give summary to the 
572  * copy summary table */
573 function rdetailApplyStatuses( row, template, statuses ) {
574         for( var j in _statusPositions ) {
575                 var stat = _statusPositions[j];
576                 var val = statuses[stat.id()];
577                 var nn = template.cloneNode(true);
578                 if(val) nn.appendChild(text(val));
579                 else nn.appendChild(text(0));
580                 row.appendChild(nn);    
581         }
582 }
583
584
585 /* --------------------------------------------------------------------- */
586 var _statusPositions = {};
587
588 /**
589  * Add one td (creating a new column) to the copy summary
590  * table for each holdable copy status
591  */
592 function rdetailBuildStatusColumns() {
593
594         rdetailGrabCopyStatuses();
595         var parent = statusRow;
596         var template = parent.removeChild(G.ui.rdetail.cp_status);
597
598         var i = 0;
599         for( i = 0; i < cp_statuses.length; i++ ) {
600
601                 var c = cp_statuses[i];
602
603                 if(c && c.holdable()) {
604
605                         var name = c.name();
606                         _statusPositions[i] = c;
607                         var node = template.cloneNode(true);
608                         var data = findNodeByName( node, config.names.rdetail.cp_status);
609
610                         data.appendChild(text(name));
611                         parent.appendChild(node);
612                 }       
613         }       
614
615         numStatuses = 0;
616         for(x in _statusPositions) numStatuses++; 
617 }
618
619 function rdetailGrabCopyStatuses() {
620         if(cp_statuses) return cp_statuses;
621    var req = new Request(FETCH_COPY_STATUSES);
622    req.send(true);
623         cp_statuses = req.result();
624         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
625 }
626
627 function _rdetailSortStatuses(a, b) {
628         return parseInt(a.id()) - parseInt(b.id());
629 }
630
631