22f89cca3d6096ce7aa3b89350e939b9721e177c
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / sidebar_extras.js
1
2 /* captures extraneous info from each record */
3
4 var subjectCache = {};
5 var authorCache = {};
6 var seriesCache = {};
7
8 function resultBuildCaches(records) {
9         for( var r in records ) {
10                 var rec = records[r];
11                 for( var s in rec.subject() ) 
12                         subjectCache[s] == null ? subjectCache[s] = 1 : subjectCache[s]++;
13                 authorCache[rec.author()] = 1;
14                 for( var s in rec.series() ) seriesCache[rec.series()[s]] = 1;
15         }
16 }
17
18 function resultSortSubjects(a, b) { return -(a.count - b.count); } /* sort in reverse */
19 function resultDrawSubjects() {
20
21         var subjs = [];
22         for( var s in subjectCache )
23                 subjs.push( { sub : s, count : subjectCache[s] } );
24         subjs.sort(resultSortSubjects);
25
26         var ss = [];
27         for( var s in subjs ) ss.push(subjs[s].sub);
28
29         resultDrawSidebarTrees( 
30                 STYPE_SUBJECT, 
31                 "subjectSidebarTree", ss, 
32                 $("subject_tree_sidebar"), 
33                 $("subject_sidebar_tree_div") );
34 }
35
36 function resultDrawAuthors() {
37         var auths = new Array();
38         for( var s in authorCache ) auths.push(s);
39
40         resultDrawSidebarTrees( 
41                 STYPE_AUTHOR, 
42                 "authorSidebarTree", auths.sort(), 
43                 $("author_tree_sidebar"), 
44                 $("author_sidebar_tree_div") );
45 }
46
47 function resultDrawSeries() {
48         var sers = new Array();
49         for( var s in seriesCache ) sers.push(s);
50         resultDrawSidebarTrees( 
51                 STYPE_SERIES, 
52                 "seriesSidebarTree", sers.sort(), 
53                 $("series_tree_sidebar"), 
54                 $("series_sidebar_tree_div") );
55 }
56
57 var IESux = true;
58
59 function resultDrawSidebarTrees( stype, treeName, items, wrapperNode, destNode ) {
60         eval("tree = " + treeName);
61
62         var xrefCache = [];
63         var found = false;
64         var x = 0;
65         for( var i in items ) {
66
67                 if(isNull(items[i])) continue;
68
69                 /* again, IE is a turd */
70                 if(IE) { if(x++ > 5) break; }
71                 else { if(x++ > 7) break; }
72
73                 found = true;
74
75                 var item = normalize(truncate(items[i], 65));
76                 var args = {};
77                 var href = resultQuickLink( items[i], stype );
78                 tree.addNode( stype + "_" + items[i], treeName + 'Root', item, href );
79
80 //              if( !IE ) resultFireXRefReq(treeName, stype, items[i]);
81
82                 var a = {};
83                 a.type = stype;
84                 a.term = item;
85                 xrefCache.push(a);
86         }
87
88         if(found) {
89                 unHideMe(wrapperNode);
90 //              if(IE) resultFireXRefSingle(treeName, xrefCache, stype);
91                 resultFireXRefBatch(treeName, xrefCache, stype);
92         }
93 }
94
95 /* XXX */
96 function resultFireXRefBatch(treeName, xrefCache, stype) {
97         var query = [];
98         for( var i = 0; i != xrefCache.length; i++ ) {
99                 var topic = xrefCache[i];
100                 query.push( [ topic.type, topic.term ] );
101         }
102         var req = new Request(FETCH_CROSSREF_BATCH, query);
103         var tree;
104         eval('tree=' + treeName);
105         req.request._tree = tree;
106         req.request._stype = stype;
107         req.callback(resultRenderXRefTree);
108         req.send();
109 }
110
111 var xrefCacheIndex = {};
112 xrefCacheIndex['subject'] = 0;
113 xrefCacheIndex['author'] = 0;
114 xrefCacheIndex['series'] = 0;
115
116 function resultHandleXRefResponse(r) {
117         resultFireXRefSingle( r._treename, r._cache, r._stype );
118         resultAppendCrossRef(r);
119 }
120
121
122 function resultFireXRefSingle( treeName, xrefCache, stype ) {
123         var i = xrefCacheIndex[stype]++;
124         if(xrefCache[i] == null) return;
125         var item = xrefCache[i].term;
126         var tree;
127         eval('tree=' + treeName);
128         var req = new Request(FETCH_CROSSREF, stype, item);
129         req.request._tree = tree;
130         req.request._item = item;
131         req.request._stype = stype;
132         req.request._cache = xrefCache;
133         req.request._treename = treeName;
134         req.callback(resultHandleXRefResponse);
135         req.send();
136 }
137
138 function resultFireXRefReq( treeName, stype, item ) {
139         var tree;
140         eval('tree=' + treeName);
141         var req = new Request(FETCH_CROSSREF, stype, item);
142         req.request._tree = tree;
143         req.request._item = item;
144         req.request._stype = stype;
145         req.callback(resultAppendCrossRef);
146         req.send();
147 }
148
149
150 function resultQuickLink( term, type ) {
151         var args = {};
152         args.page = MRESULT;
153         args[PARAM_OFFSET] = 0;
154         args[PARAM_TERM] = term;
155         args[PARAM_STYPE] = type;
156         return buildOPACLink(args);
157 }
158
159 /* XXX */
160 function resultRenderXRefTree(r) {
161         var tree = r._tree;
162         var res = r.getResultObject();
163         var stype = r._stype;
164
165         for( var c in res ) {
166                 var cls = res[c];
167                 for( var t in cls ) {
168                         var term = res[c][t];
169                         var froms = term['from'];
170                         var alsos = term['also'];
171                         var total = 0;
172
173                         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
174                                 var string = normalize(truncate(froms[i], 45));
175                                 if($(stype + '_' + froms[i])) continue;
176                                 tree.addNode(stype + '_' + froms[i], 
177                                         stype + '_' + t, string, resultQuickLink(froms[i],stype));
178                         }
179                         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
180                                 var string = normalize(truncate(alsos[i], 45));
181                                 if($(stype + '_' + alsos[i])) continue;
182                                 tree.addNode(stype + '_' + alsos[i], 
183                                         stype + '_' + t, string, resultQuickLink(alsos[i],stype));
184                         }
185                 }
186         }
187 }
188
189
190 /* Addes the see-from/see-also entries for this subject item */
191 function resultAppendCrossRef(r) {
192         var tree                = r._tree
193         var item                = r._item
194         var stype       = r._stype;
195         var result      = r.getResultObject();
196         if(!result) return;
197         var froms       = result['from'];
198         var alsos       = result['also'];
199
200         var total = 0;
201
202         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) 
203                 resultAddXRefItem( tree, item, stype, froms, i );
204
205         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) 
206                 resultAddXRefItem( tree, item, stype, alsos, i );
207 }
208
209 /**
210  * Adds a single entry into the see-from/also sidebar tree 
211  */
212 var collectedStrings = [];
213 function resultAddXRefItem(tree, rootItem, stype, arr, idx) {
214
215         var string = normalize(truncate(arr[idx], 45));
216
217         if( string == rootItem ) return;
218
219         if( grep( collectedStrings, 
220                 function(a) { return (a == string); }) ) return;
221
222         if($(stype + '_' + arr[idx])) return;
223
224         tree.addNode(stype + '_' + arr[idx], 
225                 stype + '_' + rootItem, string, resultQuickLink(arr[idx],stype));
226
227         collectedStrings.push(string);
228 }
229
230
231
232
233
234