2 var recordsHandled = 0;
6 /* set up the event handlers */
7 G.evt.result.hitCountReceived.push(resultSetHitInfo);
8 G.evt.result.recordReceived.push(resultDisplayRecord, resultAddCopyCounts);
9 G.evt.result.copyCountsReceived.push(resultDisplayCopyCounts);
10 G.evt.result.allRecordsReceived.push(resultBuildCaches, resultDrawSubjects, resultDrawAuthors, resultDrawSeries);
11 //G.evt.result.allRecordsReceived.push(resultBuildCaches );
13 attachEvt('result','lowHits',resultLowHits);
14 attachEvt('result','zeroHits',resultZeroHits);
16 attachEvt( "common", "locationUpdated", resultSBSubmit );
17 function resultSBSubmit(){searchBarSubmit();}
19 /* do this after we have ID's so the rank for mr pages will be correct */
20 attachEvt("result", "preCollectRecords", resultPaginate);
22 /* returns the last 'index' postion ocurring in this page */
23 function resultFinalPageIndex() {
24 if(getHitCount() < (getOffset() + getDisplayCount()))
25 return getHitCount() - 1;
26 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 args.org_unit = getLocation();
59 args.depth = getDepth();
61 args.offset = getOffset();
63 if(sort) args.sort = sort;
64 if(sortdir) args.sort_dir = sortdir;
65 if(item_type) args.item_type = item_type;
66 if(item_form) args.item_form = item_form;
69 if(getAudience()) args.audience = getAudience().split(/,/);
70 if(getLitForm()) args.lit_form = getLitForm().split(/,/);
71 if(getLanguage()) args.language = getLanguage().split(/,/);
73 //alert('form = ' + item_form + ' : type = ' + item_type);
75 var req = new Request(method, args);
76 req.callback(handler);
84 /* set the search result info, number of hits, which results we're
85 displaying, links to the next/prev pages, etc. */
86 function resultSetHitInfo() {
89 /* tell the user where the results are coming from */
90 var baseorg = findOrgUnit(getLocation());
91 var depth = getDepth();
92 var mydepth = findOrgDepth(baseorg);
93 if( findOrgDepth(baseorg) != depth ) {
95 while( mydepth > depth ) {
97 tmporg = findOrgUnit(tmporg.parent_ou());
99 unHideMe($('including_results_for'));
100 $('including_results_location').appendChild(text(tmporg.name()));
104 try{searchTimer.stop()}catch(e){}
106 if( findCurrentPage() == MRESULT ) {
107 if(getHitCount() <= lowHitCount && getTerm())
108 runEvt('result', 'lowHits');
111 if(getHitCount() == 0) {
112 runEvt('result', 'zeroHits');
117 var pages = getHitCount() / getDisplayCount();
118 if(pages % 1) pages = parseInt(pages) + 1;
122 var cpage = (getOffset()/getDisplayCount()) + 1;
124 G.ui.result.current_page.appendChild(text(cpage));
125 G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
127 /* set the offsets */
128 var offsetEnd = getDisplayCount() + getOffset();
129 if( getDisplayCount() > (getHitCount() - getOffset()))
130 offsetEnd = getHitCount();
132 G.ui.result.offset_end.appendChild(text(offsetEnd));
133 G.ui.result.offset_start.appendChild(text(getOffset() + 1));
135 G.ui.result.result_count.appendChild(text(getHitCount()));
136 unHideMe(G.ui.result.info);
139 function resultLowHits() {
141 unHideMe($('result_low_hits'));
142 if(getHitCount() > 0)
143 unHideMe($('result_low_hits_msg'));
145 var sreq = new Request(CHECK_SPELL, getTerm());
146 sreq.callback(resultSuggestSpelling);
149 var words = getTerm().split(' ');
151 while( word = words.shift() ) {
152 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
153 areq.callback(resultLowHitXRef);
157 if( !(getForm() == null || getForm() == 'all' || getForm == "") ) {
159 a[PARAM_FORM] = "all";
160 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
161 unHideMe($('low_hits_remove_format'));
164 resultSuggestSearchClass();
168 var lowHitsXRefLinkParent;
169 function resultLowHitXRef(r) {
170 if(!lowHitsXRefLink){
171 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
172 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
174 var res = r.getResultObject();
176 arr.concat(res.also);
177 if(arr && arr.length > 0) {
178 unHideMe($('low_hits_cross_ref'));
181 while( word = arr.shift() ) {
184 a[PARAM_TERM] = word;
185 var template = lowHitsXRefLink.cloneNode(true);
186 template.setAttribute('href',buildOPACLink(a));
187 template.appendChild(text(word));
188 lowHitsXRefLinkParent.appendChild(template);
189 lowHitsXRefLinkParent.appendChild(text(' '));
194 function resultZeroHits() {
196 unHideMe($('result_low_hits'));
197 unHideMe($('result_zero_hits_msg'));
198 if(getTerm()) resultExpandSearch(); /* advanced search */
201 function resultExpandSearch() {
202 var top = findOrgDepth(globalOrgTree);
203 if(getDepth() == top) return;
204 unHideMe($('low_hits_expand_range'));
205 var par = $('low_hits_expand_link').parentNode;
206 var template = par.removeChild($('low_hits_expand_link'));
208 var bottom = getDepth();
209 while( top < bottom ) {
211 a[PARAM_DEPTH] = top;
212 var temp = template.cloneNode(true);
213 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
214 temp.setAttribute('href',buildOPACLink(a));
215 par.appendChild(temp);
220 function resultSuggestSearchClass() {
221 var stype = getStype();
222 if(stype == STYPE_KEYWORD) return;
224 unHideMe($('low_hits_search_type'));
225 if(stype != STYPE_TITLE) {
226 ref = $('low_hits_title_search');
228 a[PARAM_STYPE] = STYPE_TITLE;
229 ref.setAttribute('href',buildOPACLink(a));
231 if(stype != STYPE_AUTHOR) {
232 ref = $('low_hits_author_search');
234 a[PARAM_STYPE] = STYPE_AUTHOR;
235 ref.setAttribute('href',buildOPACLink(a));
237 if(stype != STYPE_SUBJECT) {
238 ref = $('low_hits_subject_search');
240 a[PARAM_STYPE] = STYPE_SUBJECT;
241 ref.setAttribute('href',buildOPACLink(a));
243 if(stype != STYPE_KEYWORD) {
244 ref = $('low_hits_keyword_search');
246 a[PARAM_STYPE] = STYPE_KEYWORD;
247 ref.setAttribute('href',buildOPACLink(a));
249 if(stype != STYPE_SERIES) {
250 ref = $('low_hits_series_search');
252 a[PARAM_STYPE] = STYPE_SERIES;
253 ref.setAttribute('href',buildOPACLink(a));
257 function resultSuggestSpelling(r) {
258 var res = r.getResultObject();
259 var phrase = getTerm();
260 var words = phrase.split(/ /);
264 for( var w = 0; w < words.length; w++ ) {
266 var blob = grep(res, function(i){return (i.word == word);})[0];
267 if( blob.word == word ) {
268 if( blob.suggestions ) {
269 newterm += " " + blob.suggestions[0];
270 unHideMe($('did_you_mean'));
272 newterm += " " + word;
278 arg[PARAM_TERM] = newterm;
279 $('spell_check_link').setAttribute('href', buildOPACLink(arg));
280 $('spell_check_link').appendChild(text(newterm));
284 function resultPaginate() {
287 if( !(( (o+1) + getDisplayCount()) >= getHitCount()) ) {
290 args[PARAM_OFFSET] = o + getDisplayCount();
291 args[PARAM_SORT] = SORT;
292 args[PARAM_SORT_DIR] = SORT_DIR;
293 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
295 G.ui.result.next_link.setAttribute("href", buildOPACLink(args));
296 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
298 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
300 /* when hit count is divisible by display count, we have to adjust */
301 if( getHitCount() % getDisplayCount() == 0 )
302 args[PARAM_OFFSET] -= getDisplayCount();
304 G.ui.result.end_link.setAttribute("href", buildOPACLink(args));
305 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
311 args[PARAM_SORT] = SORT;
312 args[PARAM_SORT_DIR] = SORT_DIR;
313 args[PARAM_RLIST] = new CGI().param(PARAM_RLIST);
315 args[PARAM_OFFSET] = o - getDisplayCount();
316 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args));
317 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
319 args[PARAM_OFFSET] = 0;
320 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args));
321 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
324 if(getDisplayCount() < getHitCount())
325 unHideMe($('start_end_links_span'));
328 try{searchTimer.stop()}catch(e){}
331 function buildunAPISpan (span, type, id) {
335 addCSSClass(span,'unapi-id');
339 'tag:' + cgi.server_name + ',' +
341 ':' + type + '/' + id
345 /* display the record info in the record display table 'pos' is the
346 zero based position the record should have in the display table */
347 function resultDisplayRecord(rec, pos, is_mr) {
349 if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
351 recordsCache.push(rec);
353 var r = table.rows[pos + 1];
357 var rank = parseFloat(ranks[pos + getOffset()]);
358 rank = parseInt( rank * 100 );
359 var relspan = $n(r, "relevancy_span");
360 relspan.appendChild(text(rank));
361 unHideMe(relspan.parentNode);
364 var pic = $n(r, config.names.result.item_jacket);
365 pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
367 var title_link = $n(r, config.names.result.item_title);
368 var author_link = $n(r, config.names.result.item_author);
371 var onlyrec = onlyrecord[ getOffset() + pos ];
373 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
377 args[PARAM_OFFSET] = 0;
378 args[PARAM_RID] = onlyrec;
379 args[PARAM_MRID] = rec.doc_id();
380 pic.parentNode.setAttribute("href", buildOPACLink(args));
381 title_link.setAttribute("href", buildOPACLink(args));
382 title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
385 buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
387 buildTitleLink(rec, title_link);
390 args[PARAM_OFFSET] = 0;
391 args[PARAM_MRID] = rec.doc_id();
392 pic.parentNode.setAttribute("href", buildOPACLink(args));
396 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
398 buildTitleDetailLink(rec, title_link);
401 args[PARAM_OFFSET] = 0;
402 args[PARAM_RID] = rec.doc_id();
403 pic.parentNode.setAttribute("href", buildOPACLink(args));
405 unHideMe($n(r,'place_hold_span'));
406 $n(r,'place_hold_link').setAttribute(
407 'href','javascript:holdsDrawEditor({record:"'+rec.doc_id()+'",type:"T"});');
410 buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
414 if(!isNull(rec.edition())) {
415 unHideMe( $n(r, "result_table_extra_span"));
416 $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
418 if(!isNull(rec.pubdate())) {
419 unHideMe( $n(r, "result_table_extra_span"));
420 unHideMe($n(r, "result_table_pub_span"));
421 $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
423 if(!isNull(rec.publisher()) ) {
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.publisher() ));
429 if(!isNull(rec.physical_description()) ) {
430 unHideMe( $n(r, "result_table_extra_span"));
431 var t = " " + rec.physical_description();
432 //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
433 $n(r, "result_table_phys_span").appendChild( text(t));
438 resultBuildFormatIcons( r, rec, is_mr );
442 runEvt("result", "recordDrawn", rec.doc_id(), title_link);
445 if(resultPageIsDone()) {
446 runEvt('result', 'allRecordsReceived', recordsCache);
451 function _resultFindRec(id) {
452 for( var i = 0; i != recordsCache.length; i++ ) {
453 var rec = recordsCache[i];
454 if( rec && rec.doc_id() == id )
461 function resultBuildFormatIcons( row, rec, is_mr ) {
463 var ress = rec.types_of_resource();
465 for( var i in ress ) {
470 var link = $n(row, res + "_link");
472 var img = link.getElementsByTagName("img")[0];
473 removeCSSClass( img, config.css.dim );
477 if( f == modsFormatToMARC(res) )
478 addCSSClass( img, "dim2_border");
482 args[PARAM_OFFSET] = 0;
486 args[PARAM_TFORM] = modsFormatToMARC(res);
487 args[PARAM_MRID] = rec.doc_id();
491 args[PARAM_RID] = rec.doc_id();
494 link.setAttribute("href", buildOPACLink(args));
500 function resultPageIsDone(pos) {
501 return (recordsHandled == getDisplayCount()
502 || recordsHandled + getOffset() == getHitCount());
505 var resultCCHeaderApplied = false;
507 /* -------------------------------------------------------------------- */
508 /* dynamically add the copy count rows based on the org type 'countsrow'
509 is the row into which we will add TD's to hold the copy counts
510 This code generates copy count cells with an id of
511 'copy_count_cell_<depth>_<pagePosition>' */
512 function resultAddCopyCounts(rec, pagePosition) {
514 var r = table.rows[pagePosition + 1];
515 var countsrow = $n(r, config.names.result.counts_row );
516 var ccell = $n(countsrow, config.names.result.count_cell);
518 var nodes = orgNodeTrail(findOrgUnit(getLocation()));
520 var type = findOrgType(node.ou_type());
521 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
522 ccell.title = type.opac_label();
523 //addCSSClass(ccell, config.css.result.cc_cell_even);
525 var lastcell = ccell;
526 var lastheadcell = null;
529 var ccheadcell = null;
530 if(!resultCCHeaderApplied) {
531 ccrow = $('result_thead_row');
532 ccheadcell = ccrow.removeChild($n(ccrow, "result_thead_ccell"));
533 var t = ccheadcell.cloneNode(true);
535 t.appendChild(text(type.opac_label()));
536 ccrow.appendChild(t);
537 resultCCHeaderApplied = true;
543 var d = findOrgDepth(nodes[1]);
544 var d2 = findOrgDepth(nodes[nodes.length -1]);
546 for( var i = d; i <= d2 ; i++ ) {
548 ccell = ccell.cloneNode(true);
550 //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
551 //else addCSSClass(ccell, "copy_count_cell_even");
553 var node = nodes[x++];
554 var type = findOrgType(node.ou_type());
556 ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
557 ccell.title = type.opac_label();
558 countsrow.insertBefore(ccell, lastcell);
562 var t = ccheadcell.cloneNode(true);
563 t.appendChild(text(type.opac_label()));
564 ccrow.insertBefore(t, lastheadcell);
570 unHideMe($("search_info_table"));
573 /* collect copy counts for a record using method 'methodName' */
574 function resultCollectCopyCounts(rec, pagePosition, methodName) {
575 if(rec == null || rec.doc_id() == null) return;
576 var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
577 req.request.userdata = [ rec, pagePosition ];
578 req.callback(resultHandleCopyCounts);
582 function resultHandleCopyCounts(r) {
583 runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject());
587 /* display the collected copy counts */
588 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
589 if(copy_counts == null || rec == null) return;
591 while(copy_counts[i] != null) {
592 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
593 var cts = copy_counts[i];
594 cell.appendChild(text(cts.available + " / " + cts.count));
597 /* here we style opac-invisible records for xul */
599 if( cts.depth == 0 ) {
601 _debug("found an opac-shadowed record: " + rec.doc_id());
602 var row = cell.parentNode.parentNode.parentNode.parentNode.parentNode;
604 addCSSClass( row, 'no_copies' );
606 addCSSClass( row, 'shadowed' );