ScriptComponentDescriptor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // ScriptComponentDescriptor.cs
  3. //
  4. // Author:
  5. // Igor Zelmanovich <[email protected]>
  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. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.Web.Script.Serialization;
  33. namespace System.Web.UI
  34. {
  35. public class ScriptComponentDescriptor : ScriptDescriptor
  36. {
  37. string _elementID;
  38. string _type;
  39. string _id;
  40. IDictionary<string, string> _properties;
  41. IDictionary<string, string> _events;
  42. IDictionary<string, string> _references;
  43. public ScriptComponentDescriptor (string type) {
  44. if (String.IsNullOrEmpty (type))
  45. throw new ArgumentException ("Value cannot be null or empty.", "type");
  46. _type = type;
  47. }
  48. public virtual string ClientID {
  49. get {
  50. return ID;
  51. }
  52. }
  53. internal string ElementIDInternal {
  54. get {
  55. return _elementID;
  56. }
  57. set {
  58. _elementID = value;
  59. }
  60. }
  61. public virtual string ID {
  62. get {
  63. if (_id == null)
  64. return String.Empty;
  65. return _id;
  66. }
  67. set {
  68. _id = value;
  69. }
  70. }
  71. public string Type {
  72. get {
  73. return _type;
  74. }
  75. set {
  76. if (String.IsNullOrEmpty (value))
  77. throw new ArgumentException ("Value cannot be null or empty.", "value");
  78. _type = value;
  79. }
  80. }
  81. public void AddComponentProperty (string name, string componentID) {
  82. if (name == null)
  83. throw new ArgumentException ("Value cannot be null or empty.", "name");
  84. if (componentID == null)
  85. throw new ArgumentException ("Value cannot be null or empty.", "componentID");
  86. AddEntry (ref _references, String.Format ("\"{0}\"", name), String.Format ("\"{0}\"", componentID));
  87. }
  88. public void AddElementProperty (string name, string elementID) {
  89. if (name == null)
  90. throw new ArgumentException ("Value cannot be null or empty.", "name");
  91. if (elementID == null)
  92. throw new ArgumentException ("Value cannot be null or empty.", "elementID");
  93. AddEntry (ref _properties, String.Format ("\"{0}\"", name), String.Format ("$get(\"{0}\")", elementID));
  94. }
  95. public void AddEvent (string name, string handler) {
  96. if (name == null)
  97. throw new ArgumentException ("Value cannot be null or empty.", "name");
  98. if (handler == null)
  99. throw new ArgumentException ("Value cannot be null or empty.", "handler");
  100. AddEntry (ref _events, String.Format ("\"{0}\"", name), handler);
  101. }
  102. public void AddProperty (string name, object value) {
  103. if (name == null)
  104. throw new ArgumentException ("Value cannot be null or empty.", "name");
  105. string valueString;
  106. if (value == null)
  107. valueString = "null";
  108. else
  109. valueString = JavaScriptSerializer.DefaultSerializer.Serialize (value);
  110. AddEntry (ref _properties, String.Format ("\"{0}\"", name), valueString);
  111. }
  112. public void AddScriptProperty (string name, string script) {
  113. if (name == null)
  114. throw new ArgumentException ("Value cannot be null or empty.", "name");
  115. if (script == null)
  116. throw new ArgumentException ("Value cannot be null or empty.", "script");
  117. AddEntry (ref _properties, String.Format ("\"{0}\"", name), script);
  118. }
  119. void AddEntry (ref IDictionary<string, string> dictionary, string key, string value) {
  120. if (dictionary == null)
  121. dictionary = new SortedDictionary<string, string> ();
  122. if (!dictionary.ContainsKey (key))
  123. dictionary.Add (key, value);
  124. else
  125. dictionary [key] = value;
  126. }
  127. protected internal override string GetScript ()
  128. {
  129. string id = ID;
  130. if (id != String.Empty)
  131. AddProperty ("id", id);
  132. bool haveFormID = String.IsNullOrEmpty (FormID) == false;
  133. bool haveElementID = String.IsNullOrEmpty (ElementIDInternal) == false;
  134. var sb = new StringBuilder ("$create(");
  135. if (haveFormID)
  136. sb.Append ("$get(\"");
  137. sb.Append (Type);
  138. if (haveFormID)
  139. sb.Append ("\")");
  140. WriteSerializedProperties (sb);
  141. WriteSerializedEvents (sb);
  142. WriteSerializedReferences (sb);
  143. if (haveElementID)
  144. sb.AppendFormat (", $get(\"{0}\")", ElementIDInternal);
  145. sb.Append (");");
  146. return sb.ToString ();
  147. }
  148. internal static string SerializeDictionary (IDictionary<string, string> dictionary)
  149. {
  150. if (dictionary == null || dictionary.Count == 0)
  151. return "null";
  152. StringBuilder sb = new StringBuilder ("{");
  153. foreach (string key in dictionary.Keys)
  154. sb.AppendFormat ("{0}:{1},", key, dictionary [key]);
  155. sb.Length--;
  156. sb.Append ("}");
  157. return sb.ToString ();
  158. }
  159. void WriteSerializedProperties (StringBuilder sb)
  160. {
  161. sb.Append (", ");
  162. sb.Append (SerializeDictionary (_properties));
  163. }
  164. void WriteSerializedEvents (StringBuilder sb)
  165. {
  166. sb.Append (", ");
  167. sb.Append (SerializeDictionary (_events));
  168. }
  169. void WriteSerializedReferences (StringBuilder sb)
  170. {
  171. sb.Append (", ");
  172. sb.Append (SerializeDictionary (_references));
  173. }
  174. }
  175. }