themes/base: add JavaScript XMLHTTPRequest support library and load icon
[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 = 'http://' + location.hostname +
47 ( location.port ? ':' + location.port : '' ) + url;
48
49 if( code )
50 if( url.substr(url.length-1,1) == '&' )
51 url += code;
52 else
53 url += '?' + code;
54
55 xhr.open( 'GET', url, true );
56
57 xhr.onreadystatechange = function()
58 {
59 if( xhr.readyState == 4 ) {
60 callback( xhr );
61 }
62 }
63
64 xhr.send( null );
65 }
66
67 this.post = function(url,data,callback)
68 {
69 this.reinit();
70
71 var xhr = this._xmlHttp;
72 var code = this._encode( data );
73
74 xhr.onreadystatechange = function()
75 {
76 if( xhr.readyState == 4 )
77 callback( xhr );
78 }
79
80 xhr.open( 'POST', url, true );
81 xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
82 xhr.setRequestHeader( 'Content-length', code.length );
83 xhr.setRequestHeader( 'Connection', 'close' );
84 xhr.send( code );
85 }
86
87 this.cancel = function()
88 {
89 this._xmlHttp.onreadystatechange = function(){};
90 this._xmlHttp.abort();
91 }
92
93 this.send_form = function(form,callback,extra_values)
94 {
95 var code = '';
96
97 for( var i = 0; i < form.elements.length; i++ )
98 {
99 var e = form.elements[i];
100
101 if( e.options )
102 {
103 code += ( code ? '&' : '' ) +
104 form.elements[i].name + '=' + encodeURIComponent(
105 e.options[e.selectedIndex].value
106 );
107 }
108 else if( e.length )
109 {
110 for( var j = 0; j < e.length; j++ )
111 if( e[j].name ) {
112 code += ( code ? '&' : '' ) +
113 e[j].name + '=' + encodeURIComponent( e[j].value );
114 }
115 }
116 else
117 {
118 code += ( code ? '&' : '' ) +
119 e.name + '=' + encodeURIComponent( e.value );
120 }
121 }
122
123 if( typeof extra_values == 'object' )
124 for( var key in extra_values )
125 code += ( code ? '&' : '' ) +
126 key + '=' + encodeURIComponent( extra_values[key] );
127
128 return(
129 ( form.method == 'get' )
130 ? this.get( form.getAttribute('action'), code, callback )
131 : this.post( form.getAttribute('action'), code, callback )
132 );
133 }
134
135 this._encode = function(obj)
136 {
137 if( typeof obj == 'object' )
138 {
139 var code = '';
140 var self = this;
141
142 for( var k in obj )
143 code += ( code ? '&' : '' ) +
144 k + '=' + encodeURIComponent( obj[k] );
145 return code;
146 }
147
148 return obj;
149 }
150 }