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