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()] = {};
46 args.searches[getStype()].term = getTerm();
48 var form = parseForm(getForm());
49 item_type = form.item_type;
50 item_form = form.item_form;
53 args.searches = JSON2js(getSearches());
54 item_type = (getItemType()) ? getItemType().split(/,/) : null;
55 item_form = (getItemForm()) ? getItemForm().split(/,/) : null;
58 var limit = (resultFetchAllRecords) ? 1000 : getDisplayCount();
59 if( getHitCount() > 0 && (getOffset() + getDisplayCount()) > getHitCount() )
60 limit = getHitCount() - getOffset();
62 args.org_unit = getLocation();
63 args.depth = getDepth();
65 args.offset = getOffset();
67 if(sort) args.sort = sort;
68 if(sortdir) args.sort_dir = sortdir;
69 if(item_type) args.item_type = item_type;
70 if(item_form) args.item_form = item_form;
73 if(getAudience()) args.audience = getAudience().split(/,/);
74 if(getLitForm()) args.lit_form = getLitForm().split(/,/);
75 if(getLanguage()) args.language = getLanguage().split(/,/);
77 //alert(js2JSON(args));
78 var req = new Request(method, args);
79 req.callback(handler);
87 /* set the search result info, number of hits, which results we're
88 displaying, links to the next/prev pages, etc. */
89 function resultSetHitInfo() {
92 /* tell the user where the results are coming from */
93 var baseorg = findOrgUnit(getLocation());
94 var depth = getDepth();
95 var mydepth = findOrgDepth(baseorg);
96 if( findOrgDepth(baseorg) != depth ) {
98 while( mydepth > depth ) {
100 tmporg = findOrgUnit(tmporg.parent_ou());
102 unHideMe($('including_results_for'));
103 $('including_results_location').appendChild(text(tmporg.name()));
107 try{searchTimer.stop()}catch(e){}
109 if( findCurrentPage() == MRESULT ) {
110 if(getHitCount() <= lowHitCount && getTerm())
111 runEvt('result', 'lowHits');
114 if(getHitCount() == 0) {
115 runEvt('result', 'zeroHits');
120 var pages = getHitCount() / getDisplayCount();
121 if(pages % 1) pages = parseInt(pages) + 1;
125 var cpage = (getOffset()/getDisplayCount()) + 1;
127 G.ui.result.current_page.appendChild(text(cpage));
128 G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
130 /* set the offsets */
131 var offsetEnd = getDisplayCount() + getOffset();
132 if( getDisplayCount() > (getHitCount() - getOffset()))
133 offsetEnd = getHitCount();
135 G.ui.result.offset_end.appendChild(text(offsetEnd));
136 G.ui.result.offset_start.appendChild(text(getOffset() + 1));
138 G.ui.result.result_count.appendChild(text(getHitCount()));
139 unHideMe(G.ui.result.info);
142 function resultLowHits() {
144 unHideMe($('result_low_hits'));
145 if(getHitCount() > 0)
146 unHideMe($('result_low_hits_msg'));
148 var sreq = new Request(CHECK_SPELL, getTerm());
149 sreq.callback(resultSuggestSpelling);
152 var words = getTerm().split(' ');
154 while( word = words.shift() ) {
155 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
156 areq.callback(resultLowHitXRef);
160 if( !(getForm() == null || getForm() == 'all' || getForm() == "") ) {
162 a[PARAM_FORM] = "all";
163 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
164 unHideMe($('low_hits_remove_format'));
167 resultSuggestSearchClass();
171 var lowHitsXRefLinkParent;
172 function resultLowHitXRef(r) {
173 if(!lowHitsXRefLink){
174 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
175 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
177 var res = r.getResultObject();
179 arr.concat(res.also);
180 if(arr && arr.length > 0) {
181 unHideMe($('low_hits_cross_ref'));
184 while( word = arr.shift() ) {
187 a[PARAM_TERM] = word;
188 var template = lowHitsXRefLink.cloneNode(true);
189 template.setAttribute('href',buildOPACLink(a));
190 template.appendChild(text(word));
191 lowHitsXRefLinkParent.appendChild(template);
192 lowHitsXRefLinkParent.appendChild(text(' '));
197 function resultZeroHits() {
199 unHideMe($('result_low_hits'));
200 unHideMe($('result_zero_hits_msg'));
201 if(getTerm()) resultExpandSearch(); /* advanced search */
204 function resultExpandSearch() {
205 var top = findOrgDepth(globalOrgTree);
206 if(getDepth() == top) return;
207 unHideMe($('low_hits_expand_range'));
208 var par = $('low_hits_expand_link').parentNode;
209 var template = par.removeChild($('low_hits_expand_link'));
211 var bottom = getDepth();
212 while( top < bottom ) {
214 a[PARAM_DEPTH] = top;
215 var temp = template.cloneNode(true);
216 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
217 temp.setAttribute('href',buildOPACLink(a));
218 par.appendChild(temp);
223 function resultSuggestSearchClass() {
224 var stype = getStype();
225 if(stype == STYPE_KEYWORD) return;
227 unHideMe($('low_hits_search_type'));
228 if(stype != STYPE_TITLE) {
229 ref = $('low_hits_title_search');
231 a[PARAM_STYPE] = STYPE_TITLE;
232 ref.setAttribute('href',buildOPACLink(a));
234 if(stype != STYPE_AUTHOR) {
235 ref = $('low_hits_author_search');
237 a[PARAM_STYPE] = STYPE_AUTHOR;
238 ref.setAttribute('href',buildOPACLink(a));
240 if(stype != STYPE_SUBJECT) {
241 ref = $('low_hits_subject_search');
243 a[PARAM_STYPE] = STYPE_SUBJECT;
244 ref.setAttribute('href',buildOPACLink(a));
246 if(stype != STYPE_KEYWORD) {
247 ref = $('low_hits_keyword_search');
249 a[PARAM_STYPE] = STYPE_KEYWORD;
250 ref.setAttribute('href',buildOPACLink(a));
252 if(stype != STYPE_SERIES) {
253 ref = $('low_hits_series_search');
255 a[PARAM_STYPE] = STYPE_SERIES;
256 ref.setAttribute('href',buildOPACLink(a));
260 function resultSuggestSpelling(r) {
261 var res = r.getResultObject();
262 var phrase = getTerm();
263 var words = phrase.split(/ /);
267 for( var w = 0; w < words.length; w++ ) {
269 var blob = grep(res, function(i){return (i.word == word);});
270 if( blob ) blob = blob[0];
272 if( blob.word == word ) {
273 if( blob.suggestions && blob.suggestions[0] ) {
274 newterm += " " + blob.suggestions[0];
275 unHideMe($('did_you_mean'));
277 newterm += " " + word;
283 arg[PARAM_TERM] = newterm;
284 $('spell_check_link').setAttribute('href', buildOPACLink(arg));
285 $('spell_check_link').appendChild(text(newterm));
289 function resultPaginate() {
292 if( !(( (o+1) + getDisplayCount()) >= getHitCount()) ) {
295 args[PARAM_OFFSET] = o + getDisplayCount();
296 args[PARAM_SORT] = SORT;
297 args[PARAM_SORT_DIR] = SORT_DIR;
298 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
300 G.ui.result.next_link.setAttribute("href", buildOPACLink(args));
301 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
303 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
305 /* when hit count is divisible by display count, we have to adjust */
306 if( getHitCount() % getDisplayCount() == 0 )
307 args[PARAM_OFFSET] -= getDisplayCount();
309 G.ui.result.end_link.setAttribute("href", buildOPACLink(args));
310 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
316 args[PARAM_SORT] = SORT;
317 args[PARAM_SORT_DIR] = SORT_DIR;
318 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
320 args[PARAM_OFFSET] = o - getDisplayCount();
321 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args));
322 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
324 args[PARAM_OFFSET] = 0;
325 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args));
326 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
329 if(getDisplayCount() < getHitCount())
330 unHideMe($('start_end_links_span'));
333 try{searchTimer.stop()}catch(e){}
336 function buildunAPISpan (span, type, id) {
340 addCSSClass(span,'unapi-id');
344 'tag:' + cgi.server_name + ',' +
346 ':' + type + '/' + id
350 /* display the record info in the record display table 'pos' is the
351 zero based position the record should have in the display table */
352 function resultDisplayRecord(rec, pos, is_mr) {
354 if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
356 recordsCache.push(rec);
358 var r = table.rows[pos + 1];
362 var rank = parseFloat(ranks[pos + getOffset()]);
363 rank = parseInt( rank * 100 );
364 var relspan = $n(r, "relevancy_span");
365 relspan.appendChild(text(rank));
366 unHideMe(relspan.parentNode);
369 var pic = $n(r, config.names.result.item_jacket);
370 pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
372 var title_link = $n(r, config.names.result.item_title);
373 var author_link = $n(r, config.names.result.item_author);
376 var onlyrec = onlyrecord[ getOffset() + pos ];
378 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
382 args[PARAM_OFFSET] = 0;
383 args[PARAM_RID] = onlyrec;
384 args[PARAM_MRID] = rec.doc_id();
385 pic.parentNode.setAttribute("href", buildOPACLink(args));
386 title_link.setAttribute("href", buildOPACLink(args));
387 title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
390 buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
392 buildTitleLink(rec, title_link);
395 args[PARAM_OFFSET] = 0;
396 args[PARAM_MRID] = rec.doc_id();
397 pic.parentNode.setAttribute("href", buildOPACLink(args));
401 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
403 buildTitleDetailLink(rec, title_link);
406 args[PARAM_OFFSET] = 0;
407 args[PARAM_RID] = rec.doc_id();
408 pic.parentNode.setAttribute("href", buildOPACLink(args));
410 unHideMe($n(r,'place_hold_span'));
411 $n(r,'place_hold_link').setAttribute(
412 'href','javascript:holdsDrawEditor({record:"'+rec.doc_id()+'",type:"T"});');
415 buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
419 if(!isNull(rec.edition())) {
420 unHideMe( $n(r, "result_table_extra_span"));
421 $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
423 if(!isNull(rec.pubdate())) {
424 unHideMe( $n(r, "result_table_extra_span"));
425 unHideMe($n(r, "result_table_pub_span"));
426 $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
428 if(!isNull(rec.publisher()) ) {
429 unHideMe( $n(r, "result_table_extra_span"));
430 unHideMe($n(r, "result_table_pub_span"));
431 $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
434 if(!isNull(rec.physical_description()) ) {
435 unHideMe( $n(r, "result_table_extra_span"));
436 var t = " " + rec.physical_description();
437 //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
438 $n(r, "result_table_phys_span").appendChild( text(t));
443 resultBuildFormatIcons( r, rec, is_mr );
447 runEvt("result", "recordDrawn", rec.doc_id(), title_link);
450 if(resultPageIsDone()) {
451 runEvt('result', 'allRecordsReceived', recordsCache);
456 function _resultFindRec(id) {
457 for( var i = 0; i != recordsCache.length; i++ ) {
458 var rec = recordsCache[i];
459 if( rec && rec.doc_id() == id )
466 function resultBuildFormatIcons( row, rec, is_mr ) {
468 var ress = rec.types_of_resource();
470 for( var i in ress ) {
475 var link = $n(row, res + "_link");
477 var img = link.getElementsByTagName("img")[0];
478 removeCSSClass( img, config.css.dim );
482 if( f == modsFormatToMARC(res) )
483 addCSSClass( img, "dim2_border");
487 args[PARAM_OFFSET] = 0;
491 args[PARAM_TFORM] = modsFormatToMARC(res);
492 args[PARAM_MRID] = rec.doc_id();
496 args[PARAM_RID] = rec.doc_id();
499 link.setAttribute("href", buildOPACLink(args));
505 function resultPageIsDone(pos) {
506 return (recordsHandled == getDisplayCount()
507 || recordsHandled + getOffset() == getHitCount());
510 var resultCCHeaderApplied = false;
512 /* -------------------------------------------------------------------- */
513 /* dynamically add the copy count rows based on the org type 'countsrow'
514 is the row into which we will add TD's to hold the copy counts
515 This code generates copy count cells with an id of
516 'copy_count_cell_<depth>_<pagePosition>' */
517 function resultAddCopyCounts(rec, pagePosition) {
519 var r = table.rows[pagePosition + 1];
520 var countsrow = $n(r, config.names.result.counts_row );
521 var ccell = $n(countsrow, config.names.result.count_cell);
523 var nodes = orgNodeTrail(findOrgUnit(getLocation()));
525 var type = findOrgType(node.ou_type());
526 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
527 ccell.title = type.opac_label();
528 //addCSSClass(ccell, config.css.result.cc_cell_even);
530 var lastcell = ccell;
531 var lastheadcell = null;
534 var ccheadcell = null;
535 if(!resultCCHeaderApplied) {
536 ccrow = $('result_thead_row');
537 ccheadcell = ccrow.removeChild($n(ccrow, "result_thead_ccell"));
538 var t = ccheadcell.cloneNode(true);
540 t.appendChild(text(type.opac_label()));
541 ccrow.appendChild(t);
542 resultCCHeaderApplied = true;
548 var d = findOrgDepth(nodes[1]);
549 var d2 = findOrgDepth(nodes[nodes.length -1]);
551 for( var i = d; i <= d2 ; i++ ) {
553 ccell = ccell.cloneNode(true);
555 //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
556 //else addCSSClass(ccell, "copy_count_cell_even");
558 var node = nodes[x++];
559 var type = findOrgType(node.ou_type());
561 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
562 ccell.title = type.opac_label();
563 countsrow.insertBefore(ccell, lastcell);
567 var t = ccheadcell.cloneNode(true);
568 t.appendChild(text(type.opac_label()));
569 ccrow.insertBefore(t, lastheadcell);
575 unHideMe($("search_info_table"));
578 /* collect copy counts for a record using method 'methodName' */
579 function resultCollectCopyCounts(rec, pagePosition, methodName) {
580 if(rec == null || rec.doc_id() == null) return;
581 var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
582 req.request.userdata = [ rec, pagePosition ];
583 req.callback(resultHandleCopyCounts);
587 function resultHandleCopyCounts(r) {
588 runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject());
592 /* display the collected copy counts */
593 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
594 if(copy_counts == null || rec == null) return;
596 while(copy_counts[i] != null) {
597 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
598 var cts = copy_counts[i];
599 cell.appendChild(text(cts.available + " / " + cts.count));
602 /* here we style opac-invisible records for xul */
604 if( cts.depth == 0 ) {
605 if(!cts.transcendant && !cts.unshadow) {
606 _debug("found an opac-shadowed record: " + rec.doc_id());
607 var row = cell.parentNode.parentNode.parentNode.parentNode.parentNode;
609 addCSSClass( row, 'no_copies' );
611 addCSSClass( row, 'shadowed' );