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