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