Merge pull request #1504 from madrang/SafeHandle.SetInvalidRelease
[mono.git] / mcs / class / System.Web.Extensions / System.Web.UI / ScriptComponentDescriptor.cs
1 //
2 // ScriptComponentDescriptor.cs
3 //
4 // Author:
5 //   Igor Zelmanovich <igorz@mainsoft.com>
6 //
7 // (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
8 //
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.Text;
33 using System.Web.Script.Serialization;
34
35 namespace System.Web.UI
36 {
37         public class ScriptComponentDescriptor : ScriptDescriptor
38         {
39                 string _elementID;
40                 string _type;
41                 string _id;
42                 IDictionary<string, string> _properties;
43                 IDictionary<string, string> _events;
44                 IDictionary<string, string> _references;
45
46                 public ScriptComponentDescriptor (string type) {
47                         if (String.IsNullOrEmpty (type))
48                                 throw new ArgumentException ("Value cannot be null or empty.", "type");
49                         _type = type;
50                 }
51
52                 public virtual string ClientID {
53                         get {
54                                 return ID;
55                         }
56                 }
57
58                 internal string ElementIDInternal {
59                         get {
60                                 return _elementID;
61                         }
62                         set {
63                                 _elementID = value;
64                         }
65                 }
66
67                 public virtual string ID {
68                         get {
69                                 if (_id == null)
70                                         return String.Empty;
71                                 return _id;
72                         }
73                         set {
74                                 _id = value;
75                         }
76                 }
77
78                 public string Type {
79                         get {
80                                 return _type;
81                         }
82                         set {
83                                 if (String.IsNullOrEmpty (value))
84                                         throw new ArgumentException ("Value cannot be null or empty.", "value");
85                                 _type = value;
86                         }
87                 }
88
89                 public void AddComponentProperty (string name, string componentID) {
90                         if (name == null)
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");
94
95                         AddEntry (ref _references, String.Format ("\"{0}\"", name), String.Format ("\"{0}\"", componentID));
96                 }
97
98                 public void AddElementProperty (string name, string elementID) {
99                         if (name == null)
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");
103
104                         AddEntry (ref _properties, String.Format ("\"{0}\"", name), String.Format ("$get(\"{0}\")", elementID));
105                 }
106
107                 public void AddEvent (string name, string handler) {
108                         if (name == null)
109                                 throw new ArgumentException ("Value cannot be null or empty.", "name");
110                         if (handler == null)
111                                 throw new ArgumentException ("Value cannot be null or empty.", "handler");
112
113                         AddEntry (ref _events, String.Format ("\"{0}\"", name), handler);
114                 }
115
116                 public void AddProperty (string name, object value) {
117                         if (name == null)
118                                 throw new ArgumentException ("Value cannot be null or empty.", "name");
119
120                         string valueString;
121                         if (value == null)
122                                 valueString = "null";
123                         else
124                                 valueString = JavaScriptSerializer.DefaultSerializer.Serialize (value);
125
126                         AddEntry (ref _properties, String.Format ("\"{0}\"", name), valueString);
127                 }
128
129                 public void AddScriptProperty (string name, string script) {
130                         if (name == null)
131                                 throw new ArgumentException ("Value cannot be null or empty.", "name");
132                         if (script == null)
133                                 throw new ArgumentException ("Value cannot be null or empty.", "script");
134
135                         AddEntry (ref _properties, String.Format ("\"{0}\"", name), script);
136                 }
137                 
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);
143                         else
144                                 dictionary [key] = value;
145                 }
146
147                 protected internal override string GetScript ()
148                 {
149                         string id = ID;
150                         if (id != String.Empty)
151                                 AddProperty ("id", id);
152                         
153                         bool haveFormID = String.IsNullOrEmpty (FormID) == false;
154                         bool haveElementID = String.IsNullOrEmpty (ElementIDInternal) == false;
155                         var sb = new StringBuilder ("$create(");
156
157                         if (haveFormID)
158                                 sb.Append ("$get(\"");
159                         sb.Append (Type);
160                         if (haveFormID)
161                                 sb.Append ("\")");
162
163                         WriteSerializedProperties (sb);
164                         WriteSerializedEvents (sb);
165                         WriteSerializedReferences (sb);
166
167                         if (haveElementID)
168                                 sb.AppendFormat (", $get(\"{0}\")", ElementIDInternal);
169
170                         sb.Append (");");
171
172                         return sb.ToString ();
173                 }
174
175                 internal static string SerializeDictionary (IDictionary<string, string> dictionary)
176                 {
177                         if (dictionary == null || dictionary.Count == 0)
178                                 return "null";
179                         StringBuilder sb = new StringBuilder ("{");
180                         foreach (string key in dictionary.Keys)
181                                 sb.AppendFormat ("{0}:{1},", key, dictionary [key]);
182                         sb.Length--;
183                         sb.Append ("}");
184                         return sb.ToString ();
185                 }
186
187                 void WriteSerializedProperties (StringBuilder sb)
188                 {
189                         sb.Append (", ");
190                         sb.Append (SerializeDictionary (_properties));
191                 }
192
193                 void WriteSerializedEvents (StringBuilder sb)
194                 {
195                         sb.Append (", ");
196                         sb.Append (SerializeDictionary (_events));
197                 }
198
199                 void WriteSerializedReferences (StringBuilder sb)
200                 {
201                         sb.Append (", ");
202                         sb.Append (SerializeDictionary (_references));
203                 }
204         }
205 }