]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
limiting the limit var on last page so cache will not re-try search
[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 var resultFetchAllRecords = false;
7
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);
19 }
20
21 function resultSBSubmit(){searchBarSubmit();}
22
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;
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         var limit = (resultFetchAllRecords) ? 1000 : getDisplayCount();
59         if( getHitCount() > 0 && (getOffset() + getDisplayCount()) > getHitCount() ) 
60                 limit = getHitCount() - getOffset();
61
62         args.org_unit = getLocation();
63         args.depth    = getDepth();
64         args.limit    = limit;
65         args.offset   = getOffset();
66
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;
71
72
73         if(getAudience()) args.audience  = getAudience().split(/,/);
74         if(getLitForm()) args.lit_form  = getLitForm().split(/,/);
75         if(getLanguage()) args.language = getLanguage().split(/,/);
76
77         //alert(js2JSON(args));
78         var req = new Request(method, args);
79         req.callback(handler);
80         req.send();
81 }
82
83
84
85
86
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() { 
90
91
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 ) {
97                 var tmporg = baseorg;
98                 while( mydepth > depth ) {
99                         mydepth--;
100                         tmporg = findOrgUnit(tmporg.parent_ou());
101                 }
102                 unHideMe($('including_results_for'));
103                 $('including_results_location').appendChild(text(tmporg.name()));
104         }
105
106
107         try{searchTimer.stop()}catch(e){}
108
109         if( findCurrentPage() == MRESULT ) {
110                 if(getHitCount() <= lowHitCount && getTerm())
111                         runEvt('result', 'lowHits');
112         }
113
114         if(getHitCount() == 0) {
115                 runEvt('result', 'zeroHits');
116                 return;
117         }
118
119
120         var pages = getHitCount() / getDisplayCount();
121         if(pages % 1) pages = parseInt(pages) + 1;
122
123         
124
125         var cpage = (getOffset()/getDisplayCount()) + 1;
126
127         G.ui.result.current_page.appendChild(text(cpage));
128         G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
129
130         /* set the offsets */
131         var offsetEnd = getDisplayCount() + getOffset();  
132         if( getDisplayCount() > (getHitCount() - getOffset()))  
133                 offsetEnd = getHitCount();
134
135         G.ui.result.offset_end.appendChild(text(offsetEnd));
136         G.ui.result.offset_start.appendChild(text(getOffset() + 1));
137
138         G.ui.result.result_count.appendChild(text(getHitCount()));
139         unHideMe(G.ui.result.info);
140 }
141
142 function resultLowHits() {
143         showCanvas();
144         unHideMe($('result_low_hits'));
145         if(getHitCount() > 0)
146                 unHideMe($('result_low_hits_msg'));
147
148         var sreq = new Request(CHECK_SPELL, getTerm());
149         sreq.callback(resultSuggestSpelling);
150         sreq.send();
151
152         var words = getTerm().split(' ');
153         var word;
154         while( word = words.shift() ) {
155                 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
156                 areq.callback(resultLowHitXRef);
157                 areq.send();
158         }
159
160         if( !(getForm() == null || getForm() == 'all' || getForm() == "") ) {
161                 var a = {};
162                 a[PARAM_FORM] = "all";
163                 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
164                 unHideMe($('low_hits_remove_format'));
165         }
166
167         resultSuggestSearchClass();
168 }
169
170 var lowHitsXRefLink;
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'));
176         }
177         var res = r.getResultObject();
178         var arr = res.from;
179         arr.concat(res.also);
180         if(arr && arr.length > 0) {
181                 unHideMe($('low_hits_cross_ref'));
182                 var word;
183                 var c = 0;
184                 while( word = arr.shift() ) {
185                         if(c++ > 20) break;
186                         var a = {};
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(' '));
193                 }
194         }
195 }
196
197 function resultZeroHits() {
198         showCanvas();
199         unHideMe($('result_low_hits'));
200         unHideMe($('result_zero_hits_msg'));
201         if(getTerm()) resultExpandSearch(); /* advanced search */
202 }
203
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'));
210
211         var bottom = getDepth();
212         while( top < bottom ) {
213                 var a = {};
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);
219                 top++;
220         }
221 }
222
223 function resultSuggestSearchClass() {
224         var stype = getStype();
225         if(stype == STYPE_KEYWORD) return;
226         var a = {}; var ref;
227         unHideMe($('low_hits_search_type'));
228         if(stype != STYPE_TITLE) {
229                 ref = $('low_hits_title_search');
230                 unHideMe(ref);
231                 a[PARAM_STYPE] = STYPE_TITLE;
232                 ref.setAttribute('href',buildOPACLink(a));
233         }
234         if(stype != STYPE_AUTHOR) {
235                 ref = $('low_hits_author_search');
236                 unHideMe(ref);
237                 a[PARAM_STYPE] = STYPE_AUTHOR;
238                 ref.setAttribute('href',buildOPACLink(a));
239         }
240         if(stype != STYPE_SUBJECT) {
241                 ref = $('low_hits_subject_search');
242                 unHideMe(ref);
243                 a[PARAM_STYPE] = STYPE_SUBJECT;
244                 ref.setAttribute('href',buildOPACLink(a));
245         }
246         if(stype != STYPE_KEYWORD) {
247                 ref = $('low_hits_keyword_search');
248                 unHideMe(ref);
249                 a[PARAM_STYPE] = STYPE_KEYWORD;
250                 ref.setAttribute('href',buildOPACLink(a));
251         }
252         if(stype != STYPE_SERIES) {
253                 ref = $('low_hits_series_search');
254                 unHideMe(ref);
255                 a[PARAM_STYPE] = STYPE_SERIES;
256                 ref.setAttribute('href',buildOPACLink(a));
257         }
258 }
259
260 function resultSuggestSpelling(r) {
261         var res = r.getResultObject();
262         var phrase = getTerm();
263         var words = phrase.split(/ /);
264
265         var newterm = "";
266
267         for( var w = 0; w < words.length; w++ ) {
268                 var word = words[w];
269                 var blob = grep(res, function(i){return (i.word == word);});
270                 if( blob ) blob = blob[0];
271                 else continue;
272                 if( blob.word == word ) {
273                         if( blob.suggestions && blob.suggestions[0] ) {
274                                 newterm += " " + blob.suggestions[0];
275                                 unHideMe($('did_you_mean'));
276                         } else {
277                                 newterm += " " + word;
278                         }
279                 }
280         }
281
282         var arg = {};
283         arg[PARAM_TERM] = newterm;
284         $('spell_check_link').setAttribute('href', buildOPACLink(arg));
285         $('spell_check_link').appendChild(text(newterm));
286 }
287
288
289 function resultPaginate() {
290         var o = getOffset();
291
292         if( !(( (o+1) + getDisplayCount()) >= getHitCount()) ) {
293
294                 var args = {};
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);
299
300                 G.ui.result.next_link.setAttribute("href", buildOPACLink(args)); 
301                 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
302
303                 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
304
305                 /* when hit count is divisible by display count, we have to adjust */
306                 if( getHitCount() % getDisplayCount() == 0 ) 
307                         args[PARAM_OFFSET] -= getDisplayCount();
308
309                 G.ui.result.end_link.setAttribute("href", buildOPACLink(args)); 
310                 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
311         }
312
313         if( o > 0 ) {
314
315                 var args = {};
316                 args[PARAM_SORT]                = SORT;
317                 args[PARAM_SORT_DIR] = SORT_DIR;
318                 args[PARAM_RLIST]               = new CGI().param(PARAM_RLIST);
319
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);
323
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);
327         }
328
329         if(getDisplayCount() < getHitCount())
330                 unHideMe($('start_end_links_span'));
331
332         showCanvas();
333         try{searchTimer.stop()}catch(e){}
334 }
335
336 function buildunAPISpan (span, type, id) {
337         var cgi = new CGI();
338         var d = new Date();
339
340         addCSSClass(span,'unapi-id');
341
342         span.setAttribute(
343                 'title',
344                 'tag:' + cgi.server_name + ',' +
345                         d.getFullYear() +
346                         ':' + type + '/' + id
347         );
348 }
349
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) {
353
354         if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
355         recordsHandled++;
356         recordsCache.push(rec);
357
358         var r = table.rows[pos + 1];
359
360         
361         try {
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);
367         } catch(e){ }
368
369         var pic = $n(r, config.names.result.item_jacket);
370         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
371
372         var title_link = $n(r, config.names.result.item_title);
373         var author_link = $n(r, config.names.result.item_author);
374
375         if( is_mr )  {
376                 var onlyrec = onlyrecord[ getOffset() + pos ];
377                 if(onlyrec) {
378                         buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
379
380                         var args = {};
381                         args.page = RDETAIL;
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))));
388                         
389                 } else {
390                         buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
391
392                         buildTitleLink(rec, title_link); 
393                         var args = {};
394                         args.page = RRESULT;
395                         args[PARAM_OFFSET] = 0;
396                         args[PARAM_MRID] = rec.doc_id();
397                         pic.parentNode.setAttribute("href", buildOPACLink(args));
398                 }
399
400         } else {
401                 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
402
403                 buildTitleDetailLink(rec, title_link); 
404                 var args = {};
405                 args.page = RDETAIL;
406                 args[PARAM_OFFSET] = 0;
407                 args[PARAM_RID] = rec.doc_id();
408                 pic.parentNode.setAttribute("href", buildOPACLink(args));
409
410                 unHideMe($n(r,'place_hold_span'));
411                 $n(r,'place_hold_link').setAttribute(
412                         'href','javascript:holdsDrawEditor({record:"'+rec.doc_id()+'",type:"T"});');
413         }
414
415         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
416
417         if(! is_mr ) {
418         
419                 if(!isNull(rec.edition()))      {
420                         unHideMe( $n(r, "result_table_extra_span"));
421                         $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
422                 }
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() ));
427                 }
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() ));
432                 }
433
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));
439                 }
440
441         }
442
443         resultBuildFormatIcons( r, rec, is_mr );
444
445         unHideMe(r);
446         
447         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
448
449         /*
450         if(resultPageIsDone())  {
451                 runEvt('result', 'allRecordsReceived', recordsCache);
452         }
453         */
454 }
455
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 )
460                         return rec;
461         }
462         return null;
463 }
464
465
466 function resultBuildFormatIcons( row, rec, is_mr ) {
467
468         var ress = rec.types_of_resource();
469
470         for( var i in ress ) {
471
472                 var res = ress[i];
473                 if(!res) continue;
474
475                 var link = $n(row, res + "_link");
476                 link.title = res;
477                 var img = link.getElementsByTagName("img")[0];
478                 removeCSSClass( img, config.css.dim );
479
480                 var f = getForm();
481                 if( f != "all" ) {
482                         if( f == modsFormatToMARC(res) ) 
483                                 addCSSClass( img, "dim2_border");
484                 }
485
486                 var args = {};
487                 args[PARAM_OFFSET] = 0;
488
489                 if(is_mr) {
490                         args.page = RRESULT;
491                         args[PARAM_TFORM] = modsFormatToMARC(res);
492                         args[PARAM_MRID] = rec.doc_id();
493
494                 } else {
495                         args.page = RDETAIL
496                         args[PARAM_RID] = rec.doc_id();
497                 }
498
499                 link.setAttribute("href", buildOPACLink(args));
500
501         }
502 }
503
504
505 function resultPageIsDone(pos) {
506         return (recordsHandled == getDisplayCount() 
507                 || recordsHandled + getOffset() == getHitCount());
508 }
509
510 var resultCCHeaderApplied = false;
511
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) {
518
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);
522
523         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
524         var node = nodes[0];
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);
529
530         var lastcell = ccell;
531         var lastheadcell = null;
532
533         var cchead = 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);
539                 lastheadcell = t;
540                 t.appendChild(text(type.opac_label()));
541                 ccrow.appendChild(t);
542                 resultCCHeaderApplied = true;
543         }
544
545         if(nodes[1]) {
546
547                 var x = 1;
548                 var d = findOrgDepth(nodes[1]);
549                 var d2 = findOrgDepth(nodes[nodes.length -1]);
550
551                 for( var i = d; i <= d2 ; i++ ) {
552         
553                         ccell = ccell.cloneNode(true);
554
555                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
556                         //else addCSSClass(ccell, "copy_count_cell_even");
557
558                         var node = nodes[x++];
559                         var type = findOrgType(node.ou_type());
560         
561                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
562                         ccell.title = type.opac_label();
563                         countsrow.insertBefore(ccell, lastcell);
564                         lastcell = ccell;
565
566                         if(ccheadcell) {
567                                 var t = ccheadcell.cloneNode(true);
568                                 t.appendChild(text(type.opac_label()));
569                                 ccrow.insertBefore(t, lastheadcell);
570                                 lastheadcell = t;
571                         }
572                 }
573         }
574
575         unHideMe($("search_info_table"));
576 }
577
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);
584         req.send();
585 }
586
587 function resultHandleCopyCounts(r) {
588         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
589 }
590
591
592 /* display the collected copy counts */
593 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
594         if(copy_counts == null || rec == null) return;
595         var i = 0;
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));
600
601                 if(isXUL()) {
602                         /* here we style opac-invisible records for xul */
603
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; 
608                                         if( cts.count == 0 ) 
609                                                 addCSSClass( row, 'no_copies' );
610                                         else 
611                                                 addCSSClass( row, 'shadowed' );
612                                 }
613                         }
614                 }
615                 i++;
616         }
617 }
618
619