]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
added phys description to record list page
[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                 if(!isNull(rec.physical_description()) ) {
400                         /*
401                         unHideMe( $n(r, "result_table_extra_span"));
402                         unHideMe($n(r, "result_table_pub_span"));
403                         */
404                         unHideMe( $n(r, "result_table_extra_span"));
405                         var t = " " + rec.physical_description();
406                         //$n(r, "result_table_phys_span").appendChild( text(t.replace(/:.*/g,'')));
407                         $n(r, "result_table_phys_span").appendChild( text(t));
408                 }
409
410         }
411
412         resultBuildFormatIcons( r, rec, is_mr );
413
414         unHideMe(r);
415         
416         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
417
418         /*
419         if(resultPageIsDone())  {
420                 runEvt('result', 'allRecordsReceived', recordsCache);
421         }
422         */
423 }
424
425 function _resultFindRec(id) {
426         for( var i = 0; i != recordsCache.length; i++ ) {
427                 var rec = recordsCache[i];
428                 if( rec && rec.doc_id() == id )
429                         return rec;
430         }
431         return null;
432 }
433
434
435 function resultBuildFormatIcons( row, rec, is_mr ) {
436
437         var ress = rec.types_of_resource();
438
439         for( var i in ress ) {
440
441                 var res = ress[i];
442                 if(!res) continue;
443
444                 var link = $n(row, res + "_link");
445                 link.title = res;
446                 var img = link.getElementsByTagName("img")[0];
447                 removeCSSClass( img, config.css.dim );
448
449                 var f = getForm();
450                 if( f != "all" ) {
451                         if( f == modsFormatToMARC(res) ) 
452                                 addCSSClass( img, "dim2_border");
453                 }
454
455                 var args = {};
456                 args[PARAM_OFFSET] = 0;
457
458                 if(is_mr) {
459                         args.page = RRESULT;
460                         args[PARAM_TFORM] = modsFormatToMARC(res);
461                         args[PARAM_MRID] = rec.doc_id();
462
463                 } else {
464                         args.page = RDETAIL
465                         args[PARAM_RID] = rec.doc_id();
466                 }
467
468                 link.setAttribute("href", buildOPACLink(args));
469
470         }
471 }
472
473
474 function resultPageIsDone(pos) {
475         return (recordsHandled == getDisplayCount() 
476                 || recordsHandled + getOffset() == getHitCount());
477 }
478
479 var resultCCHeaderApplied = false;
480
481 /* -------------------------------------------------------------------- */
482 /* dynamically add the copy count rows based on the org type 'countsrow' 
483         is the row into which we will add TD's to hold the copy counts 
484         This code generates copy count cells with an id of
485         'copy_count_cell_<depth>_<pagePosition>'  */
486 function resultAddCopyCounts(rec, pagePosition) {
487
488         var r = table.rows[pagePosition + 1];
489         var countsrow = $n(r, config.names.result.counts_row );
490         var ccell = $n(countsrow, config.names.result.count_cell);
491
492         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
493         var node = nodes[0];
494         var type = findOrgType(node.ou_type());
495         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
496         ccell.title = type.opac_label();
497         //addCSSClass(ccell, config.css.result.cc_cell_even);
498
499         var lastcell = ccell;
500         var lastheadcell = null;
501
502         var cchead = null;
503         var ccheadcell = null;
504         if(!resultCCHeaderApplied) {
505                 ccrow = $('result_thead_row');
506                 ccheadcell =  ccrow.removeChild($n(ccrow, "result_thead_ccell"));
507                 var t = ccheadcell.cloneNode(true);
508                 lastheadcell = t;
509                 t.appendChild(text(type.opac_label()));
510                 ccrow.appendChild(t);
511                 resultCCHeaderApplied = true;
512         }
513
514         if(nodes[1]) {
515
516                 var x = 1;
517                 var d = findOrgDepth(nodes[1]);
518                 var d2 = findOrgDepth(nodes[nodes.length -1]);
519
520                 for( var i = d; i <= d2 ; i++ ) {
521         
522                         ccell = ccell.cloneNode(true);
523
524                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
525                         //else addCSSClass(ccell, "copy_count_cell_even");
526
527                         var node = nodes[x++];
528                         var type = findOrgType(node.ou_type());
529         
530                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
531                         ccell.title = type.opac_label();
532                         countsrow.insertBefore(ccell, lastcell);
533                         lastcell = ccell;
534
535                         if(ccheadcell) {
536                                 var t = ccheadcell.cloneNode(true);
537                                 t.appendChild(text(type.opac_label()));
538                                 ccrow.insertBefore(t, lastheadcell);
539                                 lastheadcell = t;
540                         }
541                 }
542         }
543
544         unHideMe($("search_info_table"));
545 }
546
547 /* collect copy counts for a record using method 'methodName' */
548 function resultCollectCopyCounts(rec, pagePosition, methodName) {
549         if(rec == null || rec.doc_id() == null) return;
550         var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
551         req.request.userdata = [ rec, pagePosition ];
552         req.callback(resultHandleCopyCounts);
553         req.send();
554 }
555
556 function resultHandleCopyCounts(r) {
557         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
558 }
559
560
561 /* display the collected copy counts */
562 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
563         if(copy_counts == null || rec == null) return;
564         var i = 0;
565         while(copy_counts[i] != null) {
566                 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
567                 /*
568                 var span = cell.getElementsByTagName("div")[0];
569                 */
570                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
571
572                 i++;
573         }
574 }
575
576