Changeset 7958 for branches/2.1/themes/default/js/scripts.js
- Timestamp:
- Nov 30, 2010, 9:42:35 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.1/themes/default/js/scripts.js
r6576 r7958 52 52 } 53 53 54 Function.prototype.pwgBind = function() {55 var __method = this, object = arguments[0], args = Array.prototype.slice.call(arguments,1);56 57 return __method.apply(object, args.concat(arguments) );58 54 function pwgBind(object, method) { 55 var args = Array.prototype.slice.call(arguments,2); 56 return function() { 57 return method.apply(object, args.concat(Array.prototype.slice.call(arguments,0)) ); 58 } 59 59 } 60 60 61 function PwgWS(urlRoot) 61 62 { … … 63 64 this.options = { 64 65 method: "GET", 65 async: 66 async: true, 66 67 onFailure: null, 67 68 onSuccess: null … … 70 71 71 72 PwgWS.prototype = { 72 73 73 callService : function(method, parameters, options) 74 74 { 75 75 if (options) 76 76 { 77 for (var prop ertyin options)78 this.options[prop erty] = options[property];77 for (var prop in options) 78 this.options[prop] = options[prop]; 79 79 } 80 try { this. transport= new XMLHttpRequest();}80 try { this.xhr = new XMLHttpRequest();} 81 81 catch(e) { 82 try { this. transport= new ActiveXObject('Msxml2.XMLHTTP'); }82 try { this.xhr = new ActiveXObject('Msxml2.XMLHTTP'); } 83 83 catch(e) { 84 try { this. transport= new ActiveXObject('Microsoft.XMLHTTP'); }84 try { this.xhr = new ActiveXObject('Microsoft.XMLHTTP'); } 85 85 catch (e){ 86 dispatchError(0, "Cannot create request object"); 86 this.error(0, "Cannot create request object"); 87 return; 87 88 } 88 89 } 89 90 } 90 this. transport.onreadystatechange = this.onStateChange.pwgBind(this);91 this.xhr.onreadystatechange = pwgBind(this, this.onStateChange); 91 92 92 93 var url = this.urlRoot+"ws.php?format=json"; … … 95 96 if (parameters) 96 97 { 97 for (var prop ertyin parameters)98 for (var prop in parameters) 98 99 { 99 if ( typeof parameters[prop erty] == 'object' && parameters[property])100 if ( typeof parameters[prop] == 'object' && parameters[prop]) 100 101 { 101 for (var i=0; i<parameters[prop erty].length; i++)102 body += "&"+prop erty+"[]="+encodeURIComponent(parameters[property][i]);102 for (var i=0; i<parameters[prop].length; i++) 103 body += "&"+prop+"[]="+encodeURIComponent(parameters[prop][i]); 103 104 } 104 105 else 105 body += "&"+prop erty+"="+encodeURIComponent(parameters[property]);106 body += "&"+prop+"="+encodeURIComponent(parameters[prop]); 106 107 } 107 108 } 108 109 109 110 if (this.options.method == "POST" ) 110 { 111 this.transport.open(this.options.method, url, this.options.async); 112 this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 113 this.transport.send(body); 114 } 111 this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 115 112 else 116 113 { 117 114 url += "&"+body; 118 this.transport.open(this.options.method, url, this.options.async); 119 this.transport.send(null); 115 body = null; 116 } 117 this.xhr.open(this.options.method, url, this.options.async); 118 try { 119 this.xhr.send(body); 120 } catch(e) { 121 this.error(0, e.message); 120 122 } 121 123 }, 122 124 123 125 onStateChange: function() { 124 var readyState = this. transport.readyState;126 var readyState = this.xhr.readyState; 125 127 if (readyState==4) 126 this.respondToReadyState(readyState); 128 { 129 try { 130 this.respondToReadyState(readyState); 131 } finally { 132 this.cleanup(); 133 } 134 } 127 135 }, 128 136 129 dispatchError: function( httpCode, text )137 error: function( httpCode, text ) 130 138 { 131 139 !this.options.onFailure || this.options.onFailure( httpCode, text); 140 this.cleanup(); 132 141 }, 133 142 134 143 respondToReadyState: function(readyState) 135 144 { 136 var transport = this.transport;137 if (readyState==4 && transport.status == 200)145 var xhr = this.xhr; 146 if (readyState==4 && xhr.status == 200) 138 147 { 139 148 var resp; 140 149 try { 141 eval('resp = ' + transport.responseText);150 resp = window.JSON && window.JSON.parse ? window.JSON.parse( xhr.responseText ) : (new Function("return " + xhr.responseText))(); 142 151 } 143 152 catch (e) { 144 this. dispatchError( 200, e.message + '\n' + transport.responseText.substr(0,512) );153 this.error( 200, e.message + '\n' + xhr.responseText.substr(0,512) ); 145 154 } 146 155 if (resp!=null) 147 156 { 148 157 if (resp.stat==null) 149 this. dispatchError( 200, "Invalid response" );158 this.error( 200, "Invalid response" ); 150 159 else if (resp.stat=='ok') 151 { 152 if (this.options.onSuccess) this.options.onSuccess( resp.result ); 153 } 160 !this.options.onSuccess || this.options.onSuccess( resp.result ); 154 161 else 155 this. dispatchError( 200, resp.err + " " + resp.message);162 this.error( 200, resp.err + " " + resp.message); 156 163 } 157 164 } 158 if (readyState==4 && transport.status != 200)159 this. dispatchError( transport.status, transport.statusText );165 if (readyState==4 && xhr.status != 200) 166 this.error( xhr.status, xhr.statusText ); 160 167 }, 161 168 169 cleanup: function() 170 { 171 if (this.xhr) this.xhr.onreadystatechange = null; 172 this.xhr = null; 173 this.options.onFailure = this.options.onSuccess = null; 174 }, 162 175 163 transport: null, 164 urlRoot: null, 165 options: {} 176 xhr: null 166 177 } 167 178 168 179 function pwgAddEventListener(elem, evt, fn) 169 180 { 170 if (window.a ttachEvent)171 elem.a ttachEvent('on'+evt, fn);181 if (window.addEventListener) 182 elem.addEventListener(evt, fn, false); 172 183 else 173 elem.a ddEventListener(evt, fn, false);184 elem.attachEvent('on'+evt, fn); 174 185 }
Note: See TracChangeset
for help on using the changeset viewer.