]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/copy_details.js
617addfb7d6643e076a98da7ae4af5845ec1d858
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / copy_details.js
1 var cpdTemplate;
2 var cpdCounter = 0;
3 var cpdNodes = {};
4
5 function cpdBuild( contextTbody, contextRow, record, callnumber, orgid, depth ) {
6 var i = cpdCheckExisting(contextRow);
7         if(i) return i;
8
9         var counter = cpdCounter++;
10
11         /* yank out all of the template rows */
12         if(!cpdTemplate) cpdTemplate = $('rdetail_volume_details_row');
13         var templateRow = cpdTemplate.cloneNode(true);
14         templateRow.id = 'cpd_row_' + counter;
15
16         /* shove a dummy a tag in before the context previous sibling */
17         /*
18         contextTbody.insertBefore( 
19                 elem('a',{name:'slot_'+templateRow.id}), contextRow.previousSibling);
20         goTo('#slot_'+templateRow.id);
21         */
22
23         if(isXUL()) {
24                 /* unhide before we unhide/clone the parent */
25                 unHideMe($n(templateRow, 'age_protect_label'));
26                 unHideMe($n(templateRow, 'create_date_label'));
27                 unHideMe($n(templateRow, 'holdable_label'));
28         }
29
30         unHideMe(templateRow);
31
32         var print = $n(templateRow,'print');
33         print.onclick = function() { cpdBuildPrintPane(
34                 contextRow, record, callnumber, orgid, depth) };
35
36         var mainTbody = $n(templateRow, 'copies_tbody');
37         var extrasRow = mainTbody.removeChild($n(mainTbody, 'copy_extras_row'));
38
39         var req = new Request(FETCH_COPIES_FROM_VOLUME, record.doc_id(), callnumber, orgid);
40         req.callback(cpdDrawCopies);
41
42         req.request.args = { 
43                 contextTbody    : contextTbody, /* tbody that holds the contextrow */
44                 contextRow              : contextRow, /* the row our new row will be inserted after */
45                 record                  : record,
46                 callnumber              : callnumber, 
47                 orgid                           : orgid,
48                 depth                           : depth,
49                 templateRow             : templateRow, /* contains everything */
50                 mainTbody               : mainTbody, /* holds the copy rows */
51                 extrasRow               : extrasRow, /* wrapper row for all extras */
52                 counter                 : counter
53         };
54
55         if( contextRow.nextSibling ) 
56                 contextTbody.insertBefore( templateRow, contextRow.nextSibling );
57         else
58                 contextTbody.appendChild( templateRow );
59
60         req.send();
61         _debug('creating new details row with id ' + templateRow.id);
62         cpdNodes[templateRow.id] = { templateRow : templateRow };
63         return templateRow.id;
64 }
65
66
67 function cpdBuildPrintWindow(record, orgid) {
68         var div = $('rdetail_print_details').cloneNode(true);
69         div.id = "";
70
71         $n(div, 'lib').appendChild(text(findOrgUnit(orgid).name()));
72         $n(div, 'title').appendChild(text(record.title()));
73         $n(div, 'author').appendChild(text(record.author()));
74         $n(div, 'edition').appendChild(text(record.edition()));
75         $n(div, 'pubdate').appendChild(text(record.pubdate()));
76         $n(div, 'publisher').appendChild(text(record.publisher()));
77         $n(div, 'phys').appendChild(text(record.physical_description()));
78
79         return div;
80 }
81
82 function cpdStylePopupWindow(div) {
83         var tds = div.getElementsByTagName('td');
84         for( var i = 0; i < tds.length ; i++ ) {
85                 var td = tds[i];
86                 var sty = td.getAttribute('style');
87                 if(!sty) sty = "";
88                 td.setAttribute('style', sty + 'padding: 2px; border: 1px solid #F0F0E0;');
89                 if( td.className && td.className.match(/hide_me/) ) 
90                         td.parentNode.removeChild(td);
91         }
92 }
93
94
95 /* builds a friendly print window for this CNs data */
96 function cpdBuildPrintPane(contextRow, record, callnumber, orgid, depth) {
97
98         var div = cpdBuildPrintWindow( record, orgid);
99
100         $n(div, 'cn').appendChild(text(callnumber));
101
102         unHideMe($n(div, 'copy_header'));
103
104         var subtbody = $n(contextRow.nextSibling, 'copies_tbody');
105         var rows = subtbody.getElementsByTagName('tr');
106
107         for( var r = 0; r < rows.length; r++ ) {
108                 var row = rows[r];
109                 if(!row) continue;
110                 var name = row.getAttribute('name');
111                 if( name.match(/extras_row/) ) continue; /* hide the copy notes, stat-cats */
112                 var clone = row.cloneNode(true);
113                 var links = clone.getElementsByTagName('a');
114                 for( var i = 0; i < links.length; i++ ) 
115                         links[i].style.display = 'none';
116
117                 $n(div, 'tbody').appendChild(clone);
118         }
119
120         cpdStylePopupWindow(div);
121         openWindow( div.innerHTML);
122 }
123
124
125
126 /* hide any open tables and if we've already 
127         fleshed this cn, just unhide it */
128 function cpdCheckExisting( contextRow ) {
129
130         var existingid;
131         var next = contextRow.nextSibling;
132
133         if( next && next.getAttribute('templateRow') ) {
134                 var obj = cpdNodes[next.id];
135                 if(obj.templateRow.className.match(/hide_me/)) 
136                         unHideMe(obj.templateRow);
137                 else hideMe(obj.templateRow);
138                 existingid = next.id;
139         }
140
141         if(existingid) _debug('row exists with id ' + existingid);
142
143         for( var o in cpdNodes ) {
144                 var node = cpdNodes[o];
145                 if( existingid && o == existingid ) continue;
146                 hideMe(node.templateRow);
147                 removeCSSClass(node.templateRow.previousSibling, 'rdetail_context_row');
148         }
149
150         addCSSClass(contextRow, 'rdetail_context_row');
151         if(existingid) return existingid;
152         return null;
153 }
154
155 /*
156 function cpdFetchCopies(r) {
157         var args = r.args;
158         args.cn = r.getResultObject();
159         var req = new Request(FETCH_COPIES_FROM_VOLUME, args.cn.id());
160         req.request.args = args;
161         req.callback(cpdDrawCopies);
162         req.send();
163 }
164 */
165
166 function cpdDrawCopies(r) {
167
168         var copies              = r.getResultObject();
169         var args                        = r.args;
170         var copytbody   = $n(args.templateRow, 'copies_tbody');
171         var copyrow             = copytbody.removeChild($n(copytbody, 'copies_row'));
172
173         if(isXUL()) {
174                 /* unhide before we unhide/clone the parent */
175                 unHideMe($n(copyrow, 'age_protect_value'));
176                 unHideMe($n(copyrow, 'create_date_value'));
177                 unHideMe($n(copyrow, 'copy_holdable_td'));
178         }
179
180         for( var i = 0; i < copies.length; i++ ) {
181                 var row = copyrow.cloneNode(true);
182                 var copyid = copies[i];
183                 var req = new Request(FETCH_FLESHED_COPY, copies[i]);
184                 req.callback(cpdDrawCopy);
185                 req.request.args = r.args;
186                 req.request.row = row;
187                 req.send();
188                 copytbody.appendChild(row);
189         }
190 }
191
192 function cpdDrawCopy(r) {
193         var copy = r.getResultObject();
194         var row  = r.row;
195
196         $n(row, 'barcode').appendChild(text(copy.barcode()));
197         $n(row, 'location').appendChild(text(copy.location().name()));
198         $n(row, 'status').appendChild(text(copy.status().name()));
199
200         if(isXUL()) {
201
202                 /* show the hold link */
203                 var l = $n(row, 'copy_hold_link');
204                 unHideMe(l);
205                 l.onclick = function() {
206                         holdsDrawEditor( 
207                                 { 
208                                         type                    : 'C',
209                                         copyObject      : copy,
210                                         onComplete      : function(){}
211                                 }
212                         );
213                 }
214
215                 if( copy.age_protect() ) 
216                         appendClear($n(row, 'age_protect_value'), text(copy.age_protect().name()));
217
218                 var cd = copy.create_date();
219                 cd = cd.replace(/T.*/, '');
220                 $n(row, 'create_date_value').appendChild(text(cd));
221
222                 var yes = $('rdetail.yes').innerHTML;
223                 var no = $('rdetail.no').innerHTML;
224
225                 if( isTrue(copy.holdable()) &&
226                                 isTrue(copy.location().holdable()) &&
227                                 isTrue(copy.status().holdable()) ) {
228                         $n(row, 'copy_is_holdable').appendChild(text(yes));     
229                 } else {
230                         $n(row, 'copy_is_holdable').appendChild(text(no));      
231                 }
232
233         }
234
235         r.args.copy = copy;
236         r.args.copyrow = row;
237         cpdShowNotes(copy, r.args)
238         cpdShowStats(copy, r.args);
239
240 }
241
242 function _cpdExtrasInit(args) {
243
244         var newrid      = 'extras_row_' + args.copy.barcode();
245         var newrow      = $(newrid);
246         if(!newrow) newrow = args.extrasRow.cloneNode(true);
247         var tbody       = $n(newrow, 'extras_tbody');
248         var rowt                = $n(tbody, 'extras_row');
249         newrow.id       = newrid;
250
251         var cr = args.copyrow;
252         var nr = cr.nextSibling;
253         var np = args.mainTbody;
254
255         /* insert the extras row into the main table */
256         if(nr) np.insertBefore( newrow, nr );
257         else np.appendChild(newrow);
258
259         var link = $n(args.copyrow, 'details_link');
260         var link2 = $n(args.copyrow, 'less_details_link');
261         var id = newrow.id;
262         link.id = id + '_morelink';
263         link2.id = id + '_lesslink';
264         unHideMe(link);
265         hideMe(link2);
266
267         link.setAttribute('href', 
268                         'javascript:unHideMe($("'+link2.id+'")); hideMe($("'+link.id+'"));unHideMe($("'+newrow.id+'"));');
269
270         link2.setAttribute('href', 
271                         'javascript:unHideMe($("'+link.id+'")); hideMe($("'+link2.id+'"));hideMe($("'+newrow.id+'"));');
272
273         return [ tbody, rowt ];
274 }
275
276 function cpdShowNotes(copy, args) {
277         var notes = copy.notes();
278         if(!notes || notes.length == 0) return;
279
280         var a = _cpdExtrasInit(args);
281         var tbody = a[0];
282         var rowt = a[1];
283
284         for( var n in notes ) {
285                 var note = notes[n];
286                 if(!isTrue(note.pub())) continue;
287                 var row = rowt.cloneNode(true);
288                 $n(row, 'key').appendChild(text(note.title()));
289                 $n(row, 'value').appendChild(text(note.value()));
290                 unHideMe($n(row, 'note'));
291                 unHideMe(row);
292                 tbody.appendChild(row);
293         }
294 }
295
296
297 function cpdShowStats(copy, args) {
298         var entries = copy.stat_cat_entry_copy_maps();
299         if(!entries || entries.length == 0) return;
300                 
301         var a = _cpdExtrasInit(args);
302         var tbody = a[0];
303         var rowt = a[1];
304
305         for( var n in entries ) {
306                 var entry = entries[n];
307                 var row = rowt.cloneNode(true);
308                 $n(row, 'key').appendChild(text(entry.stat_cat().name()));
309                 $n(row, 'value').appendChild(text(entry.stat_cat_entry().value()));
310                 unHideMe($n(row, 'cat'));
311                 unHideMe(row);
312                 tbody.appendChild(row);
313         }
314 }
315