source: trunk/web/addons/job_monarch/lib/extjs/examples/shared/code-display.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

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