]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
4e50256293783c6a941ec342ee6c466169f77aa9
[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
20 var nextContainerIndex;
21
22 function rdetailDraw() {
23         copyRowParent = G.ui.rdetail.cp_info_row.parentNode;
24         copyRow = copyRowParent.removeChild(G.ui.rdetail.cp_info_row);
25         statusRow = G.ui.rdetail.cp_status.parentNode;
26         statusRow.id = '__rdsrow';
27
28         G.ui.rdetail.cp_info_local.onclick = rdetailShowLocalCopies;
29         G.ui.rdetail.cp_info_all.onclick = rdetailShowAllCopies;
30
31         if(getLocation() == globalOrgTree.id())
32                 hideMe(G.ui.rdetail.cp_info_all);
33
34         var req = new Request(FETCH_RMODS, getRid());
35         req.callback(_rdetailDraw);
36         req.send();
37 }
38
39 function buildunAPISpan (span, type, id) {
40         var cgi = new CGI();
41         var d = new Date();
42
43         addCSSClass(span,'unapi-uri');
44
45         span.appendChild(text('unAPI'));
46         span.setAttribute(
47                 'title',
48                 'tag:' + cgi.server_name + ',' +
49                         d.getFullYear() +
50                         ':' + type + '/' + id
51         );
52 }
53
54 var rdeatilMarcFetched = false;
55 function rdetailViewMarc(r,id) {
56         hideMe($('rdetail_extras_loading'));
57         $('rdetail_view_marc_box').innerHTML = r.getResultObject();
58
59         var d = new Date();
60
61         var div = elem('div', { "class" : 'hide_me' });
62         var span = div.appendChild( elem('span') );
63
64         buildunAPISpan( span, 'biblio-record_entry', record.doc_id() );
65
66         /* add the unapi span inside a hidden div */
67         $('rdetail_view_marc_box').insertBefore(div, $('rdetail_view_marc_box').firstChild);
68 }
69
70
71 function rdetailShowLocalCopies() {
72
73         var found = false;
74         var rows = copyRowParent.getElementsByTagName("tr");
75         for( var r in rows ) {
76                 if(rows[r].id == "__rdsrow") continue;
77                 hideMe(rows[r]);
78                 if(rows[r].getAttribute && rows[r].getAttribute("local")) {
79                         unHideMe(rows[r]);
80                         found = true;
81                 }
82         }
83
84         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
85         hideMe(G.ui.rdetail.cp_info_local);
86         unHideMe(G.ui.rdetail.cp_info_all);
87         rdetailLocalOnly = true;
88 }
89
90 function rdetailShowAllCopies() {
91         var rows = copyRowParent.getElementsByTagName("tr");
92         for( var r in rows ) 
93                 if(rows[r].getAttribute && rows[r].getAttribute("hasinfo"))
94                         unHideMe(rows[r]);
95
96         hideMe(G.ui.rdetail.cp_info_all);
97         unHideMe(G.ui.rdetail.cp_info_local);
98         hideMe(G.ui.rdetail.cp_info_none);
99         rdetailLocalOnly = false;
100 }
101
102
103 function _rdetailDraw(r) {
104         record = r.getResultObject();
105
106         runEvt('rdetail', 'recordRetrieved', record.doc_id());
107
108         G.ui.rdetail.title.appendChild(text(record.title()));
109         buildSearchLink(STYPE_AUTHOR, record.author(), G.ui.rdetail.author);
110         G.ui.rdetail.isbn.appendChild(text(cleanISBN(record.isbn())));
111         G.ui.rdetail.edition.appendChild(text(record.edition()));
112         G.ui.rdetail.pubdate.appendChild(text(record.pubdate()));
113         G.ui.rdetail.publisher.appendChild(text(record.publisher()));
114         G.ui.rdetail.tor.appendChild(text(record.types_of_resource()[0]));
115         setResourcePic( G.ui.rdetail.tor_pic, record.types_of_resource()[0]);
116         G.ui.rdetail.abstr.appendChild(text(record.synopsis()));
117
118
119         $('rdetail_place_hold').setAttribute(
120                 'href','javascript:holdsDrawWindow("'+record.doc_id()+'");');
121
122         G.ui.rdetail.image.setAttribute("src", buildISBNSrc(cleanISBN(record.isbn())));
123         runEvt("rdetail", "recordDrawn");
124         recordsCache.push(record);
125
126         rdetailSetExtrasSelector();
127
128         var req = new Request(FETCH_ACONT_SUMMARY, cleanISBN(record.isbn()));
129         req.callback(rdetailHandleAddedContent);
130         req.send();
131
132         resultBuildCaches( [ record ] );
133         resultDrawSubjects();
134         resultDrawSeries();
135 }
136
137 function rdetailSetExtrasSelector() {
138         if(!grabUser()) return;
139         unHideMe($('rdetail_more_actions'));
140
141         var req = new Request( 
142                 FETCH_CONTAINERS, G.user.session, G.user.id(), 'biblio', 'bookbag' );
143         req.callback(rdetailAddBookbags);
144         req.send();
145 }
146
147 function rdetailAddBookbags(r) {
148
149         var containers = r.getResultObject();
150         var selector = $('rdetail_more_actions_selector');
151         var found = false;
152         var index = 3;
153         doSelectorActions(selector);
154
155         for( var i = 0; i != containers.length; i++ ) {
156                 found = true;
157                 var container = containers[i];
158                 insertSelectorVal( selector, index++, container.name(), 
159                         "container_" + container.id(), rdetailAddToBookbag,  1 );
160         }
161
162         nextContainerIndex = index;
163         if(!found) insertSelectorVal( selector, 3, "name", "value", 1 );
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                         var req = new Request(FETCH_TOC, cleanISBN(record.isbn()));
230                         req.callback(rdetailShowTOC);
231                         req.send();
232                         break;
233
234                 case "marc": 
235                         unHideMe($('rdetail_marc_div')); 
236                         if(rdetailMarcFetched) return;
237                         unHideMe($('rdetail_extras_loading'));
238                         rdetailMarcFetched = true;
239                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
240                         req.callback(rdetailViewMarc); 
241                         req.send();
242                         break;
243
244                 case 'cn':
245                         unHideMe($('rdetail_cn_browse_div'));
246                         rdetailShowCNBrowse(defaultCN, null, true);
247                         break;
248
249                 case 'notes':
250                         unHideMe($('rdetail_notes_div'));
251                         break;
252         }
253 }
254
255 function rdetailBuildCNList() {
256         var select = $('cn_browse_selector');
257         var index = 0;
258         var arr = [];
259         for( var cn in callnumberCache ) arr.push( cn );
260         arr.sort();
261
262         if( arr.length == 0 ) {
263                 hideMe($('rdetail_cn_browse_select_div'));
264                 return;
265         }
266
267         for( var i in arr ) {
268                 var cn = arr[i];
269                 var opt = new Option(cn);
270                 select.options[index++] = opt;
271         }
272         select.onchange = rdetailGatherCN;
273 }
274
275 function rdetailGatherCN() {
276         var cn = getSelectorVal($('cn_browse_selector'));
277         rdetailShowCNBrowse( cn, getDepth(), true );
278         setSelector( $('cn_browse_selector'), cn );
279 }
280
281
282 function rdetailShowCNBrowse( cn, depth, fromOnclick ) {
283         if(!cn) return;
284         rdetailBuildCNList();
285         setSelector( $('cn_browse_selector'), cn );
286         hideMe($('rdetail_copy_info_div'));
287         hideMe($('rdetail_reviews_div'));
288         hideMe($('rdetail_toc_div'));
289         hideMe($('rdetail_marc_div'));
290         unHideMe($('rdetail_cn_browse_div'));
291         unHideMe($('cn_browse'));
292         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
293         cnBrowseGo(cn, depth);
294 }
295
296 function rdetailHandleAddedContent(r) {
297         var resp = r.getResultObject();
298         if( resp.Review == 'true' ) unHideMe($('rdetail_reviews_link'));
299         if( resp.TOC == 'true' ) unHideMe($('rdetail_toc_link'));
300 }
301
302
303 function rdetailShowReviews(r) {
304         hideMe($('rdetail_extras_loading'));
305         var res = r.getResultObject();
306         var par = $('rdetail_reviews_div');
307         var template = par.removeChild($('rdetail_review_template'));
308         if( res && res.length > 0 ) {
309                 unHideMe($('rdetail_reviews_link'));
310                 for( var i = 0; i != res.length; i++ ) {
311                         var rev = res[i];       
312                         if( rev.text && rev.info ) {
313                                 var node = template.cloneNode(true);
314                                 $n(node, 'review_header').appendChild(text(rev.info));
315                                 $n(node, 'review_text').appendChild(text(rev.text));
316                                 par.appendChild(node);
317                         }
318                 }
319         }
320 }
321
322 function rdetailShowTOC(r) {
323         hideMe($('rdetail_extras_loading'));
324         var resp = r.getResultObject();
325         if(resp) {
326                 unHideMe($('rdetail_toc_link'));
327                 $('rdetail_toc_div').innerHTML = resp;
328         }
329 }
330
331
332 function rdetailBuildInfoRows() {
333         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
334         req.callback(_rdetailBuildInfoRows);
335         req.send();
336 }
337
338 /* pre-allocate the copy info table with all org units in correct order */
339 function _rdetailRows(node) {
340
341         if(node) {
342
343                 var row = copyRow.cloneNode(true);
344                 row.id = "cp_info_" + node.id();
345
346                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
347                 var cntd = findNodeByName( row, config.names.rdetail.cn_cell );
348                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
349         
350                 libtd.appendChild(text(node.name()));
351                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
352         
353                 if(!findOrgType(node.ou_type()).can_have_vols()) {
354
355                         row.removeChild(cntd);
356                         row.removeChild(cpctd);
357
358                         libtd.setAttribute("colspan", numStatuses + 2 );
359                         libtd.colSpan = numStatuses + 2;
360                         addCSSClass(row, 'copy_info_region_row');
361                 } 
362         
363                 copyRowParent.appendChild(row);
364
365         } else { node = globalOrgTree; }
366
367         for( var c in node.children() ) 
368                 _rdetailRows(node.children()[c]);
369 }
370
371 /* walk through the copy info and build rows where necessary */
372 var localCNFound = false;
373 function _rdetailBuildInfoRows(r) {
374
375         _rdetailRows();
376
377         var summary = r.getResultObject();
378         if(!summary) return;
379
380         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
381                 G.ui.rdetail.cp_info_loading);
382
383         var found = false;
384         for( var i = 0; i < summary.length; i++ ) {
385
386                 var arr = summary[i];
387                 globalCNCache[arr[1]] = 1;
388                 var thisOrg = findOrgUnit(arr[0]);
389                 var rowNode = $("cp_info_" + thisOrg.id());
390                 if(!rowNode) continue;
391
392                 if(rowNode.getAttribute("used")) {
393
394                         if( rowNode.nextSibling )
395                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
396                         else
397                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
398                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
399                         n.appendChild(text(thisOrg.name()));
400                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
401
402                 } else rowNode.setAttribute("used", "1");
403
404                 var cpc_temp = rowNode.removeChild(
405                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
406
407                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
408
409                 var isLocal = false;
410                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
411                         found = true; 
412                         isLocal = true; 
413                         if(!localCNFound) {
414                                 localCNFound = true;
415                                 defaultCN = arr[1];
416                         }
417                 }
418                 rdetailSetPath( thisOrg, isLocal );
419                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal );
420
421                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
422         }
423
424         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
425
426         /* now that we know what CN's we have, grab the associated notes */
427         rdetailFetchNotes();
428 }
429
430 function rdetailFetchNotes() {
431         var req = new Request(FETCH_BIBLIO_NOTES, record.doc_id());
432         req.callback(rdetailDrawNotes);
433         req.send();
434 }
435
436 var rdetailNotesTemplate;
437 function rdetailDrawNotes(r) {
438         var notes = r.getResultObject();
439
440         var tbody = $('rdetail_notes_tbody');
441         if(!rdetailNotesTemplate) 
442                 rdetailNotesTemplate = tbody.removeChild($('rdetail_notes_row'));
443
444         var found = false;
445         for( var t in notes.titles ) {
446                 var note = notes.copies[c];
447                 /* these need to go into a title notes 
448                 section different from the copy/cn notes (on the title summary?) */
449         }
450
451         for( var v in notes.volumes ) {
452                 var note = notes.copies[c];
453                 var row = rdetailNotesTemplate.cloneNode(true);
454                 found = true;
455         }
456
457         for( var c in notes.copies ) {
458                 found = true;
459                 var copynode = notes.copies[c];
460                 var copy = copynode.id;
461                 var nts = copynode.notes;
462                 for( var n in nts ) {
463                         var note = nts[n];
464                         var row = rdetailNotesTemplate.cloneNode(true);
465                         $n(row, 'rdetail_notes_title').appendChild(text(note.title()));
466                         $n(row, 'rdetail_notes_value').appendChild(text(note.value()));
467                         tbody.appendChild(row);
468                 }
469         }
470
471         if(found) unHideMe($('rdetail_viewnotes_link'));
472 }
473
474 function rdetailBuildBrowseInfo(row, cn, local) {
475         /* used for building the shelf browser */
476         if(local) {
477                 var cache = callnumberCache[cn];
478                 if( cache ) cache.count++;
479                 else callnumberCache[cn] = { count : 1 };
480         }
481
482         var depth = getDepth();
483         if( !local ) depth = findOrgDepth(globalOrgTree);
484         var a = elem("a", {href:'javascript:rdetailShowCNBrowse("' + cn + '", "'+depth+'");' }, cn);
485         addCSSClass(a, 'classic_link');
486         findNodeByName( row, config.names.rdetail.cn_cell ).appendChild(a);
487 }
488
489 /* sets the path to org as 'active' and displays the 
490         path if it's local */
491 function rdetailSetPath(org, local) {
492         if( findOrgDepth(org) == 0 ) return;
493         var row = $("cp_info_" + org.id());
494         row.setAttribute("hasinfo", "1");
495         if(local) {
496                 unHideMe(row);
497                 row.setAttribute("local", "1");
498         }
499         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
500 }
501
502 function rdetailApplyStatuses( row, template, statuses ) {
503         for( var j in _statusPositions ) {
504                 var stat = _statusPositions[j];
505                 var val = statuses[stat.id()];
506                 var nn = template.cloneNode(true);
507                 if(val) nn.appendChild(text(val));
508                 else nn.appendChild(text(0));
509                 row.appendChild(nn);    
510         }
511 }
512
513
514 /* --------------------------------------------------------------------- */
515 var _statusPositions = {};
516
517 function rdetailBuildStatusColumns() {
518
519         rdetailGrabCopyStatuses();
520         var parent = statusRow;
521         var template = parent.removeChild(G.ui.rdetail.cp_status);
522
523         var i = 0;
524         for( i = 0; i < cp_statuses.length; i++ ) {
525
526                 var c = cp_statuses[i];
527
528                 if(c && c.holdable()) {
529
530                         var name = c.name();
531                         _statusPositions[i] = c;
532                         var node = template.cloneNode(true);
533                         var data = findNodeByName( node, config.names.rdetail.cp_status);
534
535                         data.appendChild(text(name));
536                         parent.appendChild(node);
537                 }       
538         }       
539
540         numStatuses = 0;
541         for(x in _statusPositions) numStatuses++; 
542 }
543
544 function rdetailGrabCopyStatuses() {
545         if(cp_statuses) return cp_statuses;
546    var req = new Request(FETCH_COPY_STATUSES);
547    req.send(true);
548         cp_statuses = req.result();
549         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
550 }
551
552 function _rdetailSortStatuses(a, b) {
553         return parseInt(a.id()) - parseInt(b.id());
554 }
555
556