123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- 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<string, string> GetPropertyList()
- {
- var dictPropertyList = new Dictionary<string, string>();
- 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<string, string>();
- 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<string, string>(GetPropertyList());
- if (this.Attributes != null)
- {
- tagBuilder.MergeAttributes<string, string>(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 += "<div id=\"" + this.ID + "_menu\" class=\"easyui-menu\" data-options=\"onClick:function (item) { CMSFunction.DragDrop.curPopupContainerItem.remove(); }\" style=\"width:120px;\"><div>删除</div></div>";
- return tagBuilder.ToString(TagRenderMode.Normal) + tagBuilderHidden.ToString(TagRenderMode.SelfClosing);
- }
- public static DroppableContainer CreateControl(DroppableContainerOptions droppableContainerOptions, IDictionary<string, string> 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<DraggableItemPosition> GetContainerItems(string containerID)
- {
- IList<DraggableItemPosition> result = new List<DraggableItemPosition>();
- 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;
- }
- }
- }
|