]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/mresult.js
46ba55d47f59e83163f994d43fe812414ff7803e
[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         //idsCookie = new HTTP.Cookies();
22
23         if(getOffset() == 0) {
24                 swapCanvas($('loading_alt'));
25                 searchTimer = new Timer('searchTimer',$('loading_alt_span'));
26                 searchTimer.start();
27         }
28
29         table = G.ui.result.main_table;
30
31         while( table.parentNode.rows.length <= (getDisplayCount() + 1) )  /* add an extra row so IE and safari won't complain */
32                 table.appendChild(G.ui.result.row_template.cloneNode(true));
33
34         if(getOffset() == 0 || getHitCount() == null ) 
35                 mresultCollectIds(FETCH_MRIDS_FULL); 
36         else  
37                 mresultCollectIds(FETCH_MRIDS);
38 }
39
40 function mresultGetCount() {
41         var form = (getForm() == "all") ? null : getForm();
42         var req = new Request(FETCH_MRCOUNT, 
43                         getStype(), getTerm(), getLocation(), getDepth(), form );
44         req.callback(mresultHandleCount);
45         req.send();
46 }
47
48 function mresultHandleCount(r) {
49         HITCOUNT = parseInt(r.getResultObject());
50         runEvt('result', 'hitCountReceived');
51 }
52
53
54 /* performs the actual search */
55 function mresultCollectIds(method) {
56
57         if( (getOffset() > 0) && (getOffset() < mresultPreCache) ) {
58                 //alert('cached: ' + idsCookie.read(COOKIE_IDS));
59                 var c = JSON2js(idsCookie.read(COOKIE_IDS));
60                 if(c) { records = c[0]; ransk = c[1]; }
61         }
62
63         if(     getOffset() != 0 && 
64                         records[getOffset()] != null && 
65                         records[resultFinalPageIndex()] != null) {
66                         //alert('we have cookies...  offset : ' + getOffset() );
67                         runEvt('result', 'hitCountReceived');
68                         mresultCollectRecords(); 
69
70         } else {
71
72                 var form = (getForm() == "all") ? null : getForm();
73                 var req = new Request(method, getStype(), getTerm(), 
74                         getLocation(), getDepth(), mresultPreCache, getOffset(), form );
75                 req.callback(mresultHandleMRIds);
76                 req.send();
77         }
78 }
79
80 function mresultHandleMRIds(r) {
81         var res = r.getResultObject();
82
83         if(res.count != null) {
84                 if( getOffset() == 0 ) HITCOUNT = res.count;
85                 runEvt('result', 'hitCountReceived');
86         } 
87         runEvt('result', 'idsReceived', res.ids);
88 }
89
90 function mresultSetRecords(idstruct) {
91         if(!idstruct) return;
92         var o = getOffset();
93
94         for( var x = o; x < idstruct.length + o; x++ ) {
95                 if( idstruct[x-o] != null ) {
96                         var r = parseInt(idstruct[x - o][0]);
97                         var ra = parseFloat(idstruct[x - o][1]);
98                         var or = parseInt(idstruct[x - o][2]);
99                         if(!isNull(r) && !isNaN(r)) records[x] = r;
100                         if(!isNull(ra) && !isNaN(ra)) ranks[x] = ra;
101                         if(!isNull(or) && !isNaN(or)) onlyrecord[x] = or;
102                 }
103         }
104
105         if(getOffset() == 0) {
106                 idsCookie.remove(COOKIE_IDS);
107                 idsCookie.write(COOKIE_IDS, js2JSON([ records, ranks ]), '+1d' );
108                 //alert('Set cookies: ' + idsCookie.read(COOKIE_IDS) + ' : ' + idsCookie.read(COOKIE_IDS).length );
109         }
110
111         TOPRANK = ranks[getOffset()];
112 }
113
114 function mresultCollectRecords() {
115         if(getHitCount() > 0 ) runEvt("result", "preCollectRecords");
116         var i = 0;
117         for( var x = getOffset(); x!= getDisplayCount() + getOffset(); x++ ) {
118                 if(isNull(records[x])) break;
119                 if(isNaN(records[x])) continue;
120                 var req = new Request(FETCH_MRMODS, records[x]);
121                 req.request.userdata = i++;
122                 req.callback(mresultHandleMods);
123                 req.send();
124         }
125 }
126
127 function mresultHandleMods(r) {
128         var rec = r.getResultObject();
129         var pagePosition = r.userdata;
130         runEvt('result', 'recordReceived', rec, pagePosition, true);
131         resultCollectCopyCounts(rec, pagePosition, FETCH_MR_COPY_COUNTS);
132 }
133
134