(function(){var _win = window;var _doc = _win.document; var AimaRPC = function(isContainer,env){ this._env = env;/*{{{*/ this._isContainer = isContainer; this._listeners = {}; this._mode = false; this._ready = false; this._hasTimeOut = false; if(!this._isContainer){ this._loadMyGadgetId(); this._callbacks = {}; this._callbackIdx = 0; if(env.name){ this._helperName = env.name; } this.initMessageListenerForGadget(); this._sender = env.sender; this._receiver = false; } this._timeOut=5000; };/*}}}*/ AimaRPC._getPMBinding = function(){ if(typeof(this._msgBinding) !== 'undefined'){/*{{{*/ return this._msgBinding; } if(typeof(window.postMessage) !== 'undefined'){ if(typeof(_win.addEventListener) !== 'undefined'){ this._msgBinding = function(callback,useCapture){ window.addEventListener('message',callback,useCapture); }; }else if(typeof(_win.attachEvent) !== 'undefined'){ this._msgBinding = function(callback,dummy){ window.attachEvent('onmessage',callback); }; } } return this._msgBinding; };/*}}}*/ AimaRPC.ContainerPrefix = 'aima_gadget_'; AimaRPC.HelperPrefix = 'aima_helper_'; AimaRPC.Param_GadgetId = 'aima_cid'; AimaRPC.Debuging = false; AimaRPC.Debug = function(message){ if(AimaRPC.Debuging && typeof(console) !== 'undefined' && typeof(console.debug) !== 'undefined'){/*{{{*/ console.debug(message); }else{ var content = ''; var x = null; for(x in message){ // content += "\n" + message[x]; } //alert(content); } };/*}}}*/ AimaRPC.prototype = { //container functions addMessageListener : function(message,callback){ if(!this._isContainer){/*{{{*/ return this; } if(this._listeners[message]){ this._listeners[message].push(callback); }else{ this._listeners[message] = [callback]; } AimaRPC.Debug([this._isContainer?'container':'gadget',message,' listener added']); return this; },/*}}}*/ removeMessageListener : function(message,idx){ if(!this._isContainer){/*{{{*/ return this; } if(this._listeners[message]){ if(arguments.length > 1){ if(idx>0 && this._listeners[message].length >= idx){ for(i=idx-1,len=this._listeners[message].length-1;i0){ var message = this._messages.shift(); AimaRPC.Debug([this._isContainer?'container':'gadget','sending next message',message]); this._fimSending = false; this.call(message.act,message.params,message.callback); }else{ this._fimSending = false; } },/*}}}*/ //common private functions _readyNow : function(){ this._ready = true;/*{{{*/ if(this._readyCallback){ this._readyCallback(); } },/*}}}*/ _onTimeOut: function(){ this._hasTimeOut = true;/*{{{*/ if(this._timeoutCallback){ this._timeoutCallback(); } },/*}}}*/ _enablePM : function(){ var binding = AimaRPC._getPMBinding();/*{{{*/ if(binding){ var oThis = this; var callback = function(message){ if(oThis._receiverDomain && message.origin !== oThis._receiverDomain){ AimaRPC.Debug([oThis._isContainer?'container':'gadget','ignored message from',message.origin]); return false; } var content = oThis._decode(message.data); AimaRPC.Debug([oThis._isContainer?'container':'gadget','received message:',content]); if(content && content.data && content.data.act){ var act = content.data.act; var params = content.data.params; var result = oThis._fireMessageReceived(act,params,message.origin); AimaRPC.Debug([oThis._isContainer?'container':'gadget','result:',result]); if(content.callback){ AimaRPC.Debug([oThis._isContainer?'container':'gadget','callback request']); var sender = message.source; if(sender){ AimaRPC.Debug([oThis._isContainer?'container':'gadget',content.callback + '-callback:',message.origin]); var response = {callback:false,id:0,data:{act:'_aimacallback',params:{callback:content.callback,result:result} }}; sender.postMessage(oThis._encode(response),message.origin); } } }else{ AimaRPC.Debug([oThis._isContainer?'container':'gadget','received message is bad.',content]); } }; binding(callback); this._mode = 'pm'; this._waitting(); }else{ return this._enableFIM(); } },/*}}}*/ _enableFIM : function(){ _win.IsAimaContainer = true;/*{{{*/ if(!this._isContainer){ AimaRPC.Debug(['create helper for FIM',this._sender]); this.createHelper('gadget_inner',this._sender); } var oThis = this; _win.onMessage = function(message,callbackId){ //var content = oThis._decode(message); content = message;//under FIM-mode,we can pass an object directly AimaRPC.Debug([oThis._isContainer?'container':'gadget','received message:',content]); //@todo check content var act = content.data.act; var params = content.data.params; //@rewrite params.callback by current callbackId params.callback = callbackId; var result = oThis._fireMessageReceived(act,params,message.origin); AimaRPC.Debug([oThis._isContainer?'container':'gadget','result:',result]); //under FIM-mode,onMessage was called synchronized,return directly return {callback:false,id:0,data:{act:'_aimacallback',params:{callback:content.callback,result:result}} }; }; AimaRPC.Debug([this._isContainer?'container':'gadget','now listening']); this._mode = 'fim'; this._waitting(); },/*}}}*/ _waitting : function(){ //needed by clinet/*{{{*/ if(this._isContainer){ return true; } var oThis = this; var loading = { times : 0, detect : function(timeout){ AimaRPC.Debug(['detect if ready']); if(this.times++ > 10){ AimaRPC.Debug(['time out']); oThis._onTimeOut(); return true; } if(this._mode === 'fim'){ var helper = oThis._getInnerHelper(); if(!helper){ AimaRPC.Debug(['helper not loaded']); return false; } if(!helper.contentWindow.sendMessage){ AimaRPC.Debug(['helper sendMessage has not setup']); return false; } } var receiver = oThis._getReceiver(); if(!receiver){ AimaRPC.Debug(['receiver has not setup']); return false; } AimaRPC.Debug(['ready now']); oThis._readyNow(); return true; }, run : function(timeout){ if(!this.detect()){ var pLoading = this; AimaRPC.Debug(['waitting']); _win.setTimeout(function(){pLoading.run(timeout);},timeout); } } }; loading.run(parseInt(this._timeOut/10,10)); },/*}}}*/ _fireMessageReceived : function(act,params,origin){ //should take the result returned by the last listener as current result var result = null;/*{{{*/ var x = null; var i = 0; var len = 0; for(x in this._listeners){ if(x === act || x === '*'){ //if(this._listeners[act]){ for(i=0,len=this._listeners[x].length; i -1){ var query = url.substr(url.indexOf("?")+1).split("&"); var i = 0; var len = 0; for(i=0,len=query.length; i