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