You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by zh...@apache.org on 2008/06/11 06:49:29 UTC

svn commit: r666520 - in /incubator/shindig/trunk: config/container.js features/pom.xml features/views/urltemplatetest.js features/views/views.js

Author: zhen
Date: Tue Jun 10 21:49:28 2008
New Revision: 666520

URL: http://svn.apache.org/viewvc?rev=666520&view=rev
Log:
SHINDIG-365
SHINDIG-366

Initial implementation of new views features: gadgets.views.bind, gadgets.views.View.bind, gadgets.views.View.getUrlTemplate.

Note that currently only basic variable substitution is implemented.


Added:
    incubator/shindig/trunk/features/views/urltemplatetest.js
Modified:
    incubator/shindig/trunk/config/container.js
    incubator/shindig/trunk/features/pom.xml
    incubator/shindig/trunk/features/views/views.js

Modified: incubator/shindig/trunk/config/container.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/config/container.js?rev=666520&r1=666519&r2=666520&view=diff
==============================================================================
--- incubator/shindig/trunk/config/container.js (original)
+++ incubator/shindig/trunk/config/container.js Tue Jun 10 21:49:28 2008
@@ -64,10 +64,12 @@
   "views" : {
     "profile" : {
       "isOnlyVisible" : false,
+      "urlTemplate" : "http://localhost/gadgets/profile?{var}",
       "aliases": ["DASHBOARD", "default"]
     },
     "canvas" : {
       "isOnlyVisible" : true,
+      "urlTemplate" : "http://localhost/gadgets/canvas?{var}",
       "aliases" : ["FULL_PAGE"]
     }
   },

Modified: incubator/shindig/trunk/features/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/pom.xml?rev=666520&r1=666519&r2=666520&view=diff
==============================================================================
--- incubator/shindig/trunk/features/pom.xml (original)
+++ incubator/shindig/trunk/features/pom.xml Tue Jun 10 21:49:28 2008
@@ -87,9 +87,11 @@
                 <sourceDirectory>${basedir}</sourceDirectory>
                 <sources>
                   <source>mockenv.js</source>
+                  <source>core/config.js</source>
                   <source>core/auth.js</source>
                   <source>core/util.js</source>
                   <source>core/prefs.js</source>
+                  <source>views/views.js</source>
                   <source>opensocial-reference/opensocial.js</source>
                   <source>opensocial-reference/activity.js</source>
                   <source>opensocial-reference/address.js</source>
@@ -123,6 +125,7 @@
                     <type>TESTCASES</type>
                     <includes>
                       <include>core/*test.js</include>
+                      <include>views/*test.js</include>
                     </includes>
                   </testSuite>
                  <testSuite>

Added: incubator/shindig/trunk/features/views/urltemplatetest.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/views/urltemplatetest.js?rev=666520&view=auto
==============================================================================
--- incubator/shindig/trunk/features/views/urltemplatetest.js (added)
+++ incubator/shindig/trunk/features/views/urltemplatetest.js Tue Jun 10 21:49:28 2008
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @fileoverview
+ *
+ * Unittests for URL template functions of gadgets.views.
+ */
+
+function UrlTemplateTest(name) {
+  TestCase.call(this, name);
+}
+
+UrlTemplateTest.inherits(TestCase);
+
+UrlTemplateTest.prototype.setUp = function() {
+};
+
+UrlTemplateTest.prototype.tearDown = function() {
+};
+
+UrlTemplateTest.prototype.batchTest = function(testcases) {
+  for (var i = 0; i < testcases.length; ++i) {
+    var testcase = testcases[i];
+    var urlTemplate = testcase[0];
+    var environment = testcase[1];
+    var expected = testcase[2];
+
+    if (typeof expected === 'string') {
+      this.assertEquals(expected, gadgets.views.bind(urlTemplate, environment));
+    } else {
+      var fallenThrough = false;
+      try {
+        gadgets.views.bind(urlTemplate, environment);
+        fallenThrough = true;
+      } catch (e) {
+        this.assertEquals(expected.message, e.message);
+      }
+      this.assertFalse(fallenThrough);
+    }
+  }
+};
+
+UrlTemplateTest.prototype.testVariableSubstitution = function() {
+  this.batchTest([
+    [
+      'http://host/path/{open}{social}{0.8}{d-_-b}',
+      {
+        'open': 'O',
+        'social': 'S',
+        '0.8': 'v0.8',
+        'd-_-b': '!'
+      },
+      'http://host/path/OSv0.8!'
+    ],
+
+    [
+      'http://host/path/{undefined_value}/suffix',
+      {
+        'value': 'undefined'
+      },
+      'http://host/path//suffix'
+    ],
+
+    [
+      'http://host/path/{recurring}{recurring}{recurring}',
+      {
+        'recurring': '.'
+      },
+      'http://host/path/...'
+    ],
+
+    [
+      'http://host/path/{invalid definition!!!}',
+      {
+        'value': 'defined'
+      },
+      new Error('Invalid syntax : {invalid definition!!!}')
+    ]
+
+  ]);
+};
+

Modified: incubator/shindig/trunk/features/views/views.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/views/views.js?rev=666520&r1=666519&r2=666520&view=diff
==============================================================================
--- incubator/shindig/trunk/features/views/views.js (original)
+++ incubator/shindig/trunk/features/views/views.js Tue Jun 10 21:49:28 2008
@@ -84,6 +84,59 @@
   gadgets.config.register("views", null, init);
 
   return {
+
+    /**
+     * Binds a URL template with variables in the passed environment
+     * to produce a URL string.
+     *
+     * The URL template conforms to the IETF draft spec:
+     * http://bitworking.org/projects/URI-Templates/spec/draft-gregorio-uritemplate-03.html
+     *
+     * @param {string} urlTemplate A URL template for a container view.
+     * @param {Map&lt;string, string&gt;} environment A set of named variables.
+     * @return {string} A URL string with substituted variables.
+     */
+    bind : function(urlTemplate, environment) {
+      function getVar(varName, defaultVal) {
+        return environment.hasOwnProperty(varName) ?
+               environment[varName] : defaultVal;
+      }
+
+      // TODO Validate environment
+      // TODO Validate urlTemplate
+
+      var varRE = /^([a-zA-Z0-9][a-zA-Z0-9_.-]*)$/,
+          expansionRE = /\{([^}]*)\}/g,
+          result = [],
+          textStart = 0,
+          group,
+          match,
+          varName;
+
+      while (group = expansionRE.exec(urlTemplate)) {
+        result.push(urlTemplate.substring(textStart, group.index));
+        textStart = expansionRE.lastIndex;
+        if (match = group[1].match(varRE)) {
+          // TODO Add support for "var=default_value" syntax
+          varName = match[1];
+          result.push(getVar(varName, ''));
+        } else {
+          // TODO Add support for "-op|arg|vars" syntax
+          // TODO Parse the "-opt" operator
+          // TODO Parse the "-neg" operator
+          // TODO Parse the "-prefix" operator
+          // TODO Parse the "-suffix" operator
+          // TODO Parse the "-join" operator
+          // TODO Parse the "-list" operator
+          throw new Error('Invalid syntax : ' + group[0]);
+        }
+      }
+      // TODO Throw an exception if no variable is defined at all.
+      result.push(urlTemplate.substr(textStart));
+
+      return result.join('');
+    },
+
     /**
      * Attempts to navigate to this gadget in a different view. If the container
      * supports parameters will pass the optional parameters along to the gadget
@@ -146,6 +199,29 @@
 };
 
 /**
+ * Returns the associated URL template of the view.
+ * The URL template conforms to the IETF draft spec:
+ * http://bitworking.org/projects/URI-Templates/spec/draft-gregorio-uritemplate-03.html
+ * @return {string} A URL template.
+ */
+gadgets.views.View.prototype.getUrlTemplate = function() {
+  return gadgets.config &&
+         gadgets.config.views &&
+         gadgets.config.views[this.name_] &&
+         gadgets.config.views[this.name_].urlTemplate;
+};
+
+/**
+ * Binds the view's URL template with variables in the passed environment
+ * to produce a URL string.
+ * @param {Map&lt;string, string&gt;} environment A set of named variables.
+ * @return {string} A URL string with substituted variables.
+ */
+gadgets.views.View.prototype.bind = function(environment) {
+  return gadgets.views.bind(this.getUrlTemplate(), environment);
+};
+
+/**
  * @return {Boolean} True if this is the only visible gadget on the page.
  */
 gadgets.views.View.prototype.isOnlyVisibleGadget = function() {