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