Update of /cvsroot/dynapi/dynapi2x/src/util
In directory usw-pr-cvs1:/tmp/cvs-serv19326/src/util

Added Files:
        cookie.js ioelement.js 
Log Message:
initial import


--- NEW FILE ---
/*
   DynAPI Distribution
   Cookie functions

   The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
*/ 

/*
This is not tested, should work like this:

                var c = new Cookie('mycookieset');
                c.set('name','MyName');  // re-saves cookie each time a value is added
                c.add('array',[1,2,3]);
                
                var c = new Cookie('mycookieset');  // auto-retrieves saved cookie 
object
                var name = c.get('name');
                var array = c.get('array');
                array[array.length] = 4;
                c.add('name',name+' MyLastName');
                c.add('array',array);
*/

function Cookie(name) {
        this.inherit('DynObject');
        this.name = name;
        this.data = {};
        this.exists = false;
        var c = dynapi.functions.getCookie(this.name);
        if (c) {
                this.exists = true;
                var a = c.split(',');
                var x,n,v;
                for (var i=0;i<a.length;a++) {
                        x = a[i].split('=');
                        n = x[0];
                        v = x[1];
                        if (n && v) this.data[n] = v;
                }
                //var i1 = c.indexOf('expires=');
                this._save(false);
        }
        else this._save();
}
var p = dynapi.setPrototype('Cookie','DynObject');
p.get = function(name) {
        return this.data[name];
};
p.getAll = function() {
        return data;
};
p.add = function(name,value) {
        this.data[name] = value;
        this._save();
};
p.remove = function(name) {
        this.data[name] = null;
        delete this.data[name];
        this._save();
};
p.setExpires = function(days) {
        this.expires = days;
};
p.destroy = function() {
        dynapi.functions.deleteCookie(this.name);
};
p._save = function(write) {
        var s = '';
        for (var i in this.data) {
                var v = this.data[i];
                if (v) s += i + '=' + escape(v) + ',';
        }
        s = s.substring(0,s.length-1);
        var f = 'Saved';
        if (write!=false) dynapi.functions.setCookie(this.name,s,this.expires);
        else f = 'Found';
        dynapi.debug.print(f+' Cookie: name='+this.name+' data={'+s+'}');
};

dynapi.functions.setCookie = function(name,value,days) {
        if (days) {
                var date=new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires="; expires="+date.toGMTString();
        } 
        else expires = "";
        dynapi.frame.document.cookie = name+"="+value+expires+"; path=/";
};
dynapi.functions.getCookie = function(name) {
        var nameEQ = name+"=";
        var ca = dynapi.frame.document.cookie.split(';');
        for(var i=0;i<ca.length;i++) {
                var c=ca[i];
                while (c.charAt(0)==' ') c=c.substring(1,c.length);
                if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
        }
        return null;
};
dynapi.functions.deleteCookie = function(name) {
        dynapi.functions.setCookie(name,"",-1);
};

--- NEW FILE ---
/*
        DynAPI Distribution
        IOElement Class

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: dynapi.api.DynLayer

        // pages loaded must include this code:
        <script>
                if (parent.DynAPI) parent.IOElement.notify(this);
        </script>

*/

function IOElement(hiddenThreads) {
        this.inherit('DynLayer');
        this._elmID = this.id+'elm';
        this._requests = {};    // [url,data,fn,method,id,timer,cargo]
        this._cargoID='';
        this._requestList = [];
        this._requestIndex = -1;
        this._retryID=null;

        this.api={};
        this.api.source=this;
        this.failTime = 50000;
        this._elmBusy=[];
        this._elmThread=null;
        this._maxThreads=(hiddenThreads>1 && hiddenThreads<=8)? hiddenThreads : 1;
        this._hidden = (hiddenThreads!=null)? true : false;
        if (this._hidden) {
                this.setVisible(false);
                var o = this;
                if (!dynapi.loaded) {
                        dynapi.onLoad(function() {
                                dynapi.document.addChild(o);
                        });
                }
                else dynapi.document.addChild(o);
        }

        this.addEventListener(IOElement.events);
};
var p = dynapi.setPrototype('IOElement','DynLayer');
IOElement.events = {
        onprecreate : function(e) {
                var html=[],o = e.getSource();
                if (o._hidden) {
                        if (dynapi.ua.ns4) html = ['<ilayer name="',o._elmID,'" 
visibility="hide" width=0 height=0></ilayer>'];
                        else if (dynapi.ua.ie) html = ['<iframe name="',o._elmID,'" 
style="width:0px; height:0px; visibility:hidden"></iframe>'];  //style="display:none"
                        else html = ['<iframe name="',o._elmID,'" style="width:0px; 
height:0px; visibility:hidden"></iframe>'];
                        o.html=html.join('')
                        o._elmThread=o._elmID;
                        o._elmBusy[o._elmID]=false;
                        for(var i=1;i<o._maxThreads;i++){ // setup additional threads
                                html[1]=o._elmID+'x'+i;
                                o.html+=html.join('');
                                o._elmBusy[o._elmID+'x'+i]=false; // flag as not busy
                        }
                }else{
                        if (dynapi.ua.ns4) o.html = '<layer width='+o.w+' 
height='+o.h+' name="'+o._elmID+'" visibility="inherit"></ilayer>';
                        else o.html = '<iframe name="'+o._elmID+'" width='+o.w+' 
height='+o.h+' scrolling="no" frameborder="no" marginwidth=0 marginheight=0 
style="overflow:hidden;"></iframe>';
                }
        },
        oncreate : function(e) {
                var o = e.getSource();
                if (o.getScope()) {
                        if (o._requestList.length>0) o._doRequest();
                }
        }
};
p.setTimeout=function(ms){if(!isNaN(ms))this.failTime=ms;}
p.getVariable = function(name) {
        return this.getScope()[name];
};
p.getScope = function() {
        var scope;
        if (dynapi.ua.ns4) scope = this.doc.layers[this._elmThread];
        else scope = window.frames[this._elmThread];
        if (!scope) return alert('IOElement Error: no load element');
        else return scope;
};
p.getCargo=function(){
        if(!this._cargoID) return null;
        var c=this._requests[this._cargoID][6];
        this._requests[this._cargoID][6]=null;
        return c;
};
p.isBusy = function(){
        for (var i in this._elmBusy){
                if (this._elmBusy[i]==false) return false;
        };return true
};
p.cancelAll=function(){
        this._requests=[];
        for (var i in this._elmBusy){
                this.elmThread=i;
                this._clearScope();
        }
}
p.cancel= function(id){
        id=(id)? id:this._retryID;
        var req=this._requests[id];
        if(req){
                this._elmBusy[req[4]]=false;
                this._requests[id]=null;
                this._clearScope()
                return true;
        }
};
p.retry=function(id){
        id=(id)? id:this._retryID;
        var req=this._requests[id];
        if(req) {
                window.setTimeout(this+'._doRequest("'+id+'")',50);
                return true;
        }
}
p.get = function(url,data,fn,cargo) {
        return this._request(url,data,fn,'get',cargo);
};
p.post = function(url,data,fn,cargo) {
        return this._request(url,data,fn,'post',cargo);
};
p._request = function(url,data,fn,method,cargo) {
        var id = Math.random()+'';  // create random load ID to ensure no caching
        id = 'io'+id.substring(2);
        dynapi.debug.print("IOElement "+method+" request");
        if (typeof(url)=="string") this._requests[id] = 
[url,data,fn,method,null,null,cargo];
        else if (method=="get") {
                for (var i=0;i<url.length;i++) {  // support loading several files, 
attach handler to last file
                        var src = url[i];
                        if (i<url.length-1) this._requests[id] = 
[src,null,null,method,null,null,cargo];
                        else this._requests[id] = [src,data,fn,method,null,null];
                }
        }

        this._requestList[this._requestList.length] = id;
        window.setTimeout(this+'._doRequest()',20);
        return id
};
p._doRequest = function(id) {
        if (this.isBusy()) return;
        else {
                if (this._requestIndex<this._requestList.length-1 || id) {
                        if(!id){ // direct request - used by the retry() method
                                this._requestIndex++;
                                id = this._requestList[this._requestIndex];
                        }
                        var r = this._requests[id];
                        var url = r[0];
                        var data = r[1];
                        var fn = r[2];
                        var method = r[3];
                        var elm = this._getFreeElm(); if(!elm) return;
                        r[4]=this._elmThread;
                        if (url.indexOf('http')!=0) {
                                if (url.substr(0,1)=='/') url = 
'http://'+dynapi.frame.document.domain+url;
                                else url = dynapi.documentPath+url;
                        }

                        url += (url.indexOf('?')==-1)? '?' : '&';
                        url += 'ioRequestID='+id+'&ioElementID='+this.id;

                        r[5] = 
window.setTimeout(this+'._notify("'+id+'","'+url+'",false)',this.failTime);

                        if (method=="get" || dynapi.ua.ns4) {
                                if (data) {
                                        for (var i in data) {
                                                url += '&'+i+'='+escape(data[i]);
                                        }
                                }
                                if (dynapi.ua.ns4) elm.src = url;
                                else elm.document.location.href = url;
                        }else {
                                var str = '<html><body><form name="ioDataForm" 
action="'+url+'" method="post">';
                                var i;
                                if (data) {
                                        for (i in data) {
                                                str += '<input name="'+i+'" 
type="hidden">';
                                        }
                                }
                                str += '</form></body></html>';

                                elm.document.open();
                                elm.document.write(str);
                                elm.document.close();

                                var f = elm.document.forms['ioDataForm'];
                                if (!f) return alert("IOElement Error: no form element 
found");
                                if (f && data) {
                                        for (i in data) {
                                                f[i].value = data[i];
                                        }
                                }
                                f.submit();
                        }
                        this.invokeEvent("request");
                }else {
                        // finished all
                }
        }
};
p._clearScope=function(){
        var doc=this.getScope().document
        if(doc){doc.open();doc.write('');doc.close()}
};
p._getFreeElm= function(){
        for (var i in this._elmBusy){
                if (this._elmBusy[i]==false){
                        this._elmThread=i;this._elmBusy[i]=true
                        return this.getScope()
                }
        }
};
p._notify = function(id, url, success) {
        var fn,req=this._requests[id];  if(!req)return;
        var s = (success!=null)? success : true;
        if (!this._elmBusy[req[4]] && success) {
                if(dprint)dprint('IOElement Error: '+id+' '+this._elmID+' '+url);
                return;
        }
        clearTimeout(req[5]);
        this._elmThread=req[4];
        fn = (req) ? req[2]:null;
        if(!s)this._retryID=id;
        this._cargoID=id;
        if (fn) {
                var e = new DynEvent("load",this);
                fn(e, s);
        }else{
                this.invokeEvent("response",null,s)
        }
        this._cargoID='';
        if(s)this._requests[id] = null;
        else this._clearScope();
        this._elmBusy[req[4]] = false;
        this._doRequest();
};
IOElement.notify = function(elm, fn, success) {  // ds: added success parameter to 
nicely relay server generated error messages
        if (elm) {
                var url, id;
                if (dynapi.ua.ns4) url = elm.src;
                else url = elm.document.location.href;
                if (url) {
                        elm.args = dynapi.functions.getURLArguments(url);
                        var id = elm.args["ioRequestID"];
                        var obj = DynObject.all[elm.args["ioElementID"]];
                        if (obj!=null && id!=null) {
                                elm.onload = function() {
                                        if (fn) fn(obj); // send obj fn - rmo
                                        obj._notify(id,url, success);  // ds: success
                                };
                                return obj;
                        }
                        else {
                                return false;
                        }
                }
        }
        return null;
};




_______________________________________________
Dynapi-CVS mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dynapi-cvs

Reply via email to