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