/
ajax.js
203 lines (169 loc) · 4.82 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var Ajax = {
defaults: {
method: 'get',
async: true,
format: function(data) { return data; }
},
send: function(options) {
extend(options, Ajax.defaults);
var xhr = new XMLHttpRequest(), response = new Response();
response.xhr = xhr;
if (options.user) {
xhr.open(options.method, options.url, options.async, options.user, options.password);
} else {
xhr.open(options.method, options.url, options.async);
}
if (options.headers) {
for (var i in options.headers) {
if (!options.headers.hasOwnProperty(i)) continue;
xhr.setRequestHeader(i, options.headers[i]);
}
}
if (options.progress) response.on('progress', options.complete);
if (options.complete) response.on('complete', options.complete);
if (options.error) response.on('error', options.error);
var lastIndex = 0, results;
xhr.onreadystatechange = function() {
if (xhr.readyState == 3) {
try {
results = options.format(xhr.responseText.substring(lastIndex));
lastIndex = xhr.responseText.length;
} catch(e) {}
response.trigger('progress', results);
} else if (xhr.readyState == 4) {
try {
results = options.format(xhr.responseText);
} catch(e) {
// formating error
alert(e);
}
response.trigger('complete', results);
}
}
xhr.send();
return response;
}
};
/**
* Represents a single service which can be
*/
var AjaxService = new Component({
extend: Component,
register: 'services service',
properties: ['prefix', 'user', 'password'],
events: ['progress', 'complete', 'error'],
constructor: function() {
var ajax = this.ajax = new AjaxEndpoint(), service = this;
this.headers = {};
this.prefix = '';
this.findAll('headers header').forEach(function(header) {
if (!header.hasAttribute('name') || !header.hasAttribute('value')) return;
this.headers[header.getAttribute('name')] = header.getAttribute('value');
});
this.calls = this.findAll('call').forEach(function(call) {
call.service = service;
});
},
send: function(method, url) {
return Ajax.send({
method: method,
url: this.prefix + url,
format: this.format,
headers: this.headers,
user: this.user,
password: this.password
});
},
format: function(data) {
if (data == '' || data == null) return null;
else return JSON.parse(data);
}
});
var AjaxCall = new Component({
extend: Component,
register: 'services service call',
properties: ['method', 'url', 'autoTrigger'],
events: ['progress', 'complete', 'error'],
constructor: function() {
this.css('display', 'none');
this.autoTrigger = false;
this.url = '';
this.method = 'get';
},
get autoTrigger() {
return this._autoTrigger || false;
},
set autoTrigger(value) {
if (this._autoTrigger == value) return;
this._autoTrigger = value;
clearInterval(this._autoTriggerInterval);
if (value === false) return;
this.trigger();
if (value === true || isNaN(value *= 1000)) return; // that's all
this._autoTriggerInterval = setInterval(this.trigger.boundTo(this), value);
},
trigger: function() {
this.service.send(this.method, this.url).on('complete', this.complete.boundTo(this));
},
progress: function(data) {
this.dispatchEvent(new DataEvent('progress', data));
},
complete: function(data) {
this.dispatchEvent(new DataEvent('complete', data));
},
error: function(status, data) {
this.dispatchEvent(new ErrorEvent('error', status, data));
}
});
var Response = new Class({
constructor: function() {
this.status = 'progress';
this.handlers = {complete: [], error: [], progress: []};
},
on: function(type, handler) {
if (!this.handlers.hasOwnProperty(type)) return;
var params = toArray(arguments).slice(1);
this.handlers[type].push(params);
},
un: function(type, handler) {
if (!this.handlers.hasOwnProperty(type)) {
return;
}
var handlers = this.handlers[type];
for (var i = 0; i < handlers.length; i++) {
if (handler == handlers[i][0]) {
handlers.splice(i--, 1);
}
}
},
triggerComplete: function(data) {
this.trigger('complete', data);
},
triggerError: function(error) {
this.trigger('error', error);
},
trigger: function(type, data) {
if (!this.handlers.hasOwnProperty(type)) return;
this.status = type;
var handlers = this.handlers[type];
while (handlers.length) {
var params = handlers.shift();
var handler = params[0];
params[0] = data;
var result = handler.apply(data, params);
if (result !== undefined) {
if (result instanceof Response) {
result.on('complete', this.triggerComplete.boundTo(this));
result.on('error', this.triggerError.boundTo(this));
return; // pick back up after this response is done
} else {
if (result instanceof Error && this.status == 'complete') {
this.status = 'error';
handlers = this.handlers.error;
}
data = result;
}
}
}
}
});