4f93c1f217e6700217e5e2e8c699a15aff106e7a
[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) {
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 }
259
260 function rdetailBuildCNList() {
261         var select = $('cn_browse_selector');
262         var index = 0;
263         var arr = [];
264         for( var cn in callnumberCache ) arr.push( cn );
265         arr.sort();
266
267         if( arr.length == 0 ) {
268                 hideMe($('rdetail_cn_browse_select_div'));
269                 return;
270         }
271
272         for( var i in arr ) {
273                 var cn = arr[i];
274                 var opt = new Option(cn);
275                 select.options[index++] = opt;
276         }
277         select.onchange = rdetailGatherCN;
278 }
279
280 function rdetailGatherCN() {
281         var cn = getSelectorVal($('cn_browse_selector'));
282         rdetailShowCNBrowse( cn, getDepth(), true );
283         setSelector( $('cn_browse_selector'), cn );
284 }
285
286
287 function rdetailShowCNBrowse( cn, depth, fromOnclick ) {
288         if(!cn) return;
289         rdetailBuildCNList();
290         setSelector( $('cn_browse_selector'), cn );
291         hideMe($('rdetail_copy_info_div'));
292         hideMe($('rdetail_reviews_div'));
293         hideMe($('rdetail_toc_div'));
294         hideMe($('rdetail_marc_div'));
295         unHideMe($('rdetail_cn_browse_div'));
296         unHideMe($('cn_browse'));
297         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
298         cnBrowseGo(cn, depth);
299 }
300
301 function rdetailHandleAddedContent(r) {
302         var resp = r.getResultObject();
303         if( resp.Review == 'true' ) unHideMe($('rdetail_reviews_link'));
304         if( resp.TOC == 'true' ) {
305                 unHideMe($('rdetail_toc_link'));
306         } else {
307                 if( record.toc() ) {
308                         localTOC = true;
309                         unHideMe($('rdetail_toc_link'));
310                 }
311         }
312 }
313
314
315 function rdetailShowReviews(r) {
316         hideMe($('rdetail_extras_loading'));
317         var res = r.getResultObject();
318         var par = $('rdetail_reviews_div');
319         var template = par.removeChild($('rdetail_review_template'));
320         if( res && res.length > 0 ) {
321                 unHideMe($('rdetail_reviews_link'));
322                 for( var i = 0; i != res.length; i++ ) {
323                         var rev = res[i];       
324                         if( rev.text && rev.info ) {
325                                 var node = template.cloneNode(true);
326                                 $n(node, 'review_header').appendChild(text(rev.info));
327                                 $n(node, 'review_text').appendChild(text(rev.text));
328                                 par.appendChild(node);
329                         }
330                 }
331         }
332 }
333
334 function rdetailShowTOC(r) {
335         hideMe($('rdetail_extras_loading'));
336         var resp = r.getResultObject();
337         if(resp) {
338                 unHideMe($('rdetail_toc_link'));
339                 $('rdetail_toc_div').innerHTML = resp;
340         }
341 }
342
343
344 function rdetailBuildInfoRows() {
345         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
346         req.callback(_rdetailBuildInfoRows);
347         req.send();
348 }
349
350 /* pre-allocate the copy info table with all org units in correct order */
351 function _rdetailRows(node) {
352
353         if(node) {
354
355                 var row = copyRow.cloneNode(true);
356                 row.id = "cp_info_" + node.id();
357
358                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
359                 var cntd = findNodeByName( row, config.names.rdetail.cn_cell );
360                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
361         
362                 libtd.appendChild(text(node.name()));
363                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
364         
365                 if(!findOrgType(node.ou_type()).can_have_vols()) {
366
367                         row.removeChild(cntd);
368                         row.removeChild(cpctd);
369
370                         libtd.setAttribute("colspan", numStatuses + 2 );
371                         libtd.colSpan = numStatuses + 2;
372                         addCSSClass(row, 'copy_info_region_row');
373                 } 
374         
375                 copyRowParent.appendChild(row);
376
377         } else { node = globalOrgTree; }
378
379         for( var c in node.children() ) 
380                 _rdetailRows(node.children()[c]);
381 }
382
383 /* walk through the copy info and build rows where necessary */
384 var localCNFound = false;
385 function _rdetailBuildInfoRows(r) {
386
387         _rdetailRows();
388
389         var summary = r.getResultObject();
390         if(!summary) return;
391
392         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
393                 G.ui.rdetail.cp_info_loading);
394
395         var found = false;
396         for( var i = 0; i < summary.length; i++ ) {
397
398                 var arr = summary[i];
399                 globalCNCache[arr[1]] = 1;
400                 var thisOrg = findOrgUnit(arr[0]);
401                 var rowNode = $("cp_info_" + thisOrg.id());
402                 if(!rowNode) continue;
403
404                 if(rowNode.getAttribute("used")) {
405
406                         if( rowNode.nextSibling )
407                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
408                         else
409                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
410                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
411                         n.appendChild(text(thisOrg.name()));
412                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
413
414                 } else rowNode.setAttribute("used", "1");
415
416                 var cpc_temp = rowNode.removeChild(
417                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
418
419                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
420
421                 var isLocal = false;
422                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
423                         found = true; 
424                         isLocal = true; 
425                         if(!localCNFound) {
426                                 localCNFound = true;
427                                 defaultCN = arr[1];
428                         }
429                 }
430                 rdetailSetPath( thisOrg, isLocal );
431                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal );
432
433                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
434         }
435
436         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
437
438         /* now that we know what CN's we have, grab the associated notes */
439         rdetailFetchNotes();
440 }
441
442 function rdetailFetchNotes() {
443         var req = new Request(FETCH_BIBLIO_NOTES, record.doc_id());
444         req.callback(rdetailDrawNotes);
445         req.send();
446 }
447
448 var rdetailNotesTemplate;
449 function rdetailDrawNotes(r) {
450         var notes = r.getResultObject();
451         if(!notes) return;
452
453         var tbody = $('rdetail_notes_tbody');
454         if(!rdetailNotesTemplate) 
455                 rdetailNotesTemplate = tbody.removeChild($('rdetail_notes_row'));
456
457         var found = false;
458         for( var t in notes.titles ) {
459                 var note = notes.copies[c];
460                 /* these need to go into a title notes 
461                 section different from the copy/cn notes (on the title summary?) */
462         }
463
464         for( var v in notes.volumes ) {
465                 var note = notes.copies[c];
466                 var row = rdetailNotesTemplate.cloneNode(true);
467                 found = true;
468         }
469
470         for( var c in notes.copies ) {
471                 found = true;
472                 var copynode = notes.copies[c];
473                 var copy = copynode.id;
474                 var nts = copynode.notes;
475                 for( var n in nts ) {
476                         var note = nts[n];
477                         var row = rdetailNotesTemplate.cloneNode(true);
478                         $n(row, 'rdetail_notes_title').appendChild(text(note.title()));
479                         $n(row, 'rdetail_notes_value').appendChild(text(note.value()));
480                         tbody.appendChild(row);
481                 }
482         }
483
484         if(found) unHideMe($('rdetail_viewnotes_link'));
485 }
486
487 function rdetailBuildBrowseInfo(row, cn, local) {
488         /* used for building the shelf browser */
489         if(local) {
490                 var cache = callnumberCache[cn];
491                 if( cache ) cache.count++;
492                 else callnumberCache[cn] = { count : 1 };
493         }
494
495         var depth = getDepth();
496         if( !local ) depth = findOrgDepth(globalOrgTree);
497         var a = elem("a", {href:'javascript:rdetailShowCNBrowse("' + cn + '", "'+depth+'");' }, cn);
498         addCSSClass(a, 'classic_link');
499         findNodeByName( row, config.names.rdetail.cn_cell ).appendChild(a);
500 }
501
502 /* sets the path to org as 'active' and displays the 
503         path if it's local */
504 function rdetailSetPath(org, local) {
505         if( findOrgDepth(org) == 0 ) return;
506         var row = $("cp_info_" + org.id());
507         row.setAttribute("hasinfo", "1");
508         if(local) {
509                 unHideMe(row);
510                 row.setAttribute("local", "1");
511         }
512         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
513 }
514
515 function rdetailApplyStatuses( row, template, statuses ) {
516         for( var j in _statusPositions ) {
517                 var stat = _statusPositions[j];
518                 var val = statuses[stat.id()];
519                 var nn = template.cloneNode(true);
520                 if(val) nn.appendChild(text(val));
521                 else nn.appendChild(text(0));
522                 row.appendChild(nn);    
523         }
524 }
525
526
527 /* --------------------------------------------------------------------- */
528 var _statusPositions = {};
529
530 function rdetailBuildStatusColumns() {
531
532         rdetailGrabCopyStatuses();
533         var parent = statusRow;
534         var template = parent.removeChild(G.ui.rdetail.cp_status);
535
536         var i = 0;
537         for( i = 0; i < cp_statuses.length; i++ ) {
538
539                 var c = cp_statuses[i];
540
541                 if(c && c.holdable()) {
542
543                         var name = c.name();
544                         _statusPositions[i] = c;
545                         var node = template.cloneNode(true);
546                         var data = findNodeByName( node, config.names.rdetail.cp_status);
547
548                         data.appendChild(text(name));
549                         parent.appendChild(node);
550                 }       
551         }       
552
553         numStatuses = 0;
554         for(x in _statusPositions) numStatuses++; 
555 }
556
557 function rdetailGrabCopyStatuses() {
558         if(cp_statuses) return cp_statuses;
559    var req = new Request(FETCH_COPY_STATUSES);
560    req.send(true);
561         cp_statuses = req.result();
562         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
563 }
564
565 function _rdetailSortStatuses(a, b) {
566         return parseInt(a.id()) - parseInt(b.id());
567 }
568
569