3ded13bfb83ecba0be1b400f9714ab3ffbf6ae44
[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 _oldFashioned = true;
58 var IESux = true;
59
60 function resultDrawSidebarTrees( stype, treeName, items, wrapperNode, destNode ) {
61         eval("tree = " + treeName);
62
63         var xrefCache = [];
64         var found = false;
65         var x = 0;
66         for( var i in items ) {
67
68                 if(isNull(items[i])) continue;
69
70                 /* again, IE is a turd */
71                 //if(IE) { if(x++ > 5) break; }
72                 //else { if(x++ > 7) break; }
73                 if(x++ > 7) break;
74
75                 found = true;
76
77                 var item = normalize(truncate(items[i], 65));
78                 var trunc = 65;
79                 var args = {};
80                 var href = resultQuickLink( items[i], stype );
81                 tree.addNode( stype + "_" + items[i], treeName + 'Root', item, href );
82
83                 ////if(_oldFashioned && !IE) {
84                 if(_oldFashioned && !IE ) resultFireXRefReq(treeName, stype, items[i]);
85
86                 var a = {};
87                 a.type = stype;
88                 a.term = item;
89                 xrefCache.push(a);
90         }
91
92         if(found) {
93                 unHideMe(wrapperNode);
94                 if(!_oldFashioned)
95                         resultFireXRefBatch(treeName, xrefCache, stype);
96                 //if(IE && stype == 'subject' ) {
97
98                 if(IE) resultFireXRefSingle(treeName, xrefCache, stype);
99         }
100 }
101
102 function resultFireXRefBatch(treeName, xrefCache, stype) {
103         var query = [];
104         for( var i = 0; i != xrefCache.length; i++ ) {
105                 var topic = xrefCache[i];
106                 query.push( [ topic.type, topic.term ] );
107         }
108         var req = new Request(FETCH_CROSSREF_BATCH, query);
109         var tree;
110         eval('tree=' + treeName);
111         req.request._tree = tree;
112         req.request._stype = stype;
113         req.callback(resultRenderXRefTree);
114         req.send();
115 }
116
117 var xrefCacheIndex = {};
118 xrefCacheIndex['subject'] = 0;
119 xrefCacheIndex['author'] = 0;
120 xrefCacheIndex['series'] = 0;
121
122 function resultHandleXRefResponse(r) {
123         resultFireXRefSingle( r._treename, r._cache, r._stype );
124         resultAppendCrossRef(r);
125 }
126
127
128 function resultFireXRefSingle( treeName, xrefCache, stype ) {
129         var i = xrefCacheIndex[stype]++;
130         var item = xrefCache[i].term;
131         var tree;
132         eval('tree=' + treeName);
133         var req = new Request(FETCH_CROSSREF, stype, item);
134         req.request._tree = tree;
135         req.request._item = item;
136         req.request._stype = stype;
137         req.request._cache = xrefCache;
138         req.request._treename = treeName;
139         req.callback(resultHandleXRefResponse);
140         req.send();
141 }
142
143 function resultFireXRefReq( treeName, stype, item ) {
144         var tree;
145         eval('tree=' + treeName);
146         var req = new Request(FETCH_CROSSREF, stype, item);
147         req.request._tree = tree;
148         req.request._item = item;
149         req.request._stype = stype;
150         req.callback(resultAppendCrossRef);
151         req.send();
152 }
153
154
155 function resultQuickLink( term, type ) {
156         var args = {};
157         args.page = MRESULT;
158         args[PARAM_OFFSET] = 0;
159         args[PARAM_TERM] = term;
160         args[PARAM_STYPE] = type;
161         return buildOPACLink(args);
162 }
163
164 function resultRenderXRefTree(r) {
165         var tree = r._tree;
166         var res = r.getResultObject();
167         var stype = r._stype;
168
169         for( var c in res ) {
170                 var cls = res[c];
171                 for( var t in cls ) {
172                         var term = res[c][t];
173                         var froms = term['from'];
174                         var alsos = term['also'];
175                         var total = 0;
176
177                         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
178                                 var string = normalize(truncate(froms[i], 45));
179                                 if($(stype + '_' + froms[i])) continue;
180                                 tree.addNode(stype + '_' + froms[i], 
181                                         stype + '_' + t, string, resultQuickLink(froms[i],stype));
182                         }
183                         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
184                                 var string = normalize(truncate(alsos[i], 45));
185                                 if($(stype + '_' + alsos[i])) continue;
186                                 tree.addNode(stype + '_' + alsos[i], 
187                                         stype + '_' + t, string, resultQuickLink(alsos[i],stype));
188                         }
189                 }
190         }
191 }
192
193
194 function resultAppendCrossRef(r) {
195         var tree                = r._tree
196         var item                = r._item
197         var stype       = r._stype;
198         var result      = r.getResultObject();
199         if(!result) return;
200         var froms       = result['from'];
201         var alsos       = result['also'];
202
203         var total = 0;
204
205         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
206                 var string = normalize(truncate(froms[i], 45));
207                 if($(stype + '_' + froms[i])) continue;
208                 tree.addNode(stype + '_' + froms[i], 
209                         stype + '_' + item, string, resultQuickLink(froms[i],stype));
210         }
211         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
212                 var string = normalize(truncate(alsos[i], 45));
213                 if($(stype + '_' + alsos[i])) continue;
214                 tree.addNode(stype + '_' + alsos[i], 
215                         stype + '_' + item, string, resultQuickLink(alsos[i],stype));
216         }
217 }
218
219
220
221
222