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;
46 args.searches[getStype()] = {};
49 term = term.replace(/'/g,' ');
50 term = term.replace(/\\/g,' ');
51 args.searches[getStype()].term = term;
54 var form = parseForm(getForm());
55 item_type = form.item_type;
56 item_form = form.item_form;
59 args.searches = JSON2js(getSearches());
60 item_type = (getItemType()) ? getItemType().split(/,/) : null;
61 item_form = (getItemForm()) ? getItemForm().split(/,/) : null;
64 var limit = (resultFetchAllRecords) ? 1000 : getDisplayCount();
66 if( getOffset() > 0 ) {
67 if( getHitCount() > 0 && (getOffset() + getDisplayCount()) > getHitCount() )
68 limit = getHitCount() - getOffset();
71 args.org_unit = getLocation();
72 args.depth = getDepth();
74 args.offset = getOffset();
75 args.visibility_limit = 3000;
77 if(sort) args.sort = sort;
78 if(sortdir) args.sort_dir = sortdir;
79 if(item_type) args.item_type = item_type;
80 if(item_form) args.item_form = item_form;
81 if(getAvail()) args.available = 1;
84 if(getAudience()) args.audience = getAudience().split(/,/);
85 if(getLitForm()) args.lit_form = getLitForm().split(/,/);
86 if(getLanguage()) args.language = getLanguage().split(/,/);
88 //alert(js2JSON(args));
90 _debug('SEARCH: \n' + js2JSON(args) + '\n\n');
91 var req = new Request(method, args, 1);
92 req.callback(handler);
100 /* set the search result info, number of hits, which results we're
101 displaying, links to the next/prev pages, etc. */
102 function resultSetHitInfo() {
105 /* tell the user where the results are coming from */
106 var baseorg = findOrgUnit(getLocation());
107 var depth = getDepth();
108 var mydepth = findOrgDepth(baseorg);
109 if( findOrgDepth(baseorg) != depth ) {
110 var tmporg = baseorg;
111 while( mydepth > depth ) {
113 tmporg = findOrgUnit(tmporg.parent_ou());
115 unHideMe($('including_results_for'));
116 $('including_results_location').appendChild(text(tmporg.name()));
120 try{searchTimer.stop()}catch(e){}
122 //if( findCurrentPage() == MRESULT ) {
123 if( findCurrentPage() == MRESULT ||
125 (findCurrentPage() == RRESULT &&
127 getRtype() == RTYPE_TITLE ||
128 getRtype() == RTYPE_AUTHOR ||
129 getRtype() == RTYPE_SUBJECT ||
130 getRtype() == RTYPE_SERIES ||
131 getRtype() == RTYPE_KEYWORD
136 if(getHitCount() <= lowHitCount && getTerm())
137 runEvt('result', 'lowHits');
140 if(getHitCount() == 0) {
141 runEvt('result', 'zeroHits');
146 var pages = getHitCount() / getDisplayCount();
147 if(pages % 1) pages = parseInt(pages) + 1;
151 var cpage = (getOffset()/getDisplayCount()) + 1;
153 G.ui.result.current_page.appendChild(text(cpage));
154 G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
156 $('current_page2').appendChild(text(cpage));
157 $('num_pages2').appendChild(text(pages + ")")); /* the ) is dumb */
159 /* set the offsets */
160 var offsetEnd = getDisplayCount() + getOffset();
161 if( getDisplayCount() > (getHitCount() - getOffset()))
162 offsetEnd = getHitCount();
164 G.ui.result.offset_end.appendChild(text(offsetEnd));
165 G.ui.result.offset_start.appendChild(text(getOffset() + 1));
167 $('offset_end2').appendChild(text(offsetEnd));
168 $('offset_start2').appendChild(text(getOffset() + 1));
170 G.ui.result.result_count.appendChild(text(getHitCount()));
171 unHideMe(G.ui.result.info);
173 $('result_count2').appendChild(text(getHitCount()));
174 unHideMe($('result_info_div2'));
177 function resultLowHits() {
179 unHideMe($('result_low_hits'));
180 if(getHitCount() > 0)
181 unHideMe($('result_low_hits_msg'));
183 var sreq = new Request(CHECK_SPELL, getTerm());
184 sreq.callback(resultSuggestSpelling);
187 var words = getTerm().split(' ');
189 while( word = words.shift() ) {
190 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
191 areq.callback(resultLowHitXRef);
195 if( !(getForm() == null || getForm() == 'all' || getForm() == "") ) {
197 a[PARAM_FORM] = "all";
198 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
199 unHideMe($('low_hits_remove_format'));
202 resultSuggestSearchClass();
204 if(getTerm()) resultExpandSearch(); /* advanced search */
208 var lowHitsXRefLinkParent;
209 function resultLowHitXRef(r) {
210 if(!lowHitsXRefLink){
211 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
212 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
214 var res = r.getResultObject();
216 arr.concat(res.also);
217 if(arr && arr.length > 0) {
218 unHideMe($('low_hits_cross_ref'));
221 while( word = arr.shift() ) {
224 a[PARAM_TERM] = word;
225 var template = lowHitsXRefLink.cloneNode(true);
226 template.setAttribute('href',buildOPACLink(a));
227 template.appendChild(text(word));
228 lowHitsXRefLinkParent.appendChild(template);
229 lowHitsXRefLinkParent.appendChild(text(' '));
234 function resultZeroHits() {
236 unHideMe($('result_low_hits'));
237 unHideMe($('result_zero_hits_msg'));
238 //if(getTerm()) resultExpandSearch(); /* advanced search */
241 function resultExpandSearch() {
242 var top = findOrgDepth(globalOrgTree);
243 if(getDepth() == top) return;
244 unHideMe($('low_hits_expand_range'));
245 var par = $('low_hits_expand_link').parentNode;
246 var template = par.removeChild($('low_hits_expand_link'));
248 var bottom = getDepth();
249 while( top < bottom ) {
251 a[PARAM_DEPTH] = top;
252 var temp = template.cloneNode(true);
253 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
254 temp.setAttribute('href',buildOPACLink(a));
255 par.appendChild(temp);
260 function resultSuggestSearchClass() {
261 var stype = getStype();
262 if(stype == STYPE_KEYWORD) return;
264 unHideMe($('low_hits_search_type'));
265 if(stype != STYPE_TITLE) {
266 ref = $('low_hits_title_search');
268 a[PARAM_STYPE] = STYPE_TITLE;
269 ref.setAttribute('href',buildOPACLink(a));
271 if(stype != STYPE_AUTHOR) {
272 ref = $('low_hits_author_search');
274 a[PARAM_STYPE] = STYPE_AUTHOR;
275 ref.setAttribute('href',buildOPACLink(a));
277 if(stype != STYPE_SUBJECT) {
278 ref = $('low_hits_subject_search');
280 a[PARAM_STYPE] = STYPE_SUBJECT;
281 ref.setAttribute('href',buildOPACLink(a));
283 if(stype != STYPE_KEYWORD) {
284 ref = $('low_hits_keyword_search');
286 a[PARAM_STYPE] = STYPE_KEYWORD;
287 ref.setAttribute('href',buildOPACLink(a));
289 if(stype != STYPE_SERIES) {
290 ref = $('low_hits_series_search');
292 a[PARAM_STYPE] = STYPE_SERIES;
293 ref.setAttribute('href',buildOPACLink(a));
297 function resultSuggestSpelling(r) {
298 var res = r.getResultObject();
299 var phrase = getTerm();
300 var words = phrase.split(/ /);
304 for( var w = 0; w < words.length; w++ ) {
306 var blob = grep(res, function(i){return (i.word == word);});
307 if( blob ) blob = blob[0];
309 if( blob.word == word ) {
310 if( blob.suggestions && blob.suggestions[0] ) {
311 newterm += " " + blob.suggestions[0];
312 unHideMe($('did_you_mean'));
314 newterm += " " + word;
320 arg[PARAM_TERM] = newterm;
321 $('spell_check_link').setAttribute('href', buildOPACLink(arg));
322 $('spell_check_link').appendChild(text(newterm));
326 function resultPaginate() {
329 if( !( ((o) + getDisplayCount()) >= getHitCount()) ) {
332 args[PARAM_OFFSET] = o + getDisplayCount();
333 args[PARAM_SORT] = SORT;
334 args[PARAM_SORT_DIR] = SORT_DIR;
335 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
337 G.ui.result.next_link.setAttribute("href", buildOPACLink(args));
338 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
340 $('next_link2').setAttribute("href", buildOPACLink(args));
341 addCSSClass($('next_link2'), config.css.result.nav_active);
343 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
345 /* when hit count is divisible by display count, we have to adjust */
346 if( getHitCount() % getDisplayCount() == 0 )
347 args[PARAM_OFFSET] -= getDisplayCount();
349 G.ui.result.end_link.setAttribute("href", buildOPACLink(args));
350 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
352 $('end_link2').setAttribute("href", buildOPACLink(args));
353 addCSSClass($('end_link2'), config.css.result.nav_active);
359 args[PARAM_SORT] = SORT;
360 args[PARAM_SORT_DIR] = SORT_DIR;
361 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
363 args[PARAM_OFFSET] = o - getDisplayCount();
364 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args));
365 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
367 $('prev_link2').setAttribute( "href", buildOPACLink(args));
368 addCSSClass($('prev_link2'), config.css.result.nav_active);
370 args[PARAM_OFFSET] = 0;
371 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args));
372 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
374 $('search_home_link2').setAttribute( "href", buildOPACLink(args));
375 addCSSClass($('search_home_link2'), config.css.result.nav_active);
378 if(getDisplayCount() < getHitCount()) {
379 unHideMe($('start_end_links_span'));
380 unHideMe($('start_end_links_span2'));
384 try{searchTimer.stop()}catch(e){}
387 function buildunAPISpan (span, type, id) {
391 addCSSClass(span,'unapi-id');
395 'tag:' + cgi.server_name + ',' +
397 ':' + type + '/' + id
401 /* display the record info in the record display table 'pos' is the
402 zero based position the record should have in the display table */
403 function resultDisplayRecord(rec, pos, is_mr) {
405 if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
407 recordsCache.push(rec);
409 var r = table.rows[pos + 1];
413 var rank = parseFloat(ranks[pos + getOffset()]);
414 rank = parseInt( rank * 100 );
415 var relspan = $n(r, "relevancy_span");
416 relspan.appendChild(text(rank));
417 unHideMe(relspan.parentNode);
420 var pic = $n(r, config.names.result.item_jacket);
421 pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
423 var title_link = $n(r, config.names.result.item_title);
424 var author_link = $n(r, config.names.result.item_author);
427 var onlyrec = onlyrecord[ getOffset() + pos ];
429 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
433 args[PARAM_OFFSET] = 0;
434 args[PARAM_RID] = onlyrec;
435 args[PARAM_MRID] = rec.doc_id();
436 pic.parentNode.setAttribute("href", buildOPACLink(args));
437 title_link.setAttribute("href", buildOPACLink(args));
438 title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
441 buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
443 buildTitleLink(rec, title_link);
446 args[PARAM_OFFSET] = 0;
447 args[PARAM_MRID] = rec.doc_id();
448 pic.parentNode.setAttribute("href", buildOPACLink(args));
452 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
454 buildTitleDetailLink(rec, title_link);
457 args[PARAM_OFFSET] = 0;
458 args[PARAM_RID] = rec.doc_id();
459 pic.parentNode.setAttribute("href", buildOPACLink(args));
461 unHideMe($n(r,'place_hold_span'));
462 $n(r,'place_hold_link').setAttribute(
463 'href','javascript:holdsDrawEditor({record:"'+rec.doc_id()+'",type:"T"});');
466 buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
470 if(!isNull(rec.edition())) {
471 unHideMe( $n(r, "result_table_extra_span"));
472 $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
474 if(!isNull(rec.pubdate())) {
475 unHideMe( $n(r, "result_table_extra_span"));
476 unHideMe($n(r, "result_table_pub_span"));
477 $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
479 if(!isNull(rec.publisher()) ) {
480 unHideMe( $n(r, "result_table_extra_span"));
481 unHideMe($n(r, "result_table_pub_span"));
482 $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
485 if(!isNull(rec.physical_description()) ) {
486 unHideMe( $n(r, "result_table_extra_span"));
487 var t = " " + rec.physical_description();
488 //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
489 $n(r, "result_table_phys_span").appendChild( text(t));
494 resultBuildFormatIcons( r, rec, is_mr );
498 runEvt("result", "recordDrawn", rec.doc_id(), title_link);
501 if(resultPageIsDone()) {
502 runEvt('result', 'allRecordsReceived', recordsCache);
507 function _resultFindRec(id) {
508 for( var i = 0; i != recordsCache.length; i++ ) {
509 var rec = recordsCache[i];
510 if( rec && rec.doc_id() == id )
517 function resultBuildFormatIcons( row, rec, is_mr ) {
519 var ress = rec.types_of_resource();
521 for( var i in ress ) {
526 var link = $n(row, res + "_link");
528 var img = link.getElementsByTagName("img")[0];
529 removeCSSClass( img, config.css.dim );
533 if( f == modsFormatToMARC(res) )
534 addCSSClass( img, "dim2_border");
538 args[PARAM_OFFSET] = 0;
542 args[PARAM_TFORM] = modsFormatToMARC(res);
543 args[PARAM_MRID] = rec.doc_id();
547 args[PARAM_RID] = rec.doc_id();
550 link.setAttribute("href", buildOPACLink(args));
556 function resultPageIsDone(pos) {
557 return (recordsHandled == getDisplayCount()
558 || recordsHandled + getOffset() == getHitCount());
561 var resultCCHeaderApplied = false;
563 /* -------------------------------------------------------------------- */
564 /* dynamically add the copy count rows based on the org type 'countsrow'
565 is the row into which we will add TD's to hold the copy counts
566 This code generates copy count cells with an id of
567 'copy_count_cell_<depth>_<pagePosition>' */
568 function resultAddCopyCounts(rec, pagePosition) {
570 var r = table.rows[pagePosition + 1];
571 var countsrow = $n(r, config.names.result.counts_row );
572 var ccell = $n(countsrow, config.names.result.count_cell);
574 var nodes = orgNodeTrail(findOrgUnit(getLocation()));
576 var type = findOrgType(node.ou_type());
577 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
578 ccell.title = type.opac_label();
579 //addCSSClass(ccell, config.css.result.cc_cell_even);
581 var lastcell = ccell;
582 var lastheadcell = null;
585 var ccheadcell = null;
586 if(!resultCCHeaderApplied) {
587 ccrow = $('result_thead_row');
588 ccheadcell = ccrow.removeChild($n(ccrow, "result_thead_ccell"));
589 var t = ccheadcell.cloneNode(true);
591 t.appendChild(text(type.opac_label()));
592 ccrow.appendChild(t);
593 resultCCHeaderApplied = true;
599 var d = findOrgDepth(nodes[1]);
600 var d2 = findOrgDepth(nodes[nodes.length -1]);
602 for( var i = d; i <= d2 ; i++ ) {
604 ccell = ccell.cloneNode(true);
606 //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
607 //else addCSSClass(ccell, "copy_count_cell_even");
609 var node = nodes[x++];
610 var type = findOrgType(node.ou_type());
612 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
613 ccell.title = type.opac_label();
614 countsrow.insertBefore(ccell, lastcell);
618 var t = ccheadcell.cloneNode(true);
619 t.appendChild(text(type.opac_label()));
620 ccrow.insertBefore(t, lastheadcell);
626 unHideMe($("search_info_table"));
629 /* collect copy counts for a record using method 'methodName' */
630 function resultCollectCopyCounts(rec, pagePosition, methodName) {
631 if(rec == null || rec.doc_id() == null) return;
632 var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
633 req.request.userdata = [ rec, pagePosition ];
634 req.callback(resultHandleCopyCounts);
638 function resultHandleCopyCounts(r) {
639 runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject());
643 /* display the collected copy counts */
644 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
645 if(copy_counts == null || rec == null) return;
647 while(copy_counts[i] != null) {
648 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
649 var cts = copy_counts[i];
650 cell.appendChild(text(cts.available + " / " + cts.count));
653 /* here we style opac-invisible records for xul */
655 if( cts.depth == 0 ) {
656 if(!cts.transcendant && !cts.unshadow) {
657 _debug("found an opac-shadowed record: " + rec.doc_id());
658 var row = cell.parentNode.parentNode.parentNode.parentNode.parentNode;
660 addCSSClass( row, 'no_copies' );
662 addCSSClass( row, 'shadowed' );