]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/lib/js/opac/result_common.js
2e4649d58ca987599b5e34404ad2a94e05e498c7
[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 /* collect copy counts for a record using method 'methodName' */
164 function resultCollectCopyCounts(rec, methodName) {
165         if(rec == null || rec.doc_id() == null) return;
166         var req = new Request(methodName, getLocation(), rec.doc_id() );
167         req.request.userdata = rec;
168         req.callback(resultHandleCopyCounts);
169         req.send();
170 }
171
172 function resultHandleCopyCounts(r) {
173         resultDisplayCopyCounts(r.userdata, r.getResultObject()); 
174 }
175
176
177 /* display the collected copy counts */
178 function resultDisplayCopyCounts(rec, copy_counts) {
179         if(copy_counts == null || rec == null) return;
180         var i = 0;
181         while(copy_counts[i] != null) {
182                 var cell = getId("copy_count_cell_" + i +"_" + rec.doc_id());
183                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
184                 i++;
185         }
186 }
187
188 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
189 function resultDrawSubjects() {
190         var subjs = [];
191         for( var s in subjectCache )
192                 subjs.push( { sub : s, count : subjectCache[s] } );
193         subjs.sort(resultSortSubjects);
194
195         var ss = [];
196         for( var s in subjs ) ss.push(subjs[s].sub);
197         resultDrawSidebarStuff(STYPE_SUBJECT, G.ui.sidebar.subject_item,  
198                 config.names.sidebar.subject_item, ss, G.ui.sidebar.subject);
199 }
200
201 function resultDrawAuthors() {
202         var auths = new Array();
203         for( var s in authorCache ) auths.push(s);
204         resultDrawSidebarStuff(STYPE_AUTHOR, G.ui.sidebar.author_item,  
205                 config.names.sidebar.author_item, auths.sort(), G.ui.sidebar.author);
206 }
207
208 function resultDrawSeries() {
209         var sers = new Array();
210         for( var s in seriesCache ) sers.push(s);
211         resultDrawSidebarStuff(STYPE_SERIES, G.ui.sidebar.series_item,  
212                 config.names.sidebar.series_item, sers.sort(), G.ui.sidebar.series);
213 }
214
215 /* search type, template node, href link name, array of text, node to unhide */
216 function resultDrawSidebarStuff(stype, node, linkname, items, wrapperNode) {
217         var parent = node.parentNode;
218         var template = parent.removeChild(node);
219         var x = 0;
220         var newnode = template.cloneNode(true);
221         var found = false;
222         for( var i in items ) {
223                 if(isNull(items[i])) continue;
224                 if(x++ > 7) break;
225                 buildSearchLink(stype, items[i], findNodeByName(newnode, linkname), 100);
226                 parent.appendChild(newnode);
227                 newnode = template.cloneNode(true);
228                 found = true;
229         }
230         if(found) unHideMe(wrapperNode);
231 }
232
233
234
235
236