      1 /**
      2  * @ignore
      3  * @namespace
      4  * @since OWF 5.0
      5  */
      6 OWF = window.OWF ? window.OWF : {};
      7
      8 (function (window, document, undefined) {
      9
     10     /**
     11      * @namespace
     12      */
     13     OWF.Intents = function () {
     14         var INTENTS_SERVICE_NAME = '_intents',
     15             INTENTS_SERVICE_RECEIVE_NAME = '_intents_receive',
     16             intentReceiverMap = {};
     17
     18         return /** @lends OWF.Intents */ {
     19
     20             /**
     21              * Starts an Intent.  This will send the intent and data to one or more widgets.
     22              * @param {Object} intent Object representing the Intent
     23              * @param {String} intent.action Name of the Intent
     24              * @param {String} intent.dataType Describes the data that will be sent with the intent.  It is recommended
     25              *   that this be a MIME type.
     26              * @param {Object} data Data to be sent with the intent
     27              * @param {Function} handler Function to be executed once the Intent has been sent to a destination widget
     28              * @param {Object[]} [handler.dest] The first argument passed to the handler function is the id(s) of the recipient(s)
     29              *   of the message. See <a href="./OWF.html#.getIframeId">OWF.getIframeId</a>
     30              *   for a description of this id.
     31              * @param {Object[]} [dest] Explicitly define which widgets will get this intent using Widget Proxies
     32              * @example
     33              * OWF.Intents.startActivity(
     34              *   {
     35              *    action: 'Plot',
     36              *    dataType: 'application/vnd.owf.latlon'
     37              *   },
     38              *   {
     39              *     lat: 0,
     40              *     lon: 0
     41              *   },
     42              *   function(dest) {
     43              *     //dest is an array of destination widget proxies
     44              *   }
     45              * );
     46              */
     47             startActivity:function (intent, data, handler, dest) {
     48                 var destIds = [];
     49
     50                 //pull destIds from any proxies passed in
     51                 if (dest != null) {
     52                     for (var i = 0; i < dest.length; i++) {
     53                         destIds.push(dest[i].id);
     54                     }
     55                 }
     56
     57                 gadgets.rpc.call('..', INTENTS_SERVICE_NAME,
     58                     //callback for when an intent has reached the destination widget(s)
     59                     function (ids) {
     60                         //exec handler
     61                         if (handler != null) {
     62                             var widgets = [];
     63                             if (ids != null) {
     64                                 var destWidgetIds = [].concat(ids);
     65                                 for (var i = 0; i < destWidgetIds.length; i++) {
     66                                     //get dest widget proxy
     67                                     var widget = Ozone.eventing.getWidgetProxyMap()[destWidgetIds];
     68                                     if (widget != null) {
     69                                         widgets.push(widget);
     70                                         if (widgets.length == destWidgetIds.length) {
     71                                             handler(widgets);
     72                                         }
     73                                     }
     74                                     //if null create new widget proxy
     75                                     else {
     76                                         Ozone.eventing.importWidget(destWidgetIds[i], function (wproxy) {
     77                                             widgets.push(wproxy);
     78                                             if (widgets.length == destWidgetIds.length) {
     79                                                 handler(widgets);
     80                                             }
     81                                         });
     82                                     }
     83
     84                                 }
     85                                 if (destWidgetIds.length < 1) {
     86                                     //no dest ids were sent send back an empty array
     87                                     handler(widgets);
     88                                 }
     89                             }
     90                             else {
     91                                 //no dest ids were sent send back an empty array
     92                                 handler(widgets);
     93                             }
     94                         }
     95                     }, OWF.getIframeId(), intent, data, destIds);
     96             },
     97
     98             /**
     99              * Register to receive an Intent
    100              * @param {Object} intent Object representing the Intent
    101              * @param {String} intent.action Name of the Intent
    102              * @param {String} intent.dataType Describes the data that will be sent with the intent.  It is recommended
    103              *   that this be a MIME type.
    104              * @param {Function} handler Function to be executed once an Intent has been received
    105              * @param {String} [handler.sender] The first argument passed to the handler function is the id of the sender
    106              *   of the message. See <a href="./OWF.html#.getIframeId">OWF.getIframeId</a>
    107              *   for a description of this id.
    108              * @param {Object} [handler.intent] The second argument passed to the handler function is the intent itself.
    109              * @param {Object} [handler.data] The third argument passed to the handler function is the intent data.
    110              * @example
    111              * OWF.Intents.receive(
    112              *   {
    113              *     action: 'Plot',
    114              *    dataType: 'application/vnd.owf.latlon'
    115              *   },
    116              *   function(sender,intent, data) {
    117              *     //do something with the data
    118              *   }
    119              * );
    120              */
    121             receive:function (intent, handler) {
    122                 var intentKey = owfdojo.toJson(intent);
    123
    124                 //save a list of handlers per intent
    125                 intentReceiverMap[intentKey] = handler;
    126
    127                 //register with shindig for when the intent message is sent
    128                 gadgets.rpc.register(INTENTS_SERVICE_NAME, function(sender, intent, data) {
    129                      var intentKey = owfdojo.toJson(intent);
    130
    131                     //execute the handler that matches the intent
    132                     var receiverHandler = intentReceiverMap[intentKey];
    133                     if (intentReceiverMap[intentKey] != null) {
    134                         receiverHandler.call(this, sender, intent, data);
    135                     }
    136
    137                 });
    138                 gadgets.rpc.call('..', INTENTS_SERVICE_RECEIVE_NAME, null, intent, OWF.getIframeId());
    139             }
    140         };
    141
    142     }();
    143
    144
    145 }(window, document));