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