Solstice

 view release on metacpan or  search on metacpan

javascript/remote.js  view on Meta::CPAN

   

    if(data){
        data = encodeURIComponent(JSON.stringify(data));
    }else{
        data = JSON.stringify('');
    }
    
    var postdata = 
        "solstice_session_app_key=" + solstice_session_app_key + 
        "&solstice_remote_app=" + app + 
        "&solstice_remote_action=" + action + 
        "&solstice_subsession_id=" + solstice_subsession +
        "&solstice_subsession_chain=" + solstice_subsession_chain +
        "&solstice_remote_data=" + data;

    var req;
    if (window.XMLHttpRequest) { // branch for native XMLHttpRequest object
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req) {
        req.onreadystatechange = Solstice.Remote.processXML;
        req.open("POST", url, true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(postdata);
        if (!Solstice.Remote.requests) {
            Solstice.Remote.requests = new Array();
            Solstice.Remote.request_counter = 0;
        }
        Solstice.Remote.requests[Solstice.Remote.request_counter] = req;
        Solstice.Remote.request_counter++;
        return true;
    }
    return false;
}


/**
 * Processes the returned data from the server.
 * @private
 * @type void
 */
Solstice.Remote.processXML = function () {
    for (var req_key in Solstice.Remote.requests) {
        var req = Solstice.Remote.requests[req_key];
        if (req.readyState == 4) {
            if (req.status == 200 || req.status == 0){
                var xmldoc = req.responseXML;
   
                // a bit of debugging information if we have an error in the xml returned from our ajax call
                if(xmldoc && xmldoc.parseError && xmldoc.parseError.errorCode && Solstice.development_mode){
                    var error = xmldoc.parseError;
                    alert("Error Code: "+error.errorCode+ " \nDescription: "+error.reason+'\n Line: ' +error.line+"\n Position: "+error.linepos+ "\n Source: "+error.srcText);
                }

                var actions = xmldoc.getElementsByTagName("action");
                for(var i = 0; i < actions.length; i ++){

                    //childnode[1] is the cdata block in mozilla, childnode[0] in IE
                    if(actions[i].childNodes[1]){
                        var content = actions[i].childNodes[1].nodeValue;
                    }else{
                        var content = actions[i].childNodes[0].nodeValue;
                    }

                    var type = actions[i].getAttribute('type');

                    if(type == 'action'){

                        if(content){
                            try {
                                eval(content);
                            }catch(exception){
                                alert(exception);
                            }
                        }

                    }else if( type == 'update' ){

                        var block_id = actions[i].getAttribute('block_id');
                        var replaced = document.getElementById(block_id);
                        if(replaced){
                            replaced.innerHTML = content;
                        }

                    }else if( type == 'replacement' ){

                        var block_id = actions[i].getAttribute('block_id');
                        var replaced = document.getElementById(block_id);
                        var parent = replaced.parentNode;
                        var new_block = document.createElement("div");
                        new_block.innerHTML = content;
                        if(parent){
                            parent.replaceChild(new_block, replaced);
                        }
                    }
                }
                delete Solstice.Remote.requests[req_key];
            }
        }
    }
}

/**
* @class JSON is a class we use to freeze/thaw data sent to the server.  Please see the source for license and attribution
* @constructor
*/

/*
Copyright (c) 2005 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The Software shall be used for Good, not Evil.



( run in 1.318 second using v1.01-cache-2.11-cpan-5837b0d9d2c )