showing edition, pubdate, publisher on title result page
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / result_common.js
1
2 var recordsHandled = 0;
3 var recordsCache = [];
4
5 /* set up the event handlers */
6 G.evt.result.hitCountReceived.push(resultSetHitInfo);
7 G.evt.result.recordReceived.push(resultDisplayRecord, resultAddCopyCounts);
8 G.evt.result.copyCountsReceived.push(resultDisplayCopyCounts);
9 G.evt.result.allRecordsReceived.push(resultBuildCaches, resultDrawSubjects, resultDrawAuthors, resultDrawSeries);
10
11 /* do this after we have ID's so the rank for mr pages will be correct */
12 attachEvt("result", "preCollectRecords", resultPaginate);
13
14 /* returns the last 'index' postion ocurring in this page */
15 function resultFinalPageIndex() {
16         if(getHitCount() < (getOffset() + getDisplayCount())) 
17                 return getHitCount() - 1;
18         return getOffset() + getDisplayCount() - 1;
19 }
20
21 /* set the search result info, number of hits, which results we're 
22         displaying, links to the next/prev pages, etc. */
23 function resultSetHitInfo() { 
24         var c;  
25         if( getDisplayCount() > (getHitCount() - getOffset()))  c = getHitCount();
26         else c = getDisplayCount() + getOffset();
27
28         var pages = getHitCount() / getDisplayCount();
29         if(pages % 1) pages = parseInt(pages) + 1;
30
31         G.ui.result.current_page.appendChild(text( (getOffset()/getDisplayCount()) + 1));
32         G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
33
34         G.ui.result.offset_start.appendChild(text(getOffset() + 1));
35         G.ui.result.offset_end.appendChild(text(c));
36         G.ui.result.result_count.appendChild(text(getHitCount()));
37         unHideMe(G.ui.result.info);
38 }
39
40
41 function resultPaginate() {
42         var o = getOffset();
43
44         if( !((o + getDisplayCount()) >= getHitCount()) ) {
45
46                 var args = {};
47                 args[PARAM_OFFSET] = o + getDisplayCount();
48                 G.ui.result.next_link.setAttribute("href", buildOPACLink(args)); 
49                 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
50
51                 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
52                 G.ui.result.end_link.setAttribute("href", buildOPACLink(args)); 
53                 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
54         }
55
56         if( o > 0 ) {
57
58                 var args = {};
59                 args[PARAM_OFFSET] = o - getDisplayCount();
60                 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args)); 
61                 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
62
63                 args[PARAM_OFFSET] = 0;
64                 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args)); 
65                 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
66         }
67 }
68
69
70 /* display the record info in the record display table 'pos' is the 
71                 zero based position the record should have in the display table */
72 function resultDisplayRecord(rec, pos, is_mr) {
73
74         if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
75         recordsHandled++;
76         recordsCache.push(rec);
77
78
79         var r = table.rows[pos + 1];
80         
81         try {
82                 var rank = parseFloat(ranks[pos + getOffset()]);
83                 rank = ( rank / getTopRank() ) * 100;
84                 rank = parseInt(rank) + "%";
85                 var relspan = findNodeByName(r, "relevancy_span");
86                 relspan.appendChild(text(rank));
87                 unHideMe(relspan.parentNode);
88         } catch(e){ }
89
90         var pic = findNodeByName(r, config.names.result.item_jacket);
91         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
92
93         var title_link = findNodeByName(r, config.names.result.item_title);
94         var author_link = findNodeByName(r, config.names.result.item_author);
95
96         if( is_mr )  {
97                 var onlyrec = onlyrecord[ getOffset() + pos ];
98                 if(onlyrec) {
99                         var id = rec.doc_id();
100                         rec.doc_id(onlyrec);
101                         buildTitleDetailLink(rec, title_link); 
102                         rec.doc_id(id);
103                 } else buildTitleLink(rec, title_link); 
104         } else  buildTitleDetailLink(rec, title_link); 
105
106         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
107
108         if(! is_mr ) {
109         
110                 if(!isNull(rec.edition()))      {
111                         unHideMe( findNodeByName(r, "result_table_extra_span"));
112                         findNodeByName(r, "result_table_edition_span").appendChild( text( rec.edition()) );
113                 }
114                 if(!isNull(rec.pubdate())) {
115                         unHideMe( findNodeByName(r, "result_table_extra_span"));
116                         unHideMe(findNodeByName(r, "result_table_pub_span"));
117                         findNodeByName(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
118                 }
119                 if(!isNull(rec.publisher()) ) {
120                         unHideMe( findNodeByName(r, "result_table_extra_span"));
121                         unHideMe(findNodeByName(r, "result_table_pub_span"));
122                         findNodeByName(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
123                 }
124         }
125
126         resultBuildFormatIcons( r, rec );
127
128         unHideMe(r);
129         
130         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
131
132         if(resultPageIsDone())  {
133                 /* hide the 'now loading...' message */
134                 hideMe(G.ui.common.loading);
135                 runEvt('result', 'allRecordsReceived', recordsCache);
136         }
137 }
138
139 function resultBuildFormatIcons( row, rec ) {
140
141         var ress = rec.types_of_resource();
142
143         for( var i in ress ) {
144
145                 var res = ress[i];
146                 var link = findNodeByName(row, res + "_link");
147                 link.title = res;
148                 var img = link.getElementsByTagName("img")[0];
149                 removeCSSClass( img, config.css.dim );
150
151                 var f = getForm();
152                 if( f != "all" ) {
153                         if( f != modsFormatToMARC(res) ) 
154                                 addCSSClass( img, config.css.dim2);
155                 }
156
157
158                 var args = {};
159                 args.page = RRESULT;
160                 args[PARAM_OFFSET] = 0;
161                 args[PARAM_MRID] = rec.doc_id();
162                 args[PARAM_FORM] = modsFormatToMARC(res);
163
164                 link.setAttribute("href", buildOPACLink(args));
165
166         }
167
168 }
169
170
171 function resultPageIsDone(pos) {
172         return (recordsHandled == getDisplayCount() 
173                 || recordsHandled + getOffset() == getHitCount());
174 }
175
176 var resultCCHeaderApplied = false;
177
178 /* -------------------------------------------------------------------- */
179 /* dynamically add the copy count rows based on the org type 'countsrow' 
180         is the row into which we will add TD's to hold the copy counts 
181         This code generates copy count cells with an id of
182         'copy_count_cell_<depth>_<pagePosition>'  */
183 function resultAddCopyCounts(rec, pagePosition) {
184
185         var r = table.rows[pagePosition + 1];
186         var countsrow = findNodeByName(r, config.names.result.counts_row );
187         var ccell = findNodeByName(countsrow, config.names.result.count_cell);
188
189         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
190         var node = nodes[0];
191         var type = findOrgType(node.ou_type());
192         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
193         ccell.title = type.opac_label();
194         //addCSSClass(ccell, config.css.result.cc_cell_even);
195
196         var lastcell = ccell;
197         var lastheadcell = null;
198
199         var cchead = null;
200         var ccheadcell = null;
201         if(!resultCCHeaderApplied) {
202                 ccrow = getId('result_thead_row');
203                 ccheadcell =  ccrow.removeChild(findNodeByName(ccrow, "result_thead_ccell"));
204                 var t = ccheadcell.cloneNode(true);
205                 lastheadcell = t;
206                 t.appendChild(text(type.opac_label()));
207                 ccrow.appendChild(t);
208                 resultCCHeaderApplied = true;
209         }
210
211         if(nodes[1]) {
212
213                 var x = 1;
214                 var d = findOrgDepth(nodes[1]);
215                 var d2 = findOrgDepth(nodes[nodes.length -1]);
216
217                 for( var i = d; i <= d2 ; i++ ) {
218         
219                         ccell = ccell.cloneNode(true);
220
221                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
222                         //else addCSSClass(ccell, "copy_count_cell_even");
223
224                         var node = nodes[x++];
225                         var type = findOrgType(node.ou_type());
226         
227                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
228                         ccell.title = type.opac_label();
229                         countsrow.insertBefore(ccell, lastcell);
230                         lastcell = ccell;
231
232                         if(ccheadcell) {
233                                 var t = ccheadcell.cloneNode(true);
234                                 t.appendChild(text(type.opac_label()));
235                                 ccrow.insertBefore(t, lastheadcell);
236                                 lastheadcell = t;
237                         }
238                 }
239         }
240
241         unHideMe(getId("search_info_table"));
242 }
243
244 /* collect copy counts for a record using method 'methodName' */
245 function resultCollectCopyCounts(rec, pagePosition, methodName) {
246         if(rec == null || rec.doc_id() == null) return;
247         var req = new Request(methodName, getLocation(), rec.doc_id() );
248         req.request.userdata = [ rec, pagePosition ];
249         req.callback(resultHandleCopyCounts);
250         req.send();
251 }
252
253 function resultHandleCopyCounts(r) {
254         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
255 }
256
257
258 /* display the collected copy counts */
259 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
260         if(copy_counts == null || rec == null) return;
261         var i = 0;
262         while(copy_counts[i] != null) {
263                 var cell = getId("copy_count_cell_" + i +"_" + pagePosition);
264                 /*
265                 var span = cell.getElementsByTagName("div")[0];
266                 */
267                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
268
269                 i++;
270         }
271 }
272
273
274 /* captures extraneous info from each record */
275
276 var subjectCache = {};
277 var authorCache = {};
278 var seriesCache = {};
279
280 function resultBuildCaches(records) {
281         for( var r in records ) {
282                 var rec = records[r];
283                 for( var s in rec.subject() ) 
284                         subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
285                 authorCache[rec.author()] = 1;
286                 for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
287         }
288 }
289
290 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
291 function resultDrawSubjects() {
292
293         var subjs = [];
294         for( var s in subjectCache )
295                 subjs.push( { sub : s, count : subjectCache[s] } );
296         subjs.sort(resultSortSubjects);
297
298         var ss = [];
299         for( var s in subjs ) ss.push(subjs[s].sub);
300         resultDrawSidebarStuff(STYPE_SUBJECT, G.ui.sidebar.subject_item,  
301                 config.names.sidebar.subject_item, ss, G.ui.sidebar.subject);
302 }
303
304 function resultDrawAuthors() {
305         var auths = new Array();
306         for( var s in authorCache ) auths.push(s);
307         resultDrawSidebarStuff(STYPE_AUTHOR, G.ui.sidebar.author_item,  
308                 config.names.sidebar.author_item, auths.sort(), G.ui.sidebar.author);
309 }
310
311 function resultDrawSeries() {
312         var sers = new Array();
313         for( var s in seriesCache ) sers.push(s);
314         resultDrawSidebarStuff(STYPE_SERIES, G.ui.sidebar.series_item,  
315                 config.names.sidebar.series_item, sers.sort(), G.ui.sidebar.series);
316 }
317
318 /* search type, template node, href link name, array of text, node to unhide */
319 function resultDrawSidebarStuff(stype, node, linkname, items, wrapperNode) {
320         var parent = node.parentNode;
321         var template = parent.removeChild(node);
322         var x = 0;
323         var newnode = template.cloneNode(true);
324         var found = false;
325         for( var i in items ) {
326                 if(isNull(items[i])) continue;
327                 if(x++ > 7) break;
328                 buildSearchLink(stype, items[i], findNodeByName(newnode, linkname), 100);
329                 parent.appendChild(newnode);
330                 newnode = template.cloneNode(true);
331                 found = true;
332         }
333         if(found) unHideMe(wrapperNode);
334 }
335
336
337
338
339