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