123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- using System.Drawing;
- using Bowin.Common.Utility;
- namespace Bowin.Web.Controls.Mvc
- {
- public class DraggableList : BaseFormControl
- {
- public List<DraggableItem> ItemList { get; set; }
- public string ItemSourceUrl { get; set; }
- public int ItemHeight { get; set; }
- public string TitleField { get; set; }
- public string IconField { get; set; }
- public string ValueField { get; set; }
- public int? Width { get; set; }
- public int? Height { get; set; }
- public Color ForeColor { get; set; }
- public DraggableList()
- {
- ItemList = new List<DraggableItem>();
- }
- 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-draggableList");
- string style = "";
- if (this.Width.HasValue)
- {
- style += "width: " + this.Width.Value.ToString() + "px;";
- }
- else
- {
- style += "width: 100%;";
- }
- if (this.Height.HasValue)
- {
- style += "height: " + this.Height.Value.ToString() + "px;";
- }
- else
- {
- style += "height: 100%;";
- }
- style += "overflow-x: scroll;";
- dictPropertyList.Add("style", style);
- #region data-options
- var dictOptions = new Dictionary<string, string>();
- if (this.ForeColor != null)
- {
- dictOptions.Add("foreColor", "'#" + this.ForeColor.ToRGBString() + "'");
- }
- if (!string.IsNullOrEmpty(this.ItemSourceUrl))
- {
- dictOptions.Add("url", "'" + this.ItemSourceUrl + "'");
- if (string.IsNullOrEmpty(this.TitleField))
- {
- throw new Exception("指定了ItemSourceUrl就一定要设置TitleField");
- }
- }
- if (!string.IsNullOrEmpty(this.TitleField))
- {
- dictOptions.Add("titleField", "'" + this.TitleField + "'");
- }
- if (!string.IsNullOrEmpty(this.ValueField))
- {
- dictOptions.Add("valueField", "'" + this.ValueField + "'");
- }
- if (!string.IsNullOrEmpty(this.IconField))
- {
- dictOptions.Add("iconField", "'" + this.IconField + "'");
- }
- dictOptions.Add("itemHeight", this.ItemHeight.ToString());
- 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 tabBuilder = new TagBuilder("div");
- tabBuilder.MergeAttributes<string, string>(GetPropertyList());
- if (this.Attributes != null)
- {
- tabBuilder.MergeAttributes<string, string>(this.Attributes);
- }
- return tabBuilder.ToString(TagRenderMode.Normal);
- }
- public static DraggableList CreateControl(DraggableListControlOptions dragableListOptions, IDictionary<string, string> attributes = null)
- {
- DraggableList dragableList = new DraggableList();
- dragableList.ID = dragableListOptions.ID;
- if (dragableListOptions.ItemList != null)
- {
- dragableList.ItemList.AddRange(dragableListOptions.ItemList);
- }
- dragableList.Name = dragableListOptions.Name;
- dragableList.Attributes = attributes;
- dragableList.ItemSourceUrl = dragableListOptions.ItemSourceUrl;
- dragableList.ItemList = dragableListOptions.ItemList;
- dragableList.ItemHeight = dragableListOptions.ItemHeight;
- dragableList.TitleField = dragableListOptions.TitleField;
- dragableList.ValueField = dragableListOptions.ValueField;
- dragableList.IconField = dragableListOptions.IconField;
- dragableList.Width = dragableListOptions.Width;
- dragableList.Height = dragableListOptions.Height;
- dragableList.ForeColor = dragableListOptions.ForeColor;
- return dragableList;
- }
- }
- }
|