DroppableContainer.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Web.Mvc;
  7. using System.Web;
  8. using Bowin.Common.Utility;
  9. namespace Bowin.Web.Controls.Mvc
  10. {
  11. public class DroppableContainer : BaseControl
  12. {
  13. public int? Width { get; set; }
  14. public int? Height { get; set; }
  15. public int? BorderWidth { get; set; }
  16. public Color BorderColor { get; set; }
  17. public Color BackgroundColor { get; set; }
  18. public string BackgroundUrl { get; set; }
  19. public Color ForeColor { get; set; }
  20. public DraggableListDropOptions DraggableInfo { get; set; }
  21. protected IDictionary<string, string> GetPropertyList()
  22. {
  23. var dictPropertyList = new Dictionary<string, string>();
  24. if (!string.IsNullOrEmpty(this.ID))
  25. {
  26. dictPropertyList.Add("id", this.ID);
  27. }
  28. if (!string.IsNullOrEmpty(this.Name))
  29. {
  30. dictPropertyList.Add("name", this.Name);
  31. }
  32. dictPropertyList.Add("class", "easyui-droppableContainer easyui-droppable");
  33. string styleString = "";
  34. if (Width.HasValue)
  35. {
  36. styleString += "width: " + Width.Value.ToString() + "px; ";
  37. }
  38. else
  39. {
  40. styleString += "width: 100%; ";
  41. }
  42. if (Height.HasValue)
  43. {
  44. styleString += "height: " + Height.Value.ToString() + "px; ";
  45. }
  46. else
  47. {
  48. styleString += "height: 100%; ";
  49. }
  50. if (string.IsNullOrEmpty(BackgroundUrl))
  51. //{
  52. // styleString += "background: url(" + this.BackgroundUrl + ") no-repeat; background-size: contain; ";
  53. //}
  54. //else
  55. {
  56. if (BackgroundColor != null)
  57. {
  58. styleString += "background-color: #" + BackgroundColor.ToRGBString() + "; ";
  59. }
  60. }
  61. if (BorderWidth.HasValue)
  62. {
  63. styleString += "border-style: solid; ";
  64. styleString += "border-width: " + BorderWidth.Value.ToString() + "px; ";
  65. if (BorderColor != null)
  66. {
  67. styleString += "border-color: #" + BorderColor.ToRGBString() + "; ";
  68. }
  69. }
  70. dictPropertyList.Add("style", styleString);
  71. #region data-options
  72. var dictOptions = new Dictionary<string, string>();
  73. dictOptions.Add("onDrop", "CMSFunction.DragDrop.DropFunc");
  74. if (this.DraggableInfo != null)
  75. {
  76. if (!string.IsNullOrEmpty(this.DraggableInfo.ItemSourceUrl))
  77. {
  78. dictOptions.Add("url", "'" + this.DraggableInfo.ItemSourceUrl + "'");
  79. if (string.IsNullOrEmpty(this.DraggableInfo.TitleField))
  80. {
  81. throw new Exception("指定了ItemSourceUrl就一定要设置TitleField");
  82. }
  83. if (string.IsNullOrEmpty(this.DraggableInfo.ValueField))
  84. {
  85. throw new Exception("指定了ItemSourceUrl就一定要设置ValueField");
  86. }
  87. if (string.IsNullOrEmpty(this.DraggableInfo.LeftField))
  88. {
  89. throw new Exception("指定了ItemSourceUrl就一定要设置LeftField");
  90. }
  91. if (string.IsNullOrEmpty(this.DraggableInfo.TopField))
  92. {
  93. throw new Exception("指定了ItemSourceUrl就一定要设置TopField");
  94. }
  95. }
  96. if (!string.IsNullOrEmpty(this.DraggableInfo.TitleField))
  97. {
  98. dictOptions.Add("titleField", "'" + this.DraggableInfo.TitleField + "'");
  99. }
  100. if (!string.IsNullOrEmpty(this.DraggableInfo.ValueField))
  101. {
  102. dictOptions.Add("valueField", "'" + this.DraggableInfo.ValueField + "'");
  103. }
  104. if (!string.IsNullOrEmpty(this.DraggableInfo.IconField))
  105. {
  106. dictOptions.Add("iconField", "'" + this.DraggableInfo.IconField + "'");
  107. }
  108. if (!string.IsNullOrEmpty(this.DraggableInfo.LeftField))
  109. {
  110. dictOptions.Add("leftField", "'" + this.DraggableInfo.LeftField + "'");
  111. }
  112. if (!string.IsNullOrEmpty(this.DraggableInfo.TopField))
  113. {
  114. dictOptions.Add("topField", "'" + this.DraggableInfo.TopField + "'");
  115. }
  116. if (this.DraggableInfo.ItemList != null && this.DraggableInfo.ItemList.Count > 0)
  117. {
  118. string itemString = "";
  119. this.DraggableInfo.ItemList.ForEach(x => {
  120. if (itemString != "")
  121. {
  122. itemString += ",";
  123. }
  124. itemString += "{" + string.Format(" title: '{0}', value: '{1}', icon: '{2}', left: {3}, top: {4} ",
  125. x.Title, x.Value, x.Icon, x.Left, x.Top) + "}";
  126. });
  127. dictOptions.Add("items", "[" + itemString + "]");
  128. }
  129. dictOptions.Add("itemHeight", this.DraggableInfo.ItemHeight.ToString());
  130. }
  131. if (ForeColor != null)
  132. {
  133. dictOptions.Add("foreColor", "'#" + this.ForeColor.ToRGBString() + "'");
  134. }
  135. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  136. #endregion data-options
  137. return dictPropertyList;
  138. }
  139. public override string Render()
  140. {
  141. TagBuilder tagBuilder = new TagBuilder("div");
  142. TagBuilder tagBuilderImg = new TagBuilder("img");
  143. TagBuilder tagBuilderHidden = new TagBuilder("input");
  144. if (!String.IsNullOrEmpty(this.BackgroundUrl))
  145. {
  146. tagBuilderImg.MergeAttribute("border", "0");
  147. tagBuilderImg.MergeAttribute("src", this.BackgroundUrl);
  148. tagBuilderImg.MergeAttribute("style", "width: 100%; height: 100%;");
  149. }
  150. tagBuilder.MergeAttributes<string, string>(GetPropertyList());
  151. if (this.Attributes != null)
  152. {
  153. tagBuilder.MergeAttributes<string, string>(this.Attributes);
  154. }
  155. tagBuilderHidden.MergeAttribute("type", "hidden");
  156. tagBuilderHidden.MergeAttribute("id", this.ID + "_containValues");
  157. if (!String.IsNullOrEmpty(this.BackgroundUrl))
  158. {
  159. tagBuilder.InnerHtml = tagBuilderImg.ToString(TagRenderMode.SelfClosing);
  160. }
  161. tagBuilder.InnerHtml += "<div id=\"" + this.ID + "_menu\" class=\"easyui-menu\" data-options=\"onClick:function (item) { CMSFunction.DragDrop.curPopupContainerItem.remove(); }\" style=\"width:120px;\"><div>删除</div></div>";
  162. return tagBuilder.ToString(TagRenderMode.Normal) + tagBuilderHidden.ToString(TagRenderMode.SelfClosing);
  163. }
  164. public static DroppableContainer CreateControl(DroppableContainerOptions droppableContainerOptions, IDictionary<string, string> attributes = null)
  165. {
  166. DroppableContainer droppableContainer = new DroppableContainer();
  167. droppableContainer.ID = droppableContainerOptions.ID;
  168. droppableContainer.Name = droppableContainerOptions.Name;
  169. droppableContainer.Width = droppableContainerOptions.Width;
  170. droppableContainer.Height = droppableContainerOptions.Height;
  171. droppableContainer.BorderWidth = droppableContainerOptions.BorderWidth;
  172. droppableContainer.BorderColor = droppableContainerOptions.BorderColor;
  173. droppableContainer.BackgroundColor = droppableContainerOptions.BackgroundColor;
  174. droppableContainer.BackgroundUrl = droppableContainerOptions.BackgroundUrl;
  175. droppableContainer.DraggableInfo = droppableContainerOptions.DraggableInfo;
  176. droppableContainer.ForeColor = droppableContainerOptions.ForeColor;
  177. return droppableContainer;
  178. }
  179. public static IList<DraggableItemPosition> GetContainerItems(string containerID)
  180. {
  181. IList<DraggableItemPosition> result = new List<DraggableItemPosition>();
  182. string hiddenName = containerID + "_containValues";
  183. string allValues = HttpContext.Current.Request.Form["DraggableItemPosition"];
  184. string[] eachValues = allValues.Split(',');
  185. eachValues.ToList().ForEach(x => {
  186. string[] values = x.Split('|');
  187. DraggableItemPosition itemPosition = new DraggableItemPosition();
  188. itemPosition.Value = values[0];
  189. itemPosition.Title = values[1];
  190. itemPosition.Left = Convert.ToInt32(values[2]);
  191. itemPosition.Top = Convert.ToInt32(values[3]);
  192. result.Add(itemPosition);
  193. });
  194. return result;
  195. }
  196. }
  197. }