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 Dictionary<string, string> _properties;
43 Dictionary<string, string> _events;
44 Dictionary<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 Dictionary<string, string> dictionary, string key, string value) {
139 if (dictionary == null)
140 dictionary = new Dictionary<string, string> ();
141 if (!dictionary.ContainsKey (key))
142 dictionary.Add (key, value);
145 protected internal override string GetScript () {
146 if (String.IsNullOrEmpty (FormID)) {
147 if (String.IsNullOrEmpty (ElementIDInternal))
148 return String.Format ("$create({0}, {1}, {2}, {3});", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
150 return String.Format ("$create({0}, {1}, {2}, {3}, $get(\"{4}\"));", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
153 if (String.IsNullOrEmpty (ElementIDInternal))
154 return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4});", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
156 return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4}, $get(\"{5}\"));", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
160 internal static string SerializeDictionary (Dictionary<string, string> dictionary) {
161 if (dictionary == null || dictionary.Count == 0)
163 StringBuilder sb = new StringBuilder ("{");
164 foreach (string key in dictionary.Keys) {
165 sb.AppendFormat ("{0}:{1},", key, dictionary [key]);
169 return sb.ToString ();
172 string GetSerializedProperties () {
173 return SerializeDictionary (_properties);
176 string GetSerializedEvents () {
177 return SerializeDictionary (_events);
180 string GetSerializedReferences () {
181 return SerializeDictionary (_references);