7263954583cb8805371fbb7fad5efedff77dec65
[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
92                 /* XXX */ // disabled auth lookups for now
93                 //resultFireXRefBatch(treeName, xrefCache, stype);
94         }
95 }
96
97 /* XXX */
98 function resultFireXRefBatch(treeName, xrefCache, stype) {
99         var query = [];
100         for( var i = 0; i != xrefCache.length; i++ ) {
101                 var topic = xrefCache[i];
102                 query.push( [ topic.type, topic.term ] );
103         }
104         var req = new Request(FETCH_CROSSREF_BATCH, query);
105         var tree;
106         eval('tree=' + treeName);
107         req.request._tree = tree;
108         req.request._stype = stype;
109         req.callback(resultRenderXRefTree);
110         req.send();
111 }
112
113 var xrefCacheIndex = {};
114 xrefCacheIndex['subject'] = 0;
115 xrefCacheIndex['author'] = 0;
116 xrefCacheIndex['series'] = 0;
117
118 function resultHandleXRefResponse(r) {
119         resultFireXRefSingle( r._treename, r._cache, r._stype );
120         resultAppendCrossRef(r);
121 }
122
123
124 function resultFireXRefSingle( treeName, xrefCache, stype ) {
125         var i = xrefCacheIndex[stype]++;
126         if(xrefCache[i] == null) return;
127         var item = xrefCache[i].term;
128         var tree;
129         eval('tree=' + treeName);
130         var req = new Request(FETCH_CROSSREF, stype, item);
131         req.request._tree = tree;
132         req.request._item = item;
133         req.request._stype = stype;
134         req.request._cache = xrefCache;
135         req.request._treename = treeName;
136         req.callback(resultHandleXRefResponse);
137         req.send();
138 }
139
140 function resultFireXRefReq( treeName, stype, item ) {
141         var tree;
142         eval('tree=' + treeName);
143         var req = new Request(FETCH_CROSSREF, stype, item);
144         req.request._tree = tree;
145         req.request._item = item;
146         req.request._stype = stype;
147         req.callback(resultAppendCrossRef);
148         req.send();
149 }
150
151
152 function resultQuickLink( term, type ) {
153         var args = {};
154         if(SHOW_MR_DEFAULT) {
155                 args.page = MRESULT;
156         } else {
157                 args.page = RRESULT;
158                 args[PARAM_RTYPE] = type;
159         }
160         args[PARAM_OFFSET] = 0;
161         args[PARAM_TERM] = term;
162         args[PARAM_STYPE] = type;
163         return buildOPACLink(args);
164 }
165
166 /* XXX */
167 function resultRenderXRefTree(r) {
168         var tree = r._tree;
169         var res = r.getResultObject();
170         var stype = r._stype;
171
172         for( var c in res ) {
173                 var cls = res[c];
174                 for( var t in cls ) {
175                         var term = res[c][t];
176                         var froms = term['from'];
177                         var alsos = term['also'];
178                         var total = 0;
179
180                         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
181                                 var string = normalize(truncate(froms[i], 45));
182                                 if($(stype + '_' + froms[i])) continue;
183                                 tree.addNode(stype + '_' + froms[i], 
184                                         stype + '_' + t, string, resultQuickLink(froms[i],stype));
185                         }
186                         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
187                                 var string = normalize(truncate(alsos[i], 45));
188                                 if($(stype + '_' + alsos[i])) continue;
189                                 tree.addNode(stype + '_' + alsos[i], 
190                                         stype + '_' + t, string, resultQuickLink(alsos[i],stype));
191                         }
192                 }
193         }
194 }
195
196
197 /* Addes the see-from/see-also entries for this subject item */
198 function resultAppendCrossRef(r) {
199         var tree                = r._tree
200         var item                = r._item
201         var stype       = r._stype;
202         var result      = r.getResultObject();
203         if(!result) return;
204         var froms       = result['from'];
205         var alsos       = result['also'];
206
207         var total = 0;
208
209         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) 
210                 resultAddXRefItem( tree, item, stype, froms, i );
211
212         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) 
213                 resultAddXRefItem( tree, item, stype, alsos, i );
214 }
215
216 /**
217  * Adds a single entry into the see-from/also sidebar tree 
218  */
219 var collectedStrings = [];
220 function resultAddXRefItem(tree, rootItem, stype, arr, idx) {
221
222         var string = normalize(truncate(arr[idx], 45));
223
224         if( string == rootItem ) return;
225
226         if( grep( collectedStrings, 
227                 function(a) { return (a == string); }) ) return;
228
229         if($(stype + '_' + arr[idx])) return;
230
231         tree.addNode(stype + '_' + arr[idx], 
232                 stype + '_' + rootItem, string, resultQuickLink(arr[idx],stype));
233
234         collectedStrings.push(string);
235 }
236
237
238
239
240
241