      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             widgetIframeId,
     32             readyList = [],
     33             pub = proxy;
     34
     35         // assume JSON
     36         if(widgetId.charAt(0) === '{') {
     37             widgetIframeId = widgetId;
     38             widgetId = OWF.Util.parseJson(widgetIframeId).id;
     39         }
     40         else {
     41             widgetIframeId = '{\"id\":\"' + widgetId + '\"}';
     42         }
     43
     44         if (pub == null) {
     45             pub = /** @lends Ozone.eventing.WidgetProxy.prototype */ {
     46
     47                 /**
     48                  * Id of the Widget that this proxy represents
     49                  */
     50                 id:widgetIframeId,
     51                 /**
     52                  * Flag which represents if the Widget this proxy represents
     53                  */
     54                 isReady:false,
     55                 callbacks:{},
     56                 /**
     57                  * Sends a direct message to the Widget this proxy represents
     58                  * @param {Object} dataToSend
     59                  * @example
     60                  * var widgetProxy = OWF.RPC.getWidgetProxy(id);
     61                  * widgetProxy.sendMessage({data:'foo'});
     62                  */
     63                 sendMessage:function (dataToSend) {
     64                     gadgets.rpc.call("..", 'DIRECT_MESSAGE', null, widgetId, dataToSend);
     65                 },
     66
     67                 /**
     68                  * Registers a listener function to be executed when the Widget has called notifyReady
     69                  * @param {function} readyListener function to execute
     70                  * @param {Object} readyListenerScope scope for the function to execute with
     71                  * @example
     72                  * var widgetProxy = OWF.RPC.getWidgetProxy(id);
     73                  * widgetProxy.onReady(function() { console.log("Other widget is ready!"); });
     74                  */
     75                 onReady:function (readyListener, readyListenerScope) {
     76
     77                     if (this.isReady) {
     78                         //just execute because the widget is already ready
     79                         readyListener.call(readyListenerScope);
     80                     }
     81                     else {
     82                         //save ready listeners
     83                         readyList.push({fn:readyListener, scope:readyListenerScope});
     84                     }
     85                 },
     86                 fireReady: function() {
     87                     this.isReady = true;
     88                     for (var i = 0, len = readyList.length; i < len; i++) {
     89                         readyList[i].fn.call(readyList[i].scope);
     90                     }
     91                 }
     92             };
     93         }
     94
     95         if (functions != null) {
     96             for (var ii = 0; ii < functions.length; ii++) {
     97                 var functionName = functions[ii];
     98
     99                 pub[functionName] = function (name) {
    100                     return function () {
    101                         var callback = arguments[arguments.length - 1];
    102                         var callbackExists = typeof callback == 'function';
    103                         var args = Array.prototype.slice.call(arguments, 0, callbackExists ? arguments.length - 1 : arguments.length);
    104                         if (callbackExists) {
    105                             pub.callbacks[name] = callback;
    106                         }
    107                         rpcCall.call(this, widgetId, srcId, name, args);
    108                     }
    109                 }(functionName);
    110
    111             }
    112         }
    113
    114         return pub;
    115     };
    116
    117 }(window, document));