This commit breaks compatibility with OpenSRF 0.9.
[Evergreen.git] / Open-ILS / src / javascript / backend / libs / jsonopensrfrequest.js
1 try {
2         load_lib('jsOO.js');
3         load_lib('JSON_v1.js');
4 } catch (e) {}
5
6 var __jsonopensrfreqid = 1;
7 var __jsonopensrfreq_hash = {};
8
9 function JSONOpenSRFRequest () {
10         // Cache this for later ...
11         this._hash_id = __jsonopensrfreqid;
12         __jsonopensrfreqid++;
13 }
14
15 JSONOpenSRFRequest.method('create',function (service) {
16         this._service = service;
17         __jsonopensrfreq_hash['id' + this._hash_id] = {};
18 });
19
20 JSONOpenSRFRequest.method('open',function (service, method, async) {
21         this._service = service;
22         this._method = method;
23         this._async = (async ? 1 : 0);
24         __jsonopensrfreq_hash['id' + this._hash_id] = {};
25 });
26
27 JSONOpenSRFRequest.method('close',function () {
28         this._service = null;
29         this._method = null;
30         this._async = null;
31         __jsonopensrfreq_hash['id' + this._hash_id] = {};
32 });
33
34 JSONOpenSRFRequest.method('call',function (method, async) {
35         this._method = method;
36         this._async = (async ? 1 : 0);
37 });
38
39 JSONOpenSRFRequest.method('connect', function (service) {
40
41         if (service) this._service = service;
42
43         if (!this._service)
44                 throw "call .open with a service before calling .connect";
45         try {
46                 _OILS_FUNC_jsonopensrfrequest_connect(this._hash_id,this._service);
47         } catch (e) {
48                 alert("Sorry, no JSONOpenSRFRequest support");
49         }
50
51         this.connected = __jsonopensrfreq_hash['id' + this._hash_id].connected;
52 });
53
54 JSONOpenSRFRequest.method('disconnect', function () {
55
56         if (!this._service)
57                 throw "call .connect before calling .disconnect";
58         try {
59                 _OILS_FUNC_jsonopensrfrequest_disconnect(this._hash_id);
60         } catch (e) {
61                 alert("Sorry, no JSONOpenSRFRequest support");
62         }
63
64         this.connected = __jsonopensrfreq_hash['id' + this._hash_id].connected;
65 });
66
67 JSONOpenSRFRequest.method('finish', function () {
68
69         if (!this._service)
70                 throw "call .connect before calling .finish";
71         try {
72                 _OILS_FUNC_jsonopensrfrequest_disconnect(this._hash_id);
73                 _OILS_FUNC_jsonopensrfrequest_finish(this._hash_id);
74         } catch (e) {
75                 alert("Sorry, no JSONOpenSRFRequest support");
76         }
77
78         this.connected = __jsonopensrfreq_hash['id' + this._hash_id].connected;
79 });
80
81 JSONOpenSRFRequest.method('send', function () {
82
83         if (!this._service)
84                 throw "call .open with a service and a method before calling .send";
85
86         var data = [];
87         for (var i = 0; i < arguments.length; i++) {
88                 data[i] = arguments[i];
89         }
90
91         try {
92                 //log_debug( this._hash_id + " -> " + this._service + " -> " + this._method + " -> " + this._async + " -> " + js2JSON(data));
93                 _OILS_FUNC_jsonopensrfrequest_send(this._hash_id,this._service,this._method,this._async,js2JSON(data));
94         } catch (e) {
95                 alert("Sorry, no JSONOpenSRFRequest support");
96         }
97
98         this.responseText = __jsonopensrfreq_hash['id' + this._hash_id].responseText;
99         this.readyState = __jsonopensrfreq_hash['id' + this._hash_id].readyState;
100         this.status = __jsonopensrfreq_hash['id' + this._hash_id].status;
101         this.statusText = __jsonopensrfreq_hash['id' + this._hash_id].statusText;
102         this.responseJSON = JSON2js(this.responseText);
103
104         if (this._async)
105                 this.onreadystatechange();
106 });
107
108