Author: ddumont
Date: Wed Mar 14 14:06:37 2012
New Revision: 1300552

URL: http://svn.apache.org/viewvc?rev=1300552&view=rev
Log:
SHINDIG-1727 gadgets.selection.getSelection() does not return current selection 
on initial loading of gadget

Added:
    
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-changer.xml
   (with props)
    
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-query.xml
   (with props)
Modified:
    
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-listener.xml
    shindig/trunk/features/src/main/javascript/features/selection/selection.js
    
shindig/trunk/features/src/main/javascript/features/selection/selection_container.js
    
shindig/trunk/features/src/test/javascript/features/selection/selection_test.js

Added: 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-changer.xml
URL: 
http://svn.apache.org/viewvc/shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-changer.xml?rev=1300552&view=auto
==============================================================================
--- 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-changer.xml
 (added)
+++ 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-changer.xml
 Wed Mar 14 14:06:37 2012
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<Module>
+  <ModulePrefs title="Sample Selection Changer" height="250">
+    <Require feature="selection"/>
+  </ModulePrefs>
+  <Content type="html"><![CDATA[
+    <style>
+      #output {
+        word-wrap: break-word;
+      }
+    </style>
+    <script>
+      function setSelection(selection) {
+        selection = [{type:'com.example.food', dataObject: selection}];
+        gadgets.selection.setSelection(selection);
+        document.getElementById("output").innerHTML = 'selection: '
+         + gadgets.util.escapeString('' + gadgets.json.stringify(selection)) + 
'<br/>';
+      }
+    </script>
+    <div>
+      <input type="radio" name="group1" value="Milk" 
onclick="setSelection('Milk')"/> Milk<br>
+      <input type="radio" name="group1" value="Butter" 
onclick="setSelection('Butter')"/> Butter<br>
+      <input type="radio" name="group1" value="Cheese" 
onclick="setSelection('Cheese')"/> Cheese<br>
+      <input type="radio" name="group1" value="Water" 
onclick="setSelection('Water')"/> Water<br>
+      <input type="radio" name="group1" value="Beer" 
onclick="setSelection('Beer')"/> Beer<br>
+      <input type="radio" name="group1" value="Wine" 
onclick="setSelection('Wine')"/> Wine
+    </div>
+    <div id="output"></div>
+  ]]></Content>
+</Module>

Propchange: 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-changer.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-listener.xml
URL: 
http://svn.apache.org/viewvc/shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-listener.xml?rev=1300552&r1=1300551&r2=1300552&view=diff
==============================================================================
--- 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-listener.xml
 (original)
+++ 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-listener.xml
 Wed Mar 14 14:06:37 2012
@@ -18,36 +18,38 @@
  * under the License.
 -->
 <Module>
-<ModulePrefs title="Sample Selection Listener"
-             height="250">
-<Require feature="selection"></Require>
-</ModulePrefs>
-<Content type="html">
-<![CDATA[
-<script>
+  <ModulePrefs title="Sample Selection Listener" height="250">
+    <Require feature="selection"></Require>
+  </ModulePrefs>
+  <Content type="html"><![CDATA[
+    <style>
+      #output {
+        word-wrap: break-word;
+        overflow-y: auto;
+      }
+    </style>
+    <script>
+      function selectionListener(/*Array*/ selection) {
+        var output = document.getElementById("output");
+        output.innerHTML = 'selection: '
+         + gadgets.util.escapeString('' + gadgets.json.stringify(selection))
+         + '<br/>' + output.innerHTML;
+      }
 
-function selectionListener(selection) {
-  document.getElementById("output").innerHTML =
-    "selection: " + gadgets.util.escapeString(JSON.stringify(selection) + "") 
+ "<br/>";
-}
+      function addListener() {
+        gadgets.selection.addListener(selectionListener);
+        document.getElementById("output").innerHTML = "Selection listener 
added...";
+      }
 
-function addListener() {
-  gadgets.selection.addListener(selectionListener);
-  document.getElementById("output").innerHTML = "Selection listener added...";
-}
-
-function removeListener() {
-  gadgets.selection.removeListener(selectionListener);
-  document.getElementById("output").innerHTML = "";
-}
-
-</script>
-<div>
-<input type="button" value="Add Selection Listener" onclick="addListener()"/>
-<input type="button" value="Remove Selection Listener" 
onclick="removeListener()"/>
-</div>
-<div id="output">
-</div>
-]]>
-</Content>
+      function removeListener() {
+        gadgets.selection.removeListener(selectionListener);
+        document.getElementById("output").innerHTML = "";
+      }
+    </script>
+    <div>
+      <input type="button" value="Add Selection Listener" 
onclick="addListener()"/>
+      <input type="button" value="Remove Selection Listener" 
onclick="removeListener()"/>
+    </div>
+    <div id="output"></div>
+  ]]></Content>
 </Module>

Added: 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-query.xml
URL: 
http://svn.apache.org/viewvc/shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-query.xml?rev=1300552&view=auto
==============================================================================
--- 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-query.xml
 (added)
+++ 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-query.xml
 Wed Mar 14 14:06:37 2012
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<Module>
+  <ModulePrefs title="Sample Selection Query" height="250">
+    <Require feature="selection"/>
+  </ModulePrefs>
+  <Content type="html"><![CDATA[
+    <style>
+      #output {
+        word-wrap: break-word;
+      }
+    </style>
+    <script>
+      function querySelection() {
+        var selection = gadgets.selection.getSelection();
+        document.getElementById("output").innerHTML = 'selection: '
+         + gadgets.util.escapeString('' + gadgets.json.stringify(selection)) + 
'<br/>';
+      }
+    </script>
+    <div>
+      <input type="button" value="Query Current Selection" 
onclick="querySelection()"/>
+    </div>
+    <div id="output"></div>
+  ]]></Content>
+</Module>

Propchange: 
shindig/trunk/content/samplecontainer/examples/conservcontainer/sample-selection-query.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
shindig/trunk/features/src/main/javascript/features/selection/selection.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/selection/selection.js?rev=1300552&r1=1300551&r2=1300552&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/selection/selection.js 
(original)
+++ shindig/trunk/features/src/main/javascript/features/selection/selection.js 
Wed Mar 14 14:06:37 2012
@@ -30,6 +30,18 @@ gadgets['selection'] = function() {
   var listeners,
       currentSelection;
 
+  function addListener(listener) {
+    if (!listeners) {
+      listeners  = [];
+      gadgets.rpc.call('..', 'gadgets.selection.register', function(selection) 
{
+        currentSelection = selection;
+      });
+    }
+    if (typeof listener === 'function') {
+      listeners.push(listener); // add the listener to the list
+    }
+  }
+
   gadgets.util.registerOnLoadHandler(function() {
     gadgets.rpc.register('gadgets.selection.selectionChanged', 
function(selection) {
       currentSelection = selection;
@@ -37,6 +49,10 @@ gadgets['selection'] = function() {
         listeners[i](selection);
       }
     });
+
+    // Start watching selection.
+    // TODO: change getSelection api to be async so we don't need to do this.
+    addListener(function(){});
   });
 
   return /** @scope gadgets.selection */ {
@@ -61,15 +77,7 @@ gadgets['selection'] = function() {
      * Registers a listener for selection.
      * @param {function} listener The listener to remove.
      */
-    addListener: function(listener) {
-      if (!listeners) {
-        listeners  = [];
-        gadgets.rpc.call('..', 'gadgets.selection.register');
-      }
-      if (typeof listener === 'function') {
-        listeners.push(listener); // add the listener to the list
-      }
-    },
+    addListener: addListener,
 
     /**
      * Removes a listener for selection.

Modified: 
shindig/trunk/features/src/main/javascript/features/selection/selection_container.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/selection/selection_container.js?rev=1300552&r1=1300551&r2=1300552&view=diff
==============================================================================
--- 
shindig/trunk/features/src/main/javascript/features/selection/selection_container.js
 (original)
+++ 
shindig/trunk/features/src/main/javascript/features/selection/selection_container.js
 Wed Mar 14 14:06:37 2012
@@ -68,8 +68,10 @@
     context.rpcRegister('gadgets.selection.set', function(rpcArgs, selection) {
       notifySelection(selection);
     });
+
     context.rpcRegister('gadgets.selection.register', function(rpcArgs) {
       listeningGadgets[rpcArgs.f] = 1;
+      return _selection;
     });
 
     return /** @scope gadgets.selection */ {

Modified: 
shindig/trunk/features/src/test/javascript/features/selection/selection_test.js
URL: 
http://svn.apache.org/viewvc/shindig/trunk/features/src/test/javascript/features/selection/selection_test.js?rev=1300552&r1=1300551&r2=1300552&view=diff
==============================================================================
--- 
shindig/trunk/features/src/test/javascript/features/selection/selection_test.js 
(original)
+++ 
shindig/trunk/features/src/test/javascript/features/selection/selection_test.js 
Wed Mar 14 14:06:37 2012
@@ -79,10 +79,10 @@ SelectionTest.inherits(TestCase);
 
   SelectionTest.prototype.testGadgetAddSelectionListener = function() {
     var container = new osapi.container.Container({});
-    var callback = function() {
-    };
-    gadgets.selection.addListener(callback);
-    this.assertRpcCalled("..", "gadgets.selection.register");
+    gadgets.selection.addListener(function(){});
+    this.assertRpcCalled("..", "gadgets.selection.register", function(){});
+    gadgets.selection.addListener(function(){});
+    this.assertNoRpcCalled();
   };
 
   /**


Reply via email to