This repository was archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathrx.lite.dom.ajax.compat.js
453 lines (396 loc) · 13.8 KB
/
rx.lite.dom.ajax.compat.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (c) Microsoft, Inc. All rights reserved. See License.txt in the project root for license information.
;(function (factory) {
var objectTypes = {
'function': true,
'object': true
};
function checkGlobal(value) {
return (value && value.Object === Object) ? value : null;
}
var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType) ? exports : null;
var freeModule = (objectTypes[typeof module] && module && !module.nodeType) ? module : null;
var freeGlobal = checkGlobal(freeExports && freeModule && typeof global === 'object' && global);
var freeSelf = checkGlobal(objectTypes[typeof self] && self);
var freeWindow = checkGlobal(objectTypes[typeof window] && window);
var moduleExports = (freeModule && freeModule.exports === freeExports) ? freeExports : null;
var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
var root = freeGlobal || ((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) || freeSelf || thisGlobal || Function('return this')();
// Because of build optimizers
if (typeof define === 'function' && define.amd) {
define(['rx-lite-compat'], function (Rx, exports) {
return factory(root, exports, Rx);
});
} else if (typeof module === 'object' && module && module.exports === freeExports) {
module.exports = factory(root, module.exports, require('rx-lite-compat'));
} else {
root.Rx = factory(root, {}, root.Rx);
}
}.call(this, function (root, exp, Rx, undefined) {
var Observable = Rx.Observable,
ObservableBase = Rx.ObservableBase,
dom = Rx.DOM || (Rx.DOM = {}),
hasOwnProperty = {}.hasOwnProperty,
inherits = Rx.internals.inherits;
var errorObj = {e: {}};
function tryCatcherGen(tryCatchTarget) {
return function tryCatcher() {
try {
return tryCatchTarget.apply(this, arguments);
} catch (e) {
errorObj.e = e;
return errorObj;
}
};
}
function tryCatch(fn) {
if (!isFunction(fn)) { throw new TypeError('fn must be a function'); }
return tryCatcherGen(fn);
}
function thrower(e) {
throw e;
}
// Gets the proper XMLHttpRequest for support for older IE
function getXMLHttpRequest() {
if (root.XMLHttpRequest) {
return new root.XMLHttpRequest();
} else {
var progId;
try {
var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
for(var i = 0; i < 3; i++) {
try {
progId = progIds[i];
if (new root.ActiveXObject(progId)) {
break;
}
} catch(e) { }
}
return new root.ActiveXObject(progId);
} catch (e) {
throw new Error('XMLHttpRequest is not supported by your browser');
}
}
}
// Get CORS support even for older IE
function getCORSRequest() {
var xhr = new root.XMLHttpRequest();
if ('withCredentials' in xhr) {
xhr.withCredentials = true;
return xhr;
} else if (!!root.XDomainRequest) {
return new XDomainRequest();
} else {
throw new Error('CORS is not supported by your browser');
}
}
function normalizeAjaxSuccessEvent(e, xhr, settings) {
var response = ('response' in xhr) ? xhr.response : xhr.responseText;
response = settings.responseType === 'json' ? JSON.parse(response) : response;
return {
response: response,
status: xhr.status,
responseType: xhr.responseType,
xhr: xhr,
originalEvent: e
};
}
function normalizeAjaxErrorEvent(e, xhr, type) {
return {
type: type,
status: xhr.status,
xhr: xhr,
originalEvent: e
};
}
var AjaxObservable = (function(__super__) {
inherits(AjaxObservable, __super__);
function AjaxObservable(settings) {
this._settings = settings;
__super__.call(this);
}
AjaxObservable.prototype.subscribeCore = function (o) {
var state = { isDone: false };
var xhr;
var settings = this._settings;
var normalizeError = settings.normalizeError;
var normalizeSuccess = settings.normalizeSuccess;
var processResponse = function(xhr, e){
var status = xhr.status === 1223 ? 204 : xhr.status;
if ((status >= 200 && status <= 300) || status === 0 || status === '') {
o.onNext(normalizeSuccess(e, xhr, settings));
o.onCompleted();
} else {
o.onError(settings.normalizeError(e, xhr, 'error'));
}
state.isDone = true;
};
try {
xhr = settings.createXHR();
} catch (err) {
return o.onError(err);
}
try {
if (settings.user) {
xhr.open(settings.method, settings.url, settings.async, settings.user, settings.password);
} else {
xhr.open(settings.method, settings.url, settings.async);
}
var headers = settings.headers;
for (var header in headers) {
if (hasOwnProperty.call(headers, header)) {
xhr.setRequestHeader(header, headers[header]);
}
}
xhr.timeout = settings.timeout;
xhr.ontimeout = function (e) {
settings.progressObserver && settings.progressObserver.onError(e);
o.onError(normalizeError(e, xhr, 'timeout'));
};
if(!!xhr.upload || (!('withCredentials' in xhr) && !!root.XDomainRequest)) {
xhr.onload = function(e) {
if(settings.progressObserver) {
settings.progressObserver.onNext(e);
settings.progressObserver.onCompleted();
}
processResponse(xhr, e);
};
if(settings.progressObserver) {
xhr.onprogress = function(e) {
settings.progressObserver.onNext(e);
};
}
xhr.onerror = function(e) {
settings.progressObserver && settings.progressObserver.onError(e);
o.onError(normalizeError(e, xhr, 'error'));
state.isDone = true;
};
xhr.onabort = function(e) {
settings.progressObserver && settings.progressObserver.onError(e);
o.onError(normalizeError(e, xhr, 'abort'));
state.isDone = true;
};
} else {
xhr.onreadystatechange = function (e) {
xhr.readyState === 4 && processResponse(xhr, e);
};
}
var contentType = settings.headers['Content-Type'] ||
settings.headers['Content-type'] ||
settings.headers['content-type'];
if (settings.hasContent && contentType === 'application/x-www-form-urlencoded' && typeof settings.body !== 'string') {
var newBody = [];
for (var prop in settings.body) {
if (hasOwnProperty.call(settings.body, prop)) {
newBody.push(prop + '=' + settings.body[prop]);
}
}
settings.body = newBody.join('&');
}
xhr.send(settings.hasContent && settings.body || null);
} catch (e) {
o.onError(e);
}
return new AjaxDisposable(state, xhr);
};
function AjaxDisposable(state, xhr) {
this._state = state;
this._xhr = xhr;
this.isDisposed = false;
}
AjaxDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
if (!this._state.isDone && this._xhr.readyState !== 4) { this._xhr.abort(); }
}
};
return AjaxObservable;
}(ObservableBase));
/**
* Creates an observable for an Ajax request with either a settings object with url, headers, etc or a string for a URL.
*
* @example
* source = Rx.DOM.ajax('/products');
* source = Rx.DOM.ajax( url: 'products', method: 'GET' });
*
* @param {Object} settings Can be one of the following:
*
* A string of the URL to make the Ajax call.
* An object with the following properties
* - url: URL of the request
* - body: The body of the request
* - method: Method of the request, such as GET, POST, PUT, PATCH, DELETE
* - async: Whether the request is async
* - headers: Optional headers
* - crossDomain: true if a cross domain request, else false
*
* @returns {Observable} An observable sequence containing the XMLHttpRequest.
*/
var ajaxRequest = dom.ajax = function (options) {
var settings = {
method: 'GET',
crossDomain: false,
async: true,
headers: {},
responseType: 'text',
timeout: 0,
createXHR: function(){
return this.crossDomain ? getCORSRequest() : getXMLHttpRequest()
},
normalizeError: normalizeAjaxErrorEvent,
normalizeSuccess: normalizeAjaxSuccessEvent
};
if(typeof options === 'string') {
settings.url = options;
} else {
for(var prop in options) {
if(hasOwnProperty.call(options, prop)) {
settings[prop] = options[prop];
}
}
}
if (!settings.crossDomain && !settings.headers['X-Requested-With']) {
settings.headers['X-Requested-With'] = 'XMLHttpRequest';
}
settings.hasContent = settings.body !== undefined;
return new AjaxObservable(settings);
};
/**
* Creates an observable sequence from an Ajax POST Request with the body.
*
* @param {String} url The URL to POST
* @param {Object} body The body to POST
* @returns {Observable} The observable sequence which contains the response from the Ajax POST.
*/
dom.post = function (url, body) {
var settings;
if (typeof url === 'string') {
settings = {url: url, body: body, method: 'POST' };
} else if (typeof url === 'object') {
settings = url;
settings.method = 'POST';
}
return ajaxRequest(settings);
};
/**
* Creates an observable sequence from an Ajax GET Request with the body.
*
* @param {String} url The URL to GET
* @returns {Observable} The observable sequence which contains the response from the Ajax GET.
*/
dom.get = function (url) {
var settings;
if (typeof url === 'string') {
settings = {url: url };
} else if (typeof url === 'object') {
settings = url;
}
return ajaxRequest(settings);
};
/**
* Creates an observable sequence from JSON from an Ajax request
*
* @param {String} url The URL to GET
* @returns {Observable} The observable sequence which contains the parsed JSON.
*/
dom.getJSON = function (url) {
if (!root.JSON && typeof root.JSON.parse !== 'function') { throw new TypeError('JSON is not supported in your runtime.'); }
return ajaxRequest({url: url, responseType: 'json'}).map(function (x) {
return x.response;
});
};
var destroy = (function () {
var trash = 'document' in root && root.document.createElement('div');
return function (element) {
trash.appendChild(element);
trash.innerHTML = '';
};
})();
var ScriptObservable = (function(__super__) {
inherits(ScriptObservable, __super__);
function ScriptObservable(settings) {
this._settings = settings;
__super__.call(this);
}
ScriptObservable.id = 0;
ScriptObservable.prototype.subscribeCore = function (o) {
var settings = {
jsonp: 'JSONPCallback',
async: true,
jsonpCallback: 'rxjsjsonpCallbacks' + 'callback_' + (ScriptObservable.id++).toString(36)
};
if(typeof this._settings === 'string') {
settings.url = this._settings;
} else {
for(var prop in this._settings) {
if(hasOwnProperty.call(this._settings, prop)) {
settings[prop] = this._settings[prop];
}
}
}
var script = root.document.createElement('script');
script.type = 'text/javascript';
script.async = settings.async;
script.src = settings.url.replace(settings.jsonp, settings.jsonpCallback);
root[settings.jsonpCallback] = function(data) {
root[settings.jsonpCallback].called = true;
root[settings.jsonpCallback].data = data;
};
var handler = function(e) {
if(e.type === 'load' && !root[settings.jsonpCallback].called) {
e = { type: 'error' };
}
var status = e.type === 'error' ? 400 : 200;
var data = root[settings.jsonpCallback].data;
if(status === 200) {
o.onNext({
status: status,
responseType: 'jsonp',
response: data,
originalEvent: e
});
o.onCompleted();
}
else {
o.onError({
type: 'error',
status: status,
originalEvent: e
});
}
};
script.onload = script.onreadystatechanged = script.onerror = handler;
var head = root.document.getElementsByTagName('head')[0] || root.document.documentElement;
head.insertBefore(script, head.firstChild);
return new ScriptDisposable(script);
};
function ScriptDisposable(script) {
this._script = script;
this.isDisposed = false;
}
ScriptDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
this._script.onload = this._script.onreadystatechanged = this._script.onerror = null;
destroy(this._script);
this._script = null;
}
};
return ScriptObservable;
}(ObservableBase));
/**
* Creates an observable JSONP Request with the specified settings.
* @param {Object} settings Can be one of the following:
*
* A string of the URL to make the JSONP call with the JSONPCallback=? in the url.
* An object with the following properties
* - url: URL of the request
* - jsonp: The named callback parameter for the JSONP call
* - jsonpCallback: Callback to execute. For when the JSONP callback can't be changed
*
* @returns {Observable} A cold observable containing the results from the JSONP call.
*/
dom.jsonpRequest = function (settings) {
return new ScriptObservable(settings);
};
return Rx;
}));