summaryrefslogtreecommitdiffhomepage
path: root/themes/base/htdocs/luci-static/resources/xhr.js
blob: 9ce302683c466f26dda8610e5e043b9503f72f0c (plain)
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
/*
 * xhr.js - XMLHttpRequest helper class
 * (c) 2008-2010 Jo-Philipp Wich
 */

XHR = function()
{
	this.reinit = function()
	{
		if( window.XMLHttpRequest ) {
			this._xmlHttp = new XMLHttpRequest();
		}
		else if( window.ActiveXObject ) {
			this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else {
			alert("xhr.js: XMLHttpRequest is not supported by this browser!");
		}
	}

	this.busy = function() {
		switch( this._xmlHttp.readyState )
		{
			case 1:
			case 2:
			case 3:
				return true;

			default:
				return false;
		}
	}

	this.abort = function() {
		if( this.busy() )
			this._xmlHttp.abort();
	}

	this.get = function(url,data,callback)
	{
		this.reinit();

		var xhr  = this._xmlHttp;
		var code = this._encode( data );

		url = 'http://' + location.hostname +
			( location.port ? ':' + location.port : '' ) + url;

		if( code )
			if( url.substr(url.length-1,1) == '&' )
				url += code;
			else
				url += '?' + code;

		xhr.open( 'GET', url, true );

		xhr.onreadystatechange = function()
		{
			var json = null;
			if( xhr.getResponseHeader("Content-Type") == "application/json" ) {
				try {
					json = eval('(' + xhr.responseText + ')');
				}
				catch(e) {
					json = null;
				}
			}

			if( xhr.readyState == 4 ) {
				callback( xhr, json );
			}
		}

		xhr.send( null );
	}

	this.post = function(url,data,callback)
	{
		this.reinit();

		var xhr  = this._xmlHttp;
		var code = this._encode( data );

		xhr.onreadystatechange = function()
		{
			if( xhr.readyState == 4 )
				callback( xhr );
		}

		xhr.open( 'POST', url, true );
		xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
		xhr.setRequestHeader( 'Content-length', code.length );
		xhr.setRequestHeader( 'Connection', 'close' );
		xhr.send( code );
	}

	this.cancel = function()
	{
		this._xmlHttp.onreadystatechange = function(){};
		this._xmlHttp.abort();
	}

	this.send_form = function(form,callback,extra_values)
	{
		var code = '';

		for( var i = 0; i < form.elements.length; i++ )
		{
			var e = form.elements[i];

			if( e.options )
			{
				code += ( code ? '&' : '' ) +
					form.elements[i].name + '=' + encodeURIComponent(
						e.options[e.selectedIndex].value
					);
			}
			else if( e.length )
			{
				for( var j = 0; j < e.length; j++ )
					if( e[j].name ) {
						code += ( code ? '&' : '' ) +
							e[j].name + '=' + encodeURIComponent( e[j].value );
					}
			}
			else
			{
				code += ( code ? '&' : '' ) +
					e.name + '=' + encodeURIComponent( e.value );
			}
		}

		if( typeof extra_values == 'object' )
			for( var key in extra_values )
				code += ( code ? '&' : '' ) +
					key + '=' + encodeURIComponent( extra_values[key] );

		return(
			( form.method == 'get' )
				? this.get( form.getAttribute('action'), code, callback )
				: this.post( form.getAttribute('action'), code, callback )
		);
	}

	this._encode = function(obj)
	{
		if( typeof obj == 'object' )
		{
			var code = '';
			var self = this;

			for( var k in obj )
				code += ( code ? '&' : '' ) +
					k + '=' + encodeURIComponent( obj[k] );
			return code;
		}

		return obj;
	}
}