]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
9c10ce22a06279c6675461b1b5c6bc012a4432f3
[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
19 var nextContainerIndex;
20
21 function rdetailDraw() {
22
23
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 var rdeatilMarcFetched = false;
41 function rdetailViewMarc(r,id) {
42         hideMe($('rdetail_extras_loading'));
43         $('rdetail_view_marc_box').innerHTML = r.getResultObject();
44
45         var d = new Date();
46
47         /* add the unapi span inside a hidden div */
48         $('rdetail_view_marc_box').insertBefore(
49                 elem('div', { class : 'hide_me' }).appendChild(
50                         elem(   'span',
51                                 { class : 'unAPI',
52                                   title :
53                                         'tag:open-ils.org,' +
54                                         d.getFullYear() + '-' +
55                                         (1 + d.getMonth()) + '-' +
56                                         d.getDate() +
57                                         ':biblio-record_entry/' + id },
58                                   'tag:open-ils.org,' +
59                                   d.getFullYear() + '-' +
60                                   (1 + d.getMonth()) + '-' +
61                                   d.getDate() +
62                                   ':biblio-record_entry/' + id
63                         )
64                 ),
65                 $('rdetail_view_marc_box').firstChild
66         );
67 }
68
69
70 function rdetailShowLocalCopies() {
71
72         var found = false;
73         var rows = copyRowParent.getElementsByTagName("tr");
74         for( var r in rows ) {
75                 if(rows[r].id == "__rdsrow") continue;
76                 hideMe(rows[r]);
77                 if(rows[r].getAttribute && rows[r].getAttribute("local")) {
78                         unHideMe(rows[r]);
79                         found = true;
80                 }
81         }
82
83         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
84         hideMe(G.ui.rdetail.cp_info_local);
85         unHideMe(G.ui.rdetail.cp_info_all);
86         rdetailLocalOnly = true;
87 }
88
89 function rdetailShowAllCopies() {
90         var rows = copyRowParent.getElementsByTagName("tr");
91         for( var r in rows ) 
92                 if(rows[r].getAttribute && rows[r].getAttribute("hasinfo"))
93                         unHideMe(rows[r]);
94
95         hideMe(G.ui.rdetail.cp_info_all);
96         unHideMe(G.ui.rdetail.cp_info_local);
97         hideMe(G.ui.rdetail.cp_info_none);
98         rdetailLocalOnly = false;
99 }
100
101
102 function _rdetailDraw(r) {
103         record = r.getResultObject();
104
105         runEvt('rdetail', 'recordRetrieved', record.doc_id());
106
107         G.ui.rdetail.title.appendChild(text(record.title()));
108         buildSearchLink(STYPE_AUTHOR, record.author(), G.ui.rdetail.author);
109         G.ui.rdetail.isbn.appendChild(text(cleanISBN(record.isbn())));
110         G.ui.rdetail.edition.appendChild(text(record.edition()));
111         G.ui.rdetail.pubdate.appendChild(text(record.pubdate()));
112         G.ui.rdetail.publisher.appendChild(text(record.publisher()));
113         G.ui.rdetail.tor.appendChild(text(record.types_of_resource()[0]));
114         setResourcePic( G.ui.rdetail.tor_pic, record.types_of_resource()[0]);
115         G.ui.rdetail.abstr.appendChild(text(record.synopsis()));
116
117
118         $('rdetail_place_hold').setAttribute(
119                 'href','javascript:holdsDrawWindow("'+record.doc_id()+'");');
120
121         G.ui.rdetail.image.setAttribute("src", buildISBNSrc(cleanISBN(record.isbn())));
122         runEvt("rdetail", "recordDrawn");
123         recordsCache.push(record);
124
125         rdetailSetExtrasSelector();
126
127         var req = new Request(FETCH_ACONT_SUMMARY, cleanISBN(record.isbn()));
128         req.callback(rdetailHandleAddedContent);
129         req.send();
130
131         resultBuildCaches( [ record ] );
132         resultDrawSubjects();
133         resultDrawSeries();
134 }
135
136 function rdetailSetExtrasSelector() {
137         if(!grabUser()) return;
138         unHideMe($('rdetail_more_actions'));
139
140         var req = new Request( 
141                 FETCH_CONTAINERS, G.user.session, G.user.id(), 'biblio', 'bookbag' );
142         req.callback(rdetailAddBookbags);
143         req.send();
144 }
145
146 function rdetailAddBookbags(r) {
147
148         var containers = r.getResultObject();
149         var selector = $('rdetail_more_actions_selector');
150         var found = false;
151         var index = 3;
152         doSelectorActions(selector);
153
154         for( var i = 0; i != containers.length; i++ ) {
155                 found = true;
156                 var container = containers[i];
157                 insertSelectorVal( selector, index++, container.name(), 
158                         "container_" + container.id(), rdetailAddToBookbag,  1 );
159         }
160
161         nextContainerIndex = index;
162         if(!found) insertSelectorVal( selector, 3, "name", "value", 1 );
163 }
164
165 var _actions = {};
166 function rdetailNewBookbag() {
167         var name = prompt($('rdetail_bb_new').innerHTML,"");
168         if(!name) return;
169
170         var id;
171         if( id = containerCreate( name ) ) {
172                 alert($('rdetail_bb_success').innerHTML);
173                 var selector = $('rdetail_more_actions_selector');
174                 insertSelectorVal( selector, nextContainerIndex++, name, 
175                         "container_" + id, rdetailAddToBookbag, 1 );
176                 setSelector( selector, 'start' );
177         }
178 }
179
180
181 function rdetailAddToBookbag() {
182         var selector = $('rdetail_more_actions_selector');
183         var id = selector.options[selector.selectedIndex].value;
184         setSelector( selector, 'start' );
185
186         if( containerCreateItem( id.substring(10), record.doc_id() )) {
187                 alert($('rdetail_bb_item_success').innerHTML);
188         }
189 }
190
191
192
193 var rdetailTocFetched           = false;
194 var rdetailReviewFetched        = false;
195 var rdetailMarcFetched          = false;
196
197 function rdetailShowExtra(type) {
198
199         hideMe($('rdetail_copy_info_div'));
200         hideMe($('rdetail_reviews_div'));
201         hideMe($('rdetail_toc_div'));
202         hideMe($('rdetail_marc_div'));
203         hideMe($('cn_browse'));
204         hideMe($('rdetail_cn_browse_div'));
205
206         var req;
207         switch(type) {
208                 case "copyinfo": 
209                         unHideMe($('rdetail_copy_info_div')); 
210                         break;
211
212                 case "reviews": 
213                         unHideMe($('rdetail_reviews_div')); 
214                         if(rdetailReviewFetched) break;
215                         unHideMe($('rdetail_extras_loading'));
216                         rdetailReviewFetched = true;
217                         req = new Request(FETCH_REVIEWS, cleanISBN(record.isbn()));
218                         req.callback(rdetailShowReviews);
219                         req.send();
220                         break;
221
222                 case "toc": 
223                         unHideMe($('rdetail_toc_div'));
224                         if(rdetailTocFetched) break;
225                         unHideMe($('rdetail_extras_loading'));
226                         rdetailTocFetched = true;
227                         var req = new Request(FETCH_TOC, cleanISBN(record.isbn()));
228                         req.callback(rdetailShowTOC);
229                         req.send();
230                         break;
231
232                 case "marc": 
233                         unHideMe($('rdetail_marc_div')); 
234                         if(rdetailMarcFetched) return;
235                         unHideMe($('rdetail_extras_loading'));
236                         rdetailMarcFetched = true;
237                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
238                         req.callback(rdetailViewMarc, record.doc_id()); 
239                         req.send();
240                         break;
241
242                 case 'cn':
243                         unHideMe($('rdetail_cn_browse_div'));
244                         rdetailShowCNBrowse(defaultCN, null, true);
245                         break;
246         }
247 }
248
249 function rdetailBuildCNList() {
250         var select = $('cn_browse_selector');
251         var index = 0;
252         var arr = [];
253         for( var cn in callnumberCache ) arr.push( cn );
254         arr.sort();
255
256         for( var i in arr ) {
257                 var cn = arr[i];
258                 var opt = new Option(cn);
259                 select.options[index++] = opt;
260         }
261         select.onchange = rdetailGatherCN;
262 }
263
264 function rdetailGatherCN() {
265         var cn = getSelectorVal($('cn_browse_selector'));
266         rdetailShowCNBrowse( cn, getDepth(), true );
267         setSelector( $('cn_browse_selector'), cn );
268 }
269
270
271 function rdetailShowCNBrowse( cn, depth, fromOnclick ) {
272         if(!cn) return;
273         rdetailBuildCNList();
274         setSelector( $('cn_browse_selector'), cn );
275         hideMe($('rdetail_copy_info_div'));
276         hideMe($('rdetail_reviews_div'));
277         hideMe($('rdetail_toc_div'));
278         hideMe($('rdetail_marc_div'));
279         unHideMe($('rdetail_cn_browse_div'));
280         unHideMe($('cn_browse'));
281         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
282         cnBrowseGo(cn, depth);
283 }
284
285 function rdetailHandleAddedContent(r) {
286         var resp = r.getResultObject();
287         if( resp.Review == 'true' ) unHideMe($('rdetail_reviews_link'));
288         if( resp.TOC == 'true' ) unHideMe($('rdetail_toc_link'));
289 }
290
291
292 function rdetailShowReviews(r) {
293         hideMe($('rdetail_extras_loading'));
294         var res = r.getResultObject();
295         var par = $('rdetail_reviews_div');
296         var template = par.removeChild($('rdetail_review_template'));
297         if( res && res.length > 0 ) {
298                 unHideMe($('rdetail_reviews_link'));
299                 for( var i = 0; i != res.length; i++ ) {
300                         var rev = res[i];       
301                         if( rev.text && rev.info ) {
302                                 var node = template.cloneNode(true);
303                                 $n(node, 'review_header').appendChild(text(rev.info));
304                                 $n(node, 'review_text').appendChild(text(rev.text));
305                                 par.appendChild(node);
306                         }
307                 }
308         }
309 }
310
311 function rdetailShowTOC(r) {
312         hideMe($('rdetail_extras_loading'));
313         var resp = r.getResultObject();
314         if(resp) {
315                 unHideMe($('rdetail_toc_link'));
316                 $('rdetail_toc_div').innerHTML = resp;
317         }
318 }
319
320
321 function rdetailBuildInfoRows() {
322         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
323         req.callback(_rdetailBuildInfoRows);
324         req.send();
325 }
326
327 /* pre-allocate the copy info table with all org units in correct order */
328 function _rdetailRows(node) {
329
330         if(node) {
331
332                 var row = copyRow.cloneNode(true);
333                 row.id = "cp_info_" + node.id();
334
335                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
336                 var cntd = findNodeByName( row, config.names.rdetail.cn_cell );
337                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
338         
339                 libtd.appendChild(text(node.name()));
340                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
341         
342                 if(!findOrgType(node.ou_type()).can_have_vols()) {
343
344                         row.removeChild(cntd);
345                         row.removeChild(cpctd);
346
347                         libtd.setAttribute("colspan", numStatuses + 2 );
348                         libtd.colSpan = numStatuses + 2;
349                         addCSSClass(row, 'copy_info_region_row');
350                 } 
351         
352                 copyRowParent.appendChild(row);
353
354         } else { node = globalOrgTree; }
355
356         for( var c in node.children() ) 
357                 _rdetailRows(node.children()[c]);
358 }
359
360 /* walk through the copy info and build rows where necessary */
361 var localCNFound = false;
362 function _rdetailBuildInfoRows(r) {
363
364         _rdetailRows();
365
366         var summary = r.getResultObject();
367         if(!summary) return;
368
369         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
370                 G.ui.rdetail.cp_info_loading);
371
372         var found = false;
373         for( var i = 0; i < summary.length; i++ ) {
374
375                 var arr = summary[i];
376                 var thisOrg = findOrgUnit(arr[0]);
377                 var rowNode = $("cp_info_" + thisOrg.id());
378
379                 if(rowNode.getAttribute("used")) {
380
381                         if( rowNode.nextSibling )
382                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
383                         else
384                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
385                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
386                         n.appendChild(text(thisOrg.name()));
387                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
388
389                 } else rowNode.setAttribute("used", "1");
390
391                 var cpc_temp = rowNode.removeChild(
392                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
393
394                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
395
396                 var isLocal = false;
397                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
398                         found = true; 
399                         isLocal = true; 
400                         if(!localCNFound) {
401                                 localCNFound = true;
402                                 defaultCN = arr[1];
403                         }
404                 }
405                 rdetailSetPath( thisOrg, isLocal );
406                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal );
407
408                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
409         }
410
411         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
412
413 }
414
415 function rdetailBuildBrowseInfo(row, cn, local) {
416         /* used for building the shelf browser */
417         if(local) {
418                 var cache = callnumberCache[cn];
419                 if( cache ) cache.count++;
420                 else callnumberCache[cn] = { count : 1 };
421         }
422
423         var depth = getDepth();
424         if( !local ) depth = findOrgDepth(globalOrgTree);
425         var a = elem("a", {href:'javascript:rdetailShowCNBrowse("' + cn + '", "'+depth+'");' }, cn);
426         addCSSClass(a, 'classic_link');
427         findNodeByName( row, config.names.rdetail.cn_cell ).appendChild(a);
428 }
429
430 /* sets the path to org as 'active' and displays the 
431         path if it's local */
432 function rdetailSetPath(org, local) {
433         if( findOrgDepth(org) == 0 ) return;
434         var row = $("cp_info_" + org.id());
435         row.setAttribute("hasinfo", "1");
436         if(local) {
437                 unHideMe(row);
438                 row.setAttribute("local", "1");
439         }
440         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
441 }
442
443 function rdetailApplyStatuses( row, template, statuses ) {
444         for( var j in _statusPositions ) {
445                 var stat = _statusPositions[j];
446                 var val = statuses[stat.id()];
447                 var nn = template.cloneNode(true);
448                 if(val) nn.appendChild(text(val));
449                 else nn.appendChild(text(0));
450                 row.appendChild(nn);    
451         }
452 }
453
454
455 /* --------------------------------------------------------------------- */
456 var _statusPositions = {};
457
458 function rdetailBuildStatusColumns() {
459
460         rdetailGrabCopyStatuses();
461         var parent = statusRow;
462         var template = parent.removeChild(G.ui.rdetail.cp_status);
463
464         var i = 0;
465         for( i = 0; i < cp_statuses.length; i++ ) {
466
467                 var c = cp_statuses[i];
468
469                 if(c && c.holdable()) {
470
471                         var name = c.name();
472                         _statusPositions[i] = c;
473                         var node = template.cloneNode(true);
474                         var data = findNodeByName( node, config.names.rdetail.cp_status);
475
476                         data.appendChild(text(name));
477                         parent.appendChild(node);
478                 }       
479         }       
480
481         numStatuses = 0;
482         for(x in _statusPositions) numStatuses++; 
483 }
484
485 function rdetailGrabCopyStatuses() {
486         if(cp_statuses) return cp_statuses;
487    var req = new Request(FETCH_COPY_STATUSES);
488    req.send(true);
489         cp_statuses = req.result();
490         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
491 }
492
493 function _rdetailSortStatuses(a, b) {
494         return parseInt(a.id()) - parseInt(b.id());
495 }
496
497