holds are coming along
[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
10 var copyRowParent = null;
11 var copyRow = null;
12 var statusRow = null;
13 var numStatuses = null;
14
15 function rdetailDraw() {
16
17         copyRowParent = G.ui.rdetail.cp_info_row.parentNode;
18         copyRow = copyRowParent.removeChild(G.ui.rdetail.cp_info_row);
19         statusRow = G.ui.rdetail.cp_status.parentNode;
20         statusRow.id = '__rdsrow';
21
22         G.ui.rdetail.cp_info_local.onclick = rdetailShowLocalCopies;
23         G.ui.rdetail.cp_info_all.onclick = rdetailShowAllCopies;
24         G.ui.rdetail.view_marc.onclick = rdetailViewMarc;
25         G.ui.rdetail.hide_marc.onclick = showCanvas;
26
27
28         if(getLocation() == globalOrgTree.id())
29                 hideMe(G.ui.rdetail.cp_info_all);
30
31         var req = new Request(FETCH_RMODS, getRid());
32         req.callback(_rdetailDraw);
33         req.send();
34 }
35
36 function rdetailViewMarc() {
37         if(!record) return;
38
39         if( G.ui.rdetail.view_marc_box.innerHTML.indexOf("style") == -1 ) {
40                 var req = new Request( FETCH_MARC_HTML, record.doc_id() );
41                 req.send(true);
42                 var html = req.result();
43                 G.ui.rdetail.view_marc_box.innerHTML = html;
44         }
45         swapCanvas(G.ui.rdetail.view_marc_div);
46 }
47
48
49 function rdetailShowLocalCopies() {
50
51         var found = false;
52         var rows = copyRowParent.getElementsByTagName("tr");
53         for( var r in rows ) {
54                 if(rows[r].id == "__rdsrow") continue;
55                 hideMe(rows[r]);
56                 if(rows[r].getAttribute && rows[r].getAttribute("local")) {
57                         unHideMe(rows[r]);
58                         found = true;
59                 }
60         }
61
62         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
63         hideMe(G.ui.rdetail.cp_info_local);
64         unHideMe(G.ui.rdetail.cp_info_all);
65 }
66
67 function rdetailShowAllCopies() {
68         var rows = copyRowParent.getElementsByTagName("tr");
69         for( var r in rows ) 
70                 if(rows[r].getAttribute && rows[r].getAttribute("hasinfo"))
71                         unHideMe(rows[r]);
72
73         hideMe(G.ui.rdetail.cp_info_all);
74         unHideMe(G.ui.rdetail.cp_info_local);
75         hideMe(G.ui.rdetail.cp_info_none);
76 }
77
78
79 function _rdetailDraw(r) {
80         record = r.getResultObject();
81
82         G.ui.rdetail.title.appendChild(text(record.title()));
83         buildSearchLink(STYPE_AUTHOR, record.author(), G.ui.rdetail.author);
84         G.ui.rdetail.isbn.appendChild(text(cleanISBN(record.isbn())));
85         G.ui.rdetail.edition.appendChild(text(record.edition()));
86         G.ui.rdetail.pubdate.appendChild(text(record.pubdate()));
87         G.ui.rdetail.publisher.appendChild(text(record.publisher()));
88         G.ui.rdetail.tor.appendChild(text(record.types_of_resource()[0]));
89         setResourcePic( G.ui.rdetail.tor_pic, record.types_of_resource()[0]);
90         G.ui.rdetail.abstr.appendChild(text(record.synopsis()));
91
92
93
94         G.ui.rdetail.image.setAttribute("src", buildISBNSrc(cleanISBN(record.isbn())));
95         runEvt("rdetail", "recordDrawn");
96 }
97
98
99 function rdetailBuildInfoRows() {
100         var req = new Request(FETCH_COPY_COUNTS_SUMMARY, record.doc_id())
101         req.callback(_rdetailBuildInfoRows);
102         req.send();
103 }
104
105 /* pre-allocate the copy info table with all org units in correct order */
106 function _rdetailRows(node) {
107
108         if(node) {
109
110                 var row = copyRow.cloneNode(true);
111                 row.id = "cp_info_" + node.id();
112
113                 var libtd = findNodeByName( row, config.names.rdetail.lib_cell );
114                 var cntd = findNodeByName( row, config.names.rdetail.cn_cell );
115                 var cpctd = findNodeByName( row, config.names.rdetail.cp_count_cell );
116         
117                 libtd.appendChild(text(node.name()));
118                 libtd.setAttribute("style", "padding-left: " + ((findOrgDepth(node) - 1)  * 9) + "px;");
119         
120                 if(!findOrgType(node.ou_type()).can_have_vols()) {
121
122                         row.removeChild(cntd);
123                         row.removeChild(cpctd);
124
125                         libtd.setAttribute("colspan", numStatuses + 2 );
126                         libtd.colSpan = numStatuses + 2;
127                         addCSSClass(row, config.css.color_3);
128                 } 
129         
130                 copyRowParent.appendChild(row);
131
132         } else { node = globalOrgTree; }
133
134         for( var c in node.children() ) 
135                 _rdetailRows(node.children()[c]);
136 }
137
138 /* walk through the copy info and build rows where necessary */
139 function _rdetailBuildInfoRows(r) {
140
141         _rdetailRows();
142
143         var summary = r.getResultObject();
144
145         G.ui.rdetail.cp_info_loading.parentNode.removeChild(
146                 G.ui.rdetail.cp_info_loading);
147
148         var found = false;
149         for( var i = 0; i < summary.length; i++ ) {
150
151                 var arr = summary[i];
152                 var thisOrg = findOrgUnit(arr[0]);
153                 var rowNode = $("cp_info_" + thisOrg.id());
154
155                 if(rowNode.getAttribute("used")) {
156
157                         if( rowNode.nextSibling )
158                                 rowNode = copyRowParent.insertBefore(copyRow.cloneNode(true), rowNode.nextSibling);
159                         else
160                                 rowNode = copyRowParent.appendChild(copyRow.cloneNode(true));
161                         var n = findNodeByName( rowNode, config.names.rdetail.lib_cell );
162                         n.appendChild(text(thisOrg.name()));
163                         n.setAttribute("style", "padding-left: " + ((findOrgDepth(thisOrg) - 1)  * 9) + "px;");
164
165                 } else rowNode.setAttribute("used", "1");
166
167                 findNodeByName( rowNode, config.names.rdetail.cn_cell ).appendChild(text(arr[1]));
168
169                 var cpc_temp = rowNode.removeChild(
170                         findNodeByName(rowNode, config.names.rdetail.cp_count_cell));
171
172                 rdetailApplyStatuses(rowNode, cpc_temp, arr[2]);
173
174                 var isLocal = false;
175                 if( orgIsMine( findOrgUnit(getLocation()), thisOrg ) ) { found = true; isLocal = true; }
176                 rdetailSetPath( thisOrg, isLocal );
177
178         }
179
180         if(!found) unHideMe(G.ui.rdetail.cp_info_none);
181
182 }
183
184 /* sets the path to org as 'active' and displays the 
185         path if it's local */
186 function rdetailSetPath(org, local) {
187         if( findOrgDepth(org) == 0 ) return;
188         var row = $("cp_info_" + org.id());
189         row.setAttribute("hasinfo", "1");
190         if(local) {
191                 unHideMe(row);
192                 row.setAttribute("local", "1");
193         }
194         rdetailSetPath(findOrgUnit(org.parent_ou()), local);
195 }
196
197 function rdetailApplyStatuses( row, template, statuses ) {
198         for( var j in _statusPositions ) {
199                 var stat = _statusPositions[j];
200                 var val = statuses[stat.id()];
201                 var nn = template.cloneNode(true);
202                 if(val) nn.appendChild(text(val));
203                 else nn.appendChild(text(0));
204                 row.appendChild(nn);    
205         }
206 }
207
208
209 /* --------------------------------------------------------------------- */
210 var _statusPositions = {};
211
212 function rdetailBuildStatusColumns() {
213
214         rdetailGrabCopyStatuses();
215         var parent = statusRow;
216         var template = parent.removeChild(G.ui.rdetail.cp_status);
217
218         var i = 0;
219         for( i = 0; i < cp_statuses.length; i++ ) {
220
221                 var c = cp_statuses[i];
222
223                 if(c && c.holdable()) {
224
225                         var name = c.name();
226                         _statusPositions[i] = c;
227                         var node = template.cloneNode(true);
228                         var data = findNodeByName( node, config.names.rdetail.cp_status);
229
230                         data.appendChild(text(name));
231                         parent.appendChild(node);
232                 }       
233         }       
234
235         numStatuses = 0;
236         for(x in _statusPositions) numStatuses++; 
237 }
238
239 function rdetailGrabCopyStatuses() {
240         if(cp_statuses) return cp_statuses;
241    var req = new Request(FETCH_COPY_STATUSES);
242    req.send(true);
243         cp_statuses = req.result();
244         cp_statuses = cp_statuses.sort(_rdetailSortStatuses);
245 }
246
247 function _rdetailSortStatuses(a, b) {
248         return parseInt(a.id()) - parseInt(b.id());
249 }
250
251