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