]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/mresult.js
patching up some bugs that popped up
[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 idsCookie = new cookieObject("ids", 1, "/", COOKIE_IDS);
8 //var idsCookie;
9 var idsCookie = new HTTP.Cookies();
10 var searchTimer;
11
12 attachEvt("common", "unload", mresultUnload);
13 attachEvt("common", "run", mresultDoSearch);
14 attachEvt("result", "idsReceived", mresultSetRecords); 
15 attachEvt("result", "idsReceived", mresultCollectRecords); 
16
17
18 function mresultUnload() { removeChildren(table); table = null;}
19
20 function mresultDoSearch() {
21
22         if(getOffset() == 0) {
23                 swapCanvas($('loading_alt'));
24                 searchTimer = new Timer('searchTimer',$('loading_alt_span'));
25                 searchTimer.start();
26         }
27
28         table = G.ui.result.main_table;
29
30         while( table.parentNode.rows.length <= (getDisplayCount() + 1) )  /* add an extra row so IE and safari won't complain */
31                 table.appendChild(G.ui.result.row_template.cloneNode(true));
32
33         if(getOffset() == 0 || getHitCount() == null ) {
34                 if( getAdvTerm() && !getTerm() ) {
35                         if(getAdvType() == ADVTYPE_MULTI ) mresultCollectAdvIds();
36                         if(getAdvType() == ADVTYPE_MARC ) mresultCollectAdvMARCIds();
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 /*
58 function mresultGetCount() {
59         var form = (getForm() == "all") ? null : getForm();
60         var req = new Request(FETCH_MRCOUNT, 
61                         getStype(), getTerm(), getLocation(), getDepth(), form );
62         req.callback(mresultHandleCount);
63         req.send();
64 }
65
66 function mresultHandleCount(r) {
67         HITCOUNT = parseInt(r.getResultObject());
68         alert('mresultHandleCount()');
69         runEvt('result', 'hitCountReceived');
70 }
71 */
72
73
74 function mresultLoadCachedSearch() {
75         if( (getOffset() > 0) && (getOffset() < mresultPreCache) ) {
76                 var c = JSON2js(idsCookie.read(COOKIE_IDS));
77                 if(c) { records = c[0]; ranks = c[1]; }
78         }
79 }
80
81 function mresultTryCachedSearch() {
82         mresultLoadCachedSearch();
83         if(     getOffset() != 0 && records[getOffset()] != null && 
84                         records[resultFinalPageIndex()] != null) {
85
86                 runEvt('result', 'hitCountReceived');
87                 mresultCollectRecords(); 
88                 return true;
89         }
90         return false;
91 }
92
93
94 /* performs the actual search */
95 function mresultCollectIds(method) {
96         if(!mresultTryCachedSearch()) {
97                 var form = (getForm() == "all") ? null : getForm();
98                 var req = new Request(method, getStype(), getTerm(), 
99                         getLocation(), getDepth(), mresultPreCache, getOffset(), form );
100                 req.callback(mresultHandleMRIds);
101                 req.send();
102         }
103 }
104
105
106 function mresultCollectAdvIds() {
107         if(!mresultTryCachedSearch()) {
108                 var form = (getForm() == "all") ? null : getForm();
109                 var req = new Request(FETCH_ADV_MRIDS, 
110                         JSON2js(getAdvTerm()), getLocation(), form, mresultPreCache );
111                 req.callback(mresultHandleMRIds);
112                 req.send();
113         }
114 }
115
116 function mresultCollectAdvMARCIds() {
117         if(!mresultTryCachedSearch()) {
118                 var form = (getForm() == "all") ? null : getForm();
119                 var req = new Request(FETCH_ADV_MARC_MRIDS, 
120                         JSON2js(getAdvTerm()), getLocation(), form );
121                 req.callback(mresultHandleMRIds);
122                 req.send();
123         }
124 }
125
126
127
128 function mresultHandleMRIds(r) {
129         var res = r.getResultObject();
130
131         if(res.count != null) {
132                 if( getOffset() == 0 ) HITCOUNT = res.count;
133                 runEvt('result', 'hitCountReceived');
134         } 
135         runEvt('result', 'idsReceived', res.ids);
136 }
137
138 function mresultSetRecords(idstruct) {
139         if(!idstruct) return;
140         var o = getOffset();
141
142         for( var x = o; x < idstruct.length + o; x++ ) {
143                 if( idstruct[x-o] != null ) {
144                         var r = parseInt(idstruct[x - o][0]);
145                         var ra = parseFloat(idstruct[x - o][1]);
146                         var or = parseInt(idstruct[x - o][2]);
147                         if(!isNull(r) && !isNaN(r)) records[x] = r;
148                         if(!isNull(ra) && !isNaN(ra)) ranks[x] = ra;
149                         if(!isNull(or) && !isNaN(or)) onlyrecord[x] = or;
150                 }
151         }
152
153         if(getOffset() == 0) {
154                 idsCookie.remove(COOKIE_IDS);
155                 idsCookie.write(COOKIE_IDS, js2JSON([ records, ranks ]), '+1d' );
156                 //alert('Set cookies: ' + idsCookie.read(COOKIE_IDS) + ' : ' + idsCookie.read(COOKIE_IDS).length );
157         }
158
159         TOPRANK = ranks[getOffset()];
160 }
161
162 function mresultCollectRecords() {
163         if(getHitCount() > 0 ) runEvt("result", "preCollectRecords");
164         var i = 0;
165         for( var x = getOffset(); x!= getDisplayCount() + getOffset(); x++ ) {
166                 if(isNull(records[x])) break;
167                 if(isNaN(records[x])) continue;
168                 var req = new Request(FETCH_MRMODS, records[x]);
169                 req.request.userdata = i++;
170                 req.callback(mresultHandleMods);
171                 req.send();
172         }
173 }
174
175 function mresultHandleMods(r) {
176         var rec = r.getResultObject();
177         var pagePosition = r.userdata;
178         runEvt('result', 'recordReceived', rec, pagePosition, true);
179         resultCollectCopyCounts(rec, pagePosition, FETCH_MR_COPY_COUNTS);
180 }
181
182