]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/result_common.js
added a callback for recordDrawn so XUL can plug in actions
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / 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         runEvt("result", "recordDrawn", rec.doc_id(), title_link);
100
101         if(resultPageIsDone()) 
102                 runEvt('result', 'allRecordsReceived', recordsCache);
103 }
104
105 function resultPageIsDone(pos) {
106         return (recordsHandled == getDisplayCount() 
107                 || recordsHandled + getOffset() == getHitCount());
108 }
109
110 /* -------------------------------------------------------------------- */
111 /* dynamically add the copy count rows based on the org type 'countsrow' 
112         is the row into which we will add TD's to hold the copy counts 
113         This code generates copy count cells with an id of
114         'copy_count_cell_<depth>_<pagePosition>'  */
115 function resultAddCopyCounts(rec, pagePosition) {
116
117         var r = table.rows[pagePosition];
118         var countsrow = findNodeByName(r, config.names.result.counts_row );
119         var ccell = findNodeByName(countsrow, config.names.result.count_cell);
120
121         var nodes = orgNodeTrail(findOrgUnit(getLocation()));
122         var node = nodes[0];
123         var type = findOrgType(node.ou_type());
124         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
125         ccell.title = type.opac_label();
126         addCSSClass(ccell, config.css.result.cc_cell_even);
127
128         var lastcell = ccell;
129
130         if(nodes[1]) {
131
132                 var x = 1;
133                 var d = findOrgDepth(nodes[1]);
134                 var d2 = findOrgDepth(nodes[nodes.length -1]);
135
136                 for( var i = d; i <= d2 ; i++ ) {
137         
138                         ccell = ccell.cloneNode(true);
139
140                         if((i % 2))
141                                 removeCSSClass(ccell, "copy_count_cell_even");
142                         else
143                                 addCSSClass(ccell, "copy_count_cell_even");
144
145                         var node = nodes[x++];
146                         var type = findOrgType(node.ou_type());
147         
148                         ccell.id = "copy_count_cell_" + type.depth() + "_" + pagePosition;
149                         ccell.title = type.opac_label();
150                         countsrow.insertBefore(ccell, lastcell);
151                         lastcell = ccell;
152                 }
153         }
154 }
155
156 /* collect copy counts for a record using method 'methodName' */
157 function resultCollectCopyCounts(rec, pagePosition, methodName) {
158         if(rec == null || rec.doc_id() == null) return;
159         var req = new Request(methodName, getLocation(), rec.doc_id() );
160         req.request.userdata = [ rec, pagePosition ];
161         req.callback(resultHandleCopyCounts);
162         req.send();
163 }
164
165 function resultHandleCopyCounts(r) {
166         runEvt('result', 'copyCountsReceived', r.userdata[0], r.userdata[1], r.getResultObject()); 
167 }
168
169
170 /* display the collected copy counts */
171 function resultDisplayCopyCounts(rec, pagePosition, copy_counts) {
172         if(copy_counts == null || rec == null) return;
173         var i = 0;
174         while(copy_counts[i] != null) {
175                 var cell = getId("copy_count_cell_" + i +"_" + pagePosition);
176                 cell.appendChild(text(copy_counts[i].available + " / " + copy_counts[i].count));
177                 i++;
178         }
179 }
180
181
182 /* captures extraneous info from each record */
183 function resultBuildCaches(records) {
184         for( var r in records ) {
185                 var rec = records[r];
186                 for( var s in rec.subject() ) 
187                         subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
188                 authorCache[rec.author()] = 1;
189                 for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
190         }
191 }
192
193
194 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
195 function resultDrawSubjects() {
196
197         var subjs = [];
198         for( var s in subjectCache )
199                 subjs.push( { sub : s, count : subjectCache[s] } );
200         subjs.sort(resultSortSubjects);
201
202         var ss = [];
203         for( var s in subjs ) ss.push(subjs[s].sub);
204         resultDrawSidebarStuff(STYPE_SUBJECT, G.ui.sidebar.subject_item,  
205                 config.names.sidebar.subject_item, ss, G.ui.sidebar.subject);
206 }
207
208 function resultDrawAuthors() {
209         var auths = new Array();
210         for( var s in authorCache ) auths.push(s);
211         resultDrawSidebarStuff(STYPE_AUTHOR, G.ui.sidebar.author_item,  
212                 config.names.sidebar.author_item, auths.sort(), G.ui.sidebar.author);
213 }
214
215 function resultDrawSeries() {
216         var sers = new Array();
217         for( var s in seriesCache ) sers.push(s);
218         resultDrawSidebarStuff(STYPE_SERIES, G.ui.sidebar.series_item,  
219                 config.names.sidebar.series_item, sers.sort(), G.ui.sidebar.series);
220 }
221
222 /* search type, template node, href link name, array of text, node to unhide */
223 function resultDrawSidebarStuff(stype, node, linkname, items, wrapperNode) {
224         var parent = node.parentNode;
225         var template = parent.removeChild(node);
226         var x = 0;
227         var newnode = template.cloneNode(true);
228         var found = false;
229         for( var i in items ) {
230                 if(isNull(items[i])) continue;
231                 if(x++ > 7) break;
232                 buildSearchLink(stype, items[i], findNodeByName(newnode, linkname), 100);
233                 parent.appendChild(newnode);
234                 newnode = template.cloneNode(true);
235                 found = true;
236         }
237         if(found) unHideMe(wrapperNode);
238 }
239
240
241
242
243