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