Subversion Repositories wimsdev

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 reyssat 1
/*
2
        DynAPI Distribution
3
        IOElement SYNChronous add-on
4
 
5
        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
6
 
7
        requires: dynapi.util.IOElement
8
*/
9
 
10
IOElementSync={}
11
IOElement.SYNC=true;
12
 
13
var p=IOElement.prototype;
14
 
15
p.activateSyncMode = function(fn,useJava){
16
        this._SyncFn=fn;
17
        this._useJava=useJava;
18
        this._hasXMLHttp=(this._hasXMLHttp==null)? IOElement._hasXMLHttp():this._hasXMLHttp;   
19
        if (!this.isSync && this._created) {
20
                window.setTimeout(this+'._initSync()',100);
21
        }
22
};
23
p._syncRequest = function(url,data,method){
24
        var dataBody;
25
        var i,mode,rq,rt={value:''},nv=[];
26
        var id = this._getRandomID(); // create random load ID to ensure no caching             
27
        if(!this.isSync) return;
28
        method=(method+'').toLowerCase();      
29
        mod=(method=='post'||method=='get')? method:'get';
30
        url=IOElement.getAbsoluteUrl(url);
31
        url += ((url.indexOf('?')==-1)? '?' : '&')+'IORequestID='+id+'&IOMethod='+mod; 
32
        for (i in data) {
33
                nv[nv.length]=i+'='+((mod!='get')? data[i]:IOElement.URLEncode(data[i]));
34
        };
35
        dataBody = nv.join('&');
36
        if(data && mod=='get') {url+='&'+dataBody;dataBody=null}       
37
        // get HTTP Request Object
38
        if(!this._hReq) this._hReq=IOElement._getHttpReq(this._jApplet);
39
        rq=this._hReq; rq.open(mod,url,false);
40
        if (mod=='post') rq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
41
        if(dynapi.ua.ns4) {
42
                rq.send(dataBody); rt.value=rq.responseText;
43
        }else {
44
                eval('try {'
45
                +' rq.send(dataBody); rt.value=rq.responseText;'
46
                +'} catch(e) {}');
47
        }
48
        if(rq.status!=200) rt.error={code:rq.status,text:'Connection Error : '+rq.statusText};
49
        return rt;
50
};
51
p._initApplet = function(){
52
        var isJava,doc,scope=this.getScope(this._elmID+'Sync');
53
        doc=scope.document;
54
        this._jApplet=doc.applets['loadApplet'];       
55
        isJava = !(navigator.javaEnabled() && this._jApplet);
56
        if(isJava) alert('IOElement Error: Java Applet not loaded. Java is not enabled on this browser');
57
        else{
58
                this.isSync=true;
59
                if (typeof(this._SyncFn)=='function') this._SyncFn();
60
                else eval(this._SyncFn);       
61
        }
62
};
63
p._initSync = function(){
64
        var evl,ua=dynapi.ua;
65
        if (this._hasXMLHttp && !this._useJava ) {
66
                this.isSync=true;
67
                if (typeof(this._SyncFn)=='function') this._SyncFn();
68
                else eval(this._SyncFn);       
69
        }else {
70
                var t,url,doc;
71
                var scope=this.getScope(this._elmID+'Sync');
72
                if(!scope) return;
73
                url=dynapi.library.path+'util/';
74
                t='<html><body><applet name="loadApplet" id="loadApplet" codebase="'+url+'" code="url.class" width="1" height="1"></applet></body></html>';
75
                doc=scope.document;
76
                doc.open();doc.write(t);doc.close();
77
                window.setTimeout(this+'._initApplet()',1000); // Allow applet to initialize
78
        }
79
};
80
IOElement._getHttpReq = function(jApplet){
81
        var o,ua=dynapi.ua;
82
        if (jApplet) o = new JavaHttpRequest(jApplet);
83
        else if(IOElement._hasXMLHttp()){
84
                if (ua.ns) o = new XMLHttpRequest();
85
                else if (ua.ie) {
86
                        var t='try {o = new ActiveXObject("Microsoft.XMLHTTP");}'
87
                        +'catch(e){o = new ActiveXObject("MSXML2.XMLHTTP");}'
88
                        eval(t);
89
                }
90
 
91
        }      
92
        return o;
93
};
94
IOElement._hasXMLHttp = function(){
95
        var state,ua=dynapi.ua;
96
        // ie for mac,ie4, ns4, ns6.1,x, does not support xmlhttprequest
97
        // while ns 6.2.x (based on gecko 0.9.4) supports xmlhttprequest but
98
        // it does not support sending string via send() - only gecko 0.9.7+ can send string
99
        // netscape 6.x needs the Java-Plugin to support java applets :(
100
        if(ua.ns && navigator.vendor=='Netscape6') state=false;
101
        else if(ua.v>4 && (ua.ns||(ua.ie && ua.win32))) state=true;
102
        return state;
103
};
104
 
105
// GetAbsoluteURL
106
IOElement.getAbsoluteUrl=function(url, docUrl) { // inspired by afroAPI urlToAbs()
107
        if(url && url.indexOf('://')>0) return url;
108
        docUrl=(docUrl)? docUrl.substring(0,docUrl.lastIndexOf('/')+1):dynapi.documentPath;
109
        url=url.replace(/^(.\/)*/,'');
110
        docUrl=docUrl.replace(/(\?.*)$/,'').replace(/(#.*)*$/,'').replace(/[^\/]*$/,'');
111
        if (url.indexOf('/')==0) return docUrl.substring(0,docUrl.indexOf('/',docUrl.indexOf('//')+2))+url;
112
        else while(url.indexOf('../')==0){
113
                url=url.replace(/^..\//,'');
114
                docUrl=docUrl.replace(/([^\/]+[\/][^\/]*)$/,'');
115
        };
116
        return docUrl+url;
117
};
118
 
119
// Java Base HttpRequest 
120
JavaHttpRequest = function(jApplet){
121
        this.applet=jApplet;
122
        this.url = "";
123
        this.responseText = "";  
124
        this.status=200;this.statusText="";
125
};
126
var jhr = JavaHttpRequest.prototype;
127
jhr.setRequestHeader = function(){}; //dummy
128
jhr.open = function(method,url){
129
  this.url = url;
130
  this.method = (method||'get');
131
};
132
jhr.send = function(data){
133
        var r,url=this.url;
134
        if (url=="") return false;
135
 
136
        this.responseText = "";
137
        if (this.applet){
138
                this.status=200;this.statusText="";
139
                if(!url.indexOf('file:///') && url.indexOf('file://')) url='file:///'+url.substr(7);
140
                r=this.applet.readURL(url, true, data||"", dynapi.documentPath);
141
                if(r) {
142
                        r=new String(r);
143
                        r=r.replace(/\n$/,'');
144
                        if(r.indexOf('Error: ')==0) {
145
                                this.status='403';
146
                                this.statusText=r.substr(7);
147
                        }
148
                        this.responseText=r;
149
                }
150
                return true;
151
        }
152
};
153