bb45b10ad916f463d48dce39855296947eea1300
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / mresult.js
1 var records = {};
2 var ranks = {};
3 var onlyrecord = {};
4 var table;
5 var idsCookie = new cookieObject("ids", 1, "/", COOKIE_IDS);
6 var searchTimer;
7
8 attachEvt("common", "unload", mresultUnload);
9 attachEvt("common", "run", mresultDoSearch);
10 attachEvt("result", "idsReceived", mresultSetRecords); 
11 attachEvt("result", "idsReceived", mresultCollectRecords); 
12
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                 mresultCollectIds(FETCH_MRIDS_FULL); 
31         else  
32                 mresultCollectIds(FETCH_MRIDS);
33 }
34
35 function mresultGetCount() {
36         var form = (getForm() == "all") ? null : getForm();
37         var req = new Request(FETCH_MRCOUNT, 
38                         getStype(), getTerm(), getLocation(), getDepth(), form );
39         req.callback(mresultHandleCount);
40         req.send();
41 }
42
43 function mresultHandleCount(r) {
44         HITCOUNT = parseInt(r.getResultObject());
45         runEvt('result', 'hitCountReceived');
46 }
47
48
49 /* performs the actual search */
50 function mresultCollectIds(method) {
51
52         if(getOffset() == 0) {
53                 idsCookie.put(COOKIE_IDS,"[]");
54                 idsCookie.write();
55
56         } else {
57                 var c = JSON2js(idsCookie.get(COOKIE_IDS));
58                 if(c && c.recs) { records = c.recs; ranks = c.ranks; } 
59         }
60
61         if(     getOffset() != 0 && 
62                         records[getOffset()] != null && 
63                         records[resultFinalPageIndex()] != null) {
64                         runEvt('result', 'hitCountReceived');
65                         mresultCollectRecords(); 
66
67         } else {
68
69                 var form = (getForm() == "all") ? null : getForm();
70                 var req = new Request(method, getStype(), getTerm(), 
71                         getLocation(), getDepth(), 100, getOffset(), form );
72                 req.callback(mresultHandleMRIds);
73                 req.send();
74         }
75 }
76
77 function mresultHandleMRIds(r) {
78         var res = r.getResultObject();
79
80         if(res.count != null) {
81                 if( getOffset() == 0 ) HITCOUNT = res.count;
82                 runEvt('result', 'hitCountReceived');
83         } 
84         runEvt('result', 'idsReceived', res.ids);
85 }
86
87 function mresultSetRecords(idstruct) {
88         if(!idstruct) return;
89         var o = getOffset();
90         for( var x = o; x < idstruct.length + o; x++ ) {
91                 if(idstruct[x-o] == null) break;
92                 records[x] = parseInt(idstruct[x - o][0]);
93                 ranks[x] = parseFloat(idstruct[x - o][1]);
94                 onlyrecord[x] = parseInt(idstruct[x - o][2]);
95         }
96         idsCookie.put(COOKIE_IDS, js2JSON({ recs: records, ranks : ranks }) );
97         idsCookie.write();
98         TOPRANK = ranks[getOffset()];
99 }
100
101 function mresultCollectRecords() {
102         if(getHitCount() > 0 ) runEvt("result", "preCollectRecords");
103         var i = 0;
104         for( var x = getOffset(); x!= getDisplayCount() + getOffset(); x++ ) {
105                 if(isNull(records[x])) break;
106                 if(isNaN(records[x])) continue;
107                 var req = new Request(FETCH_MRMODS, records[x]);
108                 req.request.userdata = i++;
109                 req.callback(mresultHandleMods);
110                 req.send();
111         }
112 }
113
114 function mresultHandleMods(r) {
115         var rec = r.getResultObject();
116         var pagePosition = r.userdata;
117         runEvt('result', 'recordReceived', rec, pagePosition, true);
118         resultCollectCopyCounts(rec, pagePosition, FETCH_MR_COPY_COUNTS);
119 }
120
121