2010-02-02 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Mon, 1 Feb 2010 23:47:21 +0000 (23:47 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Mon, 1 Feb 2010 23:47:21 +0000 (23:47 -0000)
* ScriptManager.cs: adjusted script rendering to match .NET
formatting.

* ScriptComponentDescriptor.cs: GetScript adds ID value (if
present) to the set of properties.
GetScript rewritten to use StringBuilder.

* ScriptBehaviorDescriptor.cs: GetScript adds Name, if present and
set by the user, to the descriptor's set of properties. The name
must be rendered to the client.

2010-02-02  Marek Habersack  <mhabersack@novell.com>

* ScriptBehaviorDescriptorTest.cs,
ScriptComponentDescriptorTest.cs: added tests for rendering of the
Name and ID properties.

svn path=/trunk/mcs/; revision=150684

mcs/class/System.Web.Extensions/System.Web.UI/ChangeLog
mcs/class/System.Web.Extensions/System.Web.UI/ScriptBehaviorDescriptor.cs
mcs/class/System.Web.Extensions/System.Web.UI/ScriptComponentDescriptor.cs
mcs/class/System.Web.Extensions/System.Web.UI/ScriptManager.cs
mcs/class/System.Web.Extensions/Test/System.Web.UI/ChangeLog [new file with mode: 0644]
mcs/class/System.Web.Extensions/Test/System.Web.UI/ScriptBehaviorDescriptorTest.cs
mcs/class/System.Web.Extensions/Test/System.Web.UI/ScriptComponentDescriptorTest.cs

index 1dfa98a3dcf87cfe16bec440c0e1a9413e364c26..2e5d0d861c266f33a26756f00ac130a37bfffab8 100644 (file)
@@ -1,3 +1,16 @@
+2010-02-02  Marek Habersack  <mhabersack@novell.com>
+
+       * ScriptManager.cs: adjusted script rendering to match .NET
+       formatting.
+
+       * ScriptComponentDescriptor.cs: GetScript adds ID value (if
+       present) to the set of properties.
+       GetScript rewritten to use StringBuilder.
+
+       * ScriptBehaviorDescriptor.cs: GetScript adds Name, if present and
+       set by the user, to the descriptor's set of properties. The name
+       must be rendered to the client.
+
 2009-09-28  Marek Habersack  <mhabersack@novell.com>
 
        * UpdatePanel.cs: RenderChildren stores the alternative writer in
index 31506523e22d6fcacd262a113fa9375bacd0e269..1ebe3da6e7225b794c38cadeb8ff33f854934343 100644 (file)
@@ -36,7 +36,8 @@ namespace System.Web.UI
        public class ScriptBehaviorDescriptor : ScriptComponentDescriptor
        {
                string _name;
-
+               bool _nameSet;
+               
                public ScriptBehaviorDescriptor (string type, string elementID)
                        : base (type) {
                        if (String.IsNullOrEmpty (elementID))
@@ -67,6 +68,7 @@ namespace System.Web.UI
                        }
                        set {
                                _name = value;
+                               _nameSet = true;
                        }
                }
 
@@ -78,7 +80,11 @@ namespace System.Web.UI
                        return Type;
                }
 
-               protected internal override string GetScript () {
+               protected internal override string GetScript ()
+               {
+                       if (_nameSet && !String.IsNullOrEmpty (_name))
+                               AddProperty ("name", _name);
+                       
                        return base.GetScript ();
                }
        }
index c6939d9af5dad6fb43f1e347f516db590cca76e7..c46c1aaab02cc4c0805cd510545d9039dd482362 100644 (file)
@@ -134,7 +134,7 @@ namespace System.Web.UI
 
                        AddEntry (ref _properties, String.Format ("\"{0}\"", name), script);
                }
-
+               
                void AddEntry (ref IDictionary<string, string> dictionary, string key, string value) {
                        if (dictionary == null)
                                dictionary = new SortedDictionary<string, string> ();
@@ -144,43 +144,62 @@ namespace System.Web.UI
                                dictionary [key] = value;
                }
 
-               protected internal override string GetScript () {
-                       if (String.IsNullOrEmpty (FormID)) {
-                               if (String.IsNullOrEmpty (ElementIDInternal))
-                                       return String.Format ("$create({0}, {1}, {2}, {3});", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
-                               else
-                                       return String.Format ("$create({0}, {1}, {2}, {3}, $get(\"{4}\"));", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
-                       }
-                       else {
-                               if (String.IsNullOrEmpty (ElementIDInternal))
-                                       return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4});", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
-                               else
-                                       return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4}, $get(\"{5}\"));", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
-                       }
+               protected internal override string GetScript ()
+               {
+                       string id = ID;
+                       if (id != String.Empty)
+                               AddProperty ("id", id);
+                       
+                       bool haveFormID = String.IsNullOrEmpty (FormID) == false;
+                       bool haveElementID = String.IsNullOrEmpty (ElementIDInternal) == false;
+                       var sb = new StringBuilder ("$create(");
+
+                       if (haveFormID)
+                               sb.Append ("$get(\"");
+                       sb.Append (Type);
+                       if (haveFormID)
+                               sb.Append ("\")");
+
+                       WriteSerializedProperties (sb);
+                       WriteSerializedEvents (sb);
+                       WriteSerializedReferences (sb);
+
+                       if (haveElementID)
+                               sb.AppendFormat (", $get(\"{0}\")", ElementIDInternal);
+
+                       sb.Append (");");
+
+                       return sb.ToString ();
                }
 
-               internal static string SerializeDictionary (IDictionary<string, string> dictionary) {
+               internal static string SerializeDictionary (IDictionary<string, string> dictionary)
+               {
                        if (dictionary == null || dictionary.Count == 0)
                                return "null";
                        StringBuilder sb = new StringBuilder ("{");
-                       foreach (string key in dictionary.Keys) {
+                       foreach (string key in dictionary.Keys)
                                sb.AppendFormat ("{0}:{1},", key, dictionary [key]);
-                       }
                        sb.Length--;
                        sb.Append ("}");
                        return sb.ToString ();
                }
 
-               string GetSerializedProperties () {
-                       return SerializeDictionary (_properties);
+               void WriteSerializedProperties (StringBuilder sb)
+               {
+                       sb.Append (", ");
+                       sb.Append (SerializeDictionary (_properties));
                }
 
-               string GetSerializedEvents () {
-                       return SerializeDictionary (_events);
+               void WriteSerializedEvents (StringBuilder sb)
+               {
+                       sb.Append (", ");
+                       sb.Append (SerializeDictionary (_events));
                }
 
-               string GetSerializedReferences () {
-                       return SerializeDictionary (_references);
+               void WriteSerializedReferences (StringBuilder sb)
+               {
+                       sb.Append (", ");
+                       sb.Append (SerializeDictionary (_references));
                }
        }
 }
\ No newline at end of file
index 5db5f9a5368ca3e84ed41a12509788eaeb8ad10b..a157d455cac9a039a2a5ecc9b48262fa625a0299 100644 (file)
@@ -987,7 +987,8 @@ namespace System.Web.UI
                        RegisterScriptDescriptors ((Control) scriptControl, scriptControl.GetScriptDescriptors ());
                }
 
-               void RegisterScriptDescriptors (Control control, IEnumerable<ScriptDescriptor> scriptDescriptors) {
+               void RegisterScriptDescriptors (Control control, IEnumerable<ScriptDescriptor> scriptDescriptors)
+               {
                        if (scriptDescriptors == null)
                                return;
 
@@ -999,6 +1000,7 @@ namespace System.Web.UI
                                }
                                else
                                        sb.AppendLine ("Sys.Application.add_init(function() {");
+                               sb.Append ("\t");
                                sb.AppendLine (scriptDescriptor.GetScript ());
                                sb.AppendLine ("});");
                        }
diff --git a/mcs/class/System.Web.Extensions/Test/System.Web.UI/ChangeLog b/mcs/class/System.Web.Extensions/Test/System.Web.UI/ChangeLog
new file mode 100644 (file)
index 0000000..965e2fd
--- /dev/null
@@ -0,0 +1,6 @@
+2010-02-02  Marek Habersack  <mhabersack@novell.com>
+
+       * ScriptBehaviorDescriptorTest.cs,
+       ScriptComponentDescriptorTest.cs: added tests for rendering of the
+       Name and ID properties.
+
index f7e6ab90d5f8c8f7762a207391851f228cc25af4..69d065ec5fda66585783c29dd59a6a4a876a46a0 100644 (file)
@@ -48,7 +48,8 @@ namespace Tests.System.Web.UI
                }
 
                [Test]
-               public void ScriptBehaviorDescriptor_Defaults () {
+               public void ScriptBehaviorDescriptor_Defaults ()
+               {
                        PokerScriptBehaviorDescriptor scd = new PokerScriptBehaviorDescriptor ("My.Type", "Element1");
 
                        Assert.AreEqual ("My.Type", scd.Type, "Type");
@@ -58,7 +59,15 @@ namespace Tests.System.Web.UI
                        Assert.AreEqual ("Element1", scd.ElementID, "ElementID");
 
                        string script = scd.DoGetScript ();
-                       Assert.AreEqual ("$create(My.Type, null, null, null, $get(\"Element1\"));", script);
+                       Assert.AreEqual ("$create(My.Type, null, null, null, $get(\"Element1\"));", script, "#A1");
+
+                       scd.ID = "SomeID";
+                       script = scd.DoGetScript ();
+                       Assert.AreEqual ("$create(My.Type, {\"id\":\"SomeID\"}, null, null, $get(\"Element1\"));", script, "#A2");
+
+                       scd.Name = "SomeName";
+                       script = scd.DoGetScript ();
+                       Assert.AreEqual ("$create(My.Type, {\"id\":\"SomeID\",\"name\":\"SomeName\"}, null, null, $get(\"Element1\"));", script, "#A3");
                }
 
                [Test]
index cd3161285e7a3d4d4b168f30d068f092521e5d54..cbf47d716e20fb9adfb33878fb56ca708383f6a0 100644 (file)
@@ -48,7 +48,8 @@ namespace Tests.System.Web.UI
                }
 
                [Test]
-               public void ScriptComponentDescriptor_Defaults () {
+               public void ScriptComponentDescriptor_Defaults ()
+               {
                        PokerScriptComponentDescriptor scd = new PokerScriptComponentDescriptor ("My.Type");
 
                        Assert.AreEqual ("My.Type", scd.Type, "Type");
@@ -56,7 +57,11 @@ namespace Tests.System.Web.UI
                        Assert.AreEqual (String.Empty, scd.ClientID, "ClientID");
 
                        string script = scd.DoGetScript ();
-                       Assert.AreEqual ("$create(My.Type, null, null, null);", script);
+                       Assert.AreEqual ("$create(My.Type, null, null, null);", script, "#A1");
+
+                       scd.ID = "SomeID";
+                       script = scd.DoGetScript ();
+                       Assert.AreEqual ("$create(My.Type, {\"id\":\"SomeID\"}, null, null);", script, "#A2");
                }
 
                [Test]