no longer using dtree
[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                         /*
99                         var id = rec.doc_id();
100                         rec.doc_id(onlyrec);
101                         buildTitleDetailLink(rec, title_link); 
102                         rec.doc_id(id);
103                         */
104
105                         var args = {};
106                         args.page = RDETAIL;
107                         args[PARAM_OFFSET] = 0;
108                         args[PARAM_RID] = onlyrec;
109                         args[PARAM_MRID] = rec.doc_id();
110                         pic.parentNode.setAttribute("href", buildOPACLink(args));
111                         title_link.setAttribute("href", buildOPACLink(args));
112                         title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
113                         
114                 } else {
115                         buildTitleLink(rec, title_link); 
116                         var args = {};
117                         args.page = RRESULT;
118                         args[PARAM_OFFSET] = 0;
119                         args[PARAM_MRID] = rec.doc_id();
120                         pic.parentNode.setAttribute("href", buildOPACLink(args));
121                 }
122
123         } else {
124                 buildTitleDetailLink(rec, title_link); 
125                 var args = {};
126                 args.page = RDETAIL;
127                 args[PARAM_OFFSET] = 0;
128                 args[PARAM_RID] = rec.doc_id();
129                 pic.parentNode.setAttribute("href", buildOPACLink(args));
130         }
131
132         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
133
134         if(! is_mr ) {
135         
136                 if(!isNull(rec.edition()))      {
137                         unHideMe( findNodeByName(r, "result_table_extra_span"));
138                         findNodeByName(r, "result_table_edition_span").appendChild( text( rec.edition()) );
139                 }
140                 if(!isNull(rec.pubdate())) {
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.pubdate() ));
144                 }
145                 if(!isNull(rec.publisher()) ) {
146                         unHideMe( findNodeByName(r, "result_table_extra_span"));
147                         unHideMe(findNodeByName(r, "result_table_pub_span"));
148                         findNodeByName(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
149                 }
150         }
151
152         resultBuildFormatIcons( r, rec );
153
154         unHideMe(r);
155         
156         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
157
158         if(resultPageIsDone())  {
159                 /* hide the 'now loading...' message */
160                 /*
161                 hideMe(G.ui.common.loading);
162                 */
163                 runEvt('result', 'allRecordsReceived', recordsCache);
164         }
165 }
166
167 function resultBuildFormatIcons( row, rec ) {
168
169         var ress = rec.types_of_resource();
170
171         for( var i in ress ) {
172
173                 var res = ress[i];
174                 var link = findNodeByName(row, res + "_link");
175                 link.title = res;
176                 var img = link.getElementsByTagName("img")[0];
177                 removeCSSClass( img, config.css.dim );
178
179                 var f = getForm();
180                 if( f != "all" ) {
181                         /*
182                         if( f != modsFormatToMARC(res) ) 
183                                 addCSSClass( img, config.css.dim2);
184                         else
185                                 addCSSClass( img, "dim2_border");
186                                 */
187                         if( f == modsFormatToMARC(res) ) 
188                                 addCSSClass( img, "dim2_border");
189                 }
190
191
192                 var args = {};
193                 args.page = RRESULT;
194                 args[PARAM_OFFSET] = 0;
195                 args[PARAM_MRID] = rec.doc_id();
196                 args[PARAM_FORM] = modsFormatToMARC(res);
197
198                 link.setAttribute("href", buildOPACLink(args));
199
200         }
201
202 }
203
204
205 function resultPageIsDone(pos) {
206         return (recordsHandled == getDisplayCount() 
207                 || recordsHandled + getOffset() == getHitCount());
208 }
209
210 var resultCCHeaderApplied = false;
211
212 /* -------------------------------------------------------------------- */
213 /* dynamically add the copy count rows based on the org type 'countsrow' 
214         is the row into which we will add TD's to hold the copy counts 
215         This code generates copy count cells with an id of
216         'copy_count_cell_<depth>_<pagePosition>'  */
217 function resultAddCopyCounts(rec, pagePosition) {
218
219         var r = table.rows[pagePosition + 1];
220         var countsrow = findNodeByName(r, config.names.result.counts_row );
221         var ccell = findNodeByName(countsrow, config.names.result.count_cell);
222
223         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
224         var node = nodes[0];
225         var type = findOrgType(node.ou_type());
226         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
227         ccell.title = type.opac_label();
228         //addCSSClass(ccell, config.css.result.cc_cell_even);
229
230         var lastcell = ccell;
231         var lastheadcell = null;
232
233         var cchead = null;
234         var ccheadcell = null;
235         if(!resultCCHeaderApplied) {
236                 ccrow = getId('result_thead_row');
237                 ccheadcell =  ccrow.removeChild(findNodeByName(ccrow, "result_thead_ccell"));
238                 var t = ccheadcell.cloneNode(true);
239                 lastheadcell = t;
240                 t.appendChild(text(type.opac_label()));
241                 ccrow.appendChild(t);
242                 resultCCHeaderApplied = true;
243         }
244
245         if(nodes[1]) {
246
247                 var x = 1;
248                 var d = findOrgDepth(nodes[1]);
249                 var d2 = findOrgDepth(nodes[nodes.length -1]);
250
251                 for( var i = d; i <= d2 ; i++ ) {
252         
253                         ccell = ccell.cloneNode(true);
254
255                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
256                         //else addCSSClass(ccell, "copy_count_cell_even");
257
258                         var node = nodes[x++];
259                         var type = findOrgType(node.ou_type());
260         
261                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
262                         ccell.title = type.opac_label();
263                         countsrow.insertBefore(ccell, lastcell);
264                         lastcell = ccell;
265
266                         if(ccheadcell) {
267                                 var t = ccheadcell.cloneNode(true);
268                                 t.appendChild(text(type.opac_label()));
269                                 ccrow.insertBefore(t, lastheadcell);
270                                 lastheadcell = t;
271                         }
272                 }
273         }
274
275         unHideMe(getId("search_info_table"));
276 }
277
278 /* collect copy counts for a record using method 'methodName' */
279 function resultCollectCopyCounts(rec, pagePosition, methodName) {
280         if(rec == null || rec.doc_id() == null) return;
281         var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
282         req.request.userdata = [ rec, pagePosition ];
283         req.callback(resultHandleCopyCounts);
284         req.send();
285 }
286
287 function resultHandleCopyCounts(r) {
288         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
289 }
290
291
292 /* display the collected copy counts */
293 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
294         if(copy_counts == null || rec == null) return;
295         var i = 0;
296         while(copy_counts[i] != null) {
297                 var cell = getId("copy_count_cell_" + i +"_" + pagePosition);
298                 /*
299                 var span = cell.getElementsByTagName("div")[0];
300                 */
301                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
302
303                 i++;
304         }
305 }
306
307
308 /* captures extraneous info from each record */
309
310 var subjectCache = {};
311 var authorCache = {};
312 var seriesCache = {};
313
314 function resultBuildCaches(records) {
315         for( var r in records ) {
316                 var rec = records[r];
317                 for( var s in rec.subject() ) 
318                         subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
319                 authorCache[rec.author()] = 1;
320                 for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
321         }
322 }
323
324 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
325 function resultDrawSubjects() {
326
327         var subjs = [];
328         for( var s in subjectCache )
329                 subjs.push( { sub : s, count : subjectCache[s] } );
330         subjs.sort(resultSortSubjects);
331
332         var ss = [];
333         for( var s in subjs ) ss.push(subjs[s].sub);
334
335         resultDrawSidebarTrees( 
336                 STYPE_SUBJECT, 
337                 "subjectSidebarTree", ss, 
338                 getId("subject_tree_sidebar"), 
339                 getId("subject_sidebar_tree_div") );
340 }
341
342 function resultDrawAuthors() {
343         var auths = new Array();
344         for( var s in authorCache ) auths.push(s);
345
346         resultDrawSidebarTrees( 
347                 STYPE_AUTHOR, 
348                 "authorSidebarTree", auths.sort(), 
349                 getId("author_tree_sidebar"), 
350                 getId("author_sidebar_tree_div") );
351 }
352
353 function resultDrawSeries() {
354         var sers = new Array();
355         for( var s in seriesCache ) sers.push(s);
356         resultDrawSidebarTrees( 
357                 STYPE_SERIES, 
358                 "seriesSidebarTree", sers.sort(), 
359                 getId("series_tree_sidebar"), 
360                 getId("series_sidebar_tree_div") );
361 }
362
363 function resultDrawSidebarTrees( stype, treeName, items, wrapperNode, destNode ) {
364         var tree;
365         eval("tree = " + treeName);
366
367         var found = false;
368         var x = 0;
369         for( var i in items ) {
370
371                 if(isNull(items[i])) continue;
372                 if(x++ > 7) break;
373                 found = true;
374
375                 var item = normalize(truncate(items[i], 65));
376                 var trunc = 65;
377                 var args = {};
378                 var href = resultQuickLink( items[i], stype );
379                 tree.addNode( stype + "_" + items[i], treeName + 'Root', item, href );
380
381                 /*
382                 if(!IE)
383                         setTimeout('resultFireXRefReq("'+treeName+'","'+stype+'","'+item+'");',200);
384                         */
385                 if(!IE) resultFireXRefReq(treeName, stype, items[i]);
386         }
387
388         if(found) {
389                 unHideMe(wrapperNode);
390                 //tree.close(tree.rootid);
391         }
392 }
393
394 function resultFireXRefReq( treeName, stype, item ) {
395         var tree;
396         eval('tree=' + treeName);
397         var req = new Request(FETCH_CROSSREF, stype, item);
398         req.request._tree = tree;
399         req.request._item = item;
400         req.request._stype = stype;
401         req.callback(resultAppendCrossRef);
402         req.send();
403 }
404
405
406 function resultQuickLink( term, type ) {
407         var args = {};
408         args.page = MRESULT;
409         args[PARAM_OFFSET] = 0;
410         args[PARAM_TERM] = term;
411         args[PARAM_STYPE] = type;
412         return buildOPACLink(args);
413 }
414
415
416 function resultAppendCrossRef(r) {
417         var tree                = r._tree
418         var item                = r._item
419         var stype       = r._stype;
420         var result      = r.getResultObject();
421         if(!result) return;
422         var froms       = result['from'];
423         var alsos       = result['also'];
424
425         var total = 0;
426
427         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
428                 var string = normalize(truncate(froms[i], 45));
429                 if(getId(stype + '_' + froms[i])) continue;
430                 tree.addNode(stype + '_' + froms[i], 
431                         stype + '_' + item, string, resultQuickLink(froms[i],stype));
432         }
433         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
434                 var string = normalize(truncate(alsos[i], 45));
435                 if(getId(stype + '_' + alsos[i])) continue;
436                 tree.addNode(stype + '_' + alsos[i], 
437                         stype + '_' + item, string, resultQuickLink(alsos[i],stype));
438         }
439 }
440
441
442
443
444