]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
start << >> end
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / result_common.js
1
2 var recordsHandled = 0;
3 var recordsCache = [];
4
5 /* set up the event handlers */
6 G.evt.result.hitCountReceived.push(resultSetHitInfo);
7 G.evt.result.recordReceived.push(resultDisplayRecord, resultAddCopyCounts);
8 G.evt.result.copyCountsReceived.push(resultDisplayCopyCounts);
9 G.evt.result.allRecordsReceived.push(resultBuildCaches, resultDrawSubjects, resultDrawAuthors, resultDrawSeries);
10
11 /* do this after we have ID's so the rank for mr pages will be correct */
12 attachEvt("result", "preCollectRecords", resultPaginate);
13
14 /* returns the last 'index' postion ocurring in this page */
15 function resultFinalPageIndex() {
16         if(getHitCount() < (getOffset() + getDisplayCount())) 
17                 return getHitCount() - 1;
18         return getOffset() + getDisplayCount() - 1;
19 }
20
21 /* set the search result info, number of hits, which results we're 
22         displaying, links to the next/prev pages, etc. */
23 function resultSetHitInfo() { 
24         var c;  
25         if( getDisplayCount() > (getHitCount() - getOffset()))  c = getHitCount();
26         else c = getDisplayCount() + getOffset();
27
28         var pages = getHitCount() / getDisplayCount();
29         if(pages % 1) pages = parseInt(pages) + 1;
30
31         G.ui.result.current_page.appendChild(text( (getOffset()/getDisplayCount()) + 1));
32         G.ui.result.num_pages.appendChild(text(pages + ")")); /* the ) is dumb */
33
34         G.ui.result.offset_start.appendChild(text(getOffset() + 1));
35         G.ui.result.offset_end.appendChild(text(c));
36         G.ui.result.result_count.appendChild(text(getHitCount()));
37         unHideMe(G.ui.result.info);
38 }
39
40
41 function resultPaginate() {
42         var o = getOffset();
43
44         if( !((o + getDisplayCount()) >= getHitCount()) ) {
45
46                 var args = {};
47                 args[PARAM_OFFSET] = o + getDisplayCount();
48                 G.ui.result.next_link.setAttribute("href", buildOPACLink(args)); 
49                 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
50
51                 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
52                 G.ui.result.end_link.setAttribute("href", buildOPACLink(args)); 
53                 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
54         }
55
56         if( o > 0 ) {
57
58                 var args = {};
59                 args[PARAM_OFFSET] = o - getDisplayCount();
60                 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args)); 
61                 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
62
63                 args[PARAM_OFFSET] = 0;
64                 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args)); 
65                 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
66         }
67         if(getDisplayCount() < getHitCount())
68                 unHideMe($('start_end_links_span'));
69 }
70
71
72 /* display the record info in the record display table 'pos' is the 
73                 zero based position the record should have in the display table */
74 function resultDisplayRecord(rec, pos, is_mr) {
75
76         if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
77         recordsHandled++;
78         recordsCache.push(rec);
79
80         var r = table.rows[pos + 1];
81         
82         try {
83                 var rank = parseFloat(ranks[pos + getOffset()]);
84                 rank = ( rank / getTopRank() ) * 100;
85                 rank = parseInt(rank) + "%";
86                 var relspan = findNodeByName(r, "relevancy_span");
87                 relspan.appendChild(text(rank));
88                 unHideMe(relspan.parentNode);
89         } catch(e){ }
90
91         var pic = findNodeByName(r, config.names.result.item_jacket);
92         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
93
94         var title_link = findNodeByName(r, config.names.result.item_title);
95         var author_link = findNodeByName(r, config.names.result.item_author);
96
97         if( is_mr )  {
98                 var onlyrec = onlyrecord[ getOffset() + pos ];
99                 if(onlyrec) {
100                         /*
101                         var id = rec.doc_id();
102                         rec.doc_id(onlyrec);
103                         buildTitleDetailLink(rec, title_link); 
104                         rec.doc_id(id);
105                         */
106
107                         var args = {};
108                         args.page = RDETAIL;
109                         args[PARAM_OFFSET] = 0;
110                         args[PARAM_RID] = onlyrec;
111                         args[PARAM_MRID] = rec.doc_id();
112                         pic.parentNode.setAttribute("href", buildOPACLink(args));
113                         title_link.setAttribute("href", buildOPACLink(args));
114                         title_link.appendChild(text(normalize(truncate(rec.title(), 65))));
115                         
116                 } else {
117                         buildTitleLink(rec, title_link); 
118                         var args = {};
119                         args.page = RRESULT;
120                         args[PARAM_OFFSET] = 0;
121                         args[PARAM_MRID] = rec.doc_id();
122                         pic.parentNode.setAttribute("href", buildOPACLink(args));
123                 }
124
125         } else {
126                 buildTitleDetailLink(rec, title_link); 
127                 var args = {};
128                 args.page = RDETAIL;
129                 args[PARAM_OFFSET] = 0;
130                 args[PARAM_RID] = rec.doc_id();
131                 pic.parentNode.setAttribute("href", buildOPACLink(args));
132         }
133
134         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
135
136         if(! is_mr ) {
137         
138                 if(!isNull(rec.edition()))      {
139                         unHideMe( findNodeByName(r, "result_table_extra_span"));
140                         findNodeByName(r, "result_table_edition_span").appendChild( text( rec.edition()) );
141                 }
142                 if(!isNull(rec.pubdate())) {
143                         unHideMe( findNodeByName(r, "result_table_extra_span"));
144                         unHideMe(findNodeByName(r, "result_table_pub_span"));
145                         findNodeByName(r, "result_table_pub_span").appendChild( text( rec.pubdate() ));
146                 }
147                 if(!isNull(rec.publisher()) ) {
148                         unHideMe( findNodeByName(r, "result_table_extra_span"));
149                         unHideMe(findNodeByName(r, "result_table_pub_span"));
150                         findNodeByName(r, "result_table_pub_span").appendChild( text( " " + rec.publisher() ));
151                 }
152         }
153
154         resultBuildFormatIcons( r, rec );
155
156         unHideMe(r);
157         
158         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
159
160         if(resultPageIsDone())  {
161                 /* hide the 'now loading...' message */
162                 /*
163                 hideMe(G.ui.common.loading);
164                 */
165                 runEvt('result', 'allRecordsReceived', recordsCache);
166         }
167 }
168
169 function resultBuildFormatIcons( row, rec ) {
170
171         var ress = rec.types_of_resource();
172
173         for( var i in ress ) {
174
175                 var res = ress[i];
176                 var link = findNodeByName(row, res + "_link");
177                 link.title = res;
178                 var img = link.getElementsByTagName("img")[0];
179                 removeCSSClass( img, config.css.dim );
180
181                 var f = getForm();
182                 if( f != "all" ) {
183                         /*
184                         if( f != modsFormatToMARC(res) ) 
185                                 addCSSClass( img, config.css.dim2);
186                         else
187                                 addCSSClass( img, "dim2_border");
188                                 */
189                         if( f == modsFormatToMARC(res) ) 
190                                 addCSSClass( img, "dim2_border");
191                 }
192
193
194                 var args = {};
195                 args.page = RRESULT;
196                 args[PARAM_OFFSET] = 0;
197                 args[PARAM_MRID] = rec.doc_id();
198                 args[PARAM_FORM] = modsFormatToMARC(res);
199
200                 link.setAttribute("href", buildOPACLink(args));
201
202         }
203
204 }
205
206
207 function resultPageIsDone(pos) {
208         return (recordsHandled == getDisplayCount() 
209                 || recordsHandled + getOffset() == getHitCount());
210 }
211
212 var resultCCHeaderApplied = false;
213
214 /* -------------------------------------------------------------------- */
215 /* dynamically add the copy count rows based on the org type 'countsrow' 
216         is the row into which we will add TD's to hold the copy counts 
217         This code generates copy count cells with an id of
218         'copy_count_cell_<depth>_<pagePosition>'  */
219 function resultAddCopyCounts(rec, pagePosition) {
220
221         var r = table.rows[pagePosition + 1];
222         var countsrow = findNodeByName(r, config.names.result.counts_row );
223         var ccell = findNodeByName(countsrow, config.names.result.count_cell);
224
225         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
226         var node = nodes[0];
227         var type = findOrgType(node.ou_type());
228         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
229         ccell.title = type.opac_label();
230         //addCSSClass(ccell, config.css.result.cc_cell_even);
231
232         var lastcell = ccell;
233         var lastheadcell = null;
234
235         var cchead = null;
236         var ccheadcell = null;
237         if(!resultCCHeaderApplied) {
238                 ccrow = getId('result_thead_row');
239                 ccheadcell =  ccrow.removeChild(findNodeByName(ccrow, "result_thead_ccell"));
240                 var t = ccheadcell.cloneNode(true);
241                 lastheadcell = t;
242                 t.appendChild(text(type.opac_label()));
243                 ccrow.appendChild(t);
244                 resultCCHeaderApplied = true;
245         }
246
247         if(nodes[1]) {
248
249                 var x = 1;
250                 var d = findOrgDepth(nodes[1]);
251                 var d2 = findOrgDepth(nodes[nodes.length -1]);
252
253                 for( var i = d; i <= d2 ; i++ ) {
254         
255                         ccell = ccell.cloneNode(true);
256
257                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
258                         //else addCSSClass(ccell, "copy_count_cell_even");
259
260                         var node = nodes[x++];
261                         var type = findOrgType(node.ou_type());
262         
263                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
264                         ccell.title = type.opac_label();
265                         countsrow.insertBefore(ccell, lastcell);
266                         lastcell = ccell;
267
268                         if(ccheadcell) {
269                                 var t = ccheadcell.cloneNode(true);
270                                 t.appendChild(text(type.opac_label()));
271                                 ccrow.insertBefore(t, lastheadcell);
272                                 lastheadcell = t;
273                         }
274                 }
275         }
276
277         unHideMe(getId("search_info_table"));
278 }
279
280 /* collect copy counts for a record using method 'methodName' */
281 function resultCollectCopyCounts(rec, pagePosition, methodName) {
282         if(rec == null || rec.doc_id() == null) return;
283         var req = new Request(methodName, getLocation(), rec.doc_id(), getForm() );
284         req.request.userdata = [ rec, pagePosition ];
285         req.callback(resultHandleCopyCounts);
286         req.send();
287 }
288
289 function resultHandleCopyCounts(r) {
290         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
291 }
292
293
294 /* display the collected copy counts */
295 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
296         if(copy_counts == null || rec == null) return;
297         var i = 0;
298         while(copy_counts[i] != null) {
299                 var cell = getId("copy_count_cell_" + i +"_" + pagePosition);
300                 /*
301                 var span = cell.getElementsByTagName("div")[0];
302                 */
303                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
304
305                 i++;
306         }
307 }
308
309
310 /* captures extraneous info from each record */
311
312 var subjectCache = {};
313 var authorCache = {};
314 var seriesCache = {};
315
316 function resultBuildCaches(records) {
317         for( var r in records ) {
318                 var rec = records[r];
319                 for( var s in rec.subject() ) 
320                         subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
321                 authorCache[rec.author()] = 1;
322                 for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
323         }
324 }
325
326 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
327 function resultDrawSubjects() {
328
329         var subjs = [];
330         for( var s in subjectCache )
331                 subjs.push( { sub : s, count : subjectCache[s] } );
332         subjs.sort(resultSortSubjects);
333
334         var ss = [];
335         for( var s in subjs ) ss.push(subjs[s].sub);
336
337         resultDrawSidebarTrees( 
338                 STYPE_SUBJECT, 
339                 "subjectSidebarTree", ss, 
340                 getId("subject_tree_sidebar"), 
341                 getId("subject_sidebar_tree_div") );
342 }
343
344 function resultDrawAuthors() {
345         var auths = new Array();
346         for( var s in authorCache ) auths.push(s);
347
348         resultDrawSidebarTrees( 
349                 STYPE_AUTHOR, 
350                 "authorSidebarTree", auths.sort(), 
351                 getId("author_tree_sidebar"), 
352                 getId("author_sidebar_tree_div") );
353 }
354
355 function resultDrawSeries() {
356         var sers = new Array();
357         for( var s in seriesCache ) sers.push(s);
358         resultDrawSidebarTrees( 
359                 STYPE_SERIES, 
360                 "seriesSidebarTree", sers.sort(), 
361                 getId("series_tree_sidebar"), 
362                 getId("series_sidebar_tree_div") );
363 }
364
365 function resultDrawSidebarTrees( stype, treeName, items, wrapperNode, destNode ) {
366         var tree;
367         eval("tree = " + treeName);
368
369         var found = false;
370         var x = 0;
371         for( var i in items ) {
372
373                 if(isNull(items[i])) continue;
374                 if(x++ > 7) break;
375                 found = true;
376
377                 var item = normalize(truncate(items[i], 65));
378                 var trunc = 65;
379                 var args = {};
380                 var href = resultQuickLink( items[i], stype );
381                 tree.addNode( stype + "_" + items[i], treeName + 'Root', item, href );
382
383                 /*
384                 if(!IE)
385                         setTimeout('resultFireXRefReq("'+treeName+'","'+stype+'","'+item+'");',200);
386                         */
387                 if(!IE) resultFireXRefReq(treeName, stype, items[i]);
388         }
389
390         if(found) {
391                 unHideMe(wrapperNode);
392                 //tree.close(tree.rootid);
393         }
394 }
395
396 function resultFireXRefReq( treeName, stype, item ) {
397         var tree;
398         eval('tree=' + treeName);
399         var req = new Request(FETCH_CROSSREF, stype, item);
400         req.request._tree = tree;
401         req.request._item = item;
402         req.request._stype = stype;
403         req.callback(resultAppendCrossRef);
404         req.send();
405 }
406
407
408 function resultQuickLink( term, type ) {
409         var args = {};
410         args.page = MRESULT;
411         args[PARAM_OFFSET] = 0;
412         args[PARAM_TERM] = term;
413         args[PARAM_STYPE] = type;
414         return buildOPACLink(args);
415 }
416
417
418 function resultAppendCrossRef(r) {
419         var tree                = r._tree
420         var item                = r._item
421         var stype       = r._stype;
422         var result      = r.getResultObject();
423         if(!result) return;
424         var froms       = result['from'];
425         var alsos       = result['also'];
426
427         var total = 0;
428
429         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
430                 var string = normalize(truncate(froms[i], 45));
431                 if(getId(stype + '_' + froms[i])) continue;
432                 tree.addNode(stype + '_' + froms[i], 
433                         stype + '_' + item, string, resultQuickLink(froms[i],stype));
434         }
435         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
436                 var string = normalize(truncate(alsos[i], 45));
437                 if(getId(stype + '_' + alsos[i])) continue;
438                 tree.addNode(stype + '_' + alsos[i], 
439                         stype + '_' + item, string, resultQuickLink(alsos[i],stype));
440         }
441 }
442
443
444
445
446