3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
7 // Create and append to the body, a Panel containing a block of code from the passed URL
8 function createCodePanel(url, title) {
9 var panel = new Ext.Panel({
10 hideMode: 'visibility',
16 hideCollapseTool: true,
21 renderTo: Ext.getBody(),
24 p.getUpdater().setRenderer({
25 render: Ext.isIE ? function(el, response, scripts, callback) {
27 var np = el.createChild({
34 var t = response.responseText.split("\n");
35 var c = np.child('code', true);
36 for (var i = 0, l = t.length; i < l; i++) {
37 var pre = document.createElement('pre');
39 pre.appendChild(document.createTextNode(t[i]));
41 } else if (i < (l - 1)) {
42 c.appendChild(document.createElement("br"));
46 } : function(el, response, scripts, callback) {
53 html: response.responseText
59 beforeexpand: function(p) {
67 // Patch to allow XHR to local files. From hendricd: http://extjs.com/forum/member.php?u=8730
68 Ext.apply( Ext.lib.Ajax ,
70 createXhrObject:function(transactionId)
72 var obj={ status:{isError:false}
73 , tId:transactionId}, http;
77 if(Ext.isIE7 && !!this.forceActiveX){throw("IE7forceActiveX");}
79 obj.conn= new XMLHttpRequest();
84 for (var i = 0; i < this.activeX.length; ++i) {
87 obj.conn= new ActiveXObject(this.activeX[i]);
97 obj.status.isError = typeof(obj.conn) === undefined;
103 getHttpStatus: function(reqObj){
105 var statObj = { status:0
113 if(!reqObj)throw('noobj');
114 statObj.status = reqObj.status;
116 statObj.isLocal = !reqObj.status && location.protocol == "file:" ||
117 Ext.isSafari && reqObj.status === undefined;
119 statObj.isOK = (statObj.isLocal || (statObj.status > 199 && statObj.status < 300));
120 statObj.statusText = reqObj.statusText || '';
121 } catch(e){ //status may not avail/valid yet (or called too early).
127 handleTransactionResponse:function(o, callback, isAbort)
131 callback = callback || {};
132 var responseObject=null;
134 if(!o.status.isError){
135 o.status = this.getHttpStatus(o.conn);
136 /* create and enhance the response with proper status and XMLDOM if necessary */
137 responseObject = this.createResponseObject(o, callback.argument);
140 if(o.status.isError){ /* checked again in case exception was raised - ActiveX was disabled during XML-DOM creation? */
141 // And mixin everything the XHR object had to offer as well
142 responseObject = Ext.applyIf(responseObject||{},this.createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false)));
146 responseObject.options = o.options;
147 responseObject.stat = o.status;
149 if (o.status.isOK && !o.status.isError) {
150 if (callback.success) {
151 if (!callback.scope) {
152 callback.success(responseObject);
155 callback.success.apply(callback.scope, [responseObject]);
160 if (callback.failure) {
161 if (!callback.scope) {
162 callback.failure(responseObject);
165 callback.failure.apply(callback.scope, [responseObject]);
172 this.releaseObject(o);
173 responseObject = null;
175 this.releaseObject(o);
176 return responseObject;
180 createResponseObject:function(o, callbackArg)
183 var headerObj = {},headerStr='';
185 try{ //to catch bad encoding problems here
186 obj.responseText = o.conn.responseText;
187 }catch(e){obj.responseText ='';}
189 obj.responseXML = o.conn.responseXML;
192 headerStr = o.conn.getAllResponseHeaders()||'';
195 if(o.status.isLocal){
197 o.status.isOK = ((o.status.status = (!!obj.responseText.length)?200:404) == 200);
199 if(o.status.isOK && (!obj.responseXML || obj.responseXML.childNodes.length == 0)){
202 try{ //ActiveX may be disabled
203 if(typeof(DOMParser) == 'undefined'){
204 xdoc=new ActiveXObject("Microsoft.XMLDOM");
206 xdoc.loadXML(obj.responseText);
209 try{ //Opera 9 will fail parsing non-XML content, so trap here.
210 var domParser = new DOMParser();
211 xdoc = domParser.parseFromString(obj.responseText, 'application\/xml');
213 finally{domParser = null;}
217 o.status.isError = true;
222 obj.responseXML = xdoc;
227 var parseBad = (obj.responseXML.parseError || 0) != 0 || obj.responseXML.childNodes.length == 0;
229 headerStr = 'Content-Type: ' + (obj.responseXML.contentType || 'text\/xml') + '\n' + headerStr ;
236 var header = headerStr.split('\n');
237 for (var i = 0; i < header.length; i++) {
238 var delimitPos = header[i].indexOf(':');
239 if (delimitPos != -1) {
240 headerObj[header[i].substring(0, delimitPos)] = header[i].substring(delimitPos + 2);
245 obj.status = o.status.status;
246 obj.statusText = o.status.statusText;
247 obj.getResponseHeader = headerObj;
248 obj.getAllResponseHeaders = headerStr;
251 if (typeof callbackArg !== undefined) {
252 obj.argument = callbackArg;
258 request : function(method, uri, cb, data, options) {
260 options = Ext.apply({async:true,
264 xmlData:null }, options||{});
266 var hs = options.headers;
269 if(hs.hasOwnProperty(h)){
270 this.initHeader(h, hs[h], false);
275 this.initHeader('Content-Type', 'text/xml', false);
277 data = options.xmlData;
280 return this.makeRequest(method, uri, cb, data, options);
283 asyncRequest:function(method, uri, callback, postData)
285 var o = this.getConnectionObject();
287 if (!o || o.status.isError) {
293 o.conn.open(method, uri, true);
295 o.status.isError = true;
297 return Ext.apply(o,this.handleTransactionResponse(o, callback));
302 if (this.useDefaultXhrHeader) {
303 if (!this.defaultHeaders['X-Requested-With']) {
304 this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
308 if(postData && this.useDefaultHeader){
309 this.initHeader('Content-Type', this.defaultPostHeader);
312 if (this.hasDefaultHeaders || this.hasHeaders) {
316 this.handleReadyState(o, callback);
318 try{ o.conn.send(postData || null);
320 o.status.isError=true;
322 return Ext.apply(o,this.handleTransactionResponse(o, callback));
330 makeRequest:function(method, uri, callback, postData, options)
332 var o = this.getConnectionObject();
334 if (!o || o.status.isError) {
340 o.conn.open(method, uri, options.async, options.userId, options.password);
342 o.status.isError = true;
344 var r=this.handleTransactionResponse(o, callback);
345 return Ext.apply(o,r);
348 if (this.useDefaultXhrHeader) {
349 if (!this.defaultHeaders['X-Requested-With']) {
350 this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
354 if(postData && this.useDefaultHeader){
355 this.initHeader('Content-Type', this.defaultPostHeader);
358 if (this.hasDefaultHeaders || this.hasHeaders) {
362 if(o.options.async){ //Timers won't work here as it's a blocking call
363 this.handleReadyState(o, callback);
366 try{ o.conn.send(postData || null);
368 //Ext.apply(o,this.handleTransactionResponse(o, callback));
371 return options.async?o:Ext.apply(o,this.handleTransactionResponse(o, callback));
375 Ext.lib.Ajax.forceActiveX = (document.location.protocol == 'file:');/* or other true/false mechanism */