little tweaks here and there
[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 }
100
101 var rdetailTocFetched           = false;
102 var rdetailReviewFetched        = false;
103 var rdetailMarcFetched          = false;
104
105 function rdetailShowExtra(type) {
106
107         hideMe($('rdetail_copy_info_div'));
108         hideMe($('rdetail_reviews_div'));
109         hideMe($('rdetail_toc_div'));
110         hideMe($('rdetail_marc_div'));
111
112         var req;
113         switch(type) {
114                 case "copyinfo": 
115                         unHideMe($('rdetail_copy_info_div')); 
116                         break;
117
118                 case "reviews": 
119                         unHideMe($('rdetail_reviews_div')); 
120                         if(rdetailReviewFetched) break;
121                         unHideMe($('rdetail_extras_loading'));
122                         rdetailReviewFetched = true;
123                         req = new Request(FETCH_REVIEWS, cleanISBN(record.isbn()));
124                         req.callback(rdetailShowReviews);
125                         req.send();
126                         break;
127
128                 case "toc": 
129                         unHideMe($('rdetail_toc_div'));
130                         if(rdetailTocFetched) break;
131                         unHideMe($('rdetail_extras_loading'));
132                         rdetailTocFetched = true;
133                         var req = new Request(FETCH_TOC, cleanISBN(record.isbn()));
134                         req.callback(rdetailShowTOC);
135                         req.send();
136                         break;
137
138                 case "marc": 
139                         unHideMe($('rdetail_marc_div')); 
140                         if(rdetailMarcFetched) return;
141                         unHideMe($('rdetail_extras_loading'));
142                         rdetailMarcFetched = true;
143                         var req = new Request( FETCH_MARC_HTML, record.doc_id() );
144                         req.callback(rdetailViewMarc); 
145                         req.send();
146                         break;
147         }
148 }
149
150 function rdetailHandleAddedContent(r) {
151         var resp = r.getResultObject();
152         if( resp.Review == 'true' ) unHideMe($('rdetail_reviews_link'));
153         if( resp.TOC == 'true' ) unHideMe($('rdetail_toc_link'));
154 }
155
156
157 function rdetailShowReviews(r) {
158         hideMe($('rdetail_extras_loading'));
159         var res = r.getResultObject();
160         var par = $('rdetail_reviews_div');
161         var template = par.removeChild($('rdetail_review_template'));
162         if( res && res.length > 0 ) {
163                 unHideMe($('rdetail_reviews_link'));
164                 for( var i = 0; i != res.length; i++ ) {
165                         var rev = res[i];       
166                         if( rev.text && rev.info ) {
167                                 var node = template.cloneNode(true);
168                                 $n(node, 'review_header').appendChild(text(rev.info));
169                                 $n(node, 'review_text').appendChild(text(rev.text));
170                                 par.appendChild(node);
171                         }
172                 }
173         }
174 }
175
176 function rdetailShowTOC(r) {
177         hideMe($('rdetail_extras_loading'));
178         var resp = r.getResultObject();
179         if(resp) {
180                 unHideMe($('rdetail_toc_link'));
181                 $('rdetail_toc_div').innerHTML = resp;
182         }
183 }
184
185
186 function rdetailBuildInfoRows() {
187         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
188         req.callback(_rdetailBuildInfoRows);
189         req.send();
190 }
191
192 /* pre-allocate the copy info table with all org units in correct order */
193 function _rdetailRows(node) {
194
195         if(node) {
196
197                 var row = copyRow.cloneNode(true);
198                 row.id = "cp_info_" + node.id();
199
200                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
201                 var cntd = findNodeByName( row, config.names.rdetail.cn_cell );
202                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
203         
204                 libtd.appendChild(text(node.name()));
205                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
206         
207                 if(!findOrgType(node.ou_type()).can_have_vols()) {
208
209                         row.removeChild(cntd);
210                         row.removeChild(cpctd);
211
212                         libtd.setAttribute("colspan", numStatuses + 2 );
213                         libtd.colSpan = numStatuses + 2;
214                         addCSSClass(row, 'copy_info_region_row');
215                 } 
216         
217                 copyRowParent.appendChild(row);
218
219         } else { node = globalOrgTree; }
220
221         for( var c in node.children() ) 
222                 _rdetailRows(node.children()[c]);
223 }
224
225 /* walk through the copy info and build rows where necessary */
226 function _rdetailBuildInfoRows(r) {
227
228         _rdetailRows();
229
230         var summary = r.getResultObject();
231
232         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
233                 G.ui.rdetail.cp_info_loading);
234
235         var found = false;
236         for( var i = 0; i < summary.length; i++ ) {
237
238                 var arr = summary[i];
239                 var thisOrg = findOrgUnit(arr[0]);
240                 var rowNode = $("cp_info_" + thisOrg.id());
241
242                 if(rowNode.getAttribute("used")) {
243
244                         if( rowNode.nextSibling )
245                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
246                         else
247                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
248                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
249                         n.appendChild(text(thisOrg.name()));
250                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
251
252                 } else rowNode.setAttribute("used", "1");
253
254                 findNodeByName( rowNode, config.names.rdetail.cn_cell ).appendChild(text(arr[1]));
255
256                 var cpc_temp = rowNode.removeChild(
257                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
258
259                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
260
261                 var isLocal = false;
262                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { found = true; isLocal = true; }
263                 rdetailSetPath( thisOrg, isLocal );
264
265         }
266
267         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
268
269 }
270
271 /* sets the path to org as 'active' and displays the 
272         path if it's local */
273 function rdetailSetPath(org, local) {
274         if( findOrgDepth(org) == 0 ) return;
275         var row = $("cp_info_" + org.id());
276         row.setAttribute("hasinfo", "1");
277         if(local) {
278                 unHideMe(row);
279                 row.setAttribute("local", "1");
280         }
281         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
282 }
283
284 function rdetailApplyStatuses( row, template, statuses ) {
285         for( var j in _statusPositions ) {
286                 var stat = _statusPositions[j];
287                 var val = statuses[stat.id()];
288                 var nn = template.cloneNode(true);
289                 if(val) nn.appendChild(text(val));
290                 else nn.appendChild(text(0));
291                 row.appendChild(nn);    
292         }
293 }
294
295
296 /* --------------------------------------------------------------------- */
297 var _statusPositions = {};
298
299 function rdetailBuildStatusColumns() {
300
301         rdetailGrabCopyStatuses();
302         var parent = statusRow;
303         var template = parent.removeChild(G.ui.rdetail.cp_status);
304
305         var i = 0;
306         for( i = 0; i < cp_statuses.length; i++ ) {
307
308                 var c = cp_statuses[i];
309
310                 if(c && c.holdable()) {
311
312                         var name = c.name();
313                         _statusPositions[i] = c;
314                         var node = template.cloneNode(true);
315                         var data = findNodeByName( node, config.names.rdetail.cp_status);
316
317                         data.appendChild(text(name));
318                         parent.appendChild(node);
319                 }       
320         }       
321
322         numStatuses = 0;
323         for(x in _statusPositions) numStatuses++; 
324 }
325
326 function rdetailGrabCopyStatuses() {
327         if(cp_statuses) return cp_statuses;
328    var req = new Request(FETCH_COPY_STATUSES);
329    req.send(true);
330         cp_statuses = req.result();
331         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
332 }
333
334 function _rdetailSortStatuses(a, b) {
335         return parseInt(a.id()) - parseInt(b.id());
336 }
337
338