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