pics are links
[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         var r = table.rows[pos + 1];
79         
80         try {
81                 var rank = parseFloat(ranks[pos + getOffset()]);
82                 rank = ( rank / getTopRank() ) * 100;
83                 rank = parseInt(rank) + "%";
84                 var relspan = findNodeByName(r, "relevancy_span");
85                 relspan.appendChild(text(rank));
86                 unHideMe(relspan.parentNode);
87         } catch(e){ }
88
89         var pic = findNodeByName(r, config.names.result.item_jacket);
90         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
91
92         var title_link = findNodeByName(r, config.names.result.item_title);
93         var author_link = findNodeByName(r, config.names.result.item_author);
94
95         if( is_mr )  {
96                 var onlyrec = onlyrecord[ getOffset() + pos ];
97                 if(onlyrec) {
98                         var id = rec.doc_id();
99                         rec.doc_id(onlyrec);
100                         buildTitleDetailLink(rec, title_link); 
101                         rec.doc_id(id);
102
103                         var args = {};
104                         args.page = RDETAIL;
105                         args[PARAM_OFFSET] = 0;
106                         args[PARAM_RID] = onlyrec;
107                         pic.parentNode.setAttribute("href", buildOPACLink(args));
108                         
109                 } else {
110                         buildTitleLink(rec, title_link); 
111                         var args = {};
112                         args.page = RRESULT;
113                         args[PARAM_OFFSET] = 0;
114                         args[PARAM_MRID] = rec.doc_id();
115                         pic.parentNode.setAttribute("href", buildOPACLink(args));
116                 }
117
118         } else {
119                 buildTitleDetailLink(rec, title_link); 
120                 var args = {};
121                 args.page = RDETAIL;
122                 args[PARAM_OFFSET] = 0;
123                 args[PARAM_RID] = rec.doc_id();
124                 pic.parentNode.setAttribute("href", buildOPACLink(args));
125         }
126
127         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
128
129         if(! is_mr ) {
130         
131                 if(!isNull(rec.edition()))      {
132                         unHideMe( findNodeByName(r, "result_table_extra_span"));
133                         findNodeByName(r, "result_table_edition_span").appendChild( text( rec.edition()) );
134                 }
135                 if(!isNull(rec.pubdate())) {
136                         unHideMe( findNodeByName(r, "result_table_extra_span"));
137                         unHideMe(findNodeByName(r, "result_table_pub_span"));
138                         findNodeByName(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
139                 }
140                 if(!isNull(rec.publisher()) ) {
141                         unHideMe( findNodeByName(r, "result_table_extra_span"));
142                         unHideMe(findNodeByName(r, "result_table_pub_span"));
143                         findNodeByName(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
144                 }
145         }
146
147         resultBuildFormatIcons( r, rec );
148
149         unHideMe(r);
150         
151         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
152
153         if(resultPageIsDone())  {
154                 /* hide the 'now loading...' message */
155                 hideMe(G.ui.common.loading);
156                 runEvt('result', 'allRecordsReceived', recordsCache);
157         }
158 }
159
160 function resultBuildFormatIcons( row, rec ) {
161
162         var ress = rec.types_of_resource();
163
164         for( var i in ress ) {
165
166                 var res = ress[i];
167                 var link = findNodeByName(row, res + "_link");
168                 link.title = res;
169                 var img = link.getElementsByTagName("img")[0];
170                 removeCSSClass( img, config.css.dim );
171
172                 var f = getForm();
173                 if( f != "all" ) {
174                         if( f != modsFormatToMARC(res) ) 
175                                 addCSSClass( img, config.css.dim2);
176                 }
177
178
179                 var args = {};
180                 args.page = RRESULT;
181                 args[PARAM_OFFSET] = 0;
182                 args[PARAM_MRID] = rec.doc_id();
183                 args[PARAM_FORM] = modsFormatToMARC(res);
184
185                 link.setAttribute("href", buildOPACLink(args));
186
187         }
188
189 }
190
191
192 function resultPageIsDone(pos) {
193         return (recordsHandled == getDisplayCount() 
194                 || recordsHandled + getOffset() == getHitCount());
195 }
196
197 var resultCCHeaderApplied = false;
198
199 /* -------------------------------------------------------------------- */
200 /* dynamically add the copy count rows based on the org type 'countsrow' 
201         is the row into which we will add TD's to hold the copy counts 
202         This code generates copy count cells with an id of
203         'copy_count_cell_<depth>_<pagePosition>'  */
204 function resultAddCopyCounts(rec, pagePosition) {
205
206         var r = table.rows[pagePosition + 1];
207         var countsrow = findNodeByName(r, config.names.result.counts_row );
208         var ccell = findNodeByName(countsrow, config.names.result.count_cell);
209
210         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
211         var node = nodes[0];
212         var type = findOrgType(node.ou_type());
213         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
214         ccell.title = type.opac_label();
215         //addCSSClass(ccell, config.css.result.cc_cell_even);
216
217         var lastcell = ccell;
218         var lastheadcell = null;
219
220         var cchead = null;
221         var ccheadcell = null;
222         if(!resultCCHeaderApplied) {
223                 ccrow = getId('result_thead_row');
224                 ccheadcell =  ccrow.removeChild(findNodeByName(ccrow, "result_thead_ccell"));
225                 var t = ccheadcell.cloneNode(true);
226                 lastheadcell = t;
227                 t.appendChild(text(type.opac_label()));
228                 ccrow.appendChild(t);
229                 resultCCHeaderApplied = true;
230         }
231
232         if(nodes[1]) {
233
234                 var x = 1;
235                 var d = findOrgDepth(nodes[1]);
236                 var d2 = findOrgDepth(nodes[nodes.length -1]);
237
238                 for( var i = d; i <= d2 ; i++ ) {
239         
240                         ccell = ccell.cloneNode(true);
241
242                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
243                         //else addCSSClass(ccell, "copy_count_cell_even");
244
245                         var node = nodes[x++];
246                         var type = findOrgType(node.ou_type());
247         
248                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
249                         ccell.title = type.opac_label();
250                         countsrow.insertBefore(ccell, lastcell);
251                         lastcell = ccell;
252
253                         if(ccheadcell) {
254                                 var t = ccheadcell.cloneNode(true);
255                                 t.appendChild(text(type.opac_label()));
256                                 ccrow.insertBefore(t, lastheadcell);
257                                 lastheadcell = t;
258                         }
259                 }
260         }
261
262         unHideMe(getId("search_info_table"));
263 }
264
265 /* collect copy counts for a record using method 'methodName' */
266 function resultCollectCopyCounts(rec, pagePosition, methodName) {
267         if(rec == null || rec.doc_id() == null) return;
268         var req = new Request(methodName, getLocation(), rec.doc_id() );
269         req.request.userdata = [ rec, pagePosition ];
270         req.callback(resultHandleCopyCounts);
271         req.send();
272 }
273
274 function resultHandleCopyCounts(r) {
275         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
276 }
277
278
279 /* display the collected copy counts */
280 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
281         if(copy_counts == null || rec == null) return;
282         var i = 0;
283         while(copy_counts[i] != null) {
284                 var cell = getId("copy_count_cell_" + i +"_" + pagePosition);
285                 /*
286                 var span = cell.getElementsByTagName("div")[0];
287                 */
288                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
289
290                 i++;
291         }
292 }
293
294
295 /* captures extraneous info from each record */
296
297 var subjectCache = {};
298 var authorCache = {};
299 var seriesCache = {};
300
301 function resultBuildCaches(records) {
302         for( var r in records ) {
303                 var rec = records[r];
304                 for( var s in rec.subject() ) 
305                         subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
306                 authorCache[rec.author()] = 1;
307                 for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
308         }
309 }
310
311 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
312 function resultDrawSubjects() {
313
314         var subjs = [];
315         for( var s in subjectCache )
316                 subjs.push( { sub : s, count : subjectCache[s] } );
317         subjs.sort(resultSortSubjects);
318
319         var ss = [];
320         for( var s in subjs ) ss.push(subjs[s].sub);
321         resultDrawSidebarStuff(STYPE_SUBJECT, G.ui.sidebar.subject_item,  
322                 config.names.sidebar.subject_item, ss, G.ui.sidebar.subject);
323 }
324
325 function resultDrawAuthors() {
326         var auths = new Array();
327         for( var s in authorCache ) auths.push(s);
328         resultDrawSidebarStuff(STYPE_AUTHOR, G.ui.sidebar.author_item,  
329                 config.names.sidebar.author_item, auths.sort(), G.ui.sidebar.author);
330 }
331
332 function resultDrawSeries() {
333         var sers = new Array();
334         for( var s in seriesCache ) sers.push(s);
335         resultDrawSidebarStuff(STYPE_SERIES, G.ui.sidebar.series_item,  
336                 config.names.sidebar.series_item, sers.sort(), G.ui.sidebar.series);
337 }
338
339 /* search type, template node, href link name, array of text, node to unhide */
340 function resultDrawSidebarStuff(stype, node, linkname, items, wrapperNode) {
341         var parent = node.parentNode;
342         var template = parent.removeChild(node);
343         var x = 0;
344         var newnode = template.cloneNode(true);
345         var found = false;
346         for( var i in items ) {
347                 if(isNull(items[i])) continue;
348                 if(x++ > 7) break;
349                 buildSearchLink(stype, items[i], findNodeByName(newnode, linkname), 100);
350                 parent.appendChild(newnode);
351                 newnode = template.cloneNode(true);
352                 found = true;
353         }
354         if(found) unHideMe(wrapperNode);
355 }
356
357
358
359
360