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