1 /** 2 * @ignore 3 */ 4 var Ozone = Ozone ? Ozone : {}; 5 6 /** 7 * @ignore 8 * @namespace 9 */ 10 Ozone.eventing = Ozone.eventing ? Ozone.eventing : {}; 11 12 (function (window, document, undefined) { 13 14 function rpcCall(widgetId, widgetIdCaller, functionName, var_args) { 15 gadgets.rpc.call("..", "FUNCTION_CALL", null, widgetId, widgetIdCaller, functionName, var_args); 16 } 17 18 /** 19 * Creates or updates a proxy - This is a private constructor - Do not call this directly. The other 20 * Widget APIs such as Widget Intents may return a proxy. A proxy may contain dynamic functions that were 21 * registered by the origin widget. 22 * 23 * @param {String} wid Id of the Widget this proxy represents 24 * @param {Object[]} functions Array of objects representing proxy functions 25 * @param {String} srcId Id of the source Widget who is using the proxy 26 * @param {Ozone.eventing.WidgetProxy} [proxy] A existing proxy object to be used, instead of creating a new instance 27 * @constructor 28 */ 29 Ozone.eventing.WidgetProxy = function (wid, functions, srcId, proxy) { 30 var widgetId = wid, 31 readyList = [], 32 pub = proxy; 33 34 if (pub == null) { 35 pub = /** @lends Ozone.eventing.WidgetProxy.prototype */ { 36 37 /** 38 * Id of the Widget that this proxy represents 39 */ 40 id:wid, 41 /** 42 * Flag which represents if the Widget this proxy represents 43 */ 44 isReady:false, 45 callbacks:{}, 46 /** 47 * Sends a direct message to the Widget this proxy represents 48 * @param {Object} dataToSend 49 * @example 50 * var widgetProxy = OWF.RPC.getWidgetProxy(id); 51 * widgetProxy.sendMessage({data:'foo'}); 52 */ 53 sendMessage:function (dataToSend) { 54 gadgets.rpc.call("..", 'DIRECT_MESSAGE', null, widgetId, dataToSend); 55 }, 56 57 /** 58 * Registers a listener function to be executed when the Widget has called notifyReady 59 * @param {function} readyListener function to execute 60 * @param {Object} readyListenerScope scope for the function to execute with 61 * @example 62 * var widgetProxy = OWF.RPC.getWidgetProxy(id); 63 * widgetProxy.onReady(function() { console.log("Other widget is ready!"); }); 64 */ 65 onReady:function (readyListener, readyListenerScope) { 66 67 if (this.isReady) { 68 //just execute because the widget is already ready 69 readyListener.call(readyListenerScope); 70 } 71 else { 72 //save ready listeners 73 readyList.push({fn:readyListener, scope:readyListenerScope}); 74 } 75 }, 76 fireReady: function() { 77 this.isReady = true; 78 for (var i = 0, len = readyList.length; i < len; i++) { 79 readyList[i].fn.call(readyList[i].scope); 80 } 81 } 82 }; 83 } 84 85 if (functions != null) { 86 for (var ii = 0; ii < functions.length; ii++) { 87 var functionName = functions[ii]; 88 89 pub[functionName] = function (name) { 90 return function () { 91 var callback = arguments[arguments.length - 1]; 92 var callbackExists = typeof callback == 'function'; 93 var args = Array.prototype.slice.call(arguments, 0, callbackExists ? arguments.length - 1 : arguments.length); 94 if (callbackExists) { 95 pub.callbacks[name] = callback; 96 } 97 rpcCall.call(this, widgetId, srcId, name, args); 98 } 99 }(functionName); 100 101 } 102 } 103 104 return pub; 105 }; 106 107 }(window, document));