Passed
Push — master ( b15b71...ab24e8 )
by Paul
04:46
created

+/scripts/public/ajax.js   A

Complexity

Total Complexity 14
Complexity/F 1.4

Size

Lines of Code 61
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 6
dl 0
loc 61
rs 10
wmc 14
mnd 2
bc 11
fnc 10
bpm 1.1
cpm 1.4
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A GLSR.Ajax 0 56 1
1
/** global: GLSR, XMLHttpRequest */
2
;(function() {
3
4
	'use strict';
5
6
	GLSR.Ajax = function() {
7
		/** @return void */
8
		this.get = function( url, successCallback ) {
9
			var xhr = new XMLHttpRequest();
10
			xhr.open( 'GET', url );
11
			xhr.onreadystatechange = function() {
12
				if( xhr.readyState !== 4 )return;
13
				successCallback( xhr.responseText );
14
			};
15
			xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' );
16
			xhr.send();
17
		};
18
19
		/** @return bool */
20
		this.isFileAPISupported = function() {
21
			var input = document.createElement( 'INPUT' );
22
			input.type = 'file';
23
			return 'files' in input;
24
		};
25
26
		/** @return bool */
27
		this.isFormDataSupported = function() {
28
			return !!window.FormData;
29
		};
30
31
		/** @return bool */
32
		this.isUploadSupported = function() {
33
			var xhr = new XMLHttpRequest();
34
			return !!( xhr && ( 'upload' in xhr ) && ( 'onprogress' in xhr.upload ));
35
		};
36
37
		/** @return void */
38
		this.post = function( data, successCallback, headers ) {
39
			var xhr = new XMLHttpRequest();
40
			this.setHeaders_( xhr, headers );
41
			xhr.open( 'POST', GLSR.ajaxurl );
42
			xhr.onreadystatechange = function() {
43
				if( xhr.readyState !== 4 )return;
44
				successCallback( JSON.parse( xhr.responseText ));
45
			};
46
			xhr.send({
47
				action: GLSR.action,
48
				request: data,
49
			});
50
		};
51
52
		/** @return void */
53
		this.setHeaders_ = function( xhr, headers ) {
54
			headers = headers || {};
55
			headers['X-Requested-With'] = 'XMLHttpRequest';
56
			for( var key in headers ) {
57
				if( !headers.hasOwnProperty( key ))continue;
58
				xhr.setRequestHeader( key, headers[key] );
59
			}
60
		};
61
	};
62
})();
63