]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/copy_details.js
inisial staff client integration in record details page w/ new staff js file; move...
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / copy_details.js
1 var cpdTemplate;
2 var cpdCounter = 0;
3 var cpdNodes = {};
4
5 /* showDueDate will show the due date in the OPAC */
6 var showDueDate = false;
7 /* showDueTime will show the due time (hours and minutes) in the OPAC;
8    if showDueDate is false, then showDueTime has no effect
9 */
10 var showDueTime = false;
11
12 function cpdBuild( contextTbody, contextRow, record, callnumber, orgid, depth, copy_location ) {
13         var i = cpdCheckExisting(contextRow);
14         if(i) return i;
15
16         var counter = cpdCounter++;
17
18         /* yank out all of the template rows */
19         if(!cpdTemplate) cpdTemplate = $('rdetail_volume_details_row');
20         var templateRow = cpdTemplate.cloneNode(true);
21         templateRow.id = 'cpd_row_' + counter;
22
23         /* shove a dummy a tag in before the context previous sibling */
24         /*
25         contextTbody.insertBefore( 
26                 elem('a',{name:'slot_'+templateRow.id}), contextRow.previousSibling);
27         goTo('#slot_'+templateRow.id);
28         */
29
30         if(isXUL()) {
31                 /* unhide before we unhide/clone the parent */
32                 unHideMe($n(templateRow, 'age_protect_label'));
33                 unHideMe($n(templateRow, 'create_date_label'));
34                 unHideMe($n(templateRow, 'holdable_label'));
35         }
36
37         if (isXUL() || showDueDate) {
38                 unHideMe($n(templateRow, 'due_date_label'));
39         }
40
41         unHideMe(templateRow);
42
43         var print = $n(templateRow,'print');
44         print.onclick = function() { cpdBuildPrintPane(
45                 contextRow, record, callnumber, orgid, depth) };
46
47         var mainTbody = $n(templateRow, 'copies_tbody');
48         var extrasRow = mainTbody.removeChild($n(mainTbody, 'copy_extras_row'));
49
50         var req = new Request(FETCH_COPIES_FROM_VOLUME, record.doc_id(), callnumber, orgid);
51         req.callback(cpdDrawCopies);
52
53         req.request.args = { 
54                 contextTbody    : contextTbody, /* tbody that holds the contextrow */
55                 contextRow              : contextRow, /* the row our new row will be inserted after */
56                 record                  : record,
57                 callnumber              : callnumber, 
58                 orgid                           : orgid,
59                 depth                           : depth,
60                 templateRow             : templateRow, /* contains everything */
61                 copy_location           : copy_location,
62                 mainTbody               : mainTbody, /* holds the copy rows */
63                 extrasRow               : extrasRow, /* wrapper row for all extras */
64                 counter                 : counter
65         };
66
67         if( contextRow.nextSibling ) 
68                 contextTbody.insertBefore( templateRow, contextRow.nextSibling );
69         else
70                 contextTbody.appendChild( templateRow );
71
72         req.send();
73         _debug('creating new details row with id ' + templateRow.id);
74         cpdNodes[templateRow.id] = { templateRow : templateRow };
75         return templateRow.id;
76 }
77
78
79 function cpdBuildPrintWindow(record, orgid) {
80         var div = $('rdetail_print_details').cloneNode(true);
81         div.id = "";
82
83         $n(div, 'lib').appendChild(text(findOrgUnit(orgid).name()));
84         $n(div, 'title').appendChild(text(record.title()));
85         $n(div, 'author').appendChild(text(record.author()));
86         $n(div, 'edition').appendChild(text(record.edition()));
87         $n(div, 'pubdate').appendChild(text(record.pubdate()));
88         $n(div, 'publisher').appendChild(text(record.publisher()));
89         $n(div, 'phys').appendChild(text(record.physical_description()));
90
91         return div;
92 }
93
94 function cpdStylePopupWindow(div) {
95         var tds = div.getElementsByTagName('td');
96         for( var i = 0; i < tds.length ; i++ ) {
97                 var td = tds[i];
98                 var sty = td.getAttribute('style');
99                 if(!sty) sty = "";
100                 td.setAttribute('style', sty + 'padding: 2px; border: 1px solid #F0F0E0;');
101                 if( td.className && td.className.match(/hide_me/) ) 
102                         td.parentNode.removeChild(td);
103         }
104 }
105
106
107 /* builds a friendly print window for this CNs data */
108 function cpdBuildPrintPane(contextRow, record, callnumber, orgid, depth) {
109
110         var div = cpdBuildPrintWindow( record, orgid);
111
112         $n(div, 'cn').appendChild(text(callnumber));
113
114         unHideMe($n(div, 'copy_header'));
115
116         var subtbody = $n(contextRow.nextSibling, 'copies_tbody');
117         var rows = subtbody.getElementsByTagName('tr');
118
119         for( var r = 0; r < rows.length; r++ ) {
120                 var row = rows[r];
121                 if(!row) continue;
122                 var name = row.getAttribute('name');
123                 if( name.match(/extras_row/) ) continue; /* hide the copy notes, stat-cats */
124                 var clone = row.cloneNode(true);
125                 var links = clone.getElementsByTagName('a');
126                 for( var i = 0; i < links.length; i++ ) 
127                         links[i].style.display = 'none';
128
129                 $n(div, 'tbody').appendChild(clone);
130         }
131
132         cpdStylePopupWindow(div);
133         openWindow( div.innerHTML);
134 }
135
136
137
138 /* hide any open tables and if we've already 
139         fleshed this cn, just unhide it */
140 function cpdCheckExisting( contextRow ) {
141
142         var existingid;
143         var next = contextRow.nextSibling;
144
145         if( next && next.getAttribute('templateRow') ) {
146                 var obj = cpdNodes[next.id];
147                 if(obj.templateRow.className.match(/hide_me/)) 
148                         unHideMe(obj.templateRow);
149                 else hideMe(obj.templateRow);
150                 existingid = next.id;
151         }
152
153         if(existingid) _debug('row exists with id ' + existingid);
154
155         for( var o in cpdNodes ) {
156                 var node = cpdNodes[o];
157                 if( existingid && o == existingid ) continue;
158                 hideMe(node.templateRow);
159                 removeCSSClass(node.templateRow.previousSibling, 'rdetail_context_row');
160         }
161
162         addCSSClass(contextRow, 'rdetail_context_row');
163         if(existingid) return existingid;
164         return null;
165 }
166
167 /*
168 function cpdFetchCopies(r) {
169         var args = r.args;
170         args.cn = r.getResultObject();
171         var req = new Request(FETCH_COPIES_FROM_VOLUME, args.cn.id());
172         req.request.args = args;
173         req.callback(cpdDrawCopies);
174         req.send();
175 }
176 */
177
178 function cpdDrawCopies(r) {
179
180         var copies              = r.getResultObject();
181         var args                        = r.args;
182         var copytbody   = $n(args.templateRow, 'copies_tbody');
183         var copyrow             = copytbody.removeChild($n(copytbody, 'copies_row'));
184
185         if(isXUL()) {
186                 /* unhide before we unhide/clone the parent */
187                 unHideMe($n(copyrow, 'age_protect_value'));
188                 unHideMe($n(copyrow, 'create_date_value'));
189                 unHideMe($n(copyrow, 'copy_holdable_td'));
190         }
191
192         if(isXUL() || showDueDate) {
193                 unHideMe($n(copyrow, 'copy_due_date_td'));
194         }
195
196         for( var i = 0; i < copies.length; i++ ) {
197                 var row = copyrow.cloneNode(true);
198                 var copyid = copies[i];
199                 var req = new Request(FETCH_FLESHED_COPY, copies[i]);
200                 req.callback(cpdDrawCopy);
201                 req.request.args = r.args;
202                 req.request.row = row;
203                 req.send();
204                 copytbody.appendChild(row);
205         }
206 }
207
208 function cpdDrawCopy(r) {
209         var copy = r.getResultObject();
210         var row  = r.row;
211     var trow = r.args.templateRow;
212
213     if (r.args.copy_location && copy.location().name() != r.args.copy_location) {
214         hideMe(row);
215         return;
216     }
217
218         $n(row, 'barcode').appendChild(text(copy.barcode()));
219         $n(row, 'location').appendChild(text(copy.location().name()));
220         $n(row, 'status').appendChild(text(copy.status().name()));
221
222     // append comma-separated list of part this copy is linked to
223     if(copy.parts() && copy.parts().length) {
224         unHideMe($n(trow, 'copy_part_label'));
225         unHideMe($n(row, 'copy_part'));
226         for(var i = 0; i < copy.parts().length; i++) {
227             var part = copy.parts()[i];
228             var node = $n(row, 'copy_part');
229             if(i > 0) node.appendChild(text(','));
230             node.appendChild(text(part.label()));
231         }
232     }
233
234         if(isXUL()) {
235                 /* show the hold link */
236                 var l = $n(row, 'copy_hold_link');
237                 unHideMe(l);
238                 l.onclick = function() {
239                         holdsDrawEditor( 
240                                 { 
241                                         type                    : 'C',
242                                         copyObject      : copy,
243                                         onComplete      : function(){}
244                                 }
245                         );
246                 }
247
248                 /* show the book now link */
249                 l = $n(row, 'copy_reserve_link');
250                 unHideMe(l);
251                 l.onclick = function() {
252                         // XXX FIXME this duplicates things in cat/util.js
253                         // Also needs i18n
254                         dojo.require("fieldmapper.Fieldmapper");
255                         var r = fieldmapper.standardRequest(
256                                 ["open-ils.booking",
257                                         "open-ils.booking.resources.create_from_copies"],
258                                 [G.user.session, [copy.id()]]
259                         );
260                         if (!r) {
261                                 alert("No response from server!");
262                         } else if (r.ilsevent != undefined) {
263                                 alert("Error from server:\n" + js2JSON(r));
264                         } else {
265                                 xulG.auth = {"session": {"key": G.user.session}};
266                                 xulG.bresv_interface_opts = {"booking_results": r};
267                                 location.href = "/eg/booking/reservation";
268                         }
269                 }
270
271                 if( copy.age_protect() ) 
272                         appendClear($n(row, 'age_protect_value'), text(copy.age_protect().name()));
273
274                 var cd = copy.create_date();
275                 cd = cd.replace(/T.*/, '');
276                 $n(row, 'create_date_value').appendChild(text(cd));
277
278                 var yes = $('rdetail.yes').innerHTML;
279                 var no = $('rdetail.no').innerHTML;
280
281                 if( isTrue(copy.holdable()) &&
282                                 isTrue(copy.location().holdable()) &&
283                                 isTrue(copy.status().holdable()) ) {
284                         $n(row, 'copy_is_holdable').appendChild(text(yes));     
285                 } else {
286                         $n(row, 'copy_is_holdable').appendChild(text(no));      
287                 }
288         }
289
290         if (isXUL() || showDueDate) {
291                 var circ;
292                 if( copy.circulations() ) {
293                         circ = copy.circulations()[0];
294                         if( circ ) {
295                                 var due_time = dojo.date.stamp.fromISOString(circ.due_date());
296                                 if( showDueTime ) {
297                                         $n(row, 'copy_due_date').appendChild(text(dojo.date.locale.format(due_time, {"formatLength": "medium"})));
298                                 } else {
299                                         $n(row, 'copy_due_date').appendChild(text(dojo.date.locale.format(due_time, {"selector": "date", "formatLength": "medium"})));
300                                 }
301                         }
302                 }
303         }
304
305         r.args.copy = copy;
306         r.args.copyrow = row;
307         cpdShowNotes(copy, r.args)
308         cpdShowStats(copy, r.args);
309
310 }
311
312 function _cpdExtrasInit(args) {
313
314         var newrid      = 'extras_row_' + args.copy.barcode();
315         var newrow      = $(newrid);
316         if(!newrow) newrow = args.extrasRow.cloneNode(true);
317         var tbody       = $n(newrow, 'extras_tbody');
318         var rowt                = $n(tbody, 'extras_row');
319         newrow.id       = newrid;
320
321         var cr = args.copyrow;
322         var nr = cr.nextSibling;
323         var np = args.mainTbody;
324
325         /* insert the extras row into the main table */
326         if(nr) np.insertBefore( newrow, nr );
327         else np.appendChild(newrow);
328
329         var link = $n(args.copyrow, 'details_link');
330         var link2 = $n(args.copyrow, 'less_details_link');
331         var id = newrow.id;
332         link.id = id + '_morelink';
333         link2.id = id + '_lesslink';
334         unHideMe(link);
335         hideMe(link2);
336
337         link.setAttribute('href', 
338                         'javascript:unHideMe($("'+link2.id+'")); hideMe($("'+link.id+'"));unHideMe($("'+newrow.id+'"));');
339
340         link2.setAttribute('href', 
341                         'javascript:unHideMe($("'+link.id+'")); hideMe($("'+link2.id+'"));hideMe($("'+newrow.id+'"));');
342
343         return [ tbody, rowt ];
344 }
345
346 function cpdShowNotes(copy, args) {
347         var notes = copy.notes();
348         if(!notes || notes.length == 0) return;
349
350         var a = _cpdExtrasInit(args);
351         var tbody = a[0];
352         var rowt = a[1];
353
354         for( var n in notes ) {
355                 var note = notes[n];
356                 if(!isTrue(note.pub())) continue;
357                 var row = rowt.cloneNode(true);
358                 $n(row, 'key').appendChild(text(note.title()));
359                 $n(row, 'value').appendChild(text(note.value()));
360                 unHideMe($n(row, 'note'));
361                 unHideMe(row);
362                 tbody.appendChild(row);
363         }
364 }
365
366
367 function cpdShowStats(copy, args) {
368         var entries = copy.stat_cat_entry_copy_maps();
369         if(!entries || entries.length == 0) return;
370
371         var visibleStatCat = false;
372
373         /*
374                 check all copy stat cats; if we find one that's OPAC visible,
375                 set the flag and break the loop. If we've found one, or we're
376                 in the staff client, build the table. if not, we return doing
377                 nothing, as though the stat_cat_entry_copy_map was empty or null
378         */
379
380         for( var n in entries )
381         {
382                         var entry = entries[n];
383                         if(isTrue(entry.stat_cat().opac_visible()))
384                         {
385                                 visibleStatCat = true;
386                                 break;
387                         }
388         }
389
390         if(!(isXUL() || visibleStatCat)) return;
391
392         var a = _cpdExtrasInit(args);
393         var tbody = a[0];
394         var rowt = a[1];
395
396         for( var n in entries ) {
397                 var entry = entries[n];
398                 if(!(isXUL() || isTrue(entry.stat_cat().opac_visible()))) continue;
399                 var row = rowt.cloneNode(true);
400                 $n(row, 'key').appendChild(text(entry.stat_cat().name()));
401                 $n(row, 'value').appendChild(text(entry.stat_cat_entry().value()));
402                 unHideMe($n(row, 'cat'));
403                 unHideMe(row);
404                 tbody.appendChild(row);
405         }
406 }
407