modules/admin-full: register missing wireless actions
[project/luci.git] / themes / base / htdocs / luci-static / resources / xhr.js
1 /*
2 * xhr.js - XMLHttpRequest helper class
3 * (c) 2008-2010 Jo-Philipp Wich
4 */
5
6 XHR = function()
7 {
8 this.reinit = function()
9 {
10 if( window.XMLHttpRequest ) {
11 this._xmlHttp = new XMLHttpRequest();
12 }
13 else if( window.ActiveXObject ) {
14 this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
15 }
16 else {
17 alert("xhr.js: XMLHttpRequest is not supported by this browser!");
18 }
19 }
20
21 this.busy = function() {
22 switch( this._xmlHttp.readyState )
23 {
24 case 1:
25 case 2:
26 case 3:
27 return true;
28
29 default:
30 return false;
31 }
32 }
33
34 this.abort = function() {
35 if( this.busy() )
36 this._xmlHttp.abort();
37 }
38
39 this.get = function(url,data,callback)
40 {
41 this.reinit();
42
43 var xhr = this._xmlHttp;
44 var code = this._encode( data );
45
46 url = location.protocol + '//' + location.host + url;
47
48 if( code )
49 if( url.substr(url.length-1,1) == '&' )
50 url += code;
51 else
52 url += '?' + code;
53
54 xhr.open( 'GET', url, true );
55
56 xhr.onreadystatechange = function()
57 {
58 if( xhr.readyState == 4 ) {
59 var json = null;
60 if( xhr.getResponseHeader("Content-Type") == "application/json" ) {
61 try {
62 json = eval('(' + xhr.responseText + ')');
63 }
64 catch(e) {
65 json = null;
66 }
67 }
68
69 callback( xhr, json );
70 }
71 }
72
73 xhr.send( null );
74 }
75
76 this.post = function(url,data,callback)
77 {
78 this.reinit();
79
80 var xhr = this._xmlHttp;
81 var code = this._encode( data );
82
83 xhr.onreadystatechange = function()
84 {
85 if( xhr.readyState == 4 )
86 callback( xhr );
87 }
88
89 xhr.open( 'POST', url, true );
90 xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
91 xhr.setRequestHeader( 'Content-length', code.length );
92 xhr.setRequestHeader( 'Connection', 'close' );
93 xhr.send( code );
94 }
95
96 this.cancel = function()
97 {
98 this._xmlHttp.onreadystatechange = function(){};
99 this._xmlHttp.abort();
100 }
101
102 this.send_form = function(form,callback,extra_values)
103 {
104 var code = '';
105
106 for( var i = 0; i < form.elements.length; i++ )
107 {
108 var e = form.elements[i];
109
110 if( e.options )
111 {
112 code += ( code ? '&' : '' ) +
113 form.elements[i].name + '=' + encodeURIComponent(
114 e.options[e.selectedIndex].value
115 );
116 }
117 else if( e.length )
118 {
119 for( var j = 0; j < e.length; j++ )
120 if( e[j].name ) {
121 code += ( code ? '&' : '' ) +
122 e[j].name + '=' + encodeURIComponent( e[j].value );
123 }
124 }
125 else
126 {
127 code += ( code ? '&' : '' ) +
128 e.name + '=' + encodeURIComponent( e.value );
129 }
130 }
131
132 if( typeof extra_values == 'object' )
133 for( var key in extra_values )
134 code += ( code ? '&' : '' ) +
135 key + '=' + encodeURIComponent( extra_values[key] );
136
137 return(
138 ( form.method == 'get' )
139 ? this.get( form.getAttribute('action'), code, callback )
140 : this.post( form.getAttribute('action'), code, callback )
141 );
142 }
143
144 this._encode = function(obj)
145 {
146 obj = obj ? obj : { };
147 obj['_'] = Math.random();
148
149 if( typeof obj == 'object' )
150 {
151 var code = '';
152 var self = this;
153
154 for( var k in obj )
155 code += ( code ? '&' : '' ) +
156 k + '=' + encodeURIComponent( obj[k] );
157
158 return code;
159 }
160
161 return obj;
162 }
163 }