Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / examples / shared / code-display.js
1 /*!
2  * Ext JS Library 3.3.1
3  * Copyright(c) 2006-2010 Sencha Inc.
4  * licensing@sencha.com
5  * http://www.sencha.com/license
6  */
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',
11                 title: title,
12         width: 750,
13         style: {
14                 'margin-top': '10px'
15         },
16         hideCollapseTool: true,
17         titleCollapse: true,
18         collapsible: true,
19         collapsed: true,
20                 autoScroll: true,
21                 renderTo: Ext.getBody(),
22                 listeners: {
23                         render: function(p) {
24                                 p.getUpdater().setRenderer({
25                                         render: Ext.isIE ? function(el, response, scripts, callback) {
26                                                 el.update('');
27                                                 var np = el.createChild({
28                                                         tag: 'pre',
29                                                         cls: 'code',
30                                                         cn: {
31                                                                 tag: 'code'
32                                                         }
33                                                 });
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');
38                                                         if (t[i].length) {
39                                                                 pre.appendChild(document.createTextNode(t[i]));
40                                                                 c.appendChild(pre);
41                                                         } else if (i < (l - 1)) {
42                                                                 c.appendChild(document.createElement("br"));
43                                                         }
44                                                         
45                                                 }
46                                         } : function(el, response, scripts, callback) {
47                                                 el.update('');
48                                                 el.createChild({
49                                                         tag: 'pre',
50                                                         cls: 'code',
51                                                         cn: {
52                                                                 tag: 'code',
53                                                                 html: response.responseText
54                                                         }
55                                                 });
56                                         }
57                                 });
58                         },
59                         beforeexpand: function(p) {
60                                 p.load(url);
61                         },
62                         single: true
63                 }
64         });
65 }
66
67 // Patch to allow XHR to local files. From hendricd: http://extjs.com/forum/member.php?u=8730
68 Ext.apply( Ext.lib.Ajax ,
69 { forceActiveX:false,
70   createXhrObject:function(transactionId)
71         {
72             var obj={  status:{isError:false}
73                      , tId:transactionId}, http;
74             try
75             {
76                 
77                 if(Ext.isIE7 && !!this.forceActiveX){throw("IE7forceActiveX");}
78                 
79                 obj.conn= new XMLHttpRequest();
80                 
81             }
82             catch(e)
83             {
84                 for (var i = 0; i < this.activeX.length; ++i) {
85                     try
86                     {
87                         obj.conn= new ActiveXObject(this.activeX[i]);
88                         
89                         break;
90                     }
91                     catch(e) { 
92                     }
93                 }
94             }
95             finally
96             {
97                 obj.status.isError = typeof(obj.conn) === undefined;
98             }   
99             return obj;
100             
101         },
102         
103         getHttpStatus: function(reqObj){
104         
105                 var statObj = {  status:0
106                                 ,statusText:''
107                                 ,isError:false
108                                 ,isLocal:false
109                                 ,isOK:false
110                                 ,error:null};
111                 
112                 try {
113                         if(!reqObj)throw('noobj');
114                         statObj.status = reqObj.status;
115                         
116                         statObj.isLocal = !reqObj.status && location.protocol == "file:" || 
117                                            Ext.isSafari && reqObj.status === undefined;
118                         
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).
122                       } 
123                     
124                 return statObj; 
125         
126         },
127         handleTransactionResponse:function(o, callback, isAbort)
128                 {
129         
130                 
131                 callback = callback || {};
132                 var responseObject=null;
133                 
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);
138                  }
139                 
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)));
143                    
144                  }
145                 
146                  responseObject.options = o.options;
147                  responseObject.stat = o.status;
148                  
149                  if (o.status.isOK && !o.status.isError) {
150                         if (callback.success) {
151                                 if (!callback.scope) {
152                                         callback.success(responseObject);
153                                 }
154                                 else {
155                                         callback.success.apply(callback.scope, [responseObject]);
156                                 }
157                         }
158                   } else {
159
160                         if (callback.failure) {
161                                 if (!callback.scope) {
162                                         callback.failure(responseObject);
163                                 }
164                                 else {
165                                         callback.failure.apply(callback.scope, [responseObject]);
166                                 }
167                         }
168
169                  }
170                 
171                 if(o.options.async){
172                         this.releaseObject(o);  
173                         responseObject = null;
174                 }else{ 
175                         this.releaseObject(o);
176                         return responseObject; 
177                 }
178                         
179         },
180         createResponseObject:function(o, callbackArg)
181         {
182             var obj = {};
183             var headerObj = {},headerStr='';
184
185             try{  //to catch bad encoding problems here
186                 obj.responseText = o.conn.responseText;
187             }catch(e){obj.responseText ='';}
188
189             obj.responseXML = o.conn.responseXML;
190
191             try{
192                 headerStr = o.conn.getAllResponseHeaders()||'';
193             } catch(e){}
194
195             if(o.status.isLocal){
196
197                    o.status.isOK = ((o.status.status = (!!obj.responseText.length)?200:404) == 200);
198
199                    if(o.status.isOK && (!obj.responseXML || obj.responseXML.childNodes.length == 0)){
200
201                         var xdoc=null;
202                         try{   //ActiveX may be disabled
203                                 if(typeof(DOMParser) == 'undefined'){ 
204                                         xdoc=new ActiveXObject("Microsoft.XMLDOM"); 
205                                         xdoc.async="false";
206                                         xdoc.loadXML(obj.responseText); 
207
208                                 }else{ 
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'); 
212                                         }catch(ex){}
213                                         finally{domParser = null;}
214
215                                 }
216                         } catch(ex){ 
217                                 o.status.isError = true; 
218                                 o.status.error = ex;
219
220                                 }
221
222                         obj.responseXML = xdoc;
223                     }
224
225                     if(obj.responseXML){
226
227                         var parseBad = (obj.responseXML.parseError || 0) != 0 || obj.responseXML.childNodes.length == 0;
228                         if(!parseBad){
229                                 headerStr = 'Content-Type: ' + (obj.responseXML.contentType || 'text\/xml') + '\n' + headerStr ;
230                                 }                           
231                     }           
232
233
234             }   
235
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);
241                 }
242             }
243
244             obj.tId = o.tId;
245             obj.status = o.status.status;
246             obj.statusText = o.status.statusText;
247             obj.getResponseHeader = headerObj;
248             obj.getAllResponseHeaders = headerStr;
249             obj.stat = o.status
250
251             if (typeof callbackArg !== undefined) {
252                 obj.argument = callbackArg;
253             }
254
255             return obj;
256         },
257         
258         request : function(method, uri, cb, data, options) {
259                     
260                      options = Ext.apply({async:true,
261                            headers:false,
262                            userId:null,
263                            password:null,
264                            xmlData:null }, options||{});
265                                                 
266                         var hs = options.headers;
267                         if(hs){
268                             for(var h in hs){
269                                 if(hs.hasOwnProperty(h)){
270                                     this.initHeader(h, hs[h], false);
271                                 }
272                             }
273                         }
274                         if(options.xmlData){
275                             this.initHeader('Content-Type', 'text/xml', false);
276                             method = 'POST';
277                             data = options.xmlData;
278                         }
279                                     
280                     return this.makeRequest(method, uri, cb, data, options);
281                     
282         },
283         asyncRequest:function(method, uri, callback, postData)
284         {
285             var o = this.getConnectionObject();
286
287             if (!o || o.status.isError) {
288                 return null;
289             }
290             else {
291                 o.options = options;
292                 try{
293                         o.conn.open(method, uri, true);
294                 } catch(ex){
295                         o.status.isError = true;
296                         o.status.error = ex;
297                         return Ext.apply(o,this.handleTransactionResponse(o, callback));
298                         
299                 }
300                 
301                 
302                 if (this.useDefaultXhrHeader) {
303                     if (!this.defaultHeaders['X-Requested-With']) {
304                         this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
305                     }
306                 }
307
308                 if(postData && this.useDefaultHeader){
309                     this.initHeader('Content-Type', this.defaultPostHeader);
310                 }
311
312                  if (this.hasDefaultHeaders || this.hasHeaders) {
313                     this.setHeader(o);
314                 }
315
316                 this.handleReadyState(o, callback);
317                 
318                 try{ o.conn.send(postData || null);
319                 } catch(ex){ 
320                         o.status.isError=true;
321                         o.status.error = ex;
322                         return Ext.apply(o,this.handleTransactionResponse(o, callback));
323                 }
324                         
325                                         
326                 return o;
327             }
328         },
329         
330         makeRequest:function(method, uri, callback, postData, options)
331         {
332             var o = this.getConnectionObject();
333                      
334             if (!o || o.status.isError) {
335                 return null;
336             }
337             else {
338                 o.options = options;    
339                 try{
340                         o.conn.open(method, uri, options.async, options.userId, options.password);
341                 } catch(ex){
342                         o.status.isError = true;
343                         o.status.error = ex;
344                         var r=this.handleTransactionResponse(o, callback);
345                         return Ext.apply(o,r);
346                 }
347
348                 if (this.useDefaultXhrHeader) {
349                     if (!this.defaultHeaders['X-Requested-With']) {
350                         this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
351                     }
352                 }
353
354                 if(postData && this.useDefaultHeader){
355                     this.initHeader('Content-Type', this.defaultPostHeader);
356                 }
357
358                  if (this.hasDefaultHeaders || this.hasHeaders) {
359                     this.setHeader(o);
360                 }
361
362                 if(o.options.async){ //Timers won't work here as it's a blocking call
363                         this.handleReadyState(o, callback);
364                 }
365                 
366                 try{ o.conn.send(postData || null);
367                 } catch(ex){ 
368                         //Ext.apply(o,this.handleTransactionResponse(o, callback));
369                 }
370                                 
371                 return options.async?o:Ext.apply(o,this.handleTransactionResponse(o, callback));
372             }
373    }});
374         
375 Ext.lib.Ajax.forceActiveX = (document.location.protocol == 'file:');/* or other true/false mechanism */