DymWebFormControlMappnig.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. using DymWebForm.Common;
  6. using DymWebForm.Common.BasicDataType;
  7. using DymWebForm.Common.Linq;
  8. using DymWebForm.Controls;
  9. namespace DymWebForm
  10. {
  11. public class DymWebFormControlMappnig
  12. {
  13. private readonly Type _typControl;
  14. private readonly Action<object> _delInitControl;
  15. public Action<object, object> ControlSetValue { get; set; }
  16. public Func<object, object> ControlGetValue { get; private set; }
  17. public DymWebFormValidation Validation { get; set; }
  18. //
  19. internal WebControl CreateControl()
  20. {
  21. var c = (WebControl)Activator.CreateInstance(_typControl);
  22. if (_delInitControl != null)
  23. _delInitControl(c);
  24. return c;
  25. }
  26. //
  27. private DymWebFormControlMappnig(Type controlType, Action<object> controlInit, Action<object, object> controlSetValue, Func<object, object> controlGetValue, DymWebFormValidation validation)
  28. {
  29. this._typControl = controlType;
  30. this._delInitControl = controlInit;
  31. this.ControlSetValue = controlSetValue;
  32. this.ControlGetValue = controlGetValue;
  33. this.Validation = validation;
  34. }
  35. //
  36. private static Dictionary<Type, Dictionary<Type, DymWebFormControlMappnig>> ms_dicMapping;
  37. public static void SetControlMapping<TDataType, TControl, TValidation>(
  38. Action<TControl, TValidation> init
  39. , Action<TControl, TDataType> setValueToControl
  40. , Func<TControl, TDataType> getValueFromControl
  41. , TValidation validation) where TControl : WebControl, new()
  42. where TValidation : DymWebFormValidation
  43. {
  44. if (false == ms_dicMapping.ContainsKey(typeof(TDataType)))
  45. {
  46. ms_dicMapping[typeof(TDataType)] = new Dictionary<Type, DymWebFormControlMappnig>();
  47. }
  48. ms_dicMapping[typeof(TDataType)][typeof(TValidation)] = new DymWebFormControlMappnig(
  49. typeof(TControl)
  50. , init == null ? null : new Action<object>(c => init((TControl)c, validation))
  51. , (c, d) => setValueToControl((TControl)c, (TDataType)d)
  52. , c => getValueFromControl((TControl)c)
  53. , validation
  54. );
  55. }
  56. //internal for cctor
  57. private static void Bdts<TD>(ITextControl c, TD d)
  58. {
  59. if (d != null)
  60. c.Text = d.ToString();
  61. }
  62. private static T Bdcv<T>(TextBox c) where T : struct
  63. {
  64. return TypeParser<T>.Convert(c.Text);
  65. }
  66. private static T? Bdcvn<T>(TextBox c) where T : struct
  67. {
  68. return TypeParser<T>.ConvertNullable(c.Text);
  69. }
  70. //init bulid-in
  71. static DymWebFormControlMappnig()
  72. {
  73. ms_dicMapping = new Dictionary<Type, Dictionary<Type, DymWebFormControlMappnig>>();
  74. SetControlMapping<string, TextBox, DymWebFormDefaultValidation>(null, Bdts, c => c.Text, null);
  75. SetControlMapping<string, TextBox, DymWebFormEmailValidation>(null, Bdts, c => c.Text, new DymWebFormEmailValidation());
  76. SetControlMapping<string, TextBox, DynWebFormPasswordValidation>((t, v) => t.TextMode = TextBoxMode.Password, Bdts, c => c.Text, new DynWebFormPasswordValidation());
  77. SetControlMapping<string, TextArea, DymWebFormTextAreaValidation>((t, v) =>
  78. {
  79. t.Columns = v.Cols;
  80. t.Rows = v.Rows;
  81. }, Bdts, c => c.Text, new DymWebFormTextAreaValidation());
  82. SetControlMapping<int, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<int>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  83. SetControlMapping<uint, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<uint>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  84. SetControlMapping<int?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<int>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  85. SetControlMapping<uint?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<uint>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  86. SetControlMapping<long, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<long>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  87. SetControlMapping<float, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<float>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  88. SetControlMapping<double, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<double>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  89. SetControlMapping<double?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<double>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  90. SetControlMapping<decimal, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<decimal>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  91. SetControlMapping<DateTime, DateTimeTextBox, DwfDateTimeValidation>(null, Bdts, Bdcv<DateTime>, new DwfDateTimeValidation());
  92. SetControlMapping<DateTime?, DateTimeTextBox, DwfDateTimeValidation>(null, Bdts, Bdcvn<DateTime>, new DwfDateTimeValidation());
  93. SetControlMapping<bool, CheckBox, DymWebFormDefaultValidation>(null, (c, d) => c.Checked = d, c => c.Checked, null);
  94. SetControlMapping<Guid, TextBox, DwfGuidValidation>(null, Bdts, c => new Guid(c.Text), new DwfGuidValidation());
  95. SetControlMapping<Guid?, TextBox, DwfGuidValidation>(null, Bdts, c => string.IsNullOrEmpty(c.Text) ? (Guid?)null : new Guid(c.Text), new DwfGuidValidation());
  96. }
  97. //static
  98. internal static DymWebFormControlMappnig GetInst(Type dataType, Type validationType)
  99. {
  100. if (false == ms_dicMapping.ContainsKey(dataType))
  101. throw new ArgumentException("Not registed data type:" + dataType);
  102. if (validationType == null)
  103. return ms_dicMapping[dataType].FirstValue();
  104. if (false == ms_dicMapping[dataType].ContainsKey(validationType))
  105. throw new ArgumentException("Not registed validation type of the data type:" + validationType);
  106. return ms_dicMapping[dataType][validationType];
  107. }
  108. }
  109. public abstract class DymWebFormValidation
  110. {
  111. private enum ControlType
  112. {
  113. None,
  114. Basic,
  115. Regexp,
  116. Custom,
  117. }
  118. private ControlType m_ControlType;
  119. //basic
  120. private ValidationDataType m_vdtCompareValidatorDataType;
  121. //cuctom
  122. private ServerValidateEventHandler m_svCustomValidate;
  123. private string m_strClientValidateJs;
  124. //regexp
  125. private string m_regexp;
  126. //ctor
  127. public DymWebFormValidation(ValidationDataType vdt)
  128. {
  129. m_ControlType = ControlType.Basic;
  130. m_vdtCompareValidatorDataType = vdt;
  131. }
  132. public DymWebFormValidation(ServerValidateEventHandler sve, string clientValidateJS = null)
  133. {
  134. m_ControlType = ControlType.Custom;
  135. m_svCustomValidate = sve;
  136. m_strClientValidateJs = clientValidateJS;
  137. }
  138. public DymWebFormValidation(string regexp)
  139. {
  140. m_ControlType = ControlType.Regexp;
  141. m_regexp = regexp;
  142. }
  143. public DymWebFormValidation()
  144. {
  145. m_ControlType = ControlType.None;
  146. }
  147. //get
  148. internal BaseValidator CreateValidator(Control container)
  149. {
  150. switch (m_ControlType)
  151. {
  152. case ControlType.None:
  153. return new CustomValidator() { Enabled = false };
  154. case ControlType.Basic:
  155. return new CompareValidator()
  156. {
  157. Operator = ValidationCompareOperator.DataTypeCheck,
  158. Type = m_vdtCompareValidatorDataType
  159. };
  160. case ControlType.Regexp:
  161. return new RegularExpressionValidator()
  162. {
  163. ValidationExpression = m_regexp,
  164. };
  165. case ControlType.Custom:
  166. var cv = new CustomValidator();
  167. cv.ServerValidate += m_svCustomValidate;
  168. if (string.IsNullOrEmpty(m_strClientValidateJs) == false)
  169. {
  170. cv.ClientValidationFunction = ""
  171. //+ "DWF" + "_"
  172. + "ClientValidationFunction"
  173. //+ "_" + this.GetType().Name
  174. + "_" + m_strClientValidateJs
  175. .GetHashCode()
  176. .ToBytes()
  177. .ToHexString();
  178. var js =
  179. "function " + cv.ClientValidationFunction + "(source, arguments){"
  180. + m_strClientValidateJs
  181. + "}";
  182. ScriptManager.RegisterClientScriptBlock(container.Page, container.Page.GetType(), cv.ClientValidationFunction, js, true);
  183. }
  184. return cv;
  185. default:
  186. throw new Exception("Unknown Validation ControlType");
  187. }
  188. }
  189. }
  190. public sealed class DwfGuidValidation : DymWebFormValidation
  191. {
  192. private static string clientValidateJS =
  193. "arguments.IsValid="
  194. + "/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i"
  195. + ".test(arguments.Value);";
  196. private static void Validation(object source, ServerValidateEventArgs args)
  197. {
  198. try
  199. {
  200. new Guid(args.Value);
  201. args.IsValid = true;
  202. }
  203. catch (Exception)
  204. {
  205. args.IsValid = false;
  206. }
  207. }
  208. public DwfGuidValidation()
  209. : base(Validation, clientValidateJS)
  210. {
  211. }
  212. }
  213. public sealed class DwfDateTimeValidation : DymWebFormValidation
  214. {
  215. private static string clientValidateJS =
  216. "arguments.IsValid=!isNaN(new Date(arguments.Value).getMilliseconds());";
  217. private static void Validation(object source, ServerValidateEventArgs args)
  218. {
  219. DateTime tmp;
  220. args.IsValid = DateTime.TryParse(args.Value, out tmp);
  221. }
  222. public DwfDateTimeValidation()
  223. : base(Validation, clientValidateJS)
  224. {
  225. }
  226. }
  227. public sealed class DymWebFormDefaultValidation : DymWebFormValidation
  228. {
  229. public DymWebFormDefaultValidation(ValidationDataType vdt)
  230. : base(vdt)
  231. {
  232. }
  233. public DymWebFormDefaultValidation(ServerValidateEventHandler sve, string clientValidateJS = null)
  234. : base(sve, clientValidateJS)
  235. {
  236. }
  237. }
  238. public sealed class DymWebFormEmailValidation : DymWebFormValidation
  239. {
  240. public DymWebFormEmailValidation()
  241. : base(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$")
  242. {
  243. }
  244. }
  245. public sealed class DynWebFormPasswordValidation : DymWebFormValidation
  246. {
  247. }
  248. public sealed class DymWebFormTextAreaValidation : DymWebFormValidation
  249. {
  250. public int Cols { get; private set; }
  251. public int Rows { get; private set; }
  252. public DymWebFormTextAreaValidation(int cols = 30, int rows = 5)
  253. {
  254. Cols = cols;
  255. Rows = rows;
  256. }
  257. }
  258. }