Author: johnh
Date: Thu Jul 22 01:30:36 2010
New Revision: 966475

URL: http://svn.apache.org/viewvc?rev=966475&view=rev
Log:
Update common container JS to use new JSON-RPC gadget handler.


Modified:
    shindig/trunk/features/src/main/javascript/features/container/container.js
    
shindig/trunk/features/src/main/javascript/features/container/gadget_holder.js
    shindig/trunk/features/src/main/javascript/features/container/init.js
    shindig/trunk/features/src/main/javascript/features/container/service.js
    shindig/trunk/features/src/main/javascript/features/container/util.js

Modified: 
shindig/trunk/features/src/main/javascript/features/container/container.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/container.js?rev=966475&r1=966474&r2=966475&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/container/container.js 
(original)
+++ shindig/trunk/features/src/main/javascript/features/container/container.js 
Thu Jul 22 01:30:36 2010
@@ -174,18 +174,27 @@ shindig.container.Container.prototype.cl
 
 
 /**
+ * Pre-load one gadget metadata information. More details on preloadGadgets().
+ * @param {string} gadgetUrl gadget URI to preload.
+ */
+shindig.container.Container.prototype.preloadGadget = function(gadgetUrl) {
+  this.preloadGadgets([gadgetUrl]);
+};
+
+
+/**
  * Pre-load gadgets metadata information. This is done by priming the cache,
  * and making an immediate call to fetch metadata of gadgets fully specified at
  * gadgetUrls. This will not render, and do additional callback operations.
- * @param {Object} request JSON containing ids of gadgets URIs to preload.
+ * @param {Array} gadgetUrls gadgets URIs to preload.
  */
-shindig.container.Container.prototype.preloadGadgets = function(request) {
-  var metadataRequest = {
+shindig.container.Container.prototype.preloadGadgets = function(gadgetUrls) {
+  var request = {
       'container' : window.__CONTAINER,
-      'ids' : request['ids']
+      'ids' : gadgetUrls
   };
   var self = this;
-  this.service_.getGadgetMetadata(metadataRequest, function(response) {
+  this.service_.getGadgetMetadata(request, function(response) {
     if (!response.error) {
       for (var id in response) {
         self.addPreloadedGadgetUrl_(id);
@@ -195,6 +204,22 @@ shindig.container.Container.prototype.pr
   });
 };
 
+
+/**
+ * Fetch the gadget metadata commonly used by container for user preferences.
+ * @param {string} gadgetUrl gadgets URI to fetch metadata for. to preload.
+ * @param {function(Object)} callback Function called with gadget metadata.
+ */
+shindig.container.Container.prototype.getGadgetMetadata = function(
+    gadgetUrl, callback) {
+  var request = {
+      'container' : window.__CONTAINER,
+      'ids' : [ gadgetUrl ]
+  };
+  this.service_.getGadgetMetadata(request, callback);
+};
+
+
 /**
  * Callback that occurs after instantiation/construction of this. Override to
  * provide your specific functionalities.

Modified: 
shindig/trunk/features/src/main/javascript/features/container/gadget_holder.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/gadget_holder.js?rev=966475&r1=966474&r2=966475&view=diff
==============================================================================
--- 
shindig/trunk/features/src/main/javascript/features/container/gadget_holder.js 
(original)
+++ 
shindig/trunk/features/src/main/javascript/features/container/gadget_holder.js 
Thu Jul 22 01:30:36 2010
@@ -85,13 +85,6 @@ shindig.container.GadgetHolder = functio
   this.view_ = null;
 
   /**
-   * JSON metadata about current view being rendered.
-   * @type {Object?}
-   * @private
-   */
-  this.viewInfo_ = null;
-
-  /**
    * A dynamically set social/security token.
    * Social tokens are sent with original view URLs but may need
    * to be refreshed for long lived gadgets.
@@ -197,8 +190,7 @@ shindig.container.GadgetHolder.prototype
   }
   this.renderParams_ = renderParams;
   this.view_ = renderParams['view'];
-  this.viewInfo_ = this.gadgetInfo_['views'][this.view_];
-  if (!this.viewInfo_) {
+  if (!this.gadgetInfo_['views'][this.view_]) {
     throw 'View ' + this.view_ + ' unsupported in ' + this.gadgetInfo_['url'];
   }
 
@@ -206,7 +198,7 @@ shindig.container.GadgetHolder.prototype
 
   // Set up RPC channel. RPC relay url is on gmodules, relative to base of the
   // container. Assumes container has set up forwarding to gmodules at 
/gadgets.
-  gadgets.rpc.setRelayUrl(this.iframeId_, this.viewInfo_['iframeHost'] +
+  gadgets.rpc.setRelayUrl(this.iframeId_, this.gadgetInfo_['iframeHost'] +
       '/gadgets/files/container/rpc_relay.html');
   // Pull RPC token from gadget URI
   gadgets.rpc.setAuthToken(this.iframeId_, this.getRpcToken_());
@@ -270,35 +262,35 @@ shindig.container.GadgetHolder.prototype
  * @private
  */
 shindig.container.GadgetHolder.prototype.getIframeUrl_ = function() {
-  var iframeHost = this.viewInfo_['iframeHost'] || '';
-  var uri = iframeHost + this.viewInfo_['iframePath'];
-  uri = this.updateBooleanParam_(uri, 'debug');
-  uri = this.updateBooleanParam_(uri, 'nocache');
-  uri = this.updateBooleanParam_(uri, 'testmode');
+  var uri = this.gadgetInfo_['iframeUrl'];
+  uri = shindig.container.util.updateQueryParam(uri, 'view', this.view_);
+  uri = this.updateBooleanQueryParam_(uri, 'debug');
+  uri = this.updateBooleanQueryParam_(uri, 'nocache');
+  uri = this.updateBooleanQueryParam_(uri, 'testmode');
   uri = this.updateUserPrefParams_(uri);
 
   // TODO: Share this base container logic
   // TODO: Two SD base URIs - one for container, one for gadgets
   // Need to add parent at end of query due to gadgets parsing bug
-  uri = this.addQueryParam_(uri, 'parent', shindig.container.util.parseOrigin(
-      document.location.href));
+  var parent = shindig.container.util.parseOrigin(document.location.href);
+  uri = shindig.container.util.addQueryParam(uri, 'parent', parent);
 
   // Remove existing social token if we have a new one
   if (this.securityToken_) {
     var securityTokenMatch = uri.match(/([&#?])(st=[^&#]*)/);
     if (securityTokenMatch) {
-      // TODO: Should we move the token to the hash?
       uri = uri.replace(securityTokenMatch[0], securityTokenMatch[1] +
-          'st=' + this.securityToken_);
+          'st=' + encodeURIComponent(this.securityToken_));
     }
   }
 
-  uri = this.addHashParam_(uri, 'mid', String(this.siteId_));
+  uri = shindig.container.util.addFragmentParam(uri, 'mid',
+      String(this.siteId_));
 
   if (this.hasGadgetParams_) {
     var gadgetParamText = gadgets.json.stringify(this.gadgetParams_);
-    uri = this.addHashParam_(uri, 'view-params',
-        encodeURIComponent(gadgetParamText));
+    uri = shindig.container.util.addFragmentParam(uri, 'view-params',
+        gadgetParamText);
   }
   return uri;
 };
@@ -311,12 +303,10 @@ shindig.container.GadgetHolder.prototype
  * @return {string} The URL with param append to.
  * @private
  */
-shindig.container.GadgetHolder.prototype.updateBooleanParam_
+shindig.container.GadgetHolder.prototype.updateBooleanQueryParam_
     = function(uri, param) {
-  if (this.renderParams_[param]) {
-    uri = this.addQueryParam_(uri, param, "1");
-  }
-  return uri;
+  var value = this.renderParams_[param] ? "1" : "0";
+  return shindig.container.util.updateQueryParam(uri, param, value);
 };
 
 
@@ -356,36 +346,6 @@ shindig.container.GadgetHolder.prototype
  * @private
  */
 shindig.container.GadgetHolder.prototype.getRpcToken_ = function() {
-  return this.viewInfo_['iframePath'].match(/rpctoken=([^&]+)/)[1];
-};
-
-
-/**
- * Adds a hash parameter to a URI.
- * @param {string} uri The URI.
- * @param {string} key The param key.
- * @param {string} value The param value.
- * @return {string} The new URI.
- * @private
- */
-shindig.container.GadgetHolder.prototype.addHashParam_ = function(
-    uri, key, value) {
-  return uri + ((uri.indexOf('#') == -1) ? '#' : '&') + key + '=' + value;
-};
-
-
-/**
- * Adds a query parameter to a URI.
- * @param {string} uri The URI.
- * @param {string} key The param key.
- * @param {string} value The param value.
- * @return {string} The new URI.
- * @private
- */
-shindig.container.GadgetHolder.prototype.addQueryParam_ = function(
-    uri, key, value) {
-  var hasQuery = uri.indexOf('?') != -1;
-  var insertPos = (uri.indexOf('#') != -1) ? uri.indexOf('#') : uri.length;
-  return uri.substring(0, insertPos) + (hasQuery ? '&' : '?') +
-      key + '=' + value + uri.substring(insertPos);
+  return shindig.container.util.getParamValue(
+      this.gadgetInfo_['iframeUrl'], 'rpctoken');
 };

Modified: shindig/trunk/features/src/main/javascript/features/container/init.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/init.js?rev=966475&r1=966474&r2=966475&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/container/init.js 
(original)
+++ shindig/trunk/features/src/main/javascript/features/container/init.js Thu 
Jul 22 01:30:36 2010
@@ -43,8 +43,6 @@
     var scriptSrc = getLastScriptSrc();
     if (scriptSrc) {
       window.__API_HOST = shindig.container.util.parseOrigin(scriptSrc);
-      window.__API_PREFIX_PATH = shindig.container.util.parsePrefixPath(
-          scriptSrc, '/gadgets/js/container.js');
       window.__CONTAINER = shindig.container.util.getParamValue(
           scriptSrc, 'container');
       window.__CONTAINER_HOST = shindig.container.util.parseOrigin(

Modified: 
shindig/trunk/features/src/main/javascript/features/container/service.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/service.js?rev=966475&r1=966474&r2=966475&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/container/service.js 
(original)
+++ shindig/trunk/features/src/main/javascript/features/container/service.js 
Thu Jul 22 01:30:36 2010
@@ -35,13 +35,6 @@ shindig.container.Service = function(opt
    */
   this.apiHost_ = String(shindig.container.util.getSafeJsonValue(config,
      shindig.container.ServiceConfig.API_HOST, window.__API_HOST));
-
-  /**
-   * @type {string}
-   */
-  this.apiPrefixPath_ = String(shindig.container.util.getSafeJsonValue(config,
-      shindig.container.ServiceConfig.API_PREFIX_PATH,
-      window.__API_PREFIX_PATH));
   
   /**
    * @type {string}
@@ -174,7 +167,7 @@ shindig.container.Service.prototype.proc
  * @private
  */
 shindig.container.Service.prototype.initializeOsapi_ = function() {
-  var endPoint = this.apiHost_ + this.apiPrefixPath_ + this.apiPath_;
+  var endPoint = this.apiHost_ + this.apiPath_;
   
   var osapiServicesConfig = {};
   osapiServicesConfig['gadgets.rpc'] = [ 'container.listMethods' ];
@@ -201,11 +194,9 @@ shindig.container.Service.prototype.init
  * @enum {string}
  */
 shindig.container.ServiceConfig = {};
-//Host to fetch gadget information, via XHR. 
+// Host to fetch gadget information, via XHR. 
 shindig.container.ServiceConfig.API_HOST = 'apiHost';
-// Prefix to path to fetch gadget information, via XHR. 
-shindig.container.ServiceConfig.API_PREFIX_PATH = 'apiPrefixPath';
-// Path (appears after API_PREFIX_PATH) to fetch gadget information, via XHR. 
+// Path to fetch gadget information, via XHR. 
 shindig.container.ServiceConfig.API_PATH = 'apiPath';
 // Toggle to render gadgets in the same domain. 
 shindig.container.ServiceConfig.SAME_DOMAIN = 'sameDomain';

Modified: shindig/trunk/features/src/main/javascript/features/container/util.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container/util.js?rev=966475&r1=966474&r2=966475&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/container/util.js 
(original)
+++ shindig/trunk/features/src/main/javascript/features/container/util.js Thu 
Jul 22 01:30:36 2010
@@ -37,7 +37,7 @@ shindig.container.util = {};
 /**
  * Extract protocol and domain of container page. Valid values:
  * http://www.cnn.com, chrome-extension://www.cnn.com
- * @param {string} uri The URL to extract path domain from.
+ * @param {string} uri The URL to extract protocol and domain from.
  * @return {string} The protocol and domain of container page.
  */
 shindig.container.util.parseOrigin = function(uri) {
@@ -48,24 +48,6 @@ shindig.container.util.parseOrigin = fun
 
 
 /**
- * Extract prefix path of a URL, not including opt_postfixPath.
- * @param {string} uri The URL to extract path from.
- * @param {string=} opt_postfixPath The URL postfix to avoid extracting.
- * @return {string} The path in URL, before postfixPath.
- */
-shindig.container.util.parsePrefixPath = function(uri, opt_postfixPath) {
-  var path = shindig.container.util.parsePath(uri);
-  if (path && opt_postfixPath) {
-    var endIndex = path.length - opt_postfixPath.length;
-    if (path.lastIndexOf(opt_postfixPath) == endIndex) {
-      return path.substring(0, endIndex);
-    }
-  }
-  return path;
-};
-
-
-/**
  * Extract path of a URL.
  * @param {string} uri The URL to extract path from.
  * @return {string} The path in URL.
@@ -102,6 +84,63 @@ shindig.container.util.getSafeJsonValue 
 
 
 /**
+ * Return URI with its query/fragment parameter either:
+ * - if exist, added with name=value.
+ * - otherwise, replaced with name=value.
+ * @param {string} uri URL to work with.
+ * @param {string} name of parameter to add/replace.
+ * @param {string} value of parameter to add/replace.
+ * @return {string}
+ */
+shindig.container.util.updateQueryParam = function(uri, name, value) {
+  var fragmentString = '';
+  var fragmentIndex = uri.indexOf('#');
+  if (fragmentIndex >= 0) {
+       fragmentString = uri.substring(fragmentIndex);
+    uri = uri.substring(0, fragmentIndex);
+  }
+
+  var re = new RegExp('([&?])' + name + '[^&]*');
+  if (uri.match(re)) {
+       uri = uri.replace(re, '$1' + encodeURIComponent(name) + '=' +
+        encodeURIComponent(value));
+  } else {
+       uri = shindig.container.util.addQueryParam(uri, name, value);
+  }
+  return uri + fragmentString;
+};
+
+
+/**
+ * Adds a hash parameter to a URI.
+ * @param {string} uri The URI.
+ * @param {string} key The param key.
+ * @param {string} value The param value.
+ * @return {string} The new URI.
+ */
+shindig.container.util.addFragmentParam = function(uri, key, value) {
+  return uri + ((uri.indexOf('#') == -1) ? '#' : '&')
+      + encodeURIComponent(key) + '=' + encodeURIComponent(value);
+};
+
+
+/**
+ * Adds a query parameter to a URI.
+ * @param {string} uri The URI.
+ * @param {string} key The param key.
+ * @param {string} value The param value.
+ * @return {string} The new URI.
+ */
+shindig.container.util.addQueryParam = function(uri, key, value) {
+  var hasQuery = uri.indexOf('?') != -1;
+  var insertPos = (uri.indexOf('#') != -1) ? uri.indexOf('#') : uri.length;
+  return uri.substring(0, insertPos) + (hasQuery ? '&' : '?') +
+      encodeURIComponent(key) + '=' + encodeURIComponent(value) +
+      uri.substring(insertPos);
+};
+
+
+/**
  * Merge two JSON together. Keys in json2 will replace than in json1.
  * @param {Object} json1 JSON to start merge with.
  * @param {Object} json2 JSON to append/replace json1.


Reply via email to