Ver código fonte

2010-02-02 Marek Habersack <[email protected]>

	* 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  <[email protected]>

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

svn path=/trunk/mcs/; revision=150684
Marek Habersack 16 anos atrás
pai
commit
7bf126dbdf

+ 13 - 0
mcs/class/System.Web.Extensions/System.Web.UI/ChangeLog

@@ -1,3 +1,16 @@
+2010-02-02  Marek Habersack  <[email protected]>
+
+	* 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  <[email protected]>
 
 	* UpdatePanel.cs: RenderChildren stores the alternative writer in

+ 8 - 2
mcs/class/System.Web.Extensions/System.Web.UI/ScriptBehaviorDescriptor.cs

@@ -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 ();
 		}
 	}

+ 42 - 23
mcs/class/System.Web.Extensions/System.Web.UI/ScriptComponentDescriptor.cs

@@ -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));
 		}
 	}
 }

+ 3 - 1
mcs/class/System.Web.Extensions/System.Web.UI/ScriptManager.cs

@@ -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 ("});");
 			}

+ 6 - 0
mcs/class/System.Web.Extensions/Test/System.Web.UI/ChangeLog

@@ -0,0 +1,6 @@
+2010-02-02  Marek Habersack  <[email protected]>
+
+	* ScriptBehaviorDescriptorTest.cs,
+	ScriptComponentDescriptorTest.cs: added tests for rendering of the
+	Name and ID properties.
+

+ 11 - 2
mcs/class/System.Web.Extensions/Test/System.Web.UI/ScriptBehaviorDescriptorTest.cs

@@ -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]

+ 7 - 2
mcs/class/System.Web.Extensions/Test/System.Web.UI/ScriptComponentDescriptorTest.cs

@@ -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]