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