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