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