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