2 var recordsHandled = 0;
6 var resultFetchAllRecords = false;
8 /* set up the event handlers */
9 G.evt.result.hitCountReceived.push(resultSetHitInfo);
10 G.evt.result.recordReceived.push(resultDisplayRecord, resultAddCopyCounts);
11 G.evt.result.copyCountsReceived.push(resultDisplayCopyCounts);
12 G.evt.result.allRecordsReceived.push(resultBuildCaches, resultDrawSubjects, resultDrawAuthors, resultDrawSeries);
13 //G.evt.result.allRecordsReceived.push(resultBuildCaches );
15 attachEvt('result','lowHits',resultLowHits);
16 attachEvt('result','zeroHits',resultZeroHits);
18 attachEvt( "common", "locationUpdated", resultSBSubmit );
19 function resultSBSubmit(){searchBarSubmit();}
21 /* do this after we have ID's so the rank for mr pages will be correct */
22 attachEvt("result", "preCollectRecords", resultPaginate);
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;
35 /* generic search method */
36 function resultCollectSearchIds( type, method, handler ) {
38 var sort = (getSort() == SORT_TYPE_REL) ? null : getSort();
39 var sortdir = (sort) ? ((getSortDir()) ? getSortDir() : SORT_DIR_ASC) : null;
47 args.searches[getStype()] = {};
48 args.searches[getStype()].term = getTerm();
50 var form = parseForm(getForm());
51 item_type = form.item_type;
52 item_form = form.item_form;
55 args.searches = JSON2js(getSearches());
56 item_type = (getItemType()) ? getItemType().split(/,/) : null;
57 item_form = (getItemForm()) ? getItemForm().split(/,/) : null;
60 args.org_unit = getLocation();
61 args.depth = getDepth();
63 args.limit = (resultFetchAllRecords) ? 1000 : getDisplayCount();
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('form = ' + item_form + ' : type = ' + item_type);
79 //alert(js2JSON(args));
80 var req = new Request(method, args);
81 req.callback(handler);
89 /* set the search result info, number of hits, which results we're
90 displaying, links to the next/prev pages, etc. */
91 function resultSetHitInfo() {
94 /* tell the user where the results are coming from */
95 var baseorg = findOrgUnit(getLocation());
96 var depth = getDepth();
97 var mydepth = findOrgDepth(baseorg);
98 if( findOrgDepth(baseorg) != depth ) {
100 while( mydepth > depth ) {
102 tmporg = findOrgUnit(tmporg.parent_ou());
104 unHideMe($('including_results_for'));
105 $('including_results_location').appendChild(text(tmporg.name()));
109 try{searchTimer.stop()}catch(e){}
111 if( findCurrentPage() == MRESULT ) {
112 if(getHitCount() <= lowHitCount && getTerm())
113 runEvt('result', 'lowHits');
116 if(getHitCount() == 0) {
117 runEvt('result', 'zeroHits');
122 var pages = getHitCount() / getDisplayCount();
123 if(pages % 1) pages = parseInt(pages) + 1;
127 var cpage = (getOffset()/getDisplayCount()) + 1;
129 G.ui.result.current_page.appendChild(text(cpage));
130 G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
132 /* set the offsets */
133 var offsetEnd = getDisplayCount() + getOffset();
134 if( getDisplayCount() > (getHitCount() - getOffset()))
135 offsetEnd = getHitCount();
137 G.ui.result.offset_end.appendChild(text(offsetEnd));
138 G.ui.result.offset_start.appendChild(text(getOffset() + 1));
140 G.ui.result.result_count.appendChild(text(getHitCount()));
141 unHideMe(G.ui.result.info);
144 function resultLowHits() {
146 unHideMe($('result_low_hits'));
147 if(getHitCount() > 0)
148 unHideMe($('result_low_hits_msg'));
150 var sreq = new Request(CHECK_SPELL, getTerm());
151 sreq.callback(resultSuggestSpelling);
154 var words = getTerm().split(' ');
156 while( word = words.shift() ) {
157 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
158 areq.callback(resultLowHitXRef);
162 if( !(getForm() == null || getForm() == 'all' || getForm == "") ) {
164 a[PARAM_FORM] = "all";
165 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
166 unHideMe($('low_hits_remove_format'));
169 resultSuggestSearchClass();
173 var lowHitsXRefLinkParent;
174 function resultLowHitXRef(r) {
175 if(!lowHitsXRefLink){
176 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
177 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
179 var res = r.getResultObject();
181 arr.concat(res.also);
182 if(arr && arr.length > 0) {
183 unHideMe($('low_hits_cross_ref'));
186 while( word = arr.shift() ) {
189 a[PARAM_TERM] = word;
190 var template = lowHitsXRefLink.cloneNode(true);
191 template.setAttribute('href',buildOPACLink(a));
192 template.appendChild(text(word));
193 lowHitsXRefLinkParent.appendChild(template);
194 lowHitsXRefLinkParent.appendChild(text(' '));
199 function resultZeroHits() {
201 unHideMe($('result_low_hits'));
202 unHideMe($('result_zero_hits_msg'));
203 if(getTerm()) resultExpandSearch(); /* advanced search */
206 function resultExpandSearch() {
207 var top = findOrgDepth(globalOrgTree);
208 if(getDepth() == top) return;
209 unHideMe($('low_hits_expand_range'));
210 var par = $('low_hits_expand_link').parentNode;
211 var template = par.removeChild($('low_hits_expand_link'));
213 var bottom = getDepth();
214 while( top < bottom ) {
216 a[PARAM_DEPTH] = top;
217 var temp = template.cloneNode(true);
218 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
219 temp.setAttribute('href',buildOPACLink(a));
220 par.appendChild(temp);
225 function resultSuggestSearchClass() {
226 var stype = getStype();
227 if(stype == STYPE_KEYWORD) return;
229 unHideMe($('low_hits_search_type'));
230 if(stype != STYPE_TITLE) {
231 ref = $('low_hits_title_search');
233 a[PARAM_STYPE] = STYPE_TITLE;
234 ref.setAttribute('href',buildOPACLink(a));
236 if(stype != STYPE_AUTHOR) {
237 ref = $('low_hits_author_search');
239 a[PARAM_STYPE] = STYPE_AUTHOR;
240 ref.setAttribute('href',buildOPACLink(a));
242 if(stype != STYPE_SUBJECT) {
243 ref = $('low_hits_subject_search');
245 a[PARAM_STYPE] = STYPE_SUBJECT;
246 ref.setAttribute('href',buildOPACLink(a));
248 if(stype != STYPE_KEYWORD) {
249 ref = $('low_hits_keyword_search');
251 a[PARAM_STYPE] = STYPE_KEYWORD;
252 ref.setAttribute('href',buildOPACLink(a));
254 if(stype != STYPE_SERIES) {
255 ref = $('low_hits_series_search');
257 a[PARAM_STYPE] = STYPE_SERIES;
258 ref.setAttribute('href',buildOPACLink(a));
262 function resultSuggestSpelling(r) {
263 var res = r.getResultObject();
264 var phrase = getTerm();
265 var words = phrase.split(/ /);
269 for( var w = 0; w < words.length; w++ ) {
271 var blob = grep(res, function(i){return (i.word == word);})[0];
272 if( blob.word == word ) {
273 if( blob.suggestions ) {
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 ) {
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' );