function JavaScriptCommunication() {
    var id;
    var callingMap = {};
    var rpcMap = {};

    function postMessageToCooperator(data) {
        if (id != "") // Ignore if a mash-up is not a widget (it is a main window) (and its id will be "")
            parent.postMessage(JSON.stringify(data), "*");
    }

    function postMessageToWidget(id, data) {
        // To send data to another widget (in another window object), we need the platform as the intermediate, because we can not access the window object of the other widget if our widgets are in different domains
        parent.postMessage("|" + id + "|" + JSON.stringify(data), "*");
    }

    this.setId = function (widgetId) {
        id = widgetId;
    }

    /**
     * Register a method so that the Cooperator can call this method
     */
    this.register = function (methodId, method) {
        rpcMap[methodId] = method;
    }

    /**
     * Call a method from the Cooperator
     */
    this.call = function (methodId, param, callback) {
        postMessageToCooperator({"id": id, "methodId": methodId, "role": "call", "param": param});
        callingMap[methodId] = callback;
    }

    /**
     * Call a method from other widget (Used to transfer output data to other client widget)
     */
    this.publish = function (id, methodId, param, callback) {
        postMessageToWidget(id, {"methodId": methodId, "role": "call", "param": param});
    }

    function messageListener(e) {
        try {
            var data = JSON.parse(e.data);
            var methodId = data["methodId"];
            if (data.role == "call") {
                if (rpcMap[methodId] != null) {
                    var response = rpcMap[methodId](data.param);
                    if (response == null)
                        response = {};
                    postMessageToCooperator({"id": id, "methodId": methodId, "role": "return", "data": response});
                }
            } else if (data.role == "return") {
                if (callingMap[methodId] != null)
                    callingMap[methodId](data.data);
            }
        } catch (err) {
            // The message is not from platform, don't care
        }
    }

    if (window.attachEvent) { //IE
        window.attachEvent("onmessage", messageListener, false);
    } else if (window.addEventListener) {
        window.addEventListener("message", messageListener, false);
    }
}
/**
 * Util class to enable the communication between a client widget (or client GUI of a server widget) and the Cooperator
 */
Util class to enable the communication between a client widget (or client GUI of a server widget) and the Cooperator
Util class to enable the communication between a client widget ( or client GUI of a server widget ) and the Cooperator
function JavaScriptCommunication() {
    var id;
    var callingMap = {};
    var rpcMap = {};
    function postMessageToCooperator(data) {
        if (id != "") 
            parent.postMessage(JSON.stringify(data), "*");
    }
    function postMessageToWidget(id, data) {
        parent.postMessage("|" + id + "|" + JSON.stringify(data), "*");
    }
    this.setId = function (widgetId) {
        id = widgetId;
    }
    this.register = function (methodId, method) {
        rpcMap[methodId] = method;
    }
    this.call = function (methodId, param, callback) {
        postMessageToCooperator({"id": id, "methodId": methodId, "role": "call", "param": param});
        callingMap[methodId] = callback;
    }
    this.publish = function (id, methodId, param, callback) {
        postMessageToWidget(id, {"methodId": methodId, "role": "call", "param": param});
    }
    function messageListener(e) {
        try {
            var data = JSON.parse(e.data);
            var methodId = data["methodId"];
            if (data.role == "call") {
                if (rpcMap[methodId] != null) {
                    var response = rpcMap[methodId](data.param);
                    if (response == null)
                        response = {};
                    postMessageToCooperator({"id": id, "methodId": methodId, "role": "return", "data": response});
                }
            } else if (data.role == "return") {
                if (callingMap[methodId] != null)
                    callingMap[methodId](data.data);
            }
        } catch (err) {
        }
    }
    if (window.attachEvent) { 
        window.attachEvent("onmessage", messageListener, false);
    } else if (window.addEventListener) {
        window.addEventListener("message", messageListener, false);
    }
}
function JavaScriptCommunication ( ) { var id ; var callingMap = { } ; var rpcMap = { } ; function postMessageToCooperator ( data ) { if ( id != "" ) parent . postMessage ( JSON . stringify ( data ) , "*" ) ; } function postMessageToWidget ( id , data ) { parent . postMessage ( "|" + id + "|" + JSON . stringify ( data ) , "*" ) ; } this . setId = function ( widgetId ) { id = widgetId ; } this . register = function ( methodId , method ) { rpcMap [ methodId ] = method ; } this . call = function ( methodId , param , callback ) { postMessageToCooperator ( { "id" : id , "methodId" : methodId , "role" : "call" , "param" : param } ) ; callingMap [ methodId ] = callback ; } this . publish = function ( id , methodId , param , callback ) { postMessageToWidget ( id , { "methodId" : methodId , "role" : "call" , "param" : param } ) ; } function messageListener ( e ) { try { var data = JSON . parse ( e . data ) ; var methodId = data [ "methodId" ] ; if ( data . role == "call" ) { if ( rpcMap [ methodId ] != null ) { var response = rpcMap [ methodId ] ( data . param ) ; if ( response == null ) response = { } ; postMessageToCooperator ( { "id" : id , "methodId" : methodId , "role" : "return" , "data" : response } ) ; } } else if ( data . role == "return" ) { if ( callingMap [ methodId ] != null ) callingMap [ methodId ] ( data . data ) ; } } catch ( err ) { } } if ( window . attachEvent ) { window . attachEvent ( "onmessage" , messageListener , false ) ; } else if ( window . addEventListener ) { window . addEventListener ( "message" , messageListener , false ) ; } }
Util class to enable the communication between a client widget (or client GUI of a server widget) and the Cooperator
Util class to enable the communication between a client widget ( or client GUI of a server widget ) and the Cooperator
// Ignore if a mash-up is not a widget (it is a main window) (and its id will be "")
 // To send data to another widget (in another window object), we need the platform as the intermediate, because we can not access the window object of the other widget if our widgets are in different domains
 /**
     * Register a method so that the Cooperator can call this method
     */ /**
     * Call a method from the Cooperator
     */ /**
     * Call a method from other widget (Used to transfer output data to other client widget)
     */ // The message is not from platform, don't care
 //IE
