]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
a few small patchups here and there for handling the new SSI directory structure
[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 /* set the search result info, number of hits, which results we're 
30         displaying, links to the next/prev pages, etc. */
31 function resultSetHitInfo() { 
32         if( findCurrentPage() == MRESULT ) {
33                 if(getHitCount() <= lowHitCount)
34                         runEvt('result', 'lowHits');
35                 if(getHitCount() == 0) {
36                         runEvt('result', 'zeroHits');
37                         return;
38                 }
39         }
40
41         var c;  
42         if( getDisplayCount() > (getHitCount() - getOffset()))  c = getHitCount();
43         else c = getDisplayCount() + getOffset();
44
45         var pages = getHitCount() / getDisplayCount();
46         if(pages % 1) pages = parseInt(pages) + 1;
47
48         G.ui.result.current_page.appendChild(text( (getOffset()/getDisplayCount()) + 1));
49         G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
50
51         G.ui.result.offset_start.appendChild(text(getOffset() + 1));
52         G.ui.result.offset_end.appendChild(text(c));
53         G.ui.result.result_count.appendChild(text(getHitCount()));
54         unHideMe(G.ui.result.info);
55 }
56
57 function resultLowHits() {
58         try{searchTimer.stop()}catch(e){}
59         showCanvas();
60         unHideMe($('result_low_hits'));
61         if(getHitCount() > 0)
62                 unHideMe($('result_low_hits_msg'));
63
64         var sreq = new Request(CHECK_SPELL, getTerm());
65         sreq.callback(resultSuggestSpelling);
66         sreq.send();
67
68         var words = getTerm().split(' ');
69         var word;
70         while( word = words.shift() ) {
71                 var areq = new Request(FETCH_CROSSREF, getStype(), getTerm() );
72                 areq.callback(resultLowHitXRef);
73                 areq.send();
74         }
75
76         if( !(getForm() == null || getForm() == 'all' || getForm == "") ) {
77                 var a = {};
78                 a[PARAM_FORM] = "all";
79                 $('low_hits_remove_format_link').setAttribute('href',buildOPACLink(a));
80                 unHideMe($('low_hits_remove_format'));
81         }
82
83         resultSuggestSearchClass();
84 }
85
86 var lowHitsXRefLink;
87 var lowHitsXRefLinkParent;
88 function resultLowHitXRef(r) {
89         if(!lowHitsXRefLink){
90                 lowHitsXRefLinkParent = $('low_hits_xref_link').parentNode;
91                 lowHitsXRefLink = lowHitsXRefLinkParent.removeChild($('low_hits_xref_link'));
92         }
93         var res = r.getResultObject();
94         var arr = res.from;
95         arr.concat(res.also);
96         if(arr && arr.length > 0) {
97                 unHideMe($('low_hits_cross_ref'));
98                 var word;
99                 var c = 0;
100                 while( word = arr.shift() ) {
101                         if(c++ > 20) break;
102                         var a = {};
103                         a[PARAM_TERM] = word;
104                         var template = lowHitsXRefLink.cloneNode(true);
105                         template.setAttribute('href',buildOPACLink(a));
106                         template.appendChild(text(word));
107                         lowHitsXRefLinkParent.appendChild(template);
108                         lowHitsXRefLinkParent.appendChild(text(' '));
109                 }
110         }
111 }
112
113 function resultZeroHits() {
114         unHideMe($('result_zero_hits_msg'));
115         resultExpandSearch();
116 }
117
118 function resultExpandSearch() {
119         var top = findOrgDepth(globalOrgTree);
120         if(getDepth() == top) return;
121         unHideMe($('low_hits_expand_range'));
122         var par = $('low_hits_expand_link').parentNode;
123         var template = par.removeChild($('low_hits_expand_link'));
124
125         var bottom = getDepth();
126         while( top < bottom ) {
127                 var a = {};
128                 a[PARAM_DEPTH] = top;
129                 var temp = template.cloneNode(true);
130                 temp.appendChild(text(findOrgTypeFromDepth(top).opac_label()))
131                 temp.setAttribute('href',buildOPACLink(a));
132                 par.appendChild(temp);
133                 top++;
134         }
135 }
136
137 function resultSuggestSearchClass() {
138         var stype = getStype();
139         if(stype == STYPE_KEYWORD) return;
140         var a = {}; var ref;
141         unHideMe($('low_hits_search_type'));
142         if(stype != STYPE_TITLE) {
143                 ref = $('low_hits_title_search');
144                 unHideMe(ref);
145                 a[PARAM_STYPE] = STYPE_TITLE;
146                 ref.setAttribute('href',buildOPACLink(a));
147         }
148         if(stype != STYPE_AUTHOR) {
149                 ref = $('low_hits_author_search');
150                 unHideMe(ref);
151                 a[PARAM_STYPE] = STYPE_AUTHOR;
152                 ref.setAttribute('href',buildOPACLink(a));
153         }
154         if(stype != STYPE_SUBJECT) {
155                 ref = $('low_hits_subject_search');
156                 unHideMe(ref);
157                 a[PARAM_STYPE] = STYPE_SUBJECT;
158                 ref.setAttribute('href',buildOPACLink(a));
159         }
160         if(stype != STYPE_KEYWORD) {
161                 ref = $('low_hits_keyword_search');
162                 unHideMe(ref);
163                 a[PARAM_STYPE] = STYPE_KEYWORD;
164                 ref.setAttribute('href',buildOPACLink(a));
165         }
166         if(stype != STYPE_SERIES) {
167                 ref = $('low_hits_series_search');
168                 unHideMe(ref);
169                 a[PARAM_STYPE] = STYPE_SERIES;
170                 ref.setAttribute('href',buildOPACLink(a));
171         }
172 }
173
174 function resultSuggestSpelling(r) {
175         var res = r.getResultObject();
176         if(res) {
177                 unHideMe($('did_you_mean'));
178                 var arg = {};
179                 arg[PARAM_TERM] = res;
180                 $('spell_check_link').setAttribute(
181                         'href', buildOPACLink(arg) );
182                 $('spell_check_link').appendChild(text(res));
183         }
184 }
185
186
187 function resultPaginate() {
188         var o = getOffset();
189
190         if( !((o + getDisplayCount()) >= getHitCount()) ) {
191
192                 var args = {};
193                 args[PARAM_OFFSET] = o + getDisplayCount();
194                 G.ui.result.next_link.setAttribute("href", buildOPACLink(args)); 
195                 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
196
197                 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
198                 G.ui.result.end_link.setAttribute("href", buildOPACLink(args)); 
199                 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
200         }
201
202         if( o > 0 ) {
203
204                 var args = {};
205                 args[PARAM_OFFSET] = o - getDisplayCount();
206                 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args)); 
207                 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
208
209                 args[PARAM_OFFSET] = 0;
210                 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args)); 
211                 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
212         }
213         if(getDisplayCount() < getHitCount())
214                 unHideMe($('start_end_links_span'));
215
216         showCanvas();
217         try{searchTimer.stop()}catch(e){}
218 }
219
220
221 /* display the record info in the record display table 'pos' is the 
222                 zero based position the record should have in the display table */
223 function resultDisplayRecord(rec, pos, is_mr) {
224
225         if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
226         recordsHandled++;
227         recordsCache.push(rec);
228
229         var r = table.rows[pos + 1];
230         
231         try {
232                 var rank = parseFloat(ranks[pos + getOffset()]);
233                 rank = ( rank / getTopRank() ) * 100;
234                 rank = parseInt(rank) + "%";
235                 var relspan = $n(r, "relevancy_span");
236                 relspan.appendChild(text(rank));
237                 unHideMe(relspan.parentNode);
238         } catch(e){ }
239
240         var pic = $n(r, config.names.result.item_jacket);
241         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
242
243         var title_link = $n(r, config.names.result.item_title);
244         var author_link = $n(r, config.names.result.item_author);
245
246         if( is_mr )  {
247                 var onlyrec = onlyrecord[ getOffset() + pos ];
248                 if(onlyrec) {
249                         var args = {};
250                         args.page = RDETAIL;
251                         args[PARAM_OFFSET] = 0;
252                         args[PARAM_RID] = onlyrec;
253                         args[PARAM_MRID] = rec.doc_id();
254                         pic.parentNode.setAttribute("href", buildOPACLink(args));
255                         title_link.setAttribute("href", buildOPACLink(args));
256                         title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
257                         
258                 } else {
259                         buildTitleLink(rec, title_link); 
260                         var args = {};
261                         args.page = RRESULT;
262                         args[PARAM_OFFSET] = 0;
263                         args[PARAM_MRID] = rec.doc_id();
264                         pic.parentNode.setAttribute("href", buildOPACLink(args));
265                 }
266
267         } else {
268                 buildTitleDetailLink(rec, title_link); 
269                 var args = {};
270                 args.page = RDETAIL;
271                 args[PARAM_OFFSET] = 0;
272                 args[PARAM_RID] = rec.doc_id();
273                 pic.parentNode.setAttribute("href", buildOPACLink(args));
274
275                 unHideMe($n(r,'place_hold_span'));
276                 $n(r,'place_hold_link').setAttribute(
277                         'href','javascript:holdsDrawWindow("'+rec.doc_id()+'");');
278         }
279
280         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
281
282         if(! is_mr ) {
283         
284                 if(!isNull(rec.edition()))      {
285                         unHideMe( $n(r, "result_table_extra_span"));
286                         $n(r, "result_table_edition_span").appendChild( text( rec.edition()) );
287                 }
288                 if(!isNull(rec.pubdate())) {
289                         unHideMe( $n(r, "result_table_extra_span"));
290                         unHideMe($n(r, "result_table_pub_span"));
291                         $n(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
292                 }
293                 if(!isNull(rec.publisher()) ) {
294                         unHideMe( $n(r, "result_table_extra_span"));
295                         unHideMe($n(r, "result_table_pub_span"));
296                         $n(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
297                 }
298         }
299
300         resultBuildFormatIcons( r, rec );
301
302         unHideMe(r);
303         
304         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
305
306         if(resultPageIsDone())  {
307                 //showCanvas();
308                 /* hide the 'now loading...' message */
309                 /*
310                 hideMe(G.ui.common.loading);
311                 */
312                 runEvt('result', 'allRecordsReceived', recordsCache);
313         }
314 }
315
316 function _resultFindRec(id) {
317         for( var i = 0; i != recordsCache.length; i++ ) {
318                 var rec = recordsCache[i];
319                 if( rec && rec.doc_id() == id )
320                         return rec;
321         }
322         return null;
323 }
324
325
326 function resultBuildFormatIcons( row, rec ) {
327
328         var ress = rec.types_of_resource();
329
330         for( var i in ress ) {
331
332                 var res = ress[i];
333                 var link = $n(row, res + "_link");
334                 link.title = res;
335                 var img = link.getElementsByTagName("img")[0];
336                 removeCSSClass( img, config.css.dim );
337
338                 var f = getForm();
339                 if( f != "all" ) {
340                         if( f == modsFormatToMARC(res) ) 
341                                 addCSSClass( img, "dim2_border");
342                 }
343
344                 var args = {};
345                 args.page = RRESULT;
346                 args[PARAM_OFFSET] = 0;
347                 args[PARAM_MRID] = rec.doc_id();
348                 args[PARAM_FORM] = modsFormatToMARC(res);
349
350                 link.setAttribute("href", buildOPACLink(args));
351
352         }
353 }
354
355
356 function resultPageIsDone(pos) {
357         return (recordsHandled == getDisplayCount() 
358                 || recordsHandled + getOffset() == getHitCount());
359 }
360
361 var resultCCHeaderApplied = false;
362
363 /* -------------------------------------------------------------------- */
364 /* dynamically add the copy count rows based on the org type 'countsrow' 
365         is the row into which we will add TD's to hold the copy counts 
366         This code generates copy count cells with an id of
367         'copy_count_cell_<depth>_<pagePosition>'  */
368 function resultAddCopyCounts(rec, pagePosition) {
369
370         var r = table.rows[pagePosition + 1];
371         var countsrow = $n(r, config.names.result.counts_row );
372         var ccell = $n(countsrow, config.names.result.count_cell);
373
374         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
375         var node = nodes[0];
376         var type = findOrgType(node.ou_type());
377         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
378         ccell.title = type.opac_label();
379         //addCSSClass(ccell, config.css.result.cc_cell_even);
380
381         var lastcell = ccell;
382         var lastheadcell = null;
383
384         var cchead = null;
385         var ccheadcell = null;
386         if(!resultCCHeaderApplied) {
387                 ccrow = $('result_thead_row');
388                 ccheadcell =  ccrow.removeChild($n(ccrow, "result_thead_ccell"));
389                 var t = ccheadcell.cloneNode(true);
390                 lastheadcell = t;
391                 t.appendChild(text(type.opac_label()));
392                 ccrow.appendChild(t);
393                 resultCCHeaderApplied = true;
394         }
395
396         if(nodes[1]) {
397
398                 var x = 1;
399                 var d = findOrgDepth(nodes[1]);
400                 var d2 = findOrgDepth(nodes[nodes.length -1]);
401
402                 for( var i = d; i <= d2 ; i++ ) {
403         
404                         ccell = ccell.cloneNode(true);
405
406                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
407                         //else addCSSClass(ccell, "copy_count_cell_even");
408
409                         var node = nodes[x++];
410                         var type = findOrgType(node.ou_type());
411         
412                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
413                         ccell.title = type.opac_label();
414                         countsrow.insertBefore(ccell, lastcell);
415                         lastcell = ccell;
416
417                         if(ccheadcell) {
418                                 var t = ccheadcell.cloneNode(true);
419                                 t.appendChild(text(type.opac_label()));
420                                 ccrow.insertBefore(t, lastheadcell);
421                                 lastheadcell = t;
422                         }
423                 }
424         }
425
426         unHideMe($("search_info_table"));
427 }
428
429 /* collect copy counts for a record using method 'methodName' */
430 function resultCollectCopyCounts(rec, pagePosition, methodName) {
431         if(rec == null || rec.doc_id() == null) return;
432         var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
433         req.request.userdata = [ rec, pagePosition ];
434         req.callback(resultHandleCopyCounts);
435         req.send();
436 }
437
438 function resultHandleCopyCounts(r) {
439         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
440 }
441
442
443 /* display the collected copy counts */
444 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
445         if(copy_counts == null || rec == null) return;
446         var i = 0;
447         while(copy_counts[i] != null) {
448                 var cell = $("copy_count_cell_" + i +"_" + pagePosition);
449                 /*
450                 var span = cell.getElementsByTagName("div")[0];
451                 */
452                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
453
454                 i++;
455         }
456 }
457
458