fixed build to copy over Evergreen perl mods
[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                 if(x++ > 7) break;
73
74                 found = true;
75
76                 var item = normalize(truncate(items[i], 65));
77                 var trunc = 65;
78                 var args = {};
79                 var href = resultQuickLink( items[i], stype );
80                 tree.addNode( stype + "_" + items[i], treeName + 'Root', item, href );
81
82                 if( !IE ) resultFireXRefReq(treeName, stype, items[i]);
83
84                 var a = {};
85                 a.type = stype;
86                 a.term = item;
87                 xrefCache.push(a);
88         }
89
90         if(found) {
91                 unHideMe(wrapperNode);
92                 if(IE) resultFireXRefSingle(treeName, xrefCache, stype);
93         }
94 }
95
96 /*
97 function resultFireXRefBatch(treeName, xrefCache, stype) {
98         var query = [];
99         for( var i = 0; i != xrefCache.length; i++ ) {
100                 var topic = xrefCache[i];
101                 query.push( [ topic.type, topic.term ] );
102         }
103         var req = new Request(FETCH_CROSSREF_BATCH, query);
104         var tree;
105         eval('tree=' + treeName);
106         req.request._tree = tree;
107         req.request._stype = stype;
108         req.callback(resultRenderXRefTree);
109         req.send();
110 }
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         args.page = MRESULT;
155         args[PARAM_OFFSET] = 0;
156         args[PARAM_TERM] = term;
157         args[PARAM_STYPE] = type;
158         return buildOPACLink(args);
159 }
160
161 /*
162 function resultRenderXRefTree(r) {
163         var tree = r._tree;
164         var res = r.getResultObject();
165         var stype = r._stype;
166
167         for( var c in res ) {
168                 var cls = res[c];
169                 for( var t in cls ) {
170                         var term = res[c][t];
171                         var froms = term['from'];
172                         var alsos = term['also'];
173                         var total = 0;
174
175                         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
176                                 var string = normalize(truncate(froms[i], 45));
177                                 if($(stype + '_' + froms[i])) continue;
178                                 tree.addNode(stype + '_' + froms[i], 
179                                         stype + '_' + t, string, resultQuickLink(froms[i],stype));
180                         }
181                         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
182                                 var string = normalize(truncate(alsos[i], 45));
183                                 if($(stype + '_' + alsos[i])) continue;
184                                 tree.addNode(stype + '_' + alsos[i], 
185                                         stype + '_' + t, string, resultQuickLink(alsos[i],stype));
186                         }
187                 }
188         }
189 }
190 */
191
192
193 function resultAppendCrossRef(r) {
194         var tree                = r._tree
195         var item                = r._item
196         var stype       = r._stype;
197         var result      = r.getResultObject();
198         if(!result) return;
199         var froms       = result['from'];
200         var alsos       = result['also'];
201
202         var total = 0;
203
204         for( var i = 0; (total++ < 5 && i < froms.length); i++ ) {
205                 var string = normalize(truncate(froms[i], 45));
206                 if($(stype + '_' + froms[i])) continue;
207                 tree.addNode(stype + '_' + froms[i], 
208                         stype + '_' + item, string, resultQuickLink(froms[i],stype));
209         }
210         for( var i = 0; (total++ < 10 && i < alsos.length); i++ ) {
211                 var string = normalize(truncate(alsos[i], 45));
212                 if($(stype + '_' + alsos[i])) continue;
213                 tree.addNode(stype + '_' + alsos[i], 
214                         stype + '_' + item, string, resultQuickLink(alsos[i],stype));
215         }
216 }
217
218
219
220
221