2 var recordsHandled = 0;
6 var resultFetchAllRecords = false;
8 /* set up the event handlers */
9 if( findCurrentPage() == MRESULT || findCurrentPage() == RRESULT ) {
10 G.evt.result.hitCountReceived.push(resultSetHitInfo);
11 G.evt.result.recordReceived.push(resultDisplayRecord, resultAddCopyCounts);
12 G.evt.result.copyCountsReceived.push(resultDisplayCopyCounts);
13 G.evt.result.allRecordsReceived.push(resultBuildCaches, resultDrawSubjects,
14 resultDrawAuthors, resultDrawSeries, function(){unHideMe($('result_info_2'))});
15 attachEvt('result','lowHits',resultLowHits);
16 attachEvt('result','zeroHits',resultZeroHits);
17 attachEvt( "common", "locationUpdated", resultSBSubmit );
18 /* do this after we have ID's so the rank for mr pages will be correct */
19 attachEvt("result", "preCollectRecords", resultPaginate);
22 function resultSBSubmit(){searchBarSubmit();}
24 /* returns the last 'index' postion ocurring in this page */
25 function resultFinalPageIndex() {
26 if(getHitCount() < (getOffset() + getDisplayCount()))
27 return getHitCount() - 1;
28 return getOffset() + getDisplayCount() - 1;
34 /* generic search method */
35 function resultCollectSearchIds( type, method, handler ) {
37 var sort = (getSort() == SORT_TYPE_REL) ? null : getSort();
38 var sortdir = (sort) ? ((getSortDir()) ? getSortDir() : SORT_DIR_ASC) : null;
45 var form = parseForm(getForm());
46 item_type = form.item_type;
47 item_form = form.item_form;
50 item_type = (getItemType()) ? getItemType().split(/,/) : null;
51 item_form = (getItemForm()) ? getItemForm().split(/,/) : null;
54 var limit = (resultFetchAllRecords) ? 1000 : getDisplayCount();
56 if( getOffset() > 0 ) {
57 if( getHitCount() > 0 && (getOffset() + getDisplayCount()) > getHitCount() )
58 limit = getHitCount() - getOffset();
61 args.org_unit = getLocation();
62 args.depth = getDepth();
64 args.offset = getOffset();
65 args.visibility_limit = 3000;
66 args.default_class = getStype();
68 if(sort) args.sort = sort;
69 if(sortdir) args.sort_dir = sortdir;
70 if(item_type) args.item_type = item_type;
71 if(item_form) args.item_form = item_form;
72 if(getAvail()) args.available = 1;
75 if(getAudience()) args.audience = getAudience().split(/,/);
76 if(getLitForm()) args.lit_form = getLitForm().split(/,/);
77 if(getLanguage()) args.language = getLanguage().split(/,/);
79 _debug('Search args: ' + js2JSON(args));
80 _debug('Raw query: ' + getTerm());
82 var req = new Request(method, args, getTerm(), 1);
83 req.callback(handler);
91 /* set the search result info, number of hits, which results we're
92 displaying, links to the next/prev pages, etc. */
93 function resultSetHitInfo() {
96 /* tell the user where the results are coming from */
97 var baseorg = findOrgUnit(getLocation());
98 var depth = getDepth();
99 var mydepth = findOrgDepth(baseorg);
100 if( findOrgDepth(baseorg) != depth ) {
101 var tmporg = baseorg;
102 while( mydepth > depth ) {
104 tmporg = findOrgUnit(tmporg.parent_ou());
106 unHideMe($('including_results_for'));
107 $('including_results_location').appendChild(text(tmporg.name()));
111 try{searchTimer.stop()}catch(e){}
113 //if( findCurrentPage() == MRESULT ) {
114 if( findCurrentPage() == MRESULT ||
116 (findCurrentPage() == RRESULT &&
118 getRtype() == RTYPE_TITLE ||
119 getRtype() == RTYPE_AUTHOR ||
120 getRtype() == RTYPE_SUBJECT ||
121 getRtype() == RTYPE_SERIES ||
122 getRtype() == RTYPE_KEYWORD
127 if(getHitCount() <= lowHitCount && getTerm())
128 runEvt('result', 'lowHits');
131 if(getHitCount() == 0) {
132 runEvt('result', 'zeroHits');
137 var pages = getHitCount() / getDisplayCount();
138 if(pages % 1) pages = parseInt(pages) + 1;
142 var cpage = (getOffset()/getDisplayCount()) + 1;
144 G.ui.result.current_page.appendChild(text(cpage));
145 G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
147 $('current_page2').appendChild(text(cpage));
148 $('num_pages2').appendChild(text(pages + ")")); /* the ) is dumb */
150 /* set the offsets */
151 var offsetEnd = getDisplayCount() + getOffset();
152 if( getDisplayCount() > (getHitCount() - getOffset()))
153 offsetEnd = getHitCount();
155 G.ui.result.offset_end.appendChild(text(offsetEnd));
156 G.ui.result.offset_start.appendChild(text(getOffset() + 1));
158 $('offset_end2').appendChild(text(offsetEnd));
159 $('offset_start2').appendChild(text(getOffset() + 1));
161 G.ui.result.result_count.appendChild(text(getHitCount()));
162 unHideMe(G.ui.result.info);
164 $('result_count2').appendChild(text(getHitCount()));
165 unHideMe($('result_info_div2'));
168 function resultLowHits() {
170 unHideMe($('result_low_hits'));
171 if(getHitCount() > 0)
172 unHideMe($('result_low_hits_msg'));
174 var sreq = new Request(CHECK_SPELL, getTerm());
175 sreq.callback(resultSuggestSpelling);
178 /* XXX patch to use the search results */
180 var words = getTerm().split(' ');
182 while( word = words.shift() ) {
183 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
184 areq.callback(resultLowHitXRef);
188 if( !(getForm() == null || getForm() == 'all' || getForm() == "") ) {
190 a[PARAM_FORM] = "all";
191 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
192 unHideMe($('low_hits_remove_format'));
195 resultSuggestSearchClass();
197 if(getTerm()) resultExpandSearch(); /* advanced search */
201 var lowHitsXRefLinkParent;
202 function resultLowHitXRef(r) {
203 if(!lowHitsXRefLink){
204 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
205 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
207 var res = r.getResultObject();
209 arr.concat(res.also);
210 if(arr && arr.length > 0) {
211 unHideMe($('low_hits_cross_ref'));
214 while( word = arr.shift() ) {
217 a[PARAM_TERM] = word;
218 var template = lowHitsXRefLink.cloneNode(true);
219 template.setAttribute('href',buildOPACLink(a));
220 template.appendChild(text(word));
221 lowHitsXRefLinkParent.appendChild(template);
222 lowHitsXRefLinkParent.appendChild(text(' '));
227 function resultZeroHits() {
229 unHideMe($('result_low_hits'));
230 unHideMe($('result_zero_hits_msg'));
231 //if(getTerm()) resultExpandSearch(); /* advanced search */
234 function resultExpandSearch() {
235 var top = findOrgDepth(globalOrgTree);
236 if(getDepth() == top) return;
237 unHideMe($('low_hits_expand_range'));
238 var par = $('low_hits_expand_link').parentNode;
239 var template = par.removeChild($('low_hits_expand_link'));
241 var bottom = getDepth();
242 while( top < bottom ) {
244 a[PARAM_DEPTH] = top;
245 var temp = template.cloneNode(true);
246 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
247 temp.setAttribute('href',buildOPACLink(a));
248 par.appendChild(temp);
253 function resultSuggestSearchClass() {
254 var stype = getStype();
255 if(stype == STYPE_KEYWORD) return;
257 unHideMe($('low_hits_search_type'));
258 if(stype != STYPE_TITLE) {
259 ref = $('low_hits_title_search');
261 a[PARAM_STYPE] = STYPE_TITLE;
262 ref.setAttribute('href',buildOPACLink(a));
264 if(stype != STYPE_AUTHOR) {
265 ref = $('low_hits_author_search');
267 a[PARAM_STYPE] = STYPE_AUTHOR;
268 ref.setAttribute('href',buildOPACLink(a));
270 if(stype != STYPE_SUBJECT) {
271 ref = $('low_hits_subject_search');
273 a[PARAM_STYPE] = STYPE_SUBJECT;
274 ref.setAttribute('href',buildOPACLink(a));
276 if(stype != STYPE_KEYWORD) {
277 ref = $('low_hits_keyword_search');
279 a[PARAM_STYPE] = STYPE_KEYWORD;
280 ref.setAttribute('href',buildOPACLink(a));
282 if(stype != STYPE_SERIES) {
283 ref = $('low_hits_series_search');
285 a[PARAM_STYPE] = STYPE_SERIES;
286 ref.setAttribute('href',buildOPACLink(a));
290 function resultSuggestSpelling(r) {
291 var res = r.getResultObject();
292 var phrase = getTerm();
293 var words = phrase.split(/ /);
297 for( var w = 0; w < words.length; w++ ) {
299 var blob = grep(res, function(i){return (i.word == word);});
300 if( blob ) blob = blob[0];
302 if( blob.word == word ) {
303 if( blob.suggestions && blob.suggestions[0] ) {
304 newterm += " " + blob.suggestions[0];
305 unHideMe($('did_you_mean'));
307 newterm += " " + word;
313 arg[PARAM_TERM] = newterm;
314 $('spell_check_link').setAttribute('href', buildOPACLink(arg));
315 $('spell_check_link').appendChild(text(newterm));
319 function resultPaginate() {
322 if( !( ((o) + getDisplayCount()) >= getHitCount()) ) {
325 args[PARAM_OFFSET] = o + getDisplayCount();
326 args[PARAM_SORT] = SORT;
327 args[PARAM_SORT_DIR] = SORT_DIR;
328 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
330 G.ui.result.next_link.setAttribute("href", buildOPACLink(args));
331 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
333 $('next_link2').setAttribute("href", buildOPACLink(args));
334 addCSSClass($('next_link2'), config.css.result.nav_active);
336 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
338 /* when hit count is divisible by display count, we have to adjust */
339 if( getHitCount() % getDisplayCount() == 0 )
340 args[PARAM_OFFSET] -= getDisplayCount();
342 G.ui.result.end_link.setAttribute("href", buildOPACLink(args));
343 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
345 $('end_link2').setAttribute("href", buildOPACLink(args));
346 addCSSClass($('end_link2'), config.css.result.nav_active);
352 args[PARAM_SORT] = SORT;
353 args[PARAM_SORT_DIR] = SORT_DIR;
354 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
356 args[PARAM_OFFSET] = o - getDisplayCount();
357 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args));
358 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
360 $('prev_link2').setAttribute( "href", buildOPACLink(args));
361 addCSSClass($('prev_link2'), config.css.result.nav_active);
363 args[PARAM_OFFSET] = 0;
364 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args));
365 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
367 $('search_home_link2').setAttribute( "href", buildOPACLink(args));
368 addCSSClass($('search_home_link2'), config.css.result.nav_active);
371 if(getDisplayCount() < getHitCount()) {
372 unHideMe($('start_end_links_span'));
373 unHideMe($('start_end_links_span2'));
377 try{searchTimer.stop()}catch(e){}
380 function buildunAPISpan (span, type, id) {
384 addCSSClass(span,'unapi-id');
388 'tag:' + cgi.server_name + ',' +
390 ':' + type + '/' + id
394 /* display the record info in the record display table 'pos' is the
395 zero based position the record should have in the display table */
396 function resultDisplayRecord(rec, pos, is_mr) {
398 if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
400 recordsCache.push(rec);
402 var r = table.rows[pos + 1];
406 var rank = parseFloat(ranks[pos + getOffset()]);
407 rank = parseInt( rank * 100 );
408 var relspan = $n(r, "relevancy_span");
409 relspan.appendChild(text(rank));
410 unHideMe(relspan.parentNode);
413 var pic = $n(r, config.names.result.item_jacket);
414 pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
416 var title_link = $n(r, config.names.result.item_title);
417 var author_link = $n(r, config.names.result.item_author);
420 var onlyrec = onlyrecord[ getOffset() + pos ];
422 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
426 args[PARAM_OFFSET] = 0;
427 args[PARAM_RID] = onlyrec;
428 args[PARAM_MRID] = rec.doc_id();
429 pic.parentNode.setAttribute("href", buildOPACLink(args));
430 title_link.setAttribute("href", buildOPACLink(args));
431 title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
434 buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
436 buildTitleLink(rec, title_link);
439 args[PARAM_OFFSET] = 0;
440 args[PARAM_MRID] = rec.doc_id();
441 pic.parentNode.setAttribute("href", buildOPACLink(args));
445 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
447 buildTitleDetailLink(rec, title_link);
450 args[PARAM_OFFSET] = 0;
451 args[PARAM_RID] = rec.doc_id();
452 pic.parentNode.setAttribute("href", buildOPACLink(args));
454 unHideMe($n(r,'place_hold_span'));
455 $n(r,'place_hold_link').setAttribute(
456 'href','javascript:holdsDrawEditor({record:"'+rec.doc_id()+'",type:"T"});');
459 buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
463 if(!isNull(rec.edition())) {
464 unHideMe( $n(r, "result_table_extra_span"));
465 $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
467 if(!isNull(rec.pubdate())) {
468 unHideMe( $n(r, "result_table_extra_span"));
469 unHideMe($n(r, "result_table_pub_span"));
470 $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
472 if(!isNull(rec.publisher()) ) {
473 unHideMe( $n(r, "result_table_extra_span"));
474 unHideMe($n(r, "result_table_pub_span"));
475 $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
478 if(!isNull(rec.physical_description()) ) {
479 unHideMe( $n(r, "result_table_extra_span"));
480 var t = " " + rec.physical_description();
481 //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
482 $n(r, "result_table_phys_span").appendChild( text(t));
487 resultBuildFormatIcons( r, rec, is_mr );
491 runEvt("result", "recordDrawn", rec.doc_id(), title_link);
494 if(resultPageIsDone()) {
495 runEvt('result', 'allRecordsReceived', recordsCache);
500 function _resultFindRec(id) {
501 for( var i = 0; i != recordsCache.length; i++ ) {
502 var rec = recordsCache[i];
503 if( rec && rec.doc_id() == id )
510 function resultBuildFormatIcons( row, rec, is_mr ) {
512 var ress = rec.types_of_resource();
514 for( var i in ress ) {
519 var link = $n(row, res + "_link");
521 var img = link.getElementsByTagName("img")[0];
522 removeCSSClass( img, config.css.dim );
526 if( f == modsFormatToMARC(res) )
527 addCSSClass( img, "dim2_border");
531 args[PARAM_OFFSET] = 0;
535 args[PARAM_TFORM] = modsFormatToMARC(res);
536 args[PARAM_MRID] = rec.doc_id();
540 args[PARAM_RID] = rec.doc_id();
543 link.setAttribute("href", buildOPACLink(args));
549 function resultPageIsDone(pos) {
550 return (recordsHandled == getDisplayCount()
551 || recordsHandled + getOffset() == getHitCount());
554 var resultCCHeaderApplied = false;
556 /* -------------------------------------------------------------------- */
557 /* dynamically add the copy count rows based on the org type 'countsrow'
558 is the row into which we will add TD's to hold the copy counts
559 This code generates copy count cells with an id of
560 'copy_count_cell_<depth>_<pagePosition>' */
561 function resultAddCopyCounts(rec, pagePosition) {
563 var r = table.rows[pagePosition + 1];
564 var countsrow = $n(r, config.names.result.counts_row );
565 var ccell = $n(countsrow, config.names.result.count_cell);
567 var nodes = orgNodeTrail(findOrgUnit(getLocation()));
569 var type = findOrgType(node.ou_type());
570 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
571 ccell.title = type.opac_label();
572 //addCSSClass(ccell, config.css.result.cc_cell_even);
574 var lastcell = ccell;
575 var lastheadcell = null;
578 var ccheadcell = null;
579 if(!resultCCHeaderApplied) {
580 ccrow = $('result_thead_row');
581 ccheadcell = ccrow.removeChild($n(ccrow, "result_thead_ccell"));
582 var t = ccheadcell.cloneNode(true);
584 t.appendChild(text(type.opac_label()));
585 ccrow.appendChild(t);
586 resultCCHeaderApplied = true;
592 var d = findOrgDepth(nodes[1]);
593 var d2 = findOrgDepth(nodes[nodes.length -1]);
595 for( var i = d; i <= d2 ; i++ ) {
597 ccell = ccell.cloneNode(true);
599 //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
600 //else addCSSClass(ccell, "copy_count_cell_even");
602 var node = nodes[x++];
603 var type = findOrgType(node.ou_type());
605 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
606 ccell.title = type.opac_label();
607 countsrow.insertBefore(ccell, lastcell);
611 var t = ccheadcell.cloneNode(true);
612 t.appendChild(text(type.opac_label()));
613 ccrow.insertBefore(t, lastheadcell);
619 unHideMe($("search_info_table"));
622 /* collect copy counts for a record using method 'methodName' */
623 function resultCollectCopyCounts(rec, pagePosition, methodName) {
624 if(rec == null || rec.doc_id() == null) return;
625 var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
626 req.request.userdata = [ rec, pagePosition ];
627 req.callback(resultHandleCopyCounts);
631 function resultHandleCopyCounts(r) {
632 runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject());
636 /* display the collected copy counts */
637 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
638 if(copy_counts == null || rec == null) return;
640 while(copy_counts[i] != null) {
641 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
642 var cts = copy_counts[i];
643 cell.appendChild(text(cts.available + " / " + cts.count));
646 /* here we style opac-invisible records for xul */
648 if( cts.depth == 0 ) {
649 if(!cts.transcendant && !cts.unshadow) {
650 _debug("found an opac-shadowed record: " + rec.doc_id());
651 var row = cell.parentNode.parentNode.parentNode.parentNode.parentNode;
653 addCSSClass( row, 'no_copies' );
655 addCSSClass( row, 'shadowed' );