]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/rdetail.js
added useful wrapper method
[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         doSelectorActions(selector);
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         nextContainerIndex = index;
139         if(!found) insertSelectorVal( selector, 3, "name", "value", 1 );
140 }
141
142 var _actions = {};
143 function rdetailNewBookbag() {
144         var name = prompt($('rdetail_bb_new').innerHTML,"");
145         if(!name) return;
146
147         var id;
148         if( id = containerCreate( name ) ) {
149                 alert($('rdetail_bb_success').innerHTML);
150                 var selector = $('rdetail_more_actions_selector');
151                 insertSelectorVal( selector, nextContainerIndex++, name, 
152                         "container_" + id, rdetailAddToBookbag, 1 );
153                 setSelector( selector, 'start' );
154         }
155 }
156
157
158 function rdetailAddToBookbag() {
159         var selector = $('rdetail_more_actions_selector');
160         var id = selector.options[selector.selectedIndex].value;
161         setSelector( selector, 'start' );
162
163         if( containerCreateItem( id.substring(10), record.doc_id() )) {
164                 alert($('rdetail_bb_item_success').innerHTML);
165         }
166 }
167
168
169
170 var rdetailTocFetched           = false;
171 var rdetailReviewFetched        = false;
172 var rdetailMarcFetched          = false;
173
174 function rdetailShowExtra(type) {
175
176         hideMe($('rdetail_copy_info_div'));
177         hideMe($('rdetail_reviews_div'));
178         hideMe($('rdetail_toc_div'));
179         hideMe($('rdetail_marc_div'));
180         hideMe($('cn_browse'));
181         hideMe($('rdetail_cn_browse_div'));
182
183         var req;
184         switch(type) {
185                 case "copyinfo": 
186                         unHideMe($('rdetail_copy_info_div')); 
187                         break;
188
189                 case "reviews": 
190                         unHideMe($('rdetail_reviews_div')); 
191                         if(rdetailReviewFetched) break;
192                         unHideMe($('rdetail_extras_loading'));
193                         rdetailReviewFetched = true;
194                         req = new Request(FETCH_REVIEWS, cleanISBN(record.isbn()));
195                         req.callback(rdetailShowReviews);
196                         req.send();
197                         break;
198
199                 case "toc": 
200                         unHideMe($('rdetail_toc_div'));
201                         if(rdetailTocFetched) break;
202                         unHideMe($('rdetail_extras_loading'));
203                         rdetailTocFetched = true;
204                         var req = new Request(FETCH_TOC, cleanISBN(record.isbn()));
205                         req.callback(rdetailShowTOC);
206                         req.send();
207                         break;
208
209                 case "marc": 
210                         unHideMe($('rdetail_marc_div')); 
211                         if(rdetailMarcFetched) return;
212                         unHideMe($('rdetail_extras_loading'));
213                         rdetailMarcFetched = true;
214                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
215                         req.callback(rdetailViewMarc); 
216                         req.send();
217                         break;
218
219                 case 'cn':
220                         unHideMe($('rdetail_cn_browse_div'));
221                         rdetailShowCNBrowse(defaultCN, null, true);
222                         break;
223         }
224 }
225
226 function rdetailBuildCNList() {
227         var select = $('cn_browse_selector');
228         var index = 0;
229         var arr = [];
230         for( var cn in callnumberCache ) arr.push( cn );
231         arr.sort();
232
233         for( var i in arr ) {
234                 var cn = arr[i];
235                 var opt = new Option(cn);
236                 select.options[index++] = opt;
237         }
238         select.onchange = rdetailGatherCN;
239 }
240
241 function rdetailGatherCN() {
242         var cn = getSelectorVal($('cn_browse_selector'));
243         rdetailShowCNBrowse( cn, getDepth(), true );
244         setSelector( $('cn_browse_selector'), cn );
245 }
246
247
248 function rdetailShowCNBrowse( cn, depth, fromOnclick ) {
249         if(!cn) return;
250         rdetailBuildCNList();
251         setSelector( $('cn_browse_selector'), cn );
252         hideMe($('rdetail_copy_info_div'));
253         hideMe($('rdetail_reviews_div'));
254         hideMe($('rdetail_toc_div'));
255         hideMe($('rdetail_marc_div'));
256         unHideMe($('rdetail_cn_browse_div'));
257         unHideMe($('cn_browse'));
258         if( !rdetailLocalOnly && ! fromOnclick ) depth = findOrgDepth(globalOrgTree);
259         cnBrowseGo(cn, depth);
260 }
261
262 function rdetailHandleAddedContent(r) {
263         var resp = r.getResultObject();
264         if( resp.Review == 'true' ) unHideMe($('rdetail_reviews_link'));
265         if( resp.TOC == 'true' ) unHideMe($('rdetail_toc_link'));
266 }
267
268
269 function rdetailShowReviews(r) {
270         hideMe($('rdetail_extras_loading'));
271         var res = r.getResultObject();
272         var par = $('rdetail_reviews_div');
273         var template = par.removeChild($('rdetail_review_template'));
274         if( res && res.length > 0 ) {
275                 unHideMe($('rdetail_reviews_link'));
276                 for( var i = 0; i != res.length; i++ ) {
277                         var rev = res[i];       
278                         if( rev.text && rev.info ) {
279                                 var node = template.cloneNode(true);
280                                 $n(node, 'review_header').appendChild(text(rev.info));
281                                 $n(node, 'review_text').appendChild(text(rev.text));
282                                 par.appendChild(node);
283                         }
284                 }
285         }
286 }
287
288 function rdetailShowTOC(r) {
289         hideMe($('rdetail_extras_loading'));
290         var resp = r.getResultObject();
291         if(resp) {
292                 unHideMe($('rdetail_toc_link'));
293                 $('rdetail_toc_div').innerHTML = resp;
294         }
295 }
296
297
298 function rdetailBuildInfoRows() {
299         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
300         req.callback(_rdetailBuildInfoRows);
301         req.send();
302 }
303
304 /* pre-allocate the copy info table with all org units in correct order */
305 function _rdetailRows(node) {
306
307         if(node) {
308
309                 var row = copyRow.cloneNode(true);
310                 row.id = "cp_info_" + node.id();
311
312                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
313                 var cntd = findNodeByName( row, config.names.rdetail.cn_cell );
314                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
315         
316                 libtd.appendChild(text(node.name()));
317                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
318         
319                 if(!findOrgType(node.ou_type()).can_have_vols()) {
320
321                         row.removeChild(cntd);
322                         row.removeChild(cpctd);
323
324                         libtd.setAttribute("colspan", numStatuses + 2 );
325                         libtd.colSpan = numStatuses + 2;
326                         addCSSClass(row, 'copy_info_region_row');
327                 } 
328         
329                 copyRowParent.appendChild(row);
330
331         } else { node = globalOrgTree; }
332
333         for( var c in node.children() ) 
334                 _rdetailRows(node.children()[c]);
335 }
336
337 /* walk through the copy info and build rows where necessary */
338 var localCNFound = false;
339 function _rdetailBuildInfoRows(r) {
340
341         _rdetailRows();
342
343         var summary = r.getResultObject();
344         if(!summary) return;
345
346         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
347                 G.ui.rdetail.cp_info_loading);
348
349         var found = false;
350         for( var i = 0; i < summary.length; i++ ) {
351
352                 var arr = summary[i];
353                 var thisOrg = findOrgUnit(arr[0]);
354                 var rowNode = $("cp_info_" + thisOrg.id());
355
356                 if(rowNode.getAttribute("used")) {
357
358                         if( rowNode.nextSibling )
359                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
360                         else
361                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
362                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
363                         n.appendChild(text(thisOrg.name()));
364                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
365
366                 } else rowNode.setAttribute("used", "1");
367
368                 var cpc_temp = rowNode.removeChild(
369                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
370
371                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
372
373                 var isLocal = false;
374                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { 
375                         found = true; 
376                         isLocal = true; 
377                         if(!localCNFound) {
378                                 localCNFound = true;
379                                 defaultCN = arr[1];
380                         }
381                 }
382                 rdetailSetPath( thisOrg, isLocal );
383                 rdetailBuildBrowseInfo( rowNode, arr[1], isLocal );
384
385                 if( i == summary.length - 1 && !defaultCN) defaultCN = arr[1];
386         }
387
388         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
389
390 }
391
392 function rdetailBuildBrowseInfo(row, cn, local) {
393         /* used for building the shelf browser */
394         if(local) {
395                 var cache = callnumberCache[cn];
396                 if( cache ) cache.count++;
397                 else callnumberCache[cn] = { count : 1 };
398         }
399
400         var depth = getDepth();
401         if( !local ) depth = findOrgDepth(globalOrgTree);
402         var a = elem("a", {href:'javascript:rdetailShowCNBrowse("' + cn + '", "'+depth+'");' }, cn);
403         addCSSClass(a, 'classic_link');
404         findNodeByName( row, config.names.rdetail.cn_cell ).appendChild(a);
405 }
406
407 /* sets the path to org as 'active' and displays the 
408         path if it's local */
409 function rdetailSetPath(org, local) {
410         if( findOrgDepth(org) == 0 ) return;
411         var row = $("cp_info_" + org.id());
412         row.setAttribute("hasinfo", "1");
413         if(local) {
414                 unHideMe(row);
415                 row.setAttribute("local", "1");
416         }
417         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
418 }
419
420 function rdetailApplyStatuses( row, template, statuses ) {
421         for( var j in _statusPositions ) {
422                 var stat = _statusPositions[j];
423                 var val = statuses[stat.id()];
424                 var nn = template.cloneNode(true);
425                 if(val) nn.appendChild(text(val));
426                 else nn.appendChild(text(0));
427                 row.appendChild(nn);    
428         }
429 }
430
431
432 /* --------------------------------------------------------------------- */
433 var _statusPositions = {};
434
435 function rdetailBuildStatusColumns() {
436
437         rdetailGrabCopyStatuses();
438         var parent = statusRow;
439         var template = parent.removeChild(G.ui.rdetail.cp_status);
440
441         var i = 0;
442         for( i = 0; i < cp_statuses.length; i++ ) {
443
444                 var c = cp_statuses[i];
445
446                 if(c && c.holdable()) {
447
448                         var name = c.name();
449                         _statusPositions[i] = c;
450                         var node = template.cloneNode(true);
451                         var data = findNodeByName( node, config.names.rdetail.cp_status);
452
453                         data.appendChild(text(name));
454                         parent.appendChild(node);
455                 }       
456         }       
457
458         numStatuses = 0;
459         for(x in _statusPositions) numStatuses++; 
460 }
461
462 function rdetailGrabCopyStatuses() {
463         if(cp_statuses) return cp_statuses;
464    var req = new Request(FETCH_COPY_STATUSES);
465    req.send(true);
466         cp_statuses = req.result();
467         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
468 }
469
470 function _rdetailSortStatuses(a, b) {
471         return parseInt(a.id()) - parseInt(b.id());
472 }
473
474