0635e14479c0ad94fdb1e58f1bed129af3449407
[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         while( table.parentNode.rows.length <= (getDisplayCount() + 1) )  /* add an extra row so IE and safari won't complain */
27                 table.appendChild(G.ui.result.row_template.cloneNode(true));
28
29         if(getOffset() == 0 || getHitCount() == null ) {
30                 if( getAdvTerm() && !getTerm() ) {
31                         if(getAdvType() == ADVTYPE_MULTI ) mresultCollectAdvIds();
32                         if(getAdvType() == ADVTYPE_MARC ) mresultCollectAdvMARCIds();
33                         if(getAdvType() == ADVTYPE_ISBN ) mresultCollectAdvISBNIds();
34                         if(getAdvType() == ADVTYPE_ISSN ) mresultCollectAdvISSNIds();
35                 }
36                 else {
37                         mresultCollectIds(FETCH_MRIDS_FULL); 
38                         ADVTERM = "";
39                         ADVTYPE = "";
40                 }
41
42         } else  {
43                 if( getAdvTerm() && !getTerm() ) {
44                         if(getAdvType() == ADVTYPE_MULTI ) mresultCollectAdvIds();
45                         if(getAdvType() == ADVTYPE_MARC ) mresultCollectAdvIds();
46                 }
47                 else {
48                         mresultCollectIds(FETCH_MRIDS);
49                         ADVTERM = "";
50                         ADVTYPE = "";
51                 }
52         }
53 }
54
55 /*
56 function mresultGetCount() {
57         var form = (getForm() == "all") ? null : getForm();
58         var req = new Request(FETCH_MRCOUNT, 
59                         getStype(), getTerm(), getLocation(), getDepth(), form );
60         req.callback(mresultHandleCount);
61         req.send();
62 }
63
64 function mresultHandleCount(r) {
65         HITCOUNT = parseInt(r.getResultObject());
66         alert('mresultHandleCount()');
67         runEvt('result', 'hitCountReceived');
68 }
69 */
70
71
72 function mresultLoadCachedSearch() {
73         if( (getOffset() > 0) && (getOffset() < mresultPreCache) ) {
74                 var c = JSON2js(cookieManager.read(COOKIE_IDS));
75                 if(c) { records = c[0]; ranks = c[1]; }
76         }
77 }
78
79 function mresultTryCachedSearch() {
80         mresultLoadCachedSearch();
81         if(     getOffset() != 0 && records[getOffset()] != null && 
82                         records[resultFinalPageIndex()] != null) {
83
84                 runEvt('result', 'hitCountReceived');
85                 mresultCollectRecords(); 
86                 return true;
87         }
88         return false;
89 }
90
91
92 /* performs the actual search */
93 function mresultCollectIds(method) {
94         if(!mresultTryCachedSearch()) {
95                 var form = (getForm() == "all") ? null : getForm();
96                 var req = new Request(method, getStype(), getTerm(), 
97                         getLocation(), getDepth(), mresultPreCache, getOffset(), form );
98                 req.callback(mresultHandleMRIds);
99                 req.send();
100         }
101 }
102
103
104 function mresultCollectAdvIds() {
105         if(!mresultTryCachedSearch()) {
106                 var form = (getForm() == "all") ? null : getForm();
107                 var req = new Request(FETCH_ADV_MRIDS, 
108                         JSON2js(getAdvTerm()), getLocation(), form, mresultPreCache );
109                 req.callback(mresultHandleMRIds);
110                 req.send();
111         }
112 }
113
114 function mresultCollectAdvMARCIds() {
115         if(!mresultTryCachedSearch()) {
116                 var form = (getForm() == "all") ? null : getForm();
117                 var req = new Request(FETCH_ADV_MARC_MRIDS, 
118                         JSON2js(getAdvTerm()), getLocation(), form );
119                 req.callback(mresultHandleMRIds);
120                 req.send();
121         }
122 }
123
124 function mresultCollectAdvISBNIds() {
125         if(!mresultTryCachedSearch()) {
126                 var req = new Request(FETCH_ADV_ISBN_MRIDS, getAdvTerm() );
127                 req.callback(mresultHandleMRIds);
128                 req.send();
129         }
130 }
131
132 function mresultCollectAdvISSNIds() {
133         if(!mresultTryCachedSearch()) {
134                 var req = new Request(FETCH_ADV_ISSN_MRIDS, getAdvTerm() );
135                 req.callback(mresultHandleMRIds);
136                 req.send();
137         }
138 }
139
140
141
142 function mresultHandleMRIds(r) {
143         var res = r.getResultObject();
144
145         if(res.count != null) {
146                 if( getOffset() == 0 ) HITCOUNT = res.count;
147                 runEvt('result', 'hitCountReceived');
148         } 
149         runEvt('result', 'idsReceived', res.ids);
150 }
151
152 function mresultSetRecords(idstruct) {
153         if(!idstruct) return;
154         var o = getOffset();
155
156         for( var x = o; x < idstruct.length + o; x++ ) {
157                 if( idstruct[x-o] != null ) {
158                         var r = parseInt(idstruct[x - o][0]);
159                         var ra = parseFloat(idstruct[x - o][1]);
160                         var or = parseInt(idstruct[x - o][2]);
161                         if(!isNull(r) && !isNaN(r)) records[x] = r;
162                         if(!isNull(ra) && !isNaN(ra)) ranks[x] = ra;
163                         if(!isNull(or) && !isNaN(or)) onlyrecord[x] = or;
164                 }
165         }
166
167         if(getOffset() == 0) {
168                 cookieManager.remove(COOKIE_IDS);
169                 cookieManager.write(COOKIE_IDS, js2JSON([ records, ranks ]), '+1d' );
170         }
171
172         TOPRANK = ranks[getOffset()];
173 }
174
175 function mresultCollectRecords() {
176         if(getHitCount() > 0 ) runEvt("result", "preCollectRecords");
177         var i = 0;
178         for( var x = getOffset(); x!= getDisplayCount() + getOffset(); x++ ) {
179                 if(isNull(records[x])) break;
180                 if(isNaN(records[x])) continue;
181                 var req = new Request(FETCH_MRMODS, records[x]);
182                 req.request.userdata = i++;
183                 req.callback(mresultHandleMods);
184                 req.send();
185         }
186 }
187
188 function mresultHandleMods(r) {
189         var rec = r.getResultObject();
190         var pagePosition = r.userdata;
191         runEvt('result', 'recordReceived', rec, pagePosition, true);
192         resultCollectCopyCounts(rec, pagePosition, FETCH_MR_COPY_COUNTS);
193 }
194
195