2 var recordsHandled = 0;
6 var googleBooks = true;
8 var resultFetchAllRecords = false;
9 var resultCompiledSearch = null;
11 /* set up the event handlers */
12 if( findCurrentPage() == MRESULT || findCurrentPage() == RRESULT ) {
13 G.evt.result.hitCountReceived.push(resultSetHitInfo);
14 G.evt.result.recordReceived.push(resultDisplayRecord, resultAddCopyCounts);
15 G.evt.result.copyCountsReceived.push(resultDisplayCopyCounts);
16 G.evt.result.allRecordsReceived.push(resultBuildCaches, resultDrawSubjects,
17 resultDrawAuthors, resultDrawSeries, function(){unHideMe($('result_info_2'))},
20 attachEvt('result','lowHits',resultLowHits);
21 attachEvt('result','zeroHits',resultZeroHits);
22 attachEvt( "common", "locationUpdated", resultSBSubmit );
23 /* do this after we have ID's so the rank for mr pages will be correct */
24 attachEvt("result", "preCollectRecords", resultPaginate);
27 function resultSBSubmit(){searchBarSubmit();}
29 /* returns the last 'index' postion ocurring in this page */
30 function resultFinalPageIndex() {
31 if(getHitCount() < (getOffset() + getDisplayCount()))
32 return getHitCount() - 1;
33 return getOffset() + getDisplayCount() - 1;
39 /* generic search method */
40 function resultCollectSearchIds( type, method, handler ) {
42 var sort = (getSort() == SORT_TYPE_REL) ? null : getSort();
43 var sortdir = (sort) ? ((getSortDir()) ? getSortDir() : SORT_DIR_ASC) : null;
50 var form = parseForm(getForm());
51 item_type = form.item_type;
52 item_form = form.item_form;
55 item_type = (getItemType()) ? getItemType().split(/,/) : null;
56 item_form = (getItemForm()) ? getItemForm().split(/,/) : null;
59 var limit = (resultFetchAllRecords) ? 1000 : getDisplayCount();
61 if( getOffset() > 0 ) {
62 if( getHitCount() > 0 && (getOffset() + getDisplayCount()) > getHitCount() )
63 limit = getHitCount() - getOffset();
66 args.org_unit = getLocation();
67 args.depth = getDepth();
69 args.offset = getOffset();
70 args.visibility_limit = 3000;
71 args.default_class = getStype();
73 if(sort) args.sort = sort;
74 if(sortdir) args.sort_dir = sortdir;
75 if(item_type) args.item_type = item_type;
76 if(item_form) args.item_form = item_form;
77 if(getAvail()) args.available = 1;
80 if(getAudience()) args.audience = getAudience().split(/,/);
81 if(getLitForm()) args.lit_form = getLitForm().split(/,/);
82 if(getLanguage()) args.language = getLanguage().split(/,/);
83 if(getBibLevel()) args.bib_level = getBibLevel().split(/,/);
85 _debug('Search args: ' + js2JSON(args));
86 _debug('Raw query: ' + getTerm());
88 var req = new Request(method, args, getTerm(), 1);
89 req.callback(handler);
97 /* set the search result info, number of hits, which results we're
98 displaying, links to the next/prev pages, etc. */
99 function resultSetHitInfo() {
102 /* tell the user where the results are coming from */
103 var baseorg = findOrgUnit(getLocation());
104 var depth = getDepth();
105 var mydepth = findOrgDepth(baseorg);
106 if( findOrgDepth(baseorg) != depth ) {
107 var tmporg = baseorg;
108 while( mydepth > depth ) {
110 tmporg = findOrgUnit(tmporg.parent_ou());
112 unHideMe($('including_results_for'));
113 $('including_results_location').appendChild(text(tmporg.name()));
117 try{searchTimer.stop()}catch(e){}
119 //if( findCurrentPage() == MRESULT ) {
120 if( findCurrentPage() == MRESULT ||
122 (findCurrentPage() == RRESULT &&
124 getRtype() == RTYPE_TITLE ||
125 getRtype() == RTYPE_AUTHOR ||
126 getRtype() == RTYPE_SUBJECT ||
127 getRtype() == RTYPE_SERIES ||
128 getRtype() == RTYPE_KEYWORD
133 if(getHitCount() <= lowHitCount && getTerm())
134 runEvt('result', 'lowHits');
137 if(getHitCount() == 0) {
138 runEvt('result', 'zeroHits');
143 var pages = getHitCount() / getDisplayCount();
144 if(pages % 1) pages = parseInt(pages) + 1;
148 var cpage = (getOffset()/getDisplayCount()) + 1;
150 G.ui.result.current_page.appendChild(text(cpage));
151 G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
153 $('current_page2').appendChild(text(cpage));
154 $('num_pages2').appendChild(text(pages + ")")); /* the ) is dumb */
156 /* set the offsets */
157 var offsetEnd = getDisplayCount() + getOffset();
158 if( getDisplayCount() > (getHitCount() - getOffset()))
159 offsetEnd = getHitCount();
161 G.ui.result.offset_end.appendChild(text(offsetEnd));
162 G.ui.result.offset_start.appendChild(text(getOffset() + 1));
164 $('offset_end2').appendChild(text(offsetEnd));
165 $('offset_start2').appendChild(text(getOffset() + 1));
167 G.ui.result.result_count.appendChild(text(getHitCount()));
168 unHideMe(G.ui.result.info);
170 $('result_count2').appendChild(text(getHitCount()));
171 unHideMe($('result_info_div2'));
174 function resultLowHits() {
176 unHideMe($('result_low_hits'));
177 if(getHitCount() > 0)
178 unHideMe($('result_low_hits_msg'));
181 for(var key in resultCompiledSearch.searches)
182 words.push(resultCompiledSearch.searches[key].term);
184 var sreq = new Request(CHECK_SPELL, words.join(' '));
185 sreq.callback(resultSuggestSpelling);
188 for(var key in resultCompiledSearch.searches) {
189 var areq = new Request(FETCH_CROSSREF, key, resultCompiledSearch.searches[key].term);
190 areq.callback(resultLowHitXRef);
194 if( !(getForm() == null || getForm() == 'all' || getForm() == "") ) {
196 a[PARAM_FORM] = "all";
197 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
198 unHideMe($('low_hits_remove_format'));
201 resultSuggestSearchClass();
203 if(getTerm()) resultExpandSearch(); /* advanced search */
207 var lowHitsXRefLinkParent;
208 function resultLowHitXRef(r) {
209 if(!lowHitsXRefLink){
210 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
211 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
213 var res = r.getResultObject();
215 arr.concat(res.also);
216 if(arr && arr.length > 0) {
217 unHideMe($('low_hits_cross_ref'));
220 while( word = arr.shift() ) {
223 a[PARAM_TERM] = word;
224 var template = lowHitsXRefLink.cloneNode(true);
225 template.setAttribute('href',buildOPACLink(a));
226 template.appendChild(text(word));
227 lowHitsXRefLinkParent.appendChild(template);
228 lowHitsXRefLinkParent.appendChild(text(' '));
233 function resultZeroHits() {
235 unHideMe($('result_low_hits'));
236 unHideMe($('result_zero_hits_msg'));
237 //if(getTerm()) resultExpandSearch(); /* advanced search */
240 function resultExpandSearch() {
241 var top = findOrgDepth(globalOrgTree);
242 if(getDepth() == top) return;
243 unHideMe($('low_hits_expand_range'));
244 var par = $('low_hits_expand_link').parentNode;
245 var template = par.removeChild($('low_hits_expand_link'));
247 var bottom = getDepth();
248 while( top < bottom ) {
250 a[PARAM_DEPTH] = top;
251 var temp = template.cloneNode(true);
252 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
253 temp.setAttribute('href',buildOPACLink(a));
254 par.appendChild(temp);
259 function resultSuggestSearchClass() {
260 var stype = getStype();
261 if(stype == STYPE_KEYWORD) return;
263 unHideMe($('low_hits_search_type'));
264 if(stype != STYPE_TITLE) {
265 ref = $('low_hits_title_search');
267 a[PARAM_STYPE] = STYPE_TITLE;
268 ref.setAttribute('href',buildOPACLink(a));
270 if(stype != STYPE_AUTHOR) {
271 ref = $('low_hits_author_search');
273 a[PARAM_STYPE] = STYPE_AUTHOR;
274 ref.setAttribute('href',buildOPACLink(a));
276 if(stype != STYPE_SUBJECT) {
277 ref = $('low_hits_subject_search');
279 a[PARAM_STYPE] = STYPE_SUBJECT;
280 ref.setAttribute('href',buildOPACLink(a));
282 if(stype != STYPE_KEYWORD) {
283 ref = $('low_hits_keyword_search');
285 a[PARAM_STYPE] = STYPE_KEYWORD;
286 ref.setAttribute('href',buildOPACLink(a));
288 if(stype != STYPE_SERIES) {
289 ref = $('low_hits_series_search');
291 a[PARAM_STYPE] = STYPE_SERIES;
292 ref.setAttribute('href',buildOPACLink(a));
296 function resultSuggestSpelling(r) {
297 var res = r.getResultObject();
298 var phrase = getTerm();
299 var words = phrase.split(/ /);
303 for( var w = 0; w < words.length; w++ ) {
305 var blob = grep(res, function(i){return (i.word == word);});
306 if( blob ) blob = blob[0];
308 if( blob.word == word ) {
309 if( blob.suggestions && blob.suggestions[0] ) {
310 newterm += " " + blob.suggestions[0];
311 unHideMe($('did_you_mean'));
313 newterm += " " + word;
319 arg[PARAM_TERM] = newterm;
320 $('spell_check_link').setAttribute('href', buildOPACLink(arg));
321 $('spell_check_link').appendChild(text(newterm));
325 function resultPaginate() {
328 if( !( ((o) + getDisplayCount()) >= getHitCount()) ) {
331 args[PARAM_OFFSET] = o + getDisplayCount();
332 args[PARAM_SORT] = SORT;
333 args[PARAM_SORT_DIR] = SORT_DIR;
334 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
336 G.ui.result.next_link.setAttribute("href", buildOPACLink(args));
337 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
339 $('next_link2').setAttribute("href", buildOPACLink(args));
340 addCSSClass($('next_link2'), config.css.result.nav_active);
342 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
344 /* when hit count is divisible by display count, we have to adjust */
345 if( getHitCount() % getDisplayCount() == 0 )
346 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);
360 args[PARAM_SORT] = SORT;
361 args[PARAM_SORT_DIR] = SORT_DIR;
362 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
364 args[PARAM_OFFSET] = o - getDisplayCount();
365 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args));
366 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
368 $('prev_link2').setAttribute( "href", buildOPACLink(args));
369 addCSSClass($('prev_link2'), config.css.result.nav_active);
371 args[PARAM_OFFSET] = 0;
372 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args));
373 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
375 $('search_home_link2').setAttribute( "href", buildOPACLink(args));
376 addCSSClass($('search_home_link2'), config.css.result.nav_active);
379 if(getDisplayCount() < getHitCount()) {
380 unHideMe($('start_end_links_span'));
381 unHideMe($('start_end_links_span2'));
385 try{searchTimer.stop()}catch(e){}
388 function buildunAPISpan (span, type, id) {
392 addCSSClass(span,'unapi-id');
396 'tag:' + cgi.server_name + ',' +
398 ':' + type + '/' + id
402 function unhideGoogleBooks (data) {
403 for ( var i in data ) {
404 //if (data[i].preview == 'noview') continue;
406 var gbspan = $n(document.documentElement, 'googleBooks-' + i);
407 var gba = $n(gbspan, "googleBooks-link");
413 removeCSSClass( gbspan, 'hide_me' );
417 /* display the record info in the record display table 'pos' is the
418 zero based position the record should have in the display table */
419 function resultDisplayRecord(rec, pos, is_mr) {
421 if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
423 recordsCache.push(rec);
425 var r = table.rows[pos + 1];
426 var currentISBN = cleanISBN(rec.isbn());
429 var gbspan = $n(r, "googleBooks");
433 gbspan.getAttribute('name') + '-' + currentISBN
436 if (isbnList) isbnList += ', ';
437 isbnList += currentISBN;
443 var rank = parseFloat(ranks[pos + getOffset()]);
444 rank = parseInt( rank * 100 );
445 var relspan = $n(r, "relevancy_span");
446 relspan.appendChild(text(rank));
447 unHideMe(relspan.parentNode);
451 var pic = $n(r, config.names.result.item_jacket);
452 pic.setAttribute("src", buildISBNSrc(currentISBN));
454 var title_link = $n(r, config.names.result.item_title);
455 var author_link = $n(r, config.names.result.item_author);
458 var onlyrec = onlyrecord[ getOffset() + pos ];
460 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
464 args[PARAM_OFFSET] = 0;
465 args[PARAM_RID] = onlyrec;
466 args[PARAM_MRID] = rec.doc_id();
467 pic.parentNode.setAttribute("href", buildOPACLink(args));
468 title_link.setAttribute("href", buildOPACLink(args));
469 title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
472 buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
474 buildTitleLink(rec, title_link);
477 args[PARAM_OFFSET] = 0;
478 args[PARAM_MRID] = rec.doc_id();
479 pic.parentNode.setAttribute("href", buildOPACLink(args));
483 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
485 buildTitleDetailLink(rec, title_link);
488 args[PARAM_OFFSET] = 0;
489 args[PARAM_RID] = rec.doc_id();
490 pic.parentNode.setAttribute("href", buildOPACLink(args));
492 unHideMe($n(r,'place_hold_span'));
493 $n(r,'place_hold_link').setAttribute(
494 'href','javascript:holdsDrawEditor({record:"'+rec.doc_id()+'",type:"T"});');
497 buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
501 if(!isNull(rec.edition())) {
502 unHideMe( $n(r, "result_table_extra_span"));
503 $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
505 if(!isNull(rec.pubdate())) {
506 unHideMe( $n(r, "result_table_extra_span"));
507 unHideMe($n(r, "result_table_pub_span"));
508 $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
510 if(!isNull(rec.publisher()) ) {
511 unHideMe( $n(r, "result_table_extra_span"));
512 unHideMe($n(r, "result_table_pub_span"));
513 $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
516 if(!isNull(rec.physical_description()) ) {
517 unHideMe( $n(r, "result_table_extra_span"));
518 var t = " " + rec.physical_description();
519 //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
520 $n(r, "result_table_phys_span").appendChild( text(t));
525 resultBuildFormatIcons( r, rec, is_mr );
529 runEvt("result", "recordDrawn", rec.doc_id(), title_link);
532 if(resultPageIsDone()) {
533 runEvt('result', 'allRecordsReceived', recordsCache);
538 function _resultFindRec(id) {
539 for( var i = 0; i != recordsCache.length; i++ ) {
540 var rec = recordsCache[i];
541 if( rec && rec.doc_id() == id )
548 function resultBuildFormatIcons( row, rec, is_mr ) {
550 var ress = rec.types_of_resource();
552 for( var i in ress ) {
557 var link = $n(row, res + "_link");
559 var img = link.getElementsByTagName("img")[0];
560 removeCSSClass( img, config.css.dim );
564 if( f == modsFormatToMARC(res) )
565 addCSSClass( img, "dim2_border");
569 args[PARAM_OFFSET] = 0;
573 args[PARAM_TFORM] = modsFormatToMARC(res);
574 args[PARAM_MRID] = rec.doc_id();
578 args[PARAM_RID] = rec.doc_id();
581 link.setAttribute("href", buildOPACLink(args));
586 function fetchGoogleBooks () {
587 if (isbnList && googleBooks) {
588 var scriptElement = document.createElement("script");
589 scriptElement.setAttribute("id", "jsonScript");
590 scriptElement.setAttribute("src",
591 "http://books.google.com/books?bibkeys=" +
592 escape(isbnList) + "&jscmd=viewapi&callback=unhideGoogleBooks");
593 scriptElement.setAttribute("type", "text/javascript");
594 // make the request to Google Book Search
595 document.documentElement.firstChild.appendChild(scriptElement);
599 function resultPageIsDone(pos) {
601 return (recordsHandled == getDisplayCount()
602 || recordsHandled + getOffset() == getHitCount());
605 var resultCCHeaderApplied = false;
607 /* -------------------------------------------------------------------- */
608 /* dynamically add the copy count rows based on the org type 'countsrow'
609 is the row into which we will add TD's to hold the copy counts
610 This code generates copy count cells with an id of
611 'copy_count_cell_<depth>_<pagePosition>' */
612 function resultAddCopyCounts(rec, pagePosition) {
614 var r = table.rows[pagePosition + 1];
615 var countsrow = $n(r, config.names.result.counts_row );
616 var ccell = $n(countsrow, config.names.result.count_cell);
618 var nodes = orgNodeTrail(findOrgUnit(getLocation()));
620 var type = findOrgType(node.ou_type());
621 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
622 ccell.title = type.opac_label();
623 //addCSSClass(ccell, config.css.result.cc_cell_even);
625 var lastcell = ccell;
626 var lastheadcell = null;
629 var ccheadcell = null;
630 if(!resultCCHeaderApplied) {
631 ccrow = $('result_thead_row');
632 ccheadcell = ccrow.removeChild($n(ccrow, "result_thead_ccell"));
633 var t = ccheadcell.cloneNode(true);
635 t.appendChild(text(type.opac_label()));
636 ccrow.appendChild(t);
637 resultCCHeaderApplied = true;
643 var d = findOrgDepth(nodes[1]);
644 var d2 = findOrgDepth(nodes[nodes.length -1]);
646 for( var i = d; i <= d2 ; i++ ) {
648 ccell = ccell.cloneNode(true);
650 //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
651 //else addCSSClass(ccell, "copy_count_cell_even");
653 var node = nodes[x++];
654 var type = findOrgType(node.ou_type());
656 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
657 ccell.title = type.opac_label();
658 countsrow.insertBefore(ccell, lastcell);
662 var t = ccheadcell.cloneNode(true);
663 t.appendChild(text(type.opac_label()));
664 ccrow.insertBefore(t, lastheadcell);
670 unHideMe($("search_info_table"));
673 /* collect copy counts for a record using method 'methodName' */
674 function resultCollectCopyCounts(rec, pagePosition, methodName) {
675 if(rec == null || rec.doc_id() == null) return;
676 var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
677 req.request.userdata = [ rec, pagePosition ];
678 req.callback(resultHandleCopyCounts);
682 function resultHandleCopyCounts(r) {
683 runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject());
687 /* display the collected copy counts */
688 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
689 if(copy_counts == null || rec == null) return;
691 while(copy_counts[i] != null) {
692 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
693 var cts = copy_counts[i];
694 cell.appendChild(text(cts.available + " / " + cts.count));
697 /* here we style opac-invisible records for xul */
699 if( cts.depth == 0 ) {
700 if(!cts.transcendant && !cts.unshadow) {
701 _debug("found an opac-shadowed record: " + rec.doc_id());
702 var row = cell.parentNode.parentNode.parentNode.parentNode.parentNode;
704 addCSSClass( row, 'no_copies' );
706 addCSSClass( row, 'shadowed' );