PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Lib.Web.Mvc 3.3/Lib.Web.Mvc/JQuery/JqGrid/JqGridColumnModel.cs

#
C# | 254 lines | 133 code | 42 blank | 79 comment | 44 complexity | ae8848fca8f59df853ebb079c12569f9 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. using System.Web.Mvc;
  7. using Lib.Web.Mvc.JQuery.JqGrid.DataAnnotations;
  8. using System.ComponentModel.DataAnnotations;
  9. namespace Lib.Web.Mvc.JQuery.JqGrid
  10. {
  11. /// <summary>
  12. /// jqGrid column parameters description.
  13. /// </summary>
  14. public class JqGridColumnModel
  15. {
  16. #region Properties
  17. /// <summary>
  18. /// Gets or sets the alignment of the cell in the body layer (default JqGridAligns.Left)
  19. /// </summary>
  20. public JqGridAlignments Alignment { get; set; }
  21. /// <summary>
  22. /// Gets or sets additional CSS classes for the column (separated by space).
  23. /// </summary>
  24. public string Classes { get; set; }
  25. /// <summary>
  26. /// Gets or set the value defining if this column can be edited.
  27. /// </summary>
  28. public bool? Editable { get; set; }
  29. /// <summary>
  30. /// Gets or sets the options for editable column.
  31. /// </summary>
  32. public JqGridColumnEditOptions EditOptions { get; set; }
  33. /// <summary>
  34. /// Gets or sets the rules for editable column.
  35. /// </summary>
  36. public JqGridColumnEditRules EditRules { get; set; }
  37. /// <summary>
  38. /// Gets or sets the type for editable column.
  39. /// </summary>
  40. public JqGridColumnEditTypes? EditType { get; set; }
  41. /// <summary>
  42. /// Gets or sets the value which defines if internal recalculation of the width of the column is disabled (default false).
  43. /// </summary>
  44. public bool? Fixed { get; set; }
  45. /// <summary>
  46. /// Gets or sets the predefined formatter type ('' delimited string) or custom JavaScript formatting function name.
  47. /// </summary>
  48. public string Formatter { get; set; }
  49. /// <summary>
  50. /// Gets or sets the options for predefined formatter (every predefined formatter uses only a subset of all options), which are overwriting the defaults from the language file.
  51. /// </summary>
  52. public JqGridColumnFormatterOptions FormatterOptions { get; set; }
  53. /// <summary>
  54. /// Get or sets additional, used in form editing, options for editable column.
  55. /// </summary>
  56. public JqGridColumnFormOptions FormOptions { get; set; }
  57. /// <summary>
  58. /// Gets or sets the value which defines if this column is hidden at initialization (default false).
  59. /// </summary>
  60. public bool Hidden { get; set; }
  61. /// <summary>
  62. /// Gets or sets the sorting order for first column sorting.
  63. /// </summary>
  64. public JqGridSortingOrders? InitialSortingOrder { get; set; }
  65. /// <summary>
  66. /// Gets or sets the value which defines if column can be resized (default true).
  67. /// </summary>
  68. public bool? Resizable { get; set; }
  69. /// <summary>
  70. /// Gets or sets the value defining if this column can be searched.
  71. /// </summary>
  72. public bool? Searchable { get; set; }
  73. /// <summary>
  74. /// Gets or sets the options for searchable column.
  75. /// </summary>
  76. public JqGridColumnSearchOptions SearchOptions { get; set; }
  77. /// <summary>
  78. /// Gets or sets the type of the search field for the column.
  79. /// </summary>
  80. public JqGridColumnSearchTypes? SearchType { get; set; }
  81. /// <summary>
  82. /// Gets or sets the grouping summary type.
  83. /// </summary>
  84. public JqGridColumnSummaryTypes? SummaryType { get; set; }
  85. /// <summary>
  86. /// Gets or sets the grouping summary template.
  87. /// </summary>
  88. public string SummaryTemplate { get; set; }
  89. /// <summary>
  90. /// Gets or sets the grouping summary function for custom type.
  91. /// </summary>
  92. public string SummaryFunction { get; set; }
  93. /// <summary>
  94. /// Gets or sets the value defining if this column can be sorted.
  95. /// </summary>
  96. public bool? Sortable { get; set; }
  97. /// <summary>
  98. /// Gets or sets the index name for sorting and searching (default String.Empty)
  99. /// </summary>
  100. public string Index { get; set; }
  101. /// <summary>
  102. /// Gets the unique name for the column.
  103. /// </summary>
  104. public string Name { get; private set; }
  105. /// <summary>
  106. /// Gets or sets the custom function to "unformat" a value of the cell when used in editing or client-side sorting
  107. /// </summary>
  108. public string UnFormatter { get; set; }
  109. /// <summary>
  110. /// Gets or sets the initial width in pixels of the column (default 150).
  111. /// </summary>
  112. public int? Width { get; set; }
  113. #endregion
  114. #region Constructor
  115. /// <summary>
  116. /// Initializes a new instance of the JqGridColumnModel class.
  117. /// </summary>
  118. /// <param name="name">The unique name for the column.</param>
  119. public JqGridColumnModel(string name)
  120. {
  121. SummaryType = null;
  122. SummaryTemplate = "{0}";
  123. SummaryFunction = null;
  124. Index = String.Empty;
  125. Name = name;
  126. Alignment = JqGridAlignments.Left;
  127. Hidden = false;
  128. }
  129. internal JqGridColumnModel(ModelMetadata propertyMetadata)
  130. {
  131. IEnumerable<object> customAttributes = propertyMetadata.ContainerType.GetProperty(propertyMetadata.PropertyName).GetCustomAttributes(true).AsEnumerable();
  132. JqGridColumnLayoutAttribute columnLayoutAttribute = customAttributes.OfType<JqGridColumnLayoutAttribute>().FirstOrDefault();
  133. if (columnLayoutAttribute != null)
  134. {
  135. Alignment = columnLayoutAttribute.Alignment;
  136. Classes = columnLayoutAttribute.Classes;
  137. Fixed = columnLayoutAttribute.Fixed;
  138. Resizable = columnLayoutAttribute.Resizable;
  139. Width = columnLayoutAttribute.Width;
  140. }
  141. else
  142. Alignment = JqGridAlignments.Left;
  143. JqGridColumnFormatterAttribute columnFormatterAttribute = customAttributes.OfType<JqGridColumnFormatterAttribute>().FirstOrDefault();
  144. if (columnFormatterAttribute != null)
  145. {
  146. Formatter = columnFormatterAttribute.Formatter;
  147. FormatterOptions = columnFormatterAttribute.FormatterOptions;
  148. UnFormatter = columnFormatterAttribute.UnFormatter;
  149. }
  150. JqGridColumnSortableAttribute columnSortableAttribute = customAttributes.OfType<JqGridColumnSortableAttribute>().FirstOrDefault();
  151. if (columnSortableAttribute != null)
  152. {
  153. InitialSortingOrder = columnSortableAttribute.InitialSortingOrder;
  154. Sortable = columnSortableAttribute.Sortable;
  155. Index = columnSortableAttribute.Index;
  156. }
  157. JqGridColumnSearchableAttribute columnSearchableAttribute = customAttributes.OfType<JqGridColumnSearchableAttribute>().FirstOrDefault();
  158. if (columnSearchableAttribute != null)
  159. {
  160. Searchable = columnSearchableAttribute.Searchable;
  161. if (Searchable.Value)
  162. {
  163. SearchOptions = columnSearchableAttribute.SearchOptions;
  164. SearchType = columnSearchableAttribute.SearchType;
  165. if (SearchType == JqGridColumnSearchTypes.Select)
  166. SearchOptions.DataUrl = columnSearchableAttribute.DataUrl;
  167. }
  168. }
  169. JqGridColumnEditableAttribute columnEditableAttribute = customAttributes.OfType<JqGridColumnEditableAttribute>().FirstOrDefault();
  170. if (columnEditableAttribute != null)
  171. {
  172. Editable = columnEditableAttribute.Editable;
  173. if (Editable.Value)
  174. {
  175. EditOptions = columnEditableAttribute.EditOptions;
  176. EditRules = columnEditableAttribute.EditRules;
  177. EditType = columnEditableAttribute.EditType;
  178. FormOptions = columnEditableAttribute.FormOptions;
  179. if ((propertyMetadata.ModelType == typeof(Int16)) || (propertyMetadata.ModelType == typeof(Int32)) || (propertyMetadata.ModelType == typeof(Int64)) || (propertyMetadata.ModelType == typeof(UInt16)) || (propertyMetadata.ModelType == typeof(UInt32)) || (propertyMetadata.ModelType == typeof(UInt32)))
  180. EditRules.Integer = true;
  181. else if ((propertyMetadata.ModelType == typeof(Decimal)) || (propertyMetadata.ModelType == typeof(Double)) || (propertyMetadata.ModelType == typeof(Single)))
  182. EditRules.Number = true;
  183. RequiredAttribute requiredAttribute = customAttributes.OfType<RequiredAttribute>().FirstOrDefault();
  184. if (requiredAttribute != null)
  185. EditRules.Required = true;
  186. RangeAttribute rangeAttribute = customAttributes.OfType<RangeAttribute>().FirstOrDefault();
  187. if (rangeAttribute != null)
  188. {
  189. EditRules.MaxValue = Convert.ToDouble(rangeAttribute.Maximum);
  190. EditRules.MinValue = Convert.ToDouble(rangeAttribute.Minimum);
  191. }
  192. if (EditType == JqGridColumnEditTypes.Select)
  193. EditOptions.DataUrl = columnEditableAttribute.DataUrl;
  194. StringLengthAttribute stringLengthAttribute = customAttributes.OfType<StringLengthAttribute>().FirstOrDefault();
  195. if (stringLengthAttribute != null)
  196. EditOptions.MaximumLength = stringLengthAttribute.MaximumLength;
  197. }
  198. }
  199. SummaryType = propertyMetadata.GetColumnSummaryType();
  200. SummaryTemplate = propertyMetadata.GetColumnSummaryTemplate();
  201. SummaryFunction = propertyMetadata.GetColumnSummaryFunction();
  202. Name = propertyMetadata.PropertyName;
  203. if (String.IsNullOrWhiteSpace(Index))
  204. Index = propertyMetadata.PropertyName;
  205. if (!String.IsNullOrWhiteSpace(propertyMetadata.TemplateHint) && propertyMetadata.TemplateHint.Equals("HiddenInput"))
  206. Hidden = true;
  207. else
  208. Hidden = false;
  209. }
  210. #endregion
  211. }
  212. }