2007-12-18 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[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                 Dictionary<string, string> _properties;
43                 Dictionary<string, string> _events;
44                 Dictionary<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 Dictionary<string, string> dictionary, string key, string value) {
139                         if (dictionary == null)
140                                 dictionary = new Dictionary<string, string> ();
141                         dictionary.Add (key, value);
142                 }
143
144                 protected internal override string GetScript () {
145                         if (String.IsNullOrEmpty (FormID)) {
146                                 if (String.IsNullOrEmpty (ElementIDInternal))
147                                         return String.Format ("$create({0}, {1}, {2}, {3});", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
148                                 else
149                                         return String.Format ("$create({0}, {1}, {2}, {3}, $get(\"{4}\"));", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
150                         }
151                         else {
152                                 if (String.IsNullOrEmpty (ElementIDInternal))
153                                         return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4});", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
154                                 else
155                                         return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4}, $get(\"{5}\"));", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
156                         }
157                 }
158
159                 internal static string SerializeDictionary (Dictionary<string, string> dictionary) {
160                         if (dictionary == null || dictionary.Count == 0)
161                                 return "null";
162                         StringBuilder sb = new StringBuilder ("{");
163                         foreach (string key in dictionary.Keys) {
164                                 sb.AppendFormat ("{0}:{1},", key, dictionary [key]);
165                         }
166                         sb.Length--;
167                         sb.Append ("}");
168                         return sb.ToString ();
169                 }
170
171                 string GetSerializedProperties () {
172                         return SerializeDictionary (_properties);
173                 }
174
175                 string GetSerializedEvents () {
176                         return SerializeDictionary (_events);
177                 }
178
179                 string GetSerializedReferences () {
180                         return SerializeDictionary (_references);
181                 }
182         }
183 }