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