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