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