2 // ScriptComponentDescriptor.cs
5 // Igor Zelmanovich <igorz@mainsoft.com>
7 // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
33 using System.Web.Script.Serialization;
35 namespace System.Web.UI
37 public class ScriptComponentDescriptor : ScriptDescriptor
42 IDictionary<string, string> _properties;
43 IDictionary<string, string> _events;
44 IDictionary<string, string> _references;
46 public ScriptComponentDescriptor (string type) {
47 if (String.IsNullOrEmpty (type))
48 throw new ArgumentException ("Value cannot be null or empty.", "type");
52 public virtual string ClientID {
58 internal string ElementIDInternal {
67 public virtual string ID {
83 if (String.IsNullOrEmpty (value))
84 throw new ArgumentException ("Value cannot be null or empty.", "value");
89 public void AddComponentProperty (string name, string componentID) {
91 throw new ArgumentException ("Value cannot be null or empty.", "name");
92 if (componentID == null)
93 throw new ArgumentException ("Value cannot be null or empty.", "componentID");
95 AddEntry (ref _references, String.Format ("\"{0}\"", name), String.Format ("\"{0}\"", componentID));
98 public void AddElementProperty (string name, string elementID) {
100 throw new ArgumentException ("Value cannot be null or empty.", "name");
101 if (elementID == null)
102 throw new ArgumentException ("Value cannot be null or empty.", "elementID");
104 AddEntry (ref _properties, String.Format ("\"{0}\"", name), String.Format ("$get(\"{0}\")", elementID));
107 public void AddEvent (string name, string handler) {
109 throw new ArgumentException ("Value cannot be null or empty.", "name");
111 throw new ArgumentException ("Value cannot be null or empty.", "handler");
113 AddEntry (ref _events, String.Format ("\"{0}\"", name), handler);
116 public void AddProperty (string name, object value) {
118 throw new ArgumentException ("Value cannot be null or empty.", "name");
122 valueString = "null";
124 valueString = JavaScriptSerializer.DefaultSerializer.Serialize (value);
126 AddEntry (ref _properties, String.Format ("\"{0}\"", name), valueString);
129 public void AddScriptProperty (string name, string script) {
131 throw new ArgumentException ("Value cannot be null or empty.", "name");
133 throw new ArgumentException ("Value cannot be null or empty.", "script");
135 AddEntry (ref _properties, String.Format ("\"{0}\"", name), script);
138 void AddEntry (ref IDictionary<string, string> dictionary, string key, string value) {
139 if (dictionary == null)
140 dictionary = new SortedDictionary<string, string> ();
141 if (!dictionary.ContainsKey (key))
142 dictionary.Add (key, value);
144 dictionary [key] = value;
147 protected internal override string GetScript ()
150 if (id != String.Empty)
151 AddProperty ("id", id);
153 bool haveFormID = String.IsNullOrEmpty (FormID) == false;
154 bool haveElementID = String.IsNullOrEmpty (ElementIDInternal) == false;
155 var sb = new StringBuilder ("$create(");
158 sb.Append ("$get(\"");
163 WriteSerializedProperties (sb);
164 WriteSerializedEvents (sb);
165 WriteSerializedReferences (sb);
168 sb.AppendFormat (", $get(\"{0}\")", ElementIDInternal);
172 return sb.ToString ();
175 internal static string SerializeDictionary (IDictionary<string, string> dictionary)
177 if (dictionary == null || dictionary.Count == 0)
179 StringBuilder sb = new StringBuilder ("{");
180 foreach (string key in dictionary.Keys)
181 sb.AppendFormat ("{0}:{1},", key, dictionary [key]);
184 return sb.ToString ();
187 void WriteSerializedProperties (StringBuilder sb)
190 sb.Append (SerializeDictionary (_properties));
193 void WriteSerializedEvents (StringBuilder sb)
196 sb.Append (SerializeDictionary (_events));
199 void WriteSerializedReferences (StringBuilder sb)
202 sb.Append (SerializeDictionary (_references));