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