debugging the sort order stuff, changed some terminology, only
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / mresult.js
1 //var records = {};
2 var records = [];
3 var ranks = [];
4 var onlyrecord = {};
5 var table;
6 var mresultPreCache = 200;
7 var searchTimer;
8
9 attachEvt("common", "unload", mresultUnload);
10 attachEvt("common", "run", mresultDoSearch);
11 attachEvt("result", "idsReceived", mresultSetRecords); 
12 attachEvt("result", "idsReceived", mresultCollectRecords); 
13
14 function mresultUnload() { removeChildren(table); table = null;}
15
16 function mresultDoSearch() {
17
18         if(getOffset() == 0) {
19                 swapCanvas($('loading_alt'));
20                 searchTimer = new Timer('searchTimer',$('loading_alt_span'));
21                 searchTimer.start();
22         }
23
24         table = G.ui.result.main_table;
25
26         /* add an extra row so IE and safari won't complain */
27         while( table.parentNode.rows.length <= (getDisplayCount() + 1) )  
28                 table.appendChild(G.ui.result.row_template.cloneNode(true));
29
30         /*
31         if(getOffset() == 0 || getHitCount() == null ) {
32                 if( getAdvTerm() && !getTerm() ) {
33                         if(getAdvType() == ADVTYPE_MULTI ) mresultCollectAdvIds();
34                         if(getAdvType() == ADVTYPE_MARC ) mresultCollectAdvMARCIds();
35                         if(getAdvType() == ADVTYPE_ISBN ) mresultCollectAdvISBNIds();
36                         if(getAdvType() == ADVTYPE_ISSN ) mresultCollectAdvISSNIds();
37
38                 } else {
39                         mresultCollectIds(FETCH_MRIDS_FULL); 
40                         ADVTERM = "";
41                         ADVTYPE = "";
42                 }
43
44         } else  {
45                 if( getAdvTerm() && !getTerm() ) {
46                         if(getAdvType() == ADVTYPE_MULTI ) mresultCollectAdvIds();
47                         if(getAdvType() == ADVTYPE_MARC ) mresultCollectAdvIds();
48
49                 } else {
50                         mresultCollectIds(FETCH_MRIDS);
51                         ADVTERM = "";
52                         ADVTYPE = "";
53                 }
54         }
55         */
56
57         if( getAdvTerm() && !getTerm() ) {
58                 if(getAdvType() == ADVTYPE_MULTI ) mresultCollectAdvIds();
59                 if(getAdvType() == ADVTYPE_MARC ) mresultCollectAdvMARCIds();
60                 if(getAdvType() == ADVTYPE_ISBN ) mresultCollectAdvISBNIds();
61                 if(getAdvType() == ADVTYPE_ISSN ) mresultCollectAdvISSNIds();
62
63         } else {
64                 _mresultCollectIds(); 
65                 ADVTERM = "";
66                 ADVTYPE = "";
67         }
68 }
69
70 function mresultLoadCachedSearch() {
71         if( (getOffset() > 0) && (getOffset() < mresultPreCache) ) {
72                 var c = JSON2js(cookieManager.read(COOKIE_IDS));
73                 if(c) { records = c[0]; ranks = c[1]; }
74         }
75 }
76
77 function mresultTryCachedSearch() {
78         mresultLoadCachedSearch();
79         if(     getOffset() != 0 && records[getOffset()] != null && 
80                         records[resultFinalPageIndex()] != null) {
81                 runEvt('result', 'hitCountReceived');
82                 mresultCollectRecords(); 
83                 return true;
84         }
85         return false;
86 }
87
88
89 /* performs the actual search */
90 /*
91 function mresultCollectIds(method) {
92         if(!mresultTryCachedSearch()) {
93                 var form = (getForm() == "all") ? null : getForm();
94                 var req = new Request(method, getStype(), getTerm(), 
95                         getLocation(), getDepth(), mresultPreCache, getOffset(), form );
96                 req.callback(mresultHandleMRIds);
97                 req.send();
98         }
99 }
100 */
101
102 function _mresultCollectIds() {
103         if( getOffset() == 0 || !mresultTryCachedSearch() ) {
104
105                 var form                = (!getForm() || getForm() == "all") ? null : getForm();
106                 var sort                = (getSort() == SORT_TYPE_REL) ? null : getSort(); 
107                 var sortdir = (sort) ? getSortDir() : null;
108
109                 var req = new Request(FETCH_MRIDS_, getStype(), 
110                         {       term            : getTerm(), 
111                                 sort            : sort,
112                                 sort_dir        : sortdir,
113                                 org_unit : getLocation(),
114                                 depth           : getDepth(),
115                                 limit           : mresultPreCache,
116                                 offset  : getOffset(),
117                                 format  : form } );
118
119                 req.callback(mresultHandleMRIds);
120                 req.send();
121         }
122 }
123
124
125 function mresultCollectAdvIds() {
126         if(!mresultTryCachedSearch()) {
127                 var form = (getForm() == "all") ? null : getForm();
128                 var req = new Request(FETCH_ADV_MRIDS, 
129                         JSON2js(getAdvTerm()), getLocation(), form, mresultPreCache );
130                 req.callback(mresultHandleMRIds);
131                 req.send();
132         }
133 }
134
135 function mresultCollectAdvMARCIds() {
136         if(!mresultTryCachedSearch()) {
137                 var form = (getForm() == "all") ? null : getForm();
138                 var req = new Request(FETCH_ADV_MARC_MRIDS, 
139                         JSON2js(getAdvTerm()), getLocation(), form );
140                 req.callback(mresultHandleMRIds);
141                 req.send();
142         }
143 }
144
145 function mresultCollectAdvISBNIds() {
146         if(!mresultTryCachedSearch()) {
147                 var req = new Request(FETCH_ADV_ISBN_MRIDS, getAdvTerm() );
148                 req.callback(mresultHandleMRIds);
149                 req.send();
150         }
151 }
152
153 function mresultCollectAdvISSNIds() {
154         if(!mresultTryCachedSearch()) {
155                 var req = new Request(FETCH_ADV_ISSN_MRIDS, getAdvTerm() );
156                 req.callback(mresultHandleMRIds);
157                 req.send();
158         }
159 }
160
161
162
163 function mresultHandleMRIds(r) {
164         var res = r.getResultObject();
165
166         if(res.count != null) {
167                 if( getOffset() == 0 ) HITCOUNT = res.count;
168                 runEvt('result', 'hitCountReceived');
169         } 
170         runEvt('result', 'idsReceived', res.ids);
171 }
172
173 function mresultSetRecords(idstruct) {
174         if(!idstruct) return;
175         var o = getOffset();
176
177         for( var x = o; x < idstruct.length + o; x++ ) {
178                 if( idstruct[x-o] != null ) {
179                         var r = parseInt(idstruct[x - o][0]);
180                         var ra = parseFloat(idstruct[x - o][1]);
181                         var or = parseInt(idstruct[x - o][2]);
182                         if(!isNull(r) && !isNaN(r)) records[x] = r;
183                         if(!isNull(ra) && !isNaN(ra)) ranks[x] = ra;
184                         if(!isNull(or) && !isNaN(or)) onlyrecord[x] = or;
185                 }
186         }
187
188         if(getOffset() == 0) {
189                 cookieManager.remove(COOKIE_IDS);
190                 cookieManager.write(COOKIE_IDS, js2JSON([ records, ranks ]), '+1d' );
191         }
192
193         TOPRANK = ranks[getOffset()];
194 }
195
196 function mresultCollectRecords() {
197         if(getHitCount() > 0 ) runEvt("result", "preCollectRecords");
198         var i = 0;
199         for( var x = getOffset(); x!= getDisplayCount() + getOffset(); x++ ) {
200                 if(isNull(records[x])) break;
201                 if(isNaN(records[x])) continue;
202                 var req = new Request(FETCH_MRMODS, records[x]);
203                 req.request.userdata = i++;
204                 req.callback(mresultHandleMods);
205                 req.send();
206         }
207 }
208
209 function mresultHandleMods(r) {
210         var rec = r.getResultObject();
211         var pagePosition = r.userdata;
212         runEvt('result', 'recordReceived', rec, pagePosition, true);
213         resultCollectCopyCounts(rec, pagePosition, FETCH_MR_COPY_COUNTS);
214 }
215
216