adding
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / mresult.js
1 var records = {};
2 var ranks = {};
3 var table;
4 var rowtemplate;
5 var idsCookie = new cookieObject("ids", 1, "/", COOKIE_IDS);
6
7 G.evt.common.unload.push(mresultUnload);
8 G.evt.mresult.run.push(mresultDoSearch);
9 G.evt.mresult.idsReceived.push(mresultSetRecords, mresultCollectRecords); 
10
11 function mresultUnload() { removeChildren(table); table = null;}
12
13 function mresultDoSearch() {
14
15         table = G.ui.result.main_table;
16
17         hideMe(G.ui.result.row_template);
18         while( table.parentNode.rows.length <= getDisplayCount() )  /* add an extra row so IE and safari won't complain */
19                 hideMe(table.appendChild(G.ui.result.row_template.cloneNode(true)));
20
21         if(getOffset() == 0 || getHitCount() == null ) {
22         //      mresultGetCount(); 
23                 mresultCollectIds(FETCH_MRIDS_FULL); 
24         } else { 
25                 runEvt('result', 'hitCountReceived');
26                 mresultCollectIds(FETCH_MRIDS);
27         }
28 }
29
30 function mresultGetCount() {
31         var form = (getForm() == "all") ? null : getForm();
32         var req = new Request(FETCH_MRCOUNT, 
33                         getStype(), getTerm(), getLocation(), getDepth(), form );
34         req.callback(mresultHandleCount);
35         req.send();
36 }
37
38 function mresultHandleCount(r) {
39         HITCOUNT = parseInt(r.getResultObject());
40         runEvt('result', 'hitCountReceived');
41 }
42
43
44 /* performs the actual search */
45 function mresultCollectIds(method) {
46
47         if(getOffset() == 0) {
48                 idsCookie.put(COOKIE_IDS,"");
49                 idsCookie.write();
50
51         } else {
52                 var c = JSON2js(idsCookie.get(COOKIE_IDS));
53                 if(c && c.recs) { records = c.recs; ranks = c.ranks; } 
54         }
55
56         if(     getOffset() != 0 && 
57                         records[getOffset()] != null && 
58                         records[resultFinalPageIndex()] != null) {
59                         mresultCollectRecords(); 
60
61         } else {
62
63                 var req = new Request(method, getStype(), getTerm(), 
64                         getLocation(), getDepth(), getDisplayCount() * 10, getOffset(), getForm() );
65                 req.callback(mresultHandleMRIds);
66                 req.send();
67         }
68 }
69
70 function mresultHandleMRIds(r) {
71         var res = r.getResultObject();
72
73         if(res.count != null) {
74                 HITCOUNT = res.count;
75                 runEvt('result', 'hitCountReceived');
76         } 
77         runEvt('mresult', 'idsReceived', res.ids);
78 }
79
80 function mresultSetRecords(idstruct) {
81         var o = getOffset();
82         for( var x = o; x < idstruct.length + o; x++ ) {
83                 if(idstruct[x-o] == null) break;
84                 records[x] = parseInt(idstruct[x - o][0]);
85                 ranks[x] = parseFloat(idstruct[x - o][1]);
86         }
87         idsCookie.put(COOKIE_IDS, js2JSON({ recs: records, ranks : ranks }) );
88         idsCookie.write();
89 }
90
91 function mresultCollectRecords() {
92         var i = 0;
93         for( var x = getOffset(); x!= getDisplayCount() + getOffset(); x++ ) {
94                 if(isNull(records[x])) break;
95                 var req = new Request(FETCH_MRMODS, records[x]);
96                 req.request.userdata = i++;
97                 req.callback(mresultHandleMods);
98                 req.send();
99         }
100 }
101
102 function mresultHandleMods(r) {
103         var rec = r.getResultObject();
104         var pagePosition = r.userdata;
105         runEvt('result', 'recordReceived', rec, pagePosition, true);
106         resultCollectCopyCounts(rec, pagePosition, FETCH_MR_COPY_COUNTS);
107 }
108
109