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