]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
small tweaks
[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         var arr = [];
228         for( var cn in callnumberCache ) arr.push( cn );
229         arr.sort();
230
231         for( var i in arr ) {
232                 var cn = arr[i];
233                 var opt = new Option(cn);
234                 opt.onclick = rdetailGatherCN;
235                 select.options[index++] = opt;
236         }
237 }
238
239 function rdetailGatherCN() {
240         var cn = getSelectorVal($('cn_browse_selector'));
241         rdetailShowCNBrowse( cn, getDepth(), true );
242         setSelector( $('cn_browse_selector'), cn );
243 }
244
245
246 function rdetailShowCNBrowse( cn, depth, fromOnclick ) {
247         if(!cn) return;
248         rdetailBuildCNList();
249         setSelector( $('cn_browse_selector'), cn );
250         hideMe($('rdetail_copy_info_div'));
251         hideMe($('rdetail_reviews_div'));
252         hideMe($('rdetail_toc_div'));
253         hideMe($('rdetail_marc_div'));
254         unHideMe($('rdetail_cn_browse_div'));
255         unHideMe($('cn_browse'));
256         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
257         cnBrowseGo(cn, depth);
258 }
259
260 function rdetailHandleAddedContent(r) {
261         var resp = r.getResultObject();
262         if( resp.Review == 'true' ) unHideMe($('rdetail_reviews_link'));
263         if( resp.TOC == 'true' ) unHideMe($('rdetail_toc_link'));
264 }
265
266
267 function rdetailShowReviews(r) {
268         hideMe($('rdetail_extras_loading'));
269         var res = r.getResultObject();
270         var par = $('rdetail_reviews_div');
271         var template = par.removeChild($('rdetail_review_template'));
272         if( res && res.length > 0 ) {
273                 unHideMe($('rdetail_reviews_link'));
274                 for( var i = 0; i != res.length; i++ ) {
275                         var rev = res[i];       
276                         if( rev.text && rev.info ) {
277                                 var node = template.cloneNode(true);
278                                 $n(node, 'review_header').appendChild(text(rev.info));
279                                 $n(node, 'review_text').appendChild(text(rev.text));
280                                 par.appendChild(node);
281                         }
282                 }
283         }
284 }
285
286 function rdetailShowTOC(r) {
287         hideMe($('rdetail_extras_loading'));
288         var resp = r.getResultObject();
289         if(resp) {
290                 unHideMe($('rdetail_toc_link'));
291                 $('rdetail_toc_div').innerHTML = resp;
292         }
293 }
294
295
296 function rdetailBuildInfoRows() {
297         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
298         req.callback(_rdetailBuildInfoRows);
299         req.send();
300 }
301
302 /* pre-allocate the copy info table with all org units in correct order */
303 function _rdetailRows(node) {
304
305         if(node) {
306
307                 var row = copyRow.cloneNode(true);
308                 row.id = "cp_info_" + node.id();
309
310                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
311                 var cntd = findNodeByName( row, config.names.rdetail.cn_cell );
312                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
313         
314                 libtd.appendChild(text(node.name()));
315                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
316         
317                 if(!findOrgType(node.ou_type()).can_have_vols()) {
318
319                         row.removeChild(cntd);
320                         row.removeChild(cpctd);
321
322                         libtd.setAttribute("colspan", numStatuses + 2 );
323                         libtd.colSpan = numStatuses + 2;
324                         addCSSClass(row, 'copy_info_region_row');
325                 } 
326         
327                 copyRowParent.appendChild(row);
328
329         } else { node = globalOrgTree; }
330
331         for( var c in node.children() ) 
332                 _rdetailRows(node.children()[c]);
333 }
334
335 /* walk through the copy info and build rows where necessary */
336 var localCNFound = false;
337 function _rdetailBuildInfoRows(r) {
338
339         _rdetailRows();
340
341         var summary = r.getResultObject();
342         if(!summary) return;
343
344         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
345                 G.ui.rdetail.cp_info_loading);
346
347         var found = false;
348         for( var i = 0; i < summary.length; i++ ) {
349
350                 var arr = summary[i];
351                 var thisOrg = findOrgUnit(arr[0]);
352                 var rowNode = $("cp_info_" + thisOrg.id());
353
354                 if(rowNode.getAttribute("used")) {
355
356                         if( rowNode.nextSibling )
357                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
358                         else
359                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
360                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
361                         n.appendChild(text(thisOrg.name()));
362                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
363
364                 } else rowNode.setAttribute("used", "1");
365
366                 var cpc_temp = rowNode.removeChild(
367                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
368
369                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
370
371                 var isLocal = false;
372                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
373                         found = true; 
374                         isLocal = true; 
375                         if(!localCNFound) {
376                                 localCNFound = true;
377                                 defaultCN = arr[1];
378                         }
379                 }
380                 rdetailSetPath( thisOrg, isLocal );
381                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal );
382
383                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
384         }
385
386         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
387
388 }
389
390 function rdetailBuildBrowseInfo(row, cn, local) {
391         /* used for building the shelf browser */
392         if(local) {
393                 var cache = callnumberCache[cn];
394                 if( cache ) cache.count++;
395                 else callnumberCache[cn] = { count : 1 };
396         }
397
398         var depth = getDepth();
399         if( !local ) depth = findOrgDepth(globalOrgTree);
400         var a = elem("a", {href:'javascript:rdetailShowCNBrowse("' + cn + '", "'+depth+'");' }, cn);
401         addCSSClass(a, 'classic_link');
402         findNodeByName( row, config.names.rdetail.cn_cell ).appendChild(a);
403 }
404
405 /* sets the path to org as 'active' and displays the 
406         path if it's local */
407 function rdetailSetPath(org, local) {
408         if( findOrgDepth(org) == 0 ) return;
409         var row = $("cp_info_" + org.id());
410         row.setAttribute("hasinfo", "1");
411         if(local) {
412                 unHideMe(row);
413                 row.setAttribute("local", "1");
414         }
415         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
416 }
417
418 function rdetailApplyStatuses( row, template, statuses ) {
419         for( var j in _statusPositions ) {
420                 var stat = _statusPositions[j];
421                 var val = statuses[stat.id()];
422                 var nn = template.cloneNode(true);
423                 if(val) nn.appendChild(text(val));
424                 else nn.appendChild(text(0));
425                 row.appendChild(nn);    
426         }
427 }
428
429
430 /* --------------------------------------------------------------------- */
431 var _statusPositions = {};
432
433 function rdetailBuildStatusColumns() {
434
435         rdetailGrabCopyStatuses();
436         var parent = statusRow;
437         var template = parent.removeChild(G.ui.rdetail.cp_status);
438
439         var i = 0;
440         for( i = 0; i < cp_statuses.length; i++ ) {
441
442                 var c = cp_statuses[i];
443
444                 if(c && c.holdable()) {
445
446                         var name = c.name();
447                         _statusPositions[i] = c;
448                         var node = template.cloneNode(true);
449                         var data = findNodeByName( node, config.names.rdetail.cp_status);
450
451                         data.appendChild(text(name));
452                         parent.appendChild(node);
453                 }       
454         }       
455
456         numStatuses = 0;
457         for(x in _statusPositions) numStatuses++; 
458 }
459
460 function rdetailGrabCopyStatuses() {
461         if(cp_statuses) return cp_statuses;
462    var req = new Request(FETCH_COPY_STATUSES);
463    req.send(true);
464         cp_statuses = req.result();
465         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
466 }
467
468 function _rdetailSortStatuses(a, b) {
469         return parseInt(a.id()) - parseInt(b.id());
470 }
471
472