Return-Path: X-Original-To: apmail-shindig-commits-archive@www.apache.org Delivered-To: apmail-shindig-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A687A7EA4 for ; Thu, 14 Jul 2011 18:53:45 +0000 (UTC) Received: (qmail 60378 invoked by uid 500); 14 Jul 2011 18:53:45 -0000 Delivered-To: apmail-shindig-commits-archive@shindig.apache.org Received: (qmail 60279 invoked by uid 500); 14 Jul 2011 18:53:44 -0000 Mailing-List: contact commits-help@shindig.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@shindig.apache.org Delivered-To: mailing list commits@shindig.apache.org Received: (qmail 60265 invoked by uid 99); 14 Jul 2011 18:53:44 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jul 2011 18:53:44 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jul 2011 18:53:36 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0428123889EC; Thu, 14 Jul 2011 18:53:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1146846 - in /shindig/trunk/features/src: main/javascript/features/container.gadget/ main/javascript/features/container.util/ main/javascript/features/container/ test/javascript/features/container/ Date: Thu, 14 Jul 2011 18:53:14 -0000 To: commits@shindig.apache.org From: woodser@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110714185315.0428123889EC@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: woodser Date: Thu Jul 14 18:53:14 2011 New Revision: 1146846 URL: http://svn.apache.org/viewvc?rev=1146846&view=rev Log: Committing patch to update language/pref handling in the common container: https://reviews.apache.org/r/1011/ Modified: shindig/trunk/features/src/main/javascript/features/container.gadget/gadget_site.js shindig/trunk/features/src/main/javascript/features/container.util/util.js shindig/trunk/features/src/main/javascript/features/container/container.js shindig/trunk/features/src/main/javascript/features/container/service.js shindig/trunk/features/src/test/javascript/features/container/service_test.js Modified: shindig/trunk/features/src/main/javascript/features/container.gadget/gadget_site.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container.gadget/gadget_site.js?rev=1146846&r1=1146845&r2=1146846&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/container.gadget/gadget_site.js (original) +++ shindig/trunk/features/src/main/javascript/features/container.gadget/gadget_site.js Thu Jul 14 18:53:14 2011 @@ -58,11 +58,12 @@ osapi.container.GadgetSite = function(ar this.loadingGadgetEl_ = args['bufferEl']; /** - * Unique ID of this site. - * @type {number} + * Unique ID of this site. Uses the ID of the gadgetEl, if set, or an auto-generated number. + * @type {string} * @private */ - this.id_ = osapi.container.GadgetSite.nextUniqueId_++; + this.id_ = (this.currentGadgetEl && this.currentGadgetEl_.id) ? this.currentGadgetEl_.id + : osapi.container.GadgetSite.nextUniqueId_++; /** * ID of parent gadget. @@ -141,7 +142,7 @@ osapi.container.GadgetSite.prototype.set /** - * @return {number} The ID of this gadget site. + * @return {string} The ID of this gadget site. */ osapi.container.GadgetSite.prototype.getId = function() { return this.id_; @@ -195,6 +196,7 @@ osapi.container.GadgetSite.prototype.nav var callback = opt_callback || function() {}; var request = osapi.container.util.newMetadataRequest([gadgetUrl]); var self = this; + this.service_.getGadgetMetadata(request, function(response) { var xrt = (!cached) ? (osapi.container.util.getCurrentTimeMs() - start) : 0; var gadgetInfo = response[gadgetUrl]; Modified: shindig/trunk/features/src/main/javascript/features/container.util/util.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/container.util/util.js?rev=1146846&r1=1146845&r2=1146846&view=diff ============================================================================== --- shindig/trunk/features/src/main/javascript/features/container.util/util.js (original) +++ shindig/trunk/features/src/main/javascript/features/container.util/util.js Thu Jul 14 18:53:14 2011 @@ -62,23 +62,26 @@ osapi.container.util.mergeJsons = functi * Construct a JSON request to get gadget metadata. For now, this will request * a super-set of data needed for all CC APIs requiring gadget metadata, since * the caching of response is not additive. - * @param {Array} gadgetUrls A list of gadget URLs. + * @param {Array} gadgetUrls An array of gadget URLs. * @return {Object} the resulting JSON. */ osapi.container.util.newMetadataRequest = function(gadgetUrls) { + if (!osapi.container.util.isArray(gadgetUrls)) { + gadgetUrls = [gadgetUrls]; + } return { - 'container': window.__CONTAINER, - 'ids': gadgetUrls, - 'fields': [ - 'iframeUrl', - 'modulePrefs.*', - 'needsTokenRefresh', - 'userPrefs.*', - 'views.preferredHeight', - 'views.preferredWidth', - 'expireTimeMs', - 'responseTimeMs' - ] + 'container': window.__CONTAINER, + 'ids': gadgetUrls, + 'fields': [ + 'iframeUrl', + 'modulePrefs.*', + 'needsTokenRefresh', + 'userPrefs.*', + 'views.preferredHeight', + 'views.preferredWidth', + 'expireTimeMs', + 'responseTimeMs' + ] }; }; @@ -89,12 +92,15 @@ osapi.container.util.newMetadataRequest * @return {Object} the resulting JSON. */ osapi.container.util.newTokenRequest = function(gadgetUrls) { + if (!osapi.container.util.isArray(gadgetUrls)) { + gadgetUrls = [gadgetUrls]; + } return { - 'container': window.__CONTAINER, - 'ids': gadgetUrls, - 'fields': [ - 'token' - ] + 'container': window.__CONTAINER, + 'ids': gadgetUrls, + 'fields': [ + 'token' + ] }; }; @@ -114,6 +120,16 @@ osapi.container.util.toArrayOfJsonKeys = /** + * Tests an object to see if it is an array or not. + * @param {object} ojb Object to test. + * @return {boolean} If obj is an array. + */ +osapi.container.util.isArray = function(obj) { + return Object.prototype.toString.call(obj) == "[object Array]"; +}; + + +/** * @param {Object} json to check. * @return {Boolean} true if json is empty. */ 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=1146846&r1=1146845&r2=1146846&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 14 18:53:14 2011 @@ -28,7 +28,7 @@ * @constructor */ osapi.container.Container = function(opt_config) { - var config = opt_config || {}; + var config = this.config_ = opt_config || {}; /** * A list of objects containing functions to be invoked when gadgets are @@ -173,23 +173,32 @@ osapi.container.Container.prototype.newG */ osapi.container.Container.prototype.navigateGadget = function( site, gadgetUrl, viewParams, renderParams, opt_callback) { - var callback = opt_callback || function() {}; + var callback = opt_callback || function() {}, + ContainerConfig = osapi.container.ContainerConfig, + RenderParam = osapi.container.RenderParam; + if (this.allowDefaultView_) { - renderParams[osapi.container.RenderParam.ALLOW_DEFAULT_VIEW] = true; + renderParams[RenderParam.ALLOW_DEFAULT_VIEW] = true; } if (this.renderCajole_) { - renderParams[osapi.container.RenderParam.CAJOLE] = true; + renderParams[RenderParam.CAJOLE] = true; } if (this.renderDebug_) { - renderParams[osapi.container.RenderParam.NO_CACHE] = true; - renderParams[osapi.container.RenderParam.DEBUG] = true; + renderParams[RenderParam.NO_CACHE] = true; + renderParams[RenderParam.DEBUG] = true; } if (this.renderTest_) { - renderParams[osapi.container.RenderParam.TEST_MODE] = true; + renderParams[RenderParam.TEST_MODE] = true; } this.refreshService_(); - + + // Try to retrieve preferences for the gadget if no preferences were explicitly provided. + if (this.config_[ContainerConfig.GET_PREFERENCES] && !renderParams[RenderParam.USER_PREFS]) { + renderParams[RenderParam.USER_PREFS] = + this.config_[ContainerConfig.GET_PREFERENCES](site.getId(), gadgetUrl); + } + var self = this; var selfSite = site; // TODO: Lifecycle, add ability for current gadget to cancel nav. @@ -432,6 +441,26 @@ osapi.container.ContainerConfig.PRELOAD_ * @const */ osapi.container.ContainerConfig.PRELOAD_TOKENS = 'preloadTokens'; +/** + * Used to query the language locale part of the container page. + * @type {function} + */ +osapi.container.ContainerConfig.GET_LANGUAGE = 'GET_LANGUAGE'; +/** + * Used to query the country locale part of the container page. + * @type {function} + */ +osapi.container.ContainerConfig.GET_COUNTRY = 'GET_COUNTRY'; +/** + * Used to retrieve the persisted preferences for a gadget. + * @type {function} + */ +osapi.container.ContainerConfig.GET_PREFERENCES = 'GET_PREFERENCES'; +/** + * Used to persist preferences for a gadget. + * @type {function} + */ +osapi.container.ContainerConfig.SET_PREFERENCES = 'SET_PREFERENCES'; // ----------------------------------------------------------------------------- @@ -581,12 +610,29 @@ osapi.container.Container.prototype.isRe * @private */ osapi.container.Container.prototype.registerRpcServices_ = function() { + var self = this; + this.rpcRegister('resize_iframe', function(rpcArgs, data) { var site = rpcArgs[osapi.container.GadgetSite.RPC_ARG_KEY]; if (site) { // Check if site is not already closed. site.setHeight(data); } }); + + /** + * @see setprefs.js setprefs feature. + */ + this.rpcRegister('set_pref', function(rpcArgs, key, value) { + var site = rpcArgs[osapi.container.GadgetSite.RPC_ARG_KEY]; + var setPrefs = self.config_[osapi.container.ContainerConfig.SET_PREFERENCES]; + if (site && setPrefs) { // Check if site is not already closed. + var data = {}; + for (var i = 2, j = arguments.length; i < j; i += 2) { + data[arguments[i]] = arguments[i + 1]; + } + setPrefs(site.getId(), site.getActiveGadgetHolder().getUrl(), data); + } + }); }; @@ -719,5 +765,4 @@ osapi.container.Container.prototype.newU osapi.container.Container.prototype.navigateUrl = function(site, url, renderParams){ site.render(url, renderParams); return site; -}; - \ No newline at end of file +}; \ No newline at end of file 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=1146846&r1=1146845&r2=1146846&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 14 18:53:14 2011 @@ -57,6 +57,20 @@ osapi.container.Service = function(opt_c * @private */ this.cachedTokens_ = {}; + + /** + * @see osapi.container.Container.prototype.getLanguage + */ + if (config.GET_LANGUAGE) { + this.getLanguage = config.GET_LANGUAGE; + } + + /** + * @see osapi.container.Container.prototype.getCountry + */ + if (config.GET_COUNTRY) { + this.getCountry = config.GET_COUNTRY; + } this.registerOsapiServices(); @@ -80,8 +94,7 @@ osapi.container.Service.prototype.onCons * @param {Object} request JSON object representing the request. * @param {function(Object)=} opt_callback function to call upon data receive. */ -osapi.container.Service.prototype.getGadgetMetadata = function( - request, opt_callback) { +osapi.container.Service.prototype.getGadgetMetadata = function(request, opt_callback) { // TODO: come up with an expiration mechanism to evict cached gadgets. // Can be based on renderParam['nocache']. Be careful with preloaded and // arbitrarily-navigated gadgets. The former should be indefinite, unless @@ -99,7 +112,9 @@ osapi.container.Service.prototype.getGad // Otherwise, request for uncached metadatas. } else { var self = this; - request = osapi.container.util.newMetadataRequest(uncachedUrls); + request['ids'] = uncachedUrls; + request['language'] = this.getLanguage(); + request['country'] = this.getCountry(); osapi['gadgets']['metadata'](request).execute(function(response) { // If response entirely fails, augment individual errors. @@ -321,6 +336,49 @@ osapi.container.Service.prototype.filter }; +/** + * @returns {string} Best-guess locale for current browser. + */ +osapi.container.Service.prototype.getLocale_ = function() { + var nav = window.navigator; + return nav.userLanguage || nav.systemLanguage || nav.language; +}; + + +/** + * A callback function that will return the correct language locale part to use when + * asking the server to render a gadget or when asking the server for 1 or more + * gadget's metadata. + *
+ * May be overridden by passing in a config parameter during container construction. + * * @returns {string} Language locale part. + */ +osapi.container.Service.prototype.getLanguage = function() { + try { + return this.getLocale_().split('-')[0] || "ALL"; + } catch (e) { + return "ALL"; + } +}; + + +/** + * A callback function that will return the correct country locale part to use when + * asking the server to render a gadget or when asking the server for 1 or more + * gadget's metadata. + *
+ * May be overridden by passing in a config parameter during container construction. + * @returns {string} Country locale part. + */ +osapi.container.Service.prototype.getCountry = function() { + try { + return this.getLocale_().split('-')[1] || "ALL"; + } catch (e) { + return "ALL"; + } +}; + + // ----------------------------------------------------------------------------- // Configuration // ----------------------------------------------------------------------------- Modified: shindig/trunk/features/src/test/javascript/features/container/service_test.js URL: http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/container/service_test.js?rev=1146846&r1=1146845&r2=1146846&view=diff ============================================================================== --- shindig/trunk/features/src/test/javascript/features/container/service_test.js (original) +++ shindig/trunk/features/src/test/javascript/features/container/service_test.js Thu Jul 14 18:53:14 2011 @@ -65,7 +65,14 @@ ServiceTest.prototype.setupUtilCurrentTi }; ServiceTest.prototype.testGetGadgetMetadata = function() { - var service = new osapi.container.Service(); + var service = new osapi.container.Service({ + GET_LANGUAGE: function() { + return 'pt'; + }, + GET_COUNTRY: function() { + return 'BR'; + } + }); service.cachedMetadatas_ = { 'cached1.xml' : { 'url' : 'cached1.xml', @@ -128,6 +135,11 @@ ServiceTest.prototype.testGetGadgetMetad this.assertTrue(service.cachedMetadatas_['resp1.xml'] != null); this.assertTrue(service.cachedMetadatas_['resp2.xml'] != null); this.assertTrue(service.cachedMetadatas_['resp3.xml'] != null); + + this.assertEquals('pt', service.getLanguage()); + this.assertEquals('BR', service.getCountry()); + this.assertEquals('pt', request.language); + this.assertEquals('BR', request.country); }; ServiceTest.prototype.testUncacheStaleGadgetMetadataExcept = function() {