]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/common/js/RemoteRequest.js
01be6eb9e7b087c6b90bd9c4ae3fee6420d4f7a2
[working/Evergreen.git] / Open-ILS / web / opac / common / js / RemoteRequest.js
1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "gapines.org";
3 var XML_HTTP_MAX_TRIES = 3;
4
5 var IAMXUL = false;
6 function isXUL() { return IAMXUL; }
7
8 var _allrequests = {};
9
10 function cleanRemoteRequests() {
11         for( var i in _allrequests ) 
12                 destroyRequest(_allrequests[i]);
13 }
14
15 function destroyRequest(r) {
16         if(r == null) return;
17         r.xmlhttp.onreadystatechange = function(){};
18         r.xmlhttp                               = null;
19         r.callback                              = null;
20         r.userdata                              = null;
21         _allrequests[r.id]      = null;
22 }
23
24 /* ----------------------------------------------------------------------- */
25 /* Request object */
26 function RemoteRequest( service, method ) {
27
28
29         this.service    = service;
30         this.method             = method;
31         this.xmlhttp    = false;
32         this.name               = null;
33         this.sendCount = 0;
34
35         this.type               = "POST"; /* default */
36         this.id                 = service + method + Math.random();
37         this.cancelled = false;
38
39         _allrequests[this.id] = this;
40
41         var i = 2;
42         this.params = ""; 
43
44         while(i < arguments.length) {
45                 var object = js2JSON(arguments[i++]);
46                 this.params += "&param=" + encodeURIComponent(object);
47         }
48
49         if(!this.params) { this.params = ""; }
50         this.param_string = "service=" + service + "&method=" + method + this.params;
51         if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
52 }
53
54 /* constructs our XMLHTTPRequest object */
55 RemoteRequest.prototype.buildXMLRequest = function() {
56
57         var x;
58         try { 
59                 x = new ActiveXObject("Msxml2.XMLHTTP"); 
60         } catch (e) {
61                 try { 
62                         x = new ActiveXObject("Microsoft.XMLHTTP"); 
63                 } catch (E) {
64                         x = false;
65                 }
66         }
67
68         if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
69
70         if(!x) {
71                 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
72                 return null;
73         }
74
75         this.xmlhttp = x;
76         return true;
77 }
78
79
80 function _remoteRequestCallback(id) {
81
82         var object = _allrequests[id];
83         if(object.cancelled) return;
84
85         if( object.xmlhttp.readyState == 4 ) {
86                 try {
87                         object.callback(object);
88                 } catch(E) {
89
90                         /* if we receive a communication error, retry the request up
91                                 to XML_HTTP_MAX_TRIES attempts */
92                         if( instanceOf(E, EXCommunication) ) {
93
94                                 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
95                                         if(isXUL()) throw object;
96                                          else alert("Arrrgghh, Matey! Error communicating:\n" + E  + "\n" + object.param_string);
97                                 } else {
98                                         object.buildXMLRequest();
99                                         object.send();
100                                         return;
101                                 }
102                         } else { throw E; }
103
104                 } finally { 
105                         destroyRequest(object); 
106                         object = null; 
107                 }  
108         }
109 }
110
111
112 /* define the callback we use when this request has received
113         all of its data */
114 RemoteRequest.prototype.setCompleteCallback = function(callback) {
115         if(this.cancelled) return;
116         this.callback = callback;
117         var id = this.id;
118         this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
119 }
120
121
122 /* http by default.  This makes it https. *ONLY works when
123         embedded in a XUL app. */
124 RemoteRequest.prototype.setSecure = function(bool) {
125         this.secure = bool; 
126 }
127
128 RemoteRequest.prototype.send = function(blocking) {
129
130         if(this.cancelled) return;
131
132         /* determine the xmlhttp server dynamically */
133         var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
134
135         if(isXUL()) {
136                 if(this.secure)
137                         url =   "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
138                 else
139                         url =   "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
140         }
141
142         var data = null;
143
144         if( this.type == 'GET' ) { 
145                 url +=  "?" + this.param_string; 
146         }
147
148         if(blocking) {
149                 this.xmlhttp.open(this.type, url, false);
150         } else {
151                 this.xmlhttp.open(this.type, url, true);
152         }
153
154
155         if( this.type == 'POST' ) {
156                 data = this.param_string;
157                 this.xmlhttp.setRequestHeader('Content-Type',
158                                 'application/x-www-form-urlencoded');
159         }
160
161         this.xmlhttp.send( data );
162         this.sendCount += 1;
163         return this;
164 }
165
166 /* returns the actual response text from the request */
167 RemoteRequest.prototype.getText = function() {
168         return this.xmlhttp.responseText;
169 }
170
171 RemoteRequest.prototype.isReady = function() {
172         return this.xmlhttp.readyState == 4;
173 }
174
175
176 /* returns the JSON->js result object  */
177 RemoteRequest.prototype.getResultObject = function() {
178         if(this.cancelled) return null;
179
180         var text = this.xmlhttp.responseText;
181         var obj = JSON2js(text);
182
183         if(obj == null) return null;
184         if(obj.is_err)  throw new EXCommunication(obj.err_msg); 
185         if( obj[0] != null && obj[1] == null ) obj = obj[0];
186
187         /* these are user level exceptions from the server code */
188         if(instanceOf(obj, ex)) {
189                 if(!isXUL()) alert(obj.err_msg());
190                 throw obj;
191         }
192
193         if(instanceOf(obj, perm_ex)) {
194                 /* the opac will go ahead and spit out the error msg */
195                 if(!isXUL()) alert(obj.err_msg());
196                 throw obj;
197         }
198
199         return obj;
200 }
201
202 /* adds a new parameter to the request */
203 RemoteRequest.prototype.addParam = function(param) {
204         var string = encodeURIComponent(js2JSON(param));
205         this.param_string += "&param=" + string;
206 }
207