using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Web.Mvc; using System.Web; using Bowin.Common.Utility; namespace Bowin.Web.Controls.Mvc { public class DroppableContainer : BaseControl { public int? Width { get; set; } public int? Height { get; set; } public int? BorderWidth { get; set; } public Color BorderColor { get; set; } public Color BackgroundColor { get; set; } public string BackgroundUrl { get; set; } public Color ForeColor { get; set; } public DraggableListDropOptions DraggableInfo { get; set; } protected IDictionary GetPropertyList() { var dictPropertyList = new Dictionary(); if (!string.IsNullOrEmpty(this.ID)) { dictPropertyList.Add("id", this.ID); } if (!string.IsNullOrEmpty(this.Name)) { dictPropertyList.Add("name", this.Name); } dictPropertyList.Add("class", "easyui-droppableContainer easyui-droppable"); string styleString = ""; if (Width.HasValue) { styleString += "width: " + Width.Value.ToString() + "px; "; } else { styleString += "width: 100%; "; } if (Height.HasValue) { styleString += "height: " + Height.Value.ToString() + "px; "; } else { styleString += "height: 100%; "; } if (string.IsNullOrEmpty(BackgroundUrl)) //{ // styleString += "background: url(" + this.BackgroundUrl + ") no-repeat; background-size: contain; "; //} //else { if (BackgroundColor != null) { styleString += "background-color: #" + BackgroundColor.ToRGBString() + "; "; } } if (BorderWidth.HasValue) { styleString += "border-style: solid; "; styleString += "border-width: " + BorderWidth.Value.ToString() + "px; "; if (BorderColor != null) { styleString += "border-color: #" + BorderColor.ToRGBString() + "; "; } } dictPropertyList.Add("style", styleString); #region data-options var dictOptions = new Dictionary(); dictOptions.Add("onDrop", "CMSFunction.DragDrop.DropFunc"); if (this.DraggableInfo != null) { if (!string.IsNullOrEmpty(this.DraggableInfo.ItemSourceUrl)) { dictOptions.Add("url", "'" + this.DraggableInfo.ItemSourceUrl + "'"); if (string.IsNullOrEmpty(this.DraggableInfo.TitleField)) { throw new Exception("指定了ItemSourceUrl就一定要设置TitleField"); } if (string.IsNullOrEmpty(this.DraggableInfo.ValueField)) { throw new Exception("指定了ItemSourceUrl就一定要设置ValueField"); } if (string.IsNullOrEmpty(this.DraggableInfo.LeftField)) { throw new Exception("指定了ItemSourceUrl就一定要设置LeftField"); } if (string.IsNullOrEmpty(this.DraggableInfo.TopField)) { throw new Exception("指定了ItemSourceUrl就一定要设置TopField"); } } if (!string.IsNullOrEmpty(this.DraggableInfo.TitleField)) { dictOptions.Add("titleField", "'" + this.DraggableInfo.TitleField + "'"); } if (!string.IsNullOrEmpty(this.DraggableInfo.ValueField)) { dictOptions.Add("valueField", "'" + this.DraggableInfo.ValueField + "'"); } if (!string.IsNullOrEmpty(this.DraggableInfo.IconField)) { dictOptions.Add("iconField", "'" + this.DraggableInfo.IconField + "'"); } if (!string.IsNullOrEmpty(this.DraggableInfo.LeftField)) { dictOptions.Add("leftField", "'" + this.DraggableInfo.LeftField + "'"); } if (!string.IsNullOrEmpty(this.DraggableInfo.TopField)) { dictOptions.Add("topField", "'" + this.DraggableInfo.TopField + "'"); } if (this.DraggableInfo.ItemList != null && this.DraggableInfo.ItemList.Count > 0) { string itemString = ""; this.DraggableInfo.ItemList.ForEach(x => { if (itemString != "") { itemString += ","; } itemString += "{" + string.Format(" title: '{0}', value: '{1}', icon: '{2}', left: {3}, top: {4} ", x.Title, x.Value, x.Icon, x.Left, x.Top) + "}"; }); dictOptions.Add("items", "[" + itemString + "]"); } dictOptions.Add("itemHeight", this.DraggableInfo.ItemHeight.ToString()); } if (ForeColor != null) { dictOptions.Add("foreColor", "'#" + this.ForeColor.ToRGBString() + "'"); } dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value)))); #endregion data-options return dictPropertyList; } public override string Render() { TagBuilder tagBuilder = new TagBuilder("div"); TagBuilder tagBuilderImg = new TagBuilder("img"); TagBuilder tagBuilderHidden = new TagBuilder("input"); if (!String.IsNullOrEmpty(this.BackgroundUrl)) { tagBuilderImg.MergeAttribute("border", "0"); tagBuilderImg.MergeAttribute("src", this.BackgroundUrl); tagBuilderImg.MergeAttribute("style", "width: 100%; height: 100%;"); } tagBuilder.MergeAttributes(GetPropertyList()); if (this.Attributes != null) { tagBuilder.MergeAttributes(this.Attributes); } tagBuilderHidden.MergeAttribute("type", "hidden"); tagBuilderHidden.MergeAttribute("id", this.ID + "_containValues"); if (!String.IsNullOrEmpty(this.BackgroundUrl)) { tagBuilder.InnerHtml = tagBuilderImg.ToString(TagRenderMode.SelfClosing); } tagBuilder.InnerHtml += "
删除
"; return tagBuilder.ToString(TagRenderMode.Normal) + tagBuilderHidden.ToString(TagRenderMode.SelfClosing); } public static DroppableContainer CreateControl(DroppableContainerOptions droppableContainerOptions, IDictionary attributes = null) { DroppableContainer droppableContainer = new DroppableContainer(); droppableContainer.ID = droppableContainerOptions.ID; droppableContainer.Name = droppableContainerOptions.Name; droppableContainer.Width = droppableContainerOptions.Width; droppableContainer.Height = droppableContainerOptions.Height; droppableContainer.BorderWidth = droppableContainerOptions.BorderWidth; droppableContainer.BorderColor = droppableContainerOptions.BorderColor; droppableContainer.BackgroundColor = droppableContainerOptions.BackgroundColor; droppableContainer.BackgroundUrl = droppableContainerOptions.BackgroundUrl; droppableContainer.DraggableInfo = droppableContainerOptions.DraggableInfo; droppableContainer.ForeColor = droppableContainerOptions.ForeColor; return droppableContainer; } public static IList GetContainerItems(string containerID) { IList result = new List(); string hiddenName = containerID + "_containValues"; string allValues = HttpContext.Current.Request.Form["DraggableItemPosition"]; string[] eachValues = allValues.Split(','); eachValues.ToList().ForEach(x => { string[] values = x.Split('|'); DraggableItemPosition itemPosition = new DraggableItemPosition(); itemPosition.Value = values[0]; itemPosition.Title = values[1]; itemPosition.Left = Convert.ToInt32(values[2]); itemPosition.Top = Convert.ToInt32(values[3]); result.Add(itemPosition); }); return result; } } }