]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
integrating the new advanced search interface
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / result_common.js
1
2 var recordsHandled = 0;
3 var recordsCache = [];
4 var lowHitCount = 4;
5
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 );
12
13 attachEvt('result','lowHits',resultLowHits);
14 attachEvt('result','zeroHits',resultZeroHits);
15
16 attachEvt( "common", "locationUpdated", resultSBSubmit );
17 function resultSBSubmit(){searchBarSubmit();}
18
19 /* do this after we have ID's so the rank for mr pages will be correct */
20 attachEvt("result", "preCollectRecords", resultPaginate);
21
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;
27 }
28
29
30
31
32
33 /* generic search method */
34 function resultCollectSearchIds( type, method, handler ) {
35
36         var sort                = (getSort() == SORT_TYPE_REL) ? null : getSort(); 
37         var sortdir = (sort) ? ((getSortDir()) ? getSortDir() : SORT_DIR_ASC) : null;
38
39         var item_type;
40         var item_form;
41         var args = {};
42
43         if( type ) {
44                 args.searches = {};
45                 args.searches[getStype()] = {};
46                 args.searches[getStype()].term = getTerm();
47
48                 var form = parseForm(getForm());
49                 item_type = form.item_type;
50                 item_form = form.item_form;
51
52         } else {
53                 args.searches = JSON2js(getSearches());
54                 item_type = (getItemType()) ? getItemType().split(/,/) : null;
55                 item_form = (getItemForm()) ? getItemForm().split(/,/) : null;
56         }
57
58         args.org_unit = getLocation();
59         args.depth    = getDepth();
60         args.limit    = 200;
61         args.offset   = getOffset();
62
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;
67
68         if(getAudience()) args.audience = getAudience().split(/,/);
69         if(getLitForm()) args.lit_form  = getLitForm().split(/,/);
70
71         //alert(js2JSON(args));
72
73         var req = new Request(method, args);
74         req.callback(handler);
75         req.send();
76 }
77
78
79
80
81
82 /* set the search result info, number of hits, which results we're 
83         displaying, links to the next/prev pages, etc. */
84 function resultSetHitInfo() { 
85
86
87         /* tell the user where the results are coming from */
88         var baseorg = findOrgUnit(getLocation());
89         var depth = getDepth();
90         var mydepth = findOrgDepth(baseorg);
91         if( findOrgDepth(baseorg) != depth ) {
92                 var tmporg = baseorg;
93                 while( mydepth > depth ) {
94                         mydepth--;
95                         tmporg = findOrgUnit(tmporg.parent_ou());
96                 }
97                 unHideMe($('including_results_for'));
98                 $('including_results_location').appendChild(text(tmporg.name()));
99         }
100
101
102         try{searchTimer.stop()}catch(e){}
103
104         if( findCurrentPage() == MRESULT ) {
105                 if(getHitCount() <= lowHitCount && getTerm())
106                         runEvt('result', 'lowHits');
107                 if(getHitCount() == 0) {
108                         runEvt('result', 'zeroHits');
109                         return;
110                 }
111         }
112
113         var c;  
114         if( getDisplayCount() > (getHitCount() - getOffset()))  c = getHitCount();
115         else c = getDisplayCount() + getOffset();
116
117         var pages = getHitCount() / getDisplayCount();
118         if(pages % 1) pages = parseInt(pages) + 1;
119
120         G.ui.result.current_page.appendChild(text( (getOffset()/getDisplayCount()) + 1));
121         G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
122
123         G.ui.result.offset_start.appendChild(text(getOffset() + 1));
124         G.ui.result.offset_end.appendChild(text(c));
125         G.ui.result.result_count.appendChild(text(getHitCount()));
126         unHideMe(G.ui.result.info);
127 }
128
129 function resultLowHits() {
130         showCanvas();
131         unHideMe($('result_low_hits'));
132         if(getHitCount() > 0)
133                 unHideMe($('result_low_hits_msg'));
134
135         var sreq = new Request(CHECK_SPELL, getTerm());
136         sreq.callback(resultSuggestSpelling);
137         sreq.send();
138
139         var words = getTerm().split(' ');
140         var word;
141         while( word = words.shift() ) {
142                 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
143                 areq.callback(resultLowHitXRef);
144                 areq.send();
145         }
146
147         if( !(getForm() == null || getForm() == 'all' || getForm == "") ) {
148                 var a = {};
149                 a[PARAM_FORM] = "all";
150                 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
151                 unHideMe($('low_hits_remove_format'));
152         }
153
154         resultSuggestSearchClass();
155 }
156
157 var lowHitsXRefLink;
158 var lowHitsXRefLinkParent;
159 function resultLowHitXRef(r) {
160         if(!lowHitsXRefLink){
161                 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
162                 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
163         }
164         var res = r.getResultObject();
165         var arr = res.from;
166         arr.concat(res.also);
167         if(arr && arr.length > 0) {
168                 unHideMe($('low_hits_cross_ref'));
169                 var word;
170                 var c = 0;
171                 while( word = arr.shift() ) {
172                         if(c++ > 20) break;
173                         var a = {};
174                         a[PARAM_TERM] = word;
175                         var template = lowHitsXRefLink.cloneNode(true);
176                         template.setAttribute('href',buildOPACLink(a));
177                         template.appendChild(text(word));
178                         lowHitsXRefLinkParent.appendChild(template);
179                         lowHitsXRefLinkParent.appendChild(text(' '));
180                 }
181         }
182 }
183
184 function resultZeroHits() {
185         showCanvas();
186         unHideMe($('result_low_hits'));
187         unHideMe($('result_zero_hits_msg'));
188         if(getTerm()) resultExpandSearch(); /* advanced search */
189 }
190
191 function resultExpandSearch() {
192         var top = findOrgDepth(globalOrgTree);
193         if(getDepth() == top) return;
194         unHideMe($('low_hits_expand_range'));
195         var par = $('low_hits_expand_link').parentNode;
196         var template = par.removeChild($('low_hits_expand_link'));
197
198         var bottom = getDepth();
199         while( top < bottom ) {
200                 var a = {};
201                 a[PARAM_DEPTH] = top;
202                 var temp = template.cloneNode(true);
203                 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
204                 temp.setAttribute('href',buildOPACLink(a));
205                 par.appendChild(temp);
206                 top++;
207         }
208 }
209
210 function resultSuggestSearchClass() {
211         var stype = getStype();
212         if(stype == STYPE_KEYWORD) return;
213         var a = {}; var ref;
214         unHideMe($('low_hits_search_type'));
215         if(stype != STYPE_TITLE) {
216                 ref = $('low_hits_title_search');
217                 unHideMe(ref);
218                 a[PARAM_STYPE] = STYPE_TITLE;
219                 ref.setAttribute('href',buildOPACLink(a));
220         }
221         if(stype != STYPE_AUTHOR) {
222                 ref = $('low_hits_author_search');
223                 unHideMe(ref);
224                 a[PARAM_STYPE] = STYPE_AUTHOR;
225                 ref.setAttribute('href',buildOPACLink(a));
226         }
227         if(stype != STYPE_SUBJECT) {
228                 ref = $('low_hits_subject_search');
229                 unHideMe(ref);
230                 a[PARAM_STYPE] = STYPE_SUBJECT;
231                 ref.setAttribute('href',buildOPACLink(a));
232         }
233         if(stype != STYPE_KEYWORD) {
234                 ref = $('low_hits_keyword_search');
235                 unHideMe(ref);
236                 a[PARAM_STYPE] = STYPE_KEYWORD;
237                 ref.setAttribute('href',buildOPACLink(a));
238         }
239         if(stype != STYPE_SERIES) {
240                 ref = $('low_hits_series_search');
241                 unHideMe(ref);
242                 a[PARAM_STYPE] = STYPE_SERIES;
243                 ref.setAttribute('href',buildOPACLink(a));
244         }
245 }
246
247 function resultSuggestSpelling(r) {
248         var res = r.getResultObject();
249         if(res) {
250                 unHideMe($('did_you_mean'));
251                 var arg = {};
252                 arg[PARAM_TERM] = res;
253                 $('spell_check_link').setAttribute(
254                         'href', buildOPACLink(arg) );
255                 $('spell_check_link').appendChild(text(res));
256         }
257 }
258
259
260 function resultPaginate() {
261         var o = getOffset();
262
263         if( !((o + getDisplayCount()) >= getHitCount()) ) {
264
265                 var args = {};
266                 args[PARAM_OFFSET]      = o + getDisplayCount();
267                 args[PARAM_SORT]                = SORT;
268                 args[PARAM_SORT_DIR] = SORT_DIR;
269                 args[PARAM_RLIST]               = new CGI().param(PARAM_RLIST);
270
271                 G.ui.result.next_link.setAttribute("href", buildOPACLink(args)); 
272                 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
273
274                 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
275                 G.ui.result.end_link.setAttribute("href", buildOPACLink(args)); 
276                 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
277         }
278
279         if( o > 0 ) {
280
281                 var args = {};
282                 args[PARAM_SORT]                = SORT;
283                 args[PARAM_SORT_DIR] = SORT_DIR;
284                 args[PARAM_RLIST]               = new CGI().param(PARAM_RLIST);
285
286                 args[PARAM_OFFSET] = o - getDisplayCount();
287                 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args)); 
288                 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
289
290                 args[PARAM_OFFSET] = 0;
291                 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args)); 
292                 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
293         }
294         if(getDisplayCount() < getHitCount())
295                 unHideMe($('start_end_links_span'));
296
297         showCanvas();
298         try{searchTimer.stop()}catch(e){}
299 }
300
301 function buildunAPISpan (span, type, id) {
302         var cgi = new CGI();
303         var d = new Date();
304
305         addCSSClass(span,'unapi-uri');
306
307         span.appendChild(text('unAPI'));
308         span.setAttribute(
309                 'title',
310                 'tag:' + cgi.server_name + ',' +
311                         d.getFullYear() +
312                         ':' + type + '/' + id
313         );
314 }
315
316 /* display the record info in the record display table 'pos' is the 
317                 zero based position the record should have in the display table */
318 function resultDisplayRecord(rec, pos, is_mr) {
319
320         if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
321         recordsHandled++;
322         recordsCache.push(rec);
323
324         var r = table.rows[pos + 1];
325
326         
327         try {
328                 var rank = parseFloat(ranks[pos + getOffset()]);
329                 rank            = parseInt( rank * 100 );
330                 var relspan = $n(r, "relevancy_span");
331                 relspan.appendChild(text(rank));
332                 unHideMe(relspan.parentNode);
333         } catch(e){ }
334
335         var pic = $n(r, config.names.result.item_jacket);
336         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
337
338         var title_link = $n(r, config.names.result.item_title);
339         var author_link = $n(r, config.names.result.item_author);
340
341         if( is_mr )  {
342                 var onlyrec = onlyrecord[ getOffset() + pos ];
343                 if(onlyrec) {
344                         buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
345
346                         var args = {};
347                         args.page = RDETAIL;
348                         args[PARAM_OFFSET] = 0;
349                         args[PARAM_RID] = onlyrec;
350                         args[PARAM_MRID] = rec.doc_id();
351                         pic.parentNode.setAttribute("href", buildOPACLink(args));
352                         title_link.setAttribute("href", buildOPACLink(args));
353                         title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
354                         
355                 } else {
356                         buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
357
358                         buildTitleLink(rec, title_link); 
359                         var args = {};
360                         args.page = RRESULT;
361                         args[PARAM_OFFSET] = 0;
362                         args[PARAM_MRID] = rec.doc_id();
363                         pic.parentNode.setAttribute("href", buildOPACLink(args));
364                 }
365
366         } else {
367                 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
368
369                 buildTitleDetailLink(rec, title_link); 
370                 var args = {};
371                 args.page = RDETAIL;
372                 args[PARAM_OFFSET] = 0;
373                 args[PARAM_RID] = rec.doc_id();
374                 pic.parentNode.setAttribute("href", buildOPACLink(args));
375
376                 unHideMe($n(r,'place_hold_span'));
377                 $n(r,'place_hold_link').setAttribute(
378                         'href','javascript:holdsDrawWindow("'+rec.doc_id()+'");');
379         }
380
381         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
382
383         if(! is_mr ) {
384         
385                 if(!isNull(rec.edition()))      {
386                         unHideMe( $n(r, "result_table_extra_span"));
387                         $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
388                 }
389                 if(!isNull(rec.pubdate())) {
390                         unHideMe( $n(r, "result_table_extra_span"));
391                         unHideMe($n(r, "result_table_pub_span"));
392                         $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
393                 }
394                 if(!isNull(rec.publisher()) ) {
395                         unHideMe( $n(r, "result_table_extra_span"));
396                         unHideMe($n(r, "result_table_pub_span"));
397                         $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
398                 }
399         }
400
401         resultBuildFormatIcons( r, rec, is_mr );
402
403         unHideMe(r);
404         
405         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
406
407         /*
408         if(resultPageIsDone())  {
409                 runEvt('result', 'allRecordsReceived', recordsCache);
410         }
411         */
412 }
413
414 function _resultFindRec(id) {
415         for( var i = 0; i != recordsCache.length; i++ ) {
416                 var rec = recordsCache[i];
417                 if( rec && rec.doc_id() == id )
418                         return rec;
419         }
420         return null;
421 }
422
423
424 function resultBuildFormatIcons( row, rec, is_mr ) {
425
426         var ress = rec.types_of_resource();
427
428         for( var i in ress ) {
429
430                 var res = ress[i];
431                 var link = $n(row, res + "_link");
432                 link.title = res;
433                 var img = link.getElementsByTagName("img")[0];
434                 removeCSSClass( img, config.css.dim );
435
436                 var f = getForm();
437                 if( f != "all" ) {
438                         if( f == modsFormatToMARC(res) ) 
439                                 addCSSClass( img, "dim2_border");
440                 }
441
442                 var args = {};
443                 args[PARAM_OFFSET] = 0;
444                 args[PARAM_FORM] = modsFormatToMARC(res);
445
446                 if(is_mr) {
447                         args.page = RRESULT;
448                         args[PARAM_MRID] = rec.doc_id();
449                 } else {
450                         args.page = RDETAIL
451                         args[PARAM_RID] = rec.doc_id();
452                 }
453
454                 link.setAttribute("href", buildOPACLink(args));
455
456         }
457 }
458
459
460 function resultPageIsDone(pos) {
461         return (recordsHandled == getDisplayCount() 
462                 || recordsHandled + getOffset() == getHitCount());
463 }
464
465 var resultCCHeaderApplied = false;
466
467 /* -------------------------------------------------------------------- */
468 /* dynamically add the copy count rows based on the org type 'countsrow' 
469         is the row into which we will add TD's to hold the copy counts 
470         This code generates copy count cells with an id of
471         'copy_count_cell_<depth>_<pagePosition>'  */
472 function resultAddCopyCounts(rec, pagePosition) {
473
474         var r = table.rows[pagePosition + 1];
475         var countsrow = $n(r, config.names.result.counts_row );
476         var ccell = $n(countsrow, config.names.result.count_cell);
477
478         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
479         var node = nodes[0];
480         var type = findOrgType(node.ou_type());
481         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
482         ccell.title = type.opac_label();
483         //addCSSClass(ccell, config.css.result.cc_cell_even);
484
485         var lastcell = ccell;
486         var lastheadcell = null;
487
488         var cchead = null;
489         var ccheadcell = null;
490         if(!resultCCHeaderApplied) {
491                 ccrow = $('result_thead_row');
492                 ccheadcell =  ccrow.removeChild($n(ccrow, "result_thead_ccell"));
493                 var t = ccheadcell.cloneNode(true);
494                 lastheadcell = t;
495                 t.appendChild(text(type.opac_label()));
496                 ccrow.appendChild(t);
497                 resultCCHeaderApplied = true;
498         }
499
500         if(nodes[1]) {
501
502                 var x = 1;
503                 var d = findOrgDepth(nodes[1]);
504                 var d2 = findOrgDepth(nodes[nodes.length -1]);
505
506                 for( var i = d; i <= d2 ; i++ ) {
507         
508                         ccell = ccell.cloneNode(true);
509
510                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
511                         //else addCSSClass(ccell, "copy_count_cell_even");
512
513                         var node = nodes[x++];
514                         var type = findOrgType(node.ou_type());
515         
516                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
517                         ccell.title = type.opac_label();
518                         countsrow.insertBefore(ccell, lastcell);
519                         lastcell = ccell;
520
521                         if(ccheadcell) {
522                                 var t = ccheadcell.cloneNode(true);
523                                 t.appendChild(text(type.opac_label()));
524                                 ccrow.insertBefore(t, lastheadcell);
525                                 lastheadcell = t;
526                         }
527                 }
528         }
529
530         unHideMe($("search_info_table"));
531 }
532
533 /* collect copy counts for a record using method 'methodName' */
534 function resultCollectCopyCounts(rec, pagePosition, methodName) {
535         if(rec == null || rec.doc_id() == null) return;
536         var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
537         req.request.userdata = [ rec, pagePosition ];
538         req.callback(resultHandleCopyCounts);
539         req.send();
540 }
541
542 function resultHandleCopyCounts(r) {
543         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
544 }
545
546
547 /* display the collected copy counts */
548 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
549         if(copy_counts == null || rec == null) return;
550         var i = 0;
551         while(copy_counts[i] != null) {
552                 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
553                 /*
554                 var span = cell.getElementsByTagName("div")[0];
555                 */
556                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
557
558                 i++;
559         }
560 }
561
562