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