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