Author: [email protected]
Date: Wed Jan  4 10:43:53 2012
New Revision: 1884

Log:
[AMDATUOPENSOCIAL-118] Implemented support for LIST type in user preferences 
dialog.

Modified:
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
   
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
  (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/AppDataServiceImpl.java
  Wed Jan  4 10:43:53 2012
@@ -114,6 +114,15 @@
             // Despite the generic stuff, at runtime the posted AppData values 
might still contain Integers, Doubles
             // and other primitive types. Therefore we explicitly invoke 
toString() on them.
             currentValues.put(key, values.get(key));
+            
+            if (!(values.get(key) instanceof String)) {
+                Object v = values.get(key);
+                if (v instanceof Boolean) {
+                    currentValues.put(key, (Boolean) v ? "1" : "0");
+                } else if (v instanceof Integer) {
+                    currentValues.put(key, ((Integer) v).toString());
+                }
+            }
         }
         return currentValues;
     }

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
    (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/appdatauserprefstore.js
    Wed Jan  4 10:43:53 2012
@@ -69,6 +69,7 @@
 shindig.AppDataBasedUserPrefStore.prototype.savePrefs = function(gadget) {
   var userPrefValues = {};
   for (var key in gadget.getUserPrefs()) {
+    var type = gadget.getUserPrefs()[key].datatype;
     userPrefValues[key] = gadget.getUserPrefs()[key].value;
   }
 

Modified: 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js
       (original)
+++ 
trunk/amdatu-opensocial/opensocial-shindig/src/main/resources/features/shindig.container/shindig-container.js
       Wed Jan  4 10:43:53 2012
@@ -191,7 +191,7 @@
   var gadget = shindig.container.getGadget(id);
   var prefs = gadget.getUserPrefs();
   for (var i = 1, j = arguments.length; i < j; i += 2) {
-    prefs[arguments[i]] = arguments[i + 1];
+    prefs[arguments[i]].value = arguments[i + 1];
   }
   gadget.saveUserPrefs();
 };
@@ -600,7 +600,7 @@
   $(table).append(tr);
 
   // Append label TD
-  var ltd = $('<td/>').attr('align', 'left').text(pref.display_name);
+  var ltd = $('<td/>').attr('align', 'left').attr('valign', 
'top').text(pref.display_name);
   $(tr).append(ltd);
 
   var elPrefix = 'm_' + this.id + '_';
@@ -612,29 +612,82 @@
   var rtd = $('<td/>').attr('align', 'left');
   $(tr).append(rtd);
 
+  // UserPref of type BOOL
   if ("bool" == pref.datatype.toLowerCase()) {
     field = $('<input/>').attr('type', 'checkbox').attr('name', 
name).attr('id', id);
     if (pref.value == 1 || pref.value == "true" || pref.value == "TRUE") {
       field.prop("checked", true);
     }
-  } else if ("enum" == pref.datatype.toLowerCase()) {
+    $(rtd).append(field);
+  }
+
+  // UserPref of type ENUM
+  else if ("enum" == pref.datatype.toLowerCase()) {
     field = $('<select/>').attr('name', name).attr('id', id);
     var enumValues = pref.enumValues;
-    for (var i=0; i<enumValues.length; i++) {
-      var option = $('<option/>').attr('value', 
enumValues[i].value).text(enumValues[i].displayValue);
+    for (var j=0; j<enumValues.length; j++) {
+      var option = $('<option/>').attr('value', 
enumValues[j].value).text(enumValues[j].displayValue);
       // Select the selected option
-      if (enumValues[i].value == pref.value) {
+      if (enumValues[j].value == pref.value) {
          option.prop("selected", true);
       };
       $(field).append(option);
+      $(rtd).append(field);
     }
-  } else if ("string" == pref.datatype.toLowerCase()) {
-    field = $('<input/>').attr('type', 'edit').attr('name', name).attr('id', 
id).attr('value', pref.value);
   }
 
-  $(rtd).append(field);
+  // UserPref of type LIST
+  else if ("list" == pref.datatype.toLowerCase()) {
+    field = $('<input/>').attr('type', 'edit').attr('name', name).attr('id', 
id);
+    var addButton = $('<input/>').attr('type', 'button').attr('value', 'Add');
+    var table = $('<table/>');
+    var count = 0;
+
+    // Initialize the table
+    var listItems = pref.value.split("|");
+    for (var k=0; k<listItems.length; k++) {
+      var li_id = elPrefix + '_listvalue_' + count++;
+      var tr = this.createListItem(name, li_id , listItems[k]);
+      $(table).append(tr);
+    }
+
+    var gadgetId = this.serviceName;
+    addButton.click(function(){
+      var value = document.getElementById(id).value;
+      var li_id = elPrefix + '_listvalue_' + count++;
+      var gadget = shindig.container.getGadgetFromId(gadgetId);
+      var tr = gadget.createListItem(name, li_id , value);
+      $(table).append(tr);
+    });
+    $(rtd).append(field);
+    $(rtd).append(addButton);
+    $(rtd).append(table);
+  }
+
+  // UserPref of type STRING
+  else {
+    field = $('<input/>').attr('type', 'edit').attr('name', name).attr('id', 
id).attr('value', pref.value);
+    $(rtd).append(field);
+  }
 };
 
+shindig.BaseIfrGadget.prototype.createListItem = function(name, id, value) {
+  var tr = $('<tr/>').attr('id', id);
+
+  var ltd = $('<td/>').attr('name', name + '_listvalue').attr('value', 
value).text(value);
+  var rtd = $('<td/>');
+
+  var removeButton = $('<input/>').attr('type', 'button').attr('value', 
'Remove');
+  removeButton.click(function() {
+    $(tr).remove();
+  });
+  $(rtd).append(removeButton);
+
+  $(tr).append(ltd);
+  $(tr).append(rtd);
+  return tr;
+}
+
 shindig.BaseIfrGadget.prototype.showUserPrefsDialog = function(opt_show) {
   var userPrefsDialog = document.getElementById(this.getUserPrefsDialogId());
   userPrefsDialog.style.display = (opt_show || opt_show === undefined)
@@ -652,13 +705,33 @@
       '_numfields').value;
   for (var i = 0; i < numFields; i++) {
     var input = document.getElementById('m_' + this.id + '_' + i);
-    var userPrefNamePrefix = 'm_' + this.id + '_';
-    var userPrefName = input.name.substring(userPrefNamePrefix.length);
-    var userPrefValue = input.value;
-    if (input.type == 'checkbox') {
-      this.userPrefs[userPrefName].value = input.checked ? "1" : "0";
-    } else {
-      this.userPrefs[userPrefName].value = userPrefValue;
+    // NB: for a UserPref of type HIDDEN, no input field exists
+    if (input) {
+      var userPrefNamePrefix = 'm_' + this.id + '_';
+      var userPrefName = input.name.substring(userPrefNamePrefix.length);
+      var userPrefValue = input.value;
+      if (input.type == 'checkbox') {
+        this.userPrefs[userPrefName].value = input.checked ? "1" : "0";
+      } else if (input.type == 'text') {
+        // This might be a list
+        var listFields = input.name + "_listvalue";
+        var listEntries = document.getElementsByName(listFields);
+        if (listEntries.length > 0) {
+          // This is a list! Note that items in a list are converted to
+          // a single string using a pipe-delimiter (this is according to spec)
+          userPrefValue = "";
+          for (var j=0; j<listEntries.length; j++) {
+            if (j>0) userPrefValue += "|";
+            userPrefValue += listEntries[j].value;
+          }
+          this.userPrefs[userPrefName].value = userPrefValue;
+        }
+        else {
+          this.userPrefs[userPrefName].value = userPrefValue;
+        }
+      } else {
+        this.userPrefs[userPrefName].value = userPrefValue;
+      }
     }
   }
 
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to