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, resultDrawAuthors, resultDrawSeries);
14 attachEvt('result','lowHits',resultLowHits);
15 attachEvt('result','zeroHits',resultZeroHits);
16 attachEvt( "common", "locationUpdated", resultSBSubmit );
17 /* do this after we have ID's so the rank for mr pages will be correct */
18 attachEvt("result", "preCollectRecords", resultPaginate);
21 function resultSBSubmit(){searchBarSubmit();}
23 /* returns the last 'index' postion ocurring in this page */
24 function resultFinalPageIndex() {
25 if(getHitCount() < (getOffset() + getDisplayCount()))
26 return getHitCount() - 1;
27 return getOffset() + getDisplayCount() - 1;
33 /* generic search method */
34 function resultCollectSearchIds( type, method, handler ) {
36 var sort = (getSort() == SORT_TYPE_REL) ? null : getSort();
37 var sortdir = (sort) ? ((getSortDir()) ? getSortDir() : SORT_DIR_ASC) : null;
45 args.searches[getStype()] = {};
48 term = term.replace(/'/,'');
49 args.searches[getStype()].term = term;
52 var form = parseForm(getForm());
53 item_type = form.item_type;
54 item_form = form.item_form;
57 args.searches = JSON2js(getSearches());
58 item_type = (getItemType()) ? getItemType().split(/,/) : null;
59 item_form = (getItemForm()) ? getItemForm().split(/,/) : null;
62 var limit = (resultFetchAllRecords) ? 1000 : getDisplayCount();
64 if( getOffset() > 0 ) {
65 if( getHitCount() > 0 && (getOffset() + getDisplayCount()) > getHitCount() )
66 limit = getHitCount() - getOffset();
69 args.org_unit = getLocation();
70 args.depth = getDepth();
72 args.offset = getOffset();
74 if(sort) args.sort = sort;
75 if(sortdir) args.sort_dir = sortdir;
76 if(item_type) args.item_type = item_type;
77 if(item_form) args.item_form = item_form;
80 if(getAudience()) args.audience = getAudience().split(/,/);
81 if(getLitForm()) args.lit_form = getLitForm().split(/,/);
82 if(getLanguage()) args.language = getLanguage().split(/,/);
84 //alert(js2JSON(args));
85 _debug('SEARCH: \n' + js2JSON(args) + '\n\n');
86 var req = new Request(method, args, 1);
87 req.callback(handler);
95 /* set the search result info, number of hits, which results we're
96 displaying, links to the next/prev pages, etc. */
97 function resultSetHitInfo() {
100 /* tell the user where the results are coming from */
101 var baseorg = findOrgUnit(getLocation());
102 var depth = getDepth();
103 var mydepth = findOrgDepth(baseorg);
104 if( findOrgDepth(baseorg) != depth ) {
105 var tmporg = baseorg;
106 while( mydepth > depth ) {
108 tmporg = findOrgUnit(tmporg.parent_ou());
110 unHideMe($('including_results_for'));
111 $('including_results_location').appendChild(text(tmporg.name()));
115 try{searchTimer.stop()}catch(e){}
117 if( findCurrentPage() == MRESULT ) {
118 if(getHitCount() <= lowHitCount && getTerm())
119 runEvt('result', 'lowHits');
122 if(getHitCount() == 0) {
123 runEvt('result', 'zeroHits');
128 var pages = getHitCount() / getDisplayCount();
129 if(pages % 1) pages = parseInt(pages) + 1;
133 var cpage = (getOffset()/getDisplayCount()) + 1;
135 G.ui.result.current_page.appendChild(text(cpage));
136 G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
138 /* set the offsets */
139 var offsetEnd = getDisplayCount() + getOffset();
140 if( getDisplayCount() > (getHitCount() - getOffset()))
141 offsetEnd = getHitCount();
143 G.ui.result.offset_end.appendChild(text(offsetEnd));
144 G.ui.result.offset_start.appendChild(text(getOffset() + 1));
146 G.ui.result.result_count.appendChild(text(getHitCount()));
147 unHideMe(G.ui.result.info);
150 function resultLowHits() {
152 unHideMe($('result_low_hits'));
153 if(getHitCount() > 0)
154 unHideMe($('result_low_hits_msg'));
156 var sreq = new Request(CHECK_SPELL, getTerm());
157 sreq.callback(resultSuggestSpelling);
160 var words = getTerm().split(' ');
162 while( word = words.shift() ) {
163 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
164 areq.callback(resultLowHitXRef);
168 if( !(getForm() == null || getForm() == 'all' || getForm() == "") ) {
170 a[PARAM_FORM] = "all";
171 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
172 unHideMe($('low_hits_remove_format'));
175 resultSuggestSearchClass();
177 if(getTerm()) resultExpandSearch(); /* advanced search */
181 var lowHitsXRefLinkParent;
182 function resultLowHitXRef(r) {
183 if(!lowHitsXRefLink){
184 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
185 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
187 var res = r.getResultObject();
189 arr.concat(res.also);
190 if(arr && arr.length > 0) {
191 unHideMe($('low_hits_cross_ref'));
194 while( word = arr.shift() ) {
197 a[PARAM_TERM] = word;
198 var template = lowHitsXRefLink.cloneNode(true);
199 template.setAttribute('href',buildOPACLink(a));
200 template.appendChild(text(word));
201 lowHitsXRefLinkParent.appendChild(template);
202 lowHitsXRefLinkParent.appendChild(text(' '));
207 function resultZeroHits() {
209 unHideMe($('result_low_hits'));
210 unHideMe($('result_zero_hits_msg'));
211 //if(getTerm()) resultExpandSearch(); /* advanced search */
214 function resultExpandSearch() {
215 var top = findOrgDepth(globalOrgTree);
216 if(getDepth() == top) return;
217 unHideMe($('low_hits_expand_range'));
218 var par = $('low_hits_expand_link').parentNode;
219 var template = par.removeChild($('low_hits_expand_link'));
221 var bottom = getDepth();
222 while( top < bottom ) {
224 a[PARAM_DEPTH] = top;
225 var temp = template.cloneNode(true);
226 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
227 temp.setAttribute('href',buildOPACLink(a));
228 par.appendChild(temp);
233 function resultSuggestSearchClass() {
234 var stype = getStype();
235 if(stype == STYPE_KEYWORD) return;
237 unHideMe($('low_hits_search_type'));
238 if(stype != STYPE_TITLE) {
239 ref = $('low_hits_title_search');
241 a[PARAM_STYPE] = STYPE_TITLE;
242 ref.setAttribute('href',buildOPACLink(a));
244 if(stype != STYPE_AUTHOR) {
245 ref = $('low_hits_author_search');
247 a[PARAM_STYPE] = STYPE_AUTHOR;
248 ref.setAttribute('href',buildOPACLink(a));
250 if(stype != STYPE_SUBJECT) {
251 ref = $('low_hits_subject_search');
253 a[PARAM_STYPE] = STYPE_SUBJECT;
254 ref.setAttribute('href',buildOPACLink(a));
256 if(stype != STYPE_KEYWORD) {
257 ref = $('low_hits_keyword_search');
259 a[PARAM_STYPE] = STYPE_KEYWORD;
260 ref.setAttribute('href',buildOPACLink(a));
262 if(stype != STYPE_SERIES) {
263 ref = $('low_hits_series_search');
265 a[PARAM_STYPE] = STYPE_SERIES;
266 ref.setAttribute('href',buildOPACLink(a));
270 function resultSuggestSpelling(r) {
271 var res = r.getResultObject();
272 var phrase = getTerm();
273 var words = phrase.split(/ /);
277 for( var w = 0; w < words.length; w++ ) {
279 var blob = grep(res, function(i){return (i.word == word);});
280 if( blob ) blob = blob[0];
282 if( blob.word == word ) {
283 if( blob.suggestions && blob.suggestions[0] ) {
284 newterm += " " + blob.suggestions[0];
285 unHideMe($('did_you_mean'));
287 newterm += " " + word;
293 arg[PARAM_TERM] = newterm;
294 $('spell_check_link').setAttribute('href', buildOPACLink(arg));
295 $('spell_check_link').appendChild(text(newterm));
299 function resultPaginate() {
302 if( !( ((o) + getDisplayCount()) >= getHitCount()) ) {
305 args[PARAM_OFFSET] = o + getDisplayCount();
306 args[PARAM_SORT] = SORT;
307 args[PARAM_SORT_DIR] = SORT_DIR;
308 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
310 G.ui.result.next_link.setAttribute("href", buildOPACLink(args));
311 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
313 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
315 /* when hit count is divisible by display count, we have to adjust */
316 if( getHitCount() % getDisplayCount() == 0 )
317 args[PARAM_OFFSET] -= getDisplayCount();
319 G.ui.result.end_link.setAttribute("href", buildOPACLink(args));
320 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
326 args[PARAM_SORT] = SORT;
327 args[PARAM_SORT_DIR] = SORT_DIR;
328 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
330 args[PARAM_OFFSET] = o - getDisplayCount();
331 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args));
332 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
334 args[PARAM_OFFSET] = 0;
335 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args));
336 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
339 if(getDisplayCount() < getHitCount())
340 unHideMe($('start_end_links_span'));
343 try{searchTimer.stop()}catch(e){}
346 function buildunAPISpan (span, type, id) {
350 addCSSClass(span,'unapi-id');
354 'tag:' + cgi.server_name + ',' +
356 ':' + type + '/' + id
360 /* display the record info in the record display table 'pos' is the
361 zero based position the record should have in the display table */
362 function resultDisplayRecord(rec, pos, is_mr) {
364 if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
366 recordsCache.push(rec);
368 var r = table.rows[pos + 1];
372 var rank = parseFloat(ranks[pos + getOffset()]);
373 rank = parseInt( rank * 100 );
374 var relspan = $n(r, "relevancy_span");
375 relspan.appendChild(text(rank));
376 unHideMe(relspan.parentNode);
379 var pic = $n(r, config.names.result.item_jacket);
380 pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
382 var title_link = $n(r, config.names.result.item_title);
383 var author_link = $n(r, config.names.result.item_author);
386 var onlyrec = onlyrecord[ getOffset() + pos ];
388 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
392 args[PARAM_OFFSET] = 0;
393 args[PARAM_RID] = onlyrec;
394 args[PARAM_MRID] = rec.doc_id();
395 pic.parentNode.setAttribute("href", buildOPACLink(args));
396 title_link.setAttribute("href", buildOPACLink(args));
397 title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
400 buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
402 buildTitleLink(rec, title_link);
405 args[PARAM_OFFSET] = 0;
406 args[PARAM_MRID] = rec.doc_id();
407 pic.parentNode.setAttribute("href", buildOPACLink(args));
411 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
413 buildTitleDetailLink(rec, title_link);
416 args[PARAM_OFFSET] = 0;
417 args[PARAM_RID] = rec.doc_id();
418 pic.parentNode.setAttribute("href", buildOPACLink(args));
420 unHideMe($n(r,'place_hold_span'));
421 $n(r,'place_hold_link').setAttribute(
422 'href','javascript:holdsDrawEditor({record:"'+rec.doc_id()+'",type:"T"});');
425 buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
429 if(!isNull(rec.edition())) {
430 unHideMe( $n(r, "result_table_extra_span"));
431 $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
433 if(!isNull(rec.pubdate())) {
434 unHideMe( $n(r, "result_table_extra_span"));
435 unHideMe($n(r, "result_table_pub_span"));
436 $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
438 if(!isNull(rec.publisher()) ) {
439 unHideMe( $n(r, "result_table_extra_span"));
440 unHideMe($n(r, "result_table_pub_span"));
441 $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
444 if(!isNull(rec.physical_description()) ) {
445 unHideMe( $n(r, "result_table_extra_span"));
446 var t = " " + rec.physical_description();
447 //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
448 $n(r, "result_table_phys_span").appendChild( text(t));
453 resultBuildFormatIcons( r, rec, is_mr );
457 runEvt("result", "recordDrawn", rec.doc_id(), title_link);
460 if(resultPageIsDone()) {
461 runEvt('result', 'allRecordsReceived', recordsCache);
466 function _resultFindRec(id) {
467 for( var i = 0; i != recordsCache.length; i++ ) {
468 var rec = recordsCache[i];
469 if( rec && rec.doc_id() == id )
476 function resultBuildFormatIcons( row, rec, is_mr ) {
478 var ress = rec.types_of_resource();
480 for( var i in ress ) {
485 var link = $n(row, res + "_link");
487 var img = link.getElementsByTagName("img")[0];
488 removeCSSClass( img, config.css.dim );
492 if( f == modsFormatToMARC(res) )
493 addCSSClass( img, "dim2_border");
497 args[PARAM_OFFSET] = 0;
501 args[PARAM_TFORM] = modsFormatToMARC(res);
502 args[PARAM_MRID] = rec.doc_id();
506 args[PARAM_RID] = rec.doc_id();
509 link.setAttribute("href", buildOPACLink(args));
515 function resultPageIsDone(pos) {
516 return (recordsHandled == getDisplayCount()
517 || recordsHandled + getOffset() == getHitCount());
520 var resultCCHeaderApplied = false;
522 /* -------------------------------------------------------------------- */
523 /* dynamically add the copy count rows based on the org type 'countsrow'
524 is the row into which we will add TD's to hold the copy counts
525 This code generates copy count cells with an id of
526 'copy_count_cell_<depth>_<pagePosition>' */
527 function resultAddCopyCounts(rec, pagePosition) {
529 var r = table.rows[pagePosition + 1];
530 var countsrow = $n(r, config.names.result.counts_row );
531 var ccell = $n(countsrow, config.names.result.count_cell);
533 var nodes = orgNodeTrail(findOrgUnit(getLocation()));
535 var type = findOrgType(node.ou_type());
536 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
537 ccell.title = type.opac_label();
538 //addCSSClass(ccell, config.css.result.cc_cell_even);
540 var lastcell = ccell;
541 var lastheadcell = null;
544 var ccheadcell = null;
545 if(!resultCCHeaderApplied) {
546 ccrow = $('result_thead_row');
547 ccheadcell = ccrow.removeChild($n(ccrow, "result_thead_ccell"));
548 var t = ccheadcell.cloneNode(true);
550 t.appendChild(text(type.opac_label()));
551 ccrow.appendChild(t);
552 resultCCHeaderApplied = true;
558 var d = findOrgDepth(nodes[1]);
559 var d2 = findOrgDepth(nodes[nodes.length -1]);
561 for( var i = d; i <= d2 ; i++ ) {
563 ccell = ccell.cloneNode(true);
565 //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
566 //else addCSSClass(ccell, "copy_count_cell_even");
568 var node = nodes[x++];
569 var type = findOrgType(node.ou_type());
571 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
572 ccell.title = type.opac_label();
573 countsrow.insertBefore(ccell, lastcell);
577 var t = ccheadcell.cloneNode(true);
578 t.appendChild(text(type.opac_label()));
579 ccrow.insertBefore(t, lastheadcell);
585 unHideMe($("search_info_table"));
588 /* collect copy counts for a record using method 'methodName' */
589 function resultCollectCopyCounts(rec, pagePosition, methodName) {
590 if(rec == null || rec.doc_id() == null) return;
591 var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
592 req.request.userdata = [ rec, pagePosition ];
593 req.callback(resultHandleCopyCounts);
597 function resultHandleCopyCounts(r) {
598 runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject());
602 /* display the collected copy counts */
603 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
604 if(copy_counts == null || rec == null) return;
606 while(copy_counts[i] != null) {
607 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
608 var cts = copy_counts[i];
609 cell.appendChild(text(cts.available + " / " + cts.count));
612 /* here we style opac-invisible records for xul */
614 if( cts.depth == 0 ) {
615 if(!cts.transcendant && !cts.unshadow) {
616 _debug("found an opac-shadowed record: " + rec.doc_id());
617 var row = cell.parentNode.parentNode.parentNode.parentNode.parentNode;
619 addCSSClass( row, 'no_copies' );
621 addCSSClass( row, 'shadowed' );