]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
b5ab8092c7a249d5aaf94d16913dd898191b8820
[working/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 }
68
69
70 /* display the record info in the record display table 'pos' is the 
71                 zero based position the record should have in the display table */
72 function resultDisplayRecord(rec, pos, is_mr) {
73
74         if(rec == null) rec = new mvr(); /* so the page won't die if there was an error */
75         recordsHandled++;
76         recordsCache.push(rec);
77
78         /* hide the 'now loading...' message */
79         hideMe(G.ui.common.loading);
80
81         var r = table.rows[pos];
82         
83         try {
84                 var rank = parseFloat(ranks[pos + getOffset()]);
85                 rank = ( rank / getTopRank() ) * 100;
86                 rank = parseInt(rank) + "%";
87                 var relspan = findNodeByName(r, "relevancy_span");
88                 relspan.appendChild(text(rank));
89                 unHideMe(relspan.parentNode);
90         } catch(e){ }
91
92         var pic = findNodeByName(r, config.names.result.item_jacket);
93         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
94
95         var title_link = findNodeByName(r, config.names.result.item_title);
96         var author_link = findNodeByName(r, config.names.result.item_author);
97
98         if( is_mr )  {
99                 var onlyrec = onlyrecord[ getOffset() + pos ];
100                 if(onlyrec) {
101                         var id = rec.doc_id();
102                         rec.doc_id(onlyrec);
103                         buildTitleDetailLink(rec, title_link); 
104                         rec.doc_id(id);
105                 } else buildTitleLink(rec, title_link); 
106         } else  buildTitleDetailLink(rec, title_link); 
107
108         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
109
110         resultBuildFormatIcons( r, rec );
111
112         unHideMe(r);
113         
114         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
115
116         if(resultPageIsDone()) 
117                 runEvt('result', 'allRecordsReceived', recordsCache);
118 }
119
120 function resultBuildFormatIcons( row, rec ) {
121
122         var ress = rec.types_of_resource();
123
124         for( var i in ress ) {
125
126                 var res = ress[i];
127                 var link = findNodeByName(row, res + "_link");
128                 link.title = res;
129                 var img = link.getElementsByTagName("img")[0];
130                 removeCSSClass( img, config.css.dim );
131
132                 var f = getForm();
133                 if( f != "all" ) {
134                         if( f != modsFormatToMARC(res) ) 
135                                 addCSSClass( img, config.css.dim2);
136                 }
137
138
139                 var args = {};
140                 args.page = RRESULT;
141                 args[PARAM_OFFSET] = 0;
142                 args[PARAM_MRID] = rec.doc_id();
143                 args[PARAM_FORM] = modsFormatToMARC(res);
144
145                 link.setAttribute("href", buildOPACLink(args));
146
147         }
148
149 }
150
151
152 function resultPageIsDone(pos) {
153         return (recordsHandled == getDisplayCount() 
154                 || recordsHandled + getOffset() == getHitCount());
155 }
156
157 var resultCCHeaderApplied = false;
158
159 /* -------------------------------------------------------------------- */
160 /* dynamically add the copy count rows based on the org type 'countsrow' 
161         is the row into which we will add TD's to hold the copy counts 
162         This code generates copy count cells with an id of
163         'copy_count_cell_<depth>_<pagePosition>'  */
164 function resultAddCopyCounts(rec, pagePosition) {
165
166         var r = table.rows[pagePosition];
167         var countsrow = findNodeByName(r, config.names.result.counts_row );
168         var ccell = findNodeByName(countsrow, config.names.result.count_cell);
169
170         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
171         var node = nodes[0];
172         var type = findOrgType(node.ou_type());
173         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
174         ccell.title = type.opac_label();
175         //addCSSClass(ccell, config.css.result.cc_cell_even);
176
177         var lastcell = ccell;
178         var lastheadcell = null;
179
180         var cchead = null;
181         var ccheadcell = null;
182         if(!resultCCHeaderApplied) {
183                 ccrow = getId('result_thead_row');
184                 ccheadcell =  ccrow.removeChild(findNodeByName(ccrow, "result_thead_ccell"));
185                 var t = ccheadcell.cloneNode(true);
186                 lastheadcell = t;
187                 t.appendChild(text(type.opac_label()));
188                 ccrow.appendChild(t);
189                 resultCCHeaderApplied = true;
190         }
191
192         if(nodes[1]) {
193
194                 var x = 1;
195                 var d = findOrgDepth(nodes[1]);
196                 var d2 = findOrgDepth(nodes[nodes.length -1]);
197
198                 for( var i = d; i <= d2 ; i++ ) {
199         
200                         ccell = ccell.cloneNode(true);
201
202                         //if((i % 2)) removeCSSClass(ccell, "copy_count_cell_even");
203                         //else addCSSClass(ccell, "copy_count_cell_even");
204
205                         var node = nodes[x++];
206                         var type = findOrgType(node.ou_type());
207         
208                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
209                         ccell.title = type.opac_label();
210                         countsrow.insertBefore(ccell, lastcell);
211                         lastcell = ccell;
212
213                         if(ccheadcell) {
214                                 var t = ccheadcell.cloneNode(true);
215                                 t.appendChild(text(type.opac_label()));
216                                 ccrow.insertBefore(t, lastheadcell);
217                                 lastheadcell = t;
218                         }
219                 }
220         }
221 }
222
223 /* collect copy counts for a record using method 'methodName' */
224 function resultCollectCopyCounts(rec, pagePosition, methodName) {
225         if(rec == null || rec.doc_id() == null) return;
226         var req = new Request(methodName, getLocation(), rec.doc_id() );
227         req.request.userdata = [ rec, pagePosition ];
228         req.callback(resultHandleCopyCounts);
229         req.send();
230 }
231
232 function resultHandleCopyCounts(r) {
233         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
234 }
235
236
237 /* display the collected copy counts */
238 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
239         if(copy_counts == null || rec == null) return;
240         var i = 0;
241         while(copy_counts[i] != null) {
242                 var cell = getId("copy_count_cell_" + i +"_" + pagePosition);
243                 var span = cell.getElementsByTagName("div")[0];
244                 span.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
245                 i++;
246         }
247 }
248
249
250 /* captures extraneous info from each record */
251
252 var subjectCache = {};
253 var authorCache = {};
254 var seriesCache = {};
255
256 function resultBuildCaches(records) {
257         for( var r in records ) {
258                 var rec = records[r];
259                 for( var s in rec.subject() ) 
260                         subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
261                 authorCache[rec.author()] = 1;
262                 for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
263         }
264 }
265
266 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
267 function resultDrawSubjects() {
268
269         var subjs = [];
270         for( var s in subjectCache )
271                 subjs.push( { sub : s, count : subjectCache[s] } );
272         subjs.sort(resultSortSubjects);
273
274         var ss = [];
275         for( var s in subjs ) ss.push(subjs[s].sub);
276         resultDrawSidebarStuff(STYPE_SUBJECT, G.ui.sidebar.subject_item,  
277                 config.names.sidebar.subject_item, ss, G.ui.sidebar.subject);
278 }
279
280 function resultDrawAuthors() {
281         var auths = new Array();
282         for( var s in authorCache ) auths.push(s);
283         resultDrawSidebarStuff(STYPE_AUTHOR, G.ui.sidebar.author_item,  
284                 config.names.sidebar.author_item, auths.sort(), G.ui.sidebar.author);
285 }
286
287 function resultDrawSeries() {
288         var sers = new Array();
289         for( var s in seriesCache ) sers.push(s);
290         resultDrawSidebarStuff(STYPE_SERIES, G.ui.sidebar.series_item,  
291                 config.names.sidebar.series_item, sers.sort(), G.ui.sidebar.series);
292 }
293
294 /* search type, template node, href link name, array of text, node to unhide */
295 function resultDrawSidebarStuff(stype, node, linkname, items, wrapperNode) {
296         var parent = node.parentNode;
297         var template = parent.removeChild(node);
298         var x = 0;
299         var newnode = template.cloneNode(true);
300         var found = false;
301         for( var i in items ) {
302                 if(isNull(items[i])) continue;
303                 if(x++ > 7) break;
304                 buildSearchLink(stype, items[i], findNodeByName(newnode, linkname), 100);
305                 parent.appendChild(newnode);
306                 newnode = template.cloneNode(true);
307                 found = true;
308         }
309         if(found) unHideMe(wrapperNode);
310 }
311
312
313
314
315