]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
fixing "opac" format for supercat and unapi
[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-id');
308
309         span.setAttribute(
310                 'title',
311                 'tag:' + cgi.server_name + ',' +
312                         d.getFullYear() +
313                         ':' + type + '/' + id
314         );
315 }
316
317 /* display the record info in the record display table 'pos' is the 
318                 zero based position the record should have in the display table */
319 function resultDisplayRecord(rec, pos, is_mr) {
320
321         if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
322         recordsHandled++;
323         recordsCache.push(rec);
324
325         var r = table.rows[pos + 1];
326
327         
328         try {
329                 var rank = parseFloat(ranks[pos + getOffset()]);
330                 rank            = parseInt( rank * 100 );
331                 var relspan = $n(r, "relevancy_span");
332                 relspan.appendChild(text(rank));
333                 unHideMe(relspan.parentNode);
334         } catch(e){ }
335
336         var pic = $n(r, config.names.result.item_jacket);
337         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
338
339         var title_link = $n(r, config.names.result.item_title);
340         var author_link = $n(r, config.names.result.item_author);
341
342         if( is_mr )  {
343                 var onlyrec = onlyrecord[ getOffset() + pos ];
344                 if(onlyrec) {
345                         buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', onlyrec);
346
347                         var args = {};
348                         args.page = RDETAIL;
349                         args[PARAM_OFFSET] = 0;
350                         args[PARAM_RID] = onlyrec;
351                         args[PARAM_MRID] = rec.doc_id();
352                         pic.parentNode.setAttribute("href", buildOPACLink(args));
353                         title_link.setAttribute("href", buildOPACLink(args));
354                         title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
355                         
356                 } else {
357                         buildunAPISpan($n(r,'unapi'), 'metabib-metarecord', rec.doc_id());
358
359                         buildTitleLink(rec, title_link); 
360                         var args = {};
361                         args.page = RRESULT;
362                         args[PARAM_OFFSET] = 0;
363                         args[PARAM_MRID] = rec.doc_id();
364                         pic.parentNode.setAttribute("href", buildOPACLink(args));
365                 }
366
367         } else {
368                 buildunAPISpan($n(r,'unapi'), 'biblio-record_entry', rec.doc_id());
369
370                 buildTitleDetailLink(rec, title_link); 
371                 var args = {};
372                 args.page = RDETAIL;
373                 args[PARAM_OFFSET] = 0;
374                 args[PARAM_RID] = rec.doc_id();
375                 pic.parentNode.setAttribute("href", buildOPACLink(args));
376
377                 unHideMe($n(r,'place_hold_span'));
378                 $n(r,'place_hold_link').setAttribute(
379                         'href','javascript:holdsDrawWindow("'+rec.doc_id()+'");');
380         }
381
382         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
383
384         if(! is_mr ) {
385         
386                 if(!isNull(rec.edition()))      {
387                         unHideMe( $n(r, "result_table_extra_span"));
388                         $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
389                 }
390                 if(!isNull(rec.pubdate())) {
391                         unHideMe( $n(r, "result_table_extra_span"));
392                         unHideMe($n(r, "result_table_pub_span"));
393                         $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
394                 }
395                 if(!isNull(rec.publisher()) ) {
396                         unHideMe( $n(r, "result_table_extra_span"));
397                         unHideMe($n(r, "result_table_pub_span"));
398                         $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
399                 }
400
401                 if(!isNull(rec.physical_description()) ) {
402                         unHideMe( $n(r, "result_table_extra_span"));
403                         var t = " " + rec.physical_description();
404                         //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
405                         $n(r, "result_table_phys_span").appendChild( text(t));
406                 }
407
408         }
409
410         resultBuildFormatIcons( r, rec, is_mr );
411
412         unHideMe(r);
413         
414         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
415
416         /*
417         if(resultPageIsDone())  {
418                 runEvt('result', 'allRecordsReceived', recordsCache);
419         }
420         */
421 }
422
423 function _resultFindRec(id) {
424         for( var i = 0; i != recordsCache.length; i++ ) {
425                 var rec = recordsCache[i];
426                 if( rec && rec.doc_id() == id )
427                         return rec;
428         }
429         return null;
430 }
431
432
433 function resultBuildFormatIcons( row, rec, is_mr ) {
434
435         var ress = rec.types_of_resource();
436
437         for( var i in ress ) {
438
439                 var res = ress[i];
440                 if(!res) continue;
441
442                 var link = $n(row, res + "_link");
443                 link.title = res;
444                 var img = link.getElementsByTagName("img")[0];
445                 removeCSSClass( img, config.css.dim );
446
447                 var f = getForm();
448                 if( f != "all" ) {
449                         if( f == modsFormatToMARC(res) ) 
450                                 addCSSClass( img, "dim2_border");
451                 }
452
453                 var args = {};
454                 args[PARAM_OFFSET] = 0;
455
456                 if(is_mr) {
457                         args.page = RRESULT;
458                         args[PARAM_TFORM] = modsFormatToMARC(res);
459                         args[PARAM_MRID] = rec.doc_id();
460
461                 } else {
462                         args.page = RDETAIL
463                         args[PARAM_RID] = rec.doc_id();
464                 }
465
466                 link.setAttribute("href", buildOPACLink(args));
467
468         }
469 }
470
471
472 function resultPageIsDone(pos) {
473         return (recordsHandled == getDisplayCount() 
474                 || recordsHandled + getOffset() == getHitCount());
475 }
476
477 var resultCCHeaderApplied = false;
478
479 /* -------------------------------------------------------------------- */
480 /* dynamically add the copy count rows based on the org type 'countsrow' 
481         is the row into which we will add TD's to hold the copy counts 
482         This code generates copy count cells with an id of
483         'copy_count_cell_<depth>_<pagePosition>'  */
484 function resultAddCopyCounts(rec, pagePosition) {
485
486         var r = table.rows[pagePosition + 1];
487         var countsrow = $n(r, config.names.result.counts_row );
488         var ccell = $n(countsrow, config.names.result.count_cell);
489
490         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
491         var node = nodes[0];
492         var type = findOrgType(node.ou_type());
493         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
494         ccell.title = type.opac_label();
495         //addCSSClass(ccell, config.css.result.cc_cell_even);
496
497         var lastcell = ccell;
498         var lastheadcell = null;
499
500         var cchead = null;
501         var ccheadcell = null;
502         if(!resultCCHeaderApplied) {
503                 ccrow = $('result_thead_row');
504                 ccheadcell =  ccrow.removeChild($n(ccrow, "result_thead_ccell"));
505                 var t = ccheadcell.cloneNode(true);
506                 lastheadcell = t;
507                 t.appendChild(text(type.opac_label()));
508                 ccrow.appendChild(t);
509                 resultCCHeaderApplied = true;
510         }
511
512         if(nodes[1]) {
513
514                 var x = 1;
515                 var d = findOrgDepth(nodes[1]);
516                 var d2 = findOrgDepth(nodes[nodes.length -1]);
517
518                 for( var i = d; i <= d2 ; i++ ) {
519         
520                         ccell = ccell.cloneNode(true);
521
522                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
523                         //else addCSSClass(ccell, "copy_count_cell_even");
524
525                         var node = nodes[x++];
526                         var type = findOrgType(node.ou_type());
527         
528                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
529                         ccell.title = type.opac_label();
530                         countsrow.insertBefore(ccell, lastcell);
531                         lastcell = ccell;
532
533                         if(ccheadcell) {
534                                 var t = ccheadcell.cloneNode(true);
535                                 t.appendChild(text(type.opac_label()));
536                                 ccrow.insertBefore(t, lastheadcell);
537                                 lastheadcell = t;
538                         }
539                 }
540         }
541
542         unHideMe($("search_info_table"));
543 }
544
545 /* collect copy counts for a record using method 'methodName' */
546 function resultCollectCopyCounts(rec, pagePosition, methodName) {
547         if(rec == null || rec.doc_id() == null) return;
548         var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
549         req.request.userdata = [ rec, pagePosition ];
550         req.callback(resultHandleCopyCounts);
551         req.send();
552 }
553
554 function resultHandleCopyCounts(r) {
555         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
556 }
557
558
559 /* display the collected copy counts */
560 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
561         if(copy_counts == null || rec == null) return;
562         var i = 0;
563         while(copy_counts[i] != null) {
564                 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
565                 /*
566                 var span = cell.getElementsByTagName("div")[0];
567                 */
568                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
569
570                 i++;
571         }
572 }
573
574