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