]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/lib/js/opac/result_common.js
d0e844c26526b1707fa17fe55d92afaf34afef31
[Evergreen.git] / Open-ILS / src / javascript / lib / js / opac / result_common.js
1 var subjectCache = {};
2 var authorCache = {};
3 var seriesCache = {};
4 var recordsHandled = 0;
5
6 function resultFinalPageIndex() {
7         if(getHitCount() < (getOffset() + getDisplayCount())) 
8                 return getHitCount() - 1;
9         return getOffset() + getDisplayCount() - 1;
10 }
11
12 /* set the search result info, number of hits, which results we're 
13         displaying, links to the next/prev pages, etc. */
14 function resultSetInfo() { 
15         var c;  
16         if( getDisplayCount() > (getHitCount() - getOffset()))  c = getHitCount();
17         else c = getDisplayCount() + getOffset();
18
19         var pages = getHitCount() / getDisplayCount();
20         if(pages % 1) pages = parseInt(pages) + 1;
21
22         G.ui.result.current_page.appendChild(text( (getOffset()/getDisplayCount()) + 1));
23         G.ui.result.num_pages.appendChild(text(pages + ")"));
24
25         var o = getOffset();
26
27         if( !((o + getDisplayCount()) >= getHitCount()) ) {
28
29                 var args = {};
30                 args[PARAM_OFFSET] = o + getDisplayCount();
31                 G.ui.result.next_link.setAttribute("href", buildOPACLink(args)); 
32                 addCSSClass(G.ui.result.next_link, config.css.result.nav_active);
33
34                 args[PARAM_OFFSET] = getHitCount() - (getHitCount() % getDisplayCount());
35                 G.ui.result.end_link.setAttribute("href", buildOPACLink(args)); 
36                 addCSSClass(G.ui.result.end_link, config.css.result.nav_active);
37         }
38
39         if( o > 0 ) {
40
41                 var args = {};
42                 args[PARAM_OFFSET] = o - getDisplayCount();
43                 G.ui.result.prev_link.setAttribute( "href", buildOPACLink(args)); 
44                 addCSSClass(G.ui.result.prev_link, config.css.result.nav_active);
45
46                 args[PARAM_OFFSET] = 0;
47                 G.ui.result.home_link.setAttribute( "href", buildOPACLink(args)); 
48                 addCSSClass(G.ui.result.home_link, config.css.result.nav_active);
49         }
50
51         G.ui.result.offset_start.appendChild(text(o + 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
58
59 /* display the record info in the record display table 
60         'pos' is the zero based position the record should have in the
61         display table */
62 function resultDisplayRecord(rec, rowtemplate, pos, is_mr) {
63
64         if(rec == null) rec = new mvr(); /* so the page won't die */
65         recordsHandled++;
66
67         /* hide the 'now loading...' message */
68         hideMe(G.ui.common.loading);
69
70         var r = table.rows[pos];
71
72         var pic = findNodeByName(r, config.names.result.item_jacket);
73         pic.setAttribute("src", buildISBNSrc(cleanISBN(rec.isbn())));
74
75         var title_link = findNodeByName(r, config.names.result.item_title);
76         var author_link = findNodeByName(r, config.names.result.item_author);
77
78         if( is_mr )  buildTitleLink(rec, title_link); 
79         else  buildTitleDetailLink(rec, title_link); 
80         buildSearchLink(STYPE_AUTHOR, rec.author(), author_link);
81
82         /* grab subjects, authors, and series from the record */
83         for( var s in rec.subject() ) 
84                 subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
85         authorCache[rec.author()] = 1;
86         for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
87
88         if(resultPageIsDone() && !subjectsAreDrawn) {
89                 subjectsAreDrawn = true;
90                 resultDrawSubjects();
91                 resultDrawAuthors();
92                 resultDrawSeries();
93         }
94
95
96         var countsrow = findNodeByName(r, config.names.result.counts_row);
97
98         /* adjust the width according to how many org counts are added */
99         findNodeByName(r, "result_table_title_cell").width = 
100                 resultAddCopyCounts(countsrow, rec) + "%";
101
102         unHideMe(r);
103
104 }
105
106 var subjectsAreDrawn = false;
107 function resultPageIsDone(pos) {
108         return (recordsHandled == getDisplayCount() 
109                 || recordsHandled + getOffset() == getHitCount());
110 }
111
112
113 /* -------------------------------------------------------------------- */
114 /* dynamically add the copy count rows based on the org type 
115         'countsrow' is the row into which we will add TD's to hold
116         the copy counts 
117         This code generates copy count cells with an id of
118         'copy_count_cell_<depth>_<record_id>' for later insertion of copy counts
119         return the percent width left over after the each count is added. 
120         if 3 counts are added, returns 100 - (cell.width * 3) */
121 function resultAddCopyCounts(countsrow, rec) {
122
123         var ccell = findNodeByName(countsrow, config.names.result.count_cell);
124
125
126         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
127         var node = nodes[0];
128         var type = findOrgType(node.ou_type());
129         ccell.id = "copy_count_cell_" + type.depth() + "_" + rec.doc_id();
130         ccell.title = type.opac_label();
131         addCSSClass(ccell, "copy_count_cell_even");
132
133         var lastcell = ccell;
134
135         if(nodes[1]) {
136
137                 var x = 1;
138                 var d = findOrgDepth(nodes[1]);
139                 var d2 = findOrgDepth(nodes[nodes.length -1]);
140
141                 for( var i = d; i <= d2 ; i++ ) {
142         
143                         ccell = ccell.cloneNode(true);
144
145                         if((i % 2))
146                                 removeCSSClass(ccell, "copy_count_cell_even");
147                         else
148                                 addCSSClass(ccell, "copy_count_cell_even");
149
150                         var node = nodes[x++];
151                         var type = findOrgType(node.ou_type());
152         
153                         ccell.id = "copy_count_cell_" + type.depth() + "_" + rec.doc_id();
154                         ccell.title = type.opac_label();
155                         countsrow.insertBefore(ccell, lastcell);
156                         lastcell = ccell;
157                 }
158         }
159
160         return 100 - (nodes.length * 8);
161
162 }
163
164 /* collect copy counts for a record using method 'methodName' */
165 function resultCollectCopyCounts(rec, methodName) {
166         if(rec == null || rec.doc_id() == null) return;
167         var req = new Request(methodName, getLocation(), rec.doc_id() );
168         req.request.userdata = rec;
169         req.callback(resultHandleCopyCounts);
170         req.send();
171 }
172
173 function resultHandleCopyCounts(r) {
174         resultDisplayCopyCounts(r.userdata, r.getResultObject()); 
175 }
176
177
178 /* display the collected copy counts */
179 function resultDisplayCopyCounts(rec, copy_counts) {
180         if(copy_counts == null || rec == null) return;
181         var i = 0;
182         while(copy_counts[i] != null) {
183                 var cell = getId("copy_count_cell_" + i +"_" + rec.doc_id());
184                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
185                 i++;
186         }
187 }
188
189 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
190
191 function resultDrawSubjects() {
192
193         var subjs = [];
194         for( var s in subjectCache )
195                 subjs.push( { sub : s, count : subjectCache[s] } );
196         subjs.sort(resultSortSubjects);
197
198         var template = G.ui.sidebar.subject.removeChild(G.ui.sidebar.subject_item);
199         var x = 0;
200         var newnode = template.cloneNode(true);
201
202         var found = false;
203         for( var s in subjs ) {
204                 if(isNull(subjs[s])) continue;
205                 if(x++ > 7) break;
206                 buildSearchLink(STYPE_SUBJECT, subjs[s].sub, 
207                         findNodeByName(newnode, config.names.sidebar.subject_item), 30);
208                 G.ui.sidebar.subject.appendChild(newnode);
209                 newnode = template.cloneNode(true);
210                 found = true;
211         }
212         if(found) unHideMe(G.ui.sidebar.subject);
213 }
214
215 function resultDrawAuthors() {
216
217         var template = G.ui.sidebar.author.removeChild(G.ui.sidebar.author_item);
218         var x = 0;
219         var newnode = template.cloneNode(true);
220
221         var auths = new Array();
222         for( var s in authorCache ) auths.push(s);
223         auths = auths.sort();
224
225         var found = false;
226         for( var i in auths ) {
227                 if(isNull(auths[i])) continue;
228                 if(x++ > 7) break;
229                 buildSearchLink(STYPE_AUTHOR, auths[i], findNodeByName(newnode, config.names.sidebar.author_item), 30);
230                 G.ui.sidebar.author.appendChild(newnode);
231                 newnode = template.cloneNode(true);
232                 found = true;
233         }
234         if(found) unHideMe(G.ui.sidebar.author);
235 }
236
237
238 function resultDrawSeries() {
239         var template = G.ui.sidebar.series.removeChild(G.ui.sidebar.series_item);
240         var x = 0;
241         var newnode = template.cloneNode(true);
242
243         var sers = new Array();
244         for( var s in seriesCache ) sers.push(s);
245         sers = sers.sort();
246
247         var found = false;
248         for( var i in sers ) {
249                 if(isNull(sers[i])) continue;
250                 if(x++ > 7) break;
251                 buildSearchLink(STYPE_SERIES, sers[i], findNodeByName(newnode, config.names.sidebar.series_item), 30);
252                 G.ui.sidebar.series.appendChild(newnode);
253                 newnode = template.cloneNode(true);
254                 found = true;
255         }
256         if(found) unHideMe(G.ui.sidebar.series);
257
258 }
259
260
261
262
263