wince窗体转html(基于layui)(一)

1. 改造layui form,版本2.5.5

render第440行 渲染select处修改

var wd = othis.attr('lay-width'),flex = othis.css('flex');
var style = (wd?`width:${wd};`:'')+(flex?`flex:${flex};`:'');
//替代元素
var reElem = $(['<div class="'+ (isSearch ? '' : 'layui-unselect ') + CLASS
, othis.hasClass("layui-hide")?' layui-hide':''
,(disabled ? ' layui-select-disabled' : '') +'"'
,style?` style="${style}"`:''
,'>'
,'<div class="'+ TITLE +'">'

添加自定义宽度属性“lay-width”,flex样式,以及下拉控制显示和隐藏“layui-hide”

2.需要转换的控件

Label,TextBox,ComboBox,RadioButton,ListView,CheckBox,Button

interface IControl
{
	int x1 { get; set; }
	int x2 { get; set; }
	int y1 { get; set; }
	int y2 { get; set; }

	string id { get; set; }
	float width { get; set; }
	string widthStyle { get; set; }

	Dictionary<string, string> props { get; set; }

	void Render(StringBuilder form,string idPrefix);
}

class Row
{
	public int y1 { get; set; }
	public int y2 { get; set; }

	public int no { get; set; }

	public bool hasTable { get; set; }

	public Row() {
		controls = new List<IControl>();
	}

	public List<IControl> controls { get; set; }
	public void Render(StringBuilder form,string idPrefix)
	{
		if (controls.Count > 0)
		{
			controls.Sort(ControlComparer.Instance);
			/*
			if (controls.Count > 1 && !controls.Exists(c => c.width == -1))
			{
				float total = controls.Where(c => c.width > 0).Sum(c => c.width);
				List<IControl> list = controls.Where(c => c.width > 0).ToList();
				int percent = 0;
				for (int i = 0, j = list.Count; i < j; i++)
				{
					int width = 0;
					if (i == j - 1)
					{
						width = 100 - percent;
					}
					else
					{
						width = Convert.ToInt32(list[i].width * 100 / total);
					}

					percent += width;
					list[i].widthStyle = width.ToString() + "%";
				}
			}
			*/

			if (controls.Count == 1 && (controls[0] as VLabel) != null)
			{
				controls[0].widthStyle = "100%";
			}
			form.AppendFormat("<div class=\"layui-{0}{1}\">\r\n", hasTable ? "row" : "flex", controls.Count == controls.Where(c=>c as VButton != null).Count() ? " bottom-btns" : "");
			controls.ForEach(c => c.Render(form,idPrefix));
			form.Append("</div>\r\n");
		}
	}
}

class VLabel : IControl
{
	public int x1 { get; set; }
	public int x2 { get; set; }

	public int y1 { get; set; }
	public int y2 { get; set; }

	public Dictionary<string, string> props { get; set; }

	public string id { get; set; }
	public string text { get; set; }
	public float width { get; set; }
	public string widthStyle { get; set; }
	public bool visible { get; set; }
	public string tip { get; set; }
	public int height { get; set; }
	public string align { get; set; }
	public float fontSize { get; set; }
	public string color { get; set; }
	public void Render(StringBuilder form, string idPrefix)
	{
		form.Append("    <label class=\"layui-form-label\"");
		StringBuilder style = new StringBuilder();
		if (!string.IsNullOrEmpty(widthStyle))
		{
			style.AppendFormat("width:{0};", widthStyle);
		}
		else if (width > 0)
		{
			style.AppendFormat("width:{0}px;", width);
		}

		if (!visible)
		{
			style.Append("display:none;");
		}

		if (!string.IsNullOrEmpty(align))
		{
			style.AppendFormat("text-align:{0};",align);
		}

		style.AppendFormat("height:{0}px;",height);
		if (fontSize > 0)
		{
			style.AppendFormat("font-size:{0}px;", fontSize);
		}

		if (!string.IsNullOrEmpty(color))
		{
			style.AppendFormat("color:{0};", color);
		}

		if (style.Length > 0)
		{
			form.AppendFormat(" style=\"{0}\"", style);
		}

		if (!string.IsNullOrEmpty(id))
		{
			form.AppendFormat(" id=\"{0}{1}\"",idPrefix,id);
		}

		if (!string.IsNullOrEmpty(tip))
		{
			form.AppendFormat(" title=\"{0}\"", Program.TransferXml(tip));
		}
		if (props != null && props.Count > 0)
		{
			foreach (KeyValuePair<string, string> kv in props)
			{
				form.AppendFormat(" {0}=\"{1}\"", kv.Key, Program.TransferXml(kv.Value));
			}
		}
		form.AppendFormat(">{0}</label>\r\n",text);
	}
}

class VInput : IControl
{
	public int x1 { get; set; }
	public int x2 { get; set; }

	public int y1 { get; set; }
	public int y2 { get; set; }

	public Dictionary<string, string> props { get; set; }

	public string id { get; set; }
	public string text { get; set; }
	public float width { get; set; }
	public string widthStyle { get; set; }
	public bool visible { get; set; }
	public string tip { get; set; }
	public string type { get; set; }
	public string placeholder { get; set; }
	public bool enabled { get; set; }
	public void Render(StringBuilder form, string idPrefix)
	{
		form.AppendFormat("    <input class=\"layui-input{0}\" autocomplete=\"off\"", enabled ? "" : " layui-disabled");
		if (string.IsNullOrEmpty(type))
		{
			type = "text";
		}
		form.AppendFormat(" id=\"{0}{1}\" name=\"{0}{1}\"", idPrefix,id);
		form.AppendFormat(" type=\"{0}\"", type);
		if (!enabled)
		{
			form.Append(" disabled");
		}
		StringBuilder style = new StringBuilder();
		if (!string.IsNullOrEmpty(widthStyle))
		{
			style.AppendFormat("width:{0};", widthStyle);
		}
		else if (width > 0)
		{
			style.AppendFormat("width:{0}px;", width);
		}

		if (!visible)
		{
			style.Append("display:none;");
		}

		if (style.Length > 0)
		{
			form.AppendFormat(" style=\"{0}\"", style);
		}
		if (!string.IsNullOrEmpty(text))
		{
			form.AppendFormat(" value=\"{0}\"", Program.TransferXml(text));
		}
		if (!string.IsNullOrEmpty(placeholder))
		{
			form.AppendFormat(" placeholder=\"{0}\"", Program.TransferXml(placeholder));
		}
		if (!string.IsNullOrEmpty(tip))
		{
			form.AppendFormat(" title=\"{0}\"", Program.TransferXml(tip));
		}
		if (props != null && props.Count > 0)
		{
			foreach (KeyValuePair<string, string> kv in props)
			{
				form.AppendFormat(" {0}=\"{1}\"", kv.Key, Program.TransferXml(kv.Value));
			}
		}
		form.Append(" />\r\n");
	}
}

class VButton : IControl
{
	public int x1 { get; set; }
	public int x2 { get; set; }

	public int y1 { get; set; }
	public int y2 { get; set; }

	public Dictionary<string, string> props { get; set; }

	public string id { get; set; }
	public string text { get; set; }
	public float width { get; set; }
	public string widthStyle { get; set; }
	public bool visible { get; set; }
	public bool enabled { get; set; }
	public void Render(StringBuilder form,string idPrefix)
	{
		form.AppendFormat("    <input class=\"layui-btn{0}\" type=\"button\"", enabled ? "" : " layui-disabled");
		form.AppendFormat(" id=\"{0}{1}\"",idPrefix, id);
		if (!enabled)
		{
			form.Append(" disabled");
		}
		StringBuilder style = new StringBuilder();
		if (!string.IsNullOrEmpty(widthStyle))
		{
			style.AppendFormat("width:{0};", widthStyle);
		}
		else if (width > 0)
		{
			style.AppendFormat("width:{0}px;", width);
		}

		if (!visible)
		{
			style.Append("display:none;");
		}

		if (style.Length > 0)
		{
			form.AppendFormat(" style=\"{0}\"", style);
		}
		if (!string.IsNullOrEmpty(text))
		{
			form.AppendFormat(" value=\"{0}\"", Program.TransferXml(text));
		}
		if (props != null && props.Count > 0)
		{
			foreach (KeyValuePair<string, string> kv in props)
			{
				form.AppendFormat(" {0}=\"{1}\"", kv.Key, Program.TransferXml(kv.Value));
			}
		}
		form.Append(" />\r\n");
	}
}

/// <summary>
/// Dropdown
/// </summary>
class VSelect : IControl
{
	public int x1 { get; set; }
	public int x2 { get; set; }

	public int y1 { get; set; }
	public int y2 { get; set; }

	public Dictionary<string, string> props { get; set; }

	public string id { get; set; }
	public float width { get; set; }
	public string widthStyle { get; set; }
	public bool visible { get; set; }
	public string tip { get; set; }
	public string value { get; set; }
	public bool enabled { get; set; }
	public Dictionary<string, string> items { get; set; }

	public void Render(StringBuilder form,string idPrefix)
	{
		form.AppendFormat("    <select class=\"layui-input{0}\"", enabled ? "" : " layui-disabled");
		form.AppendFormat(" lay-filter=\"{0}{1}\" id=\"{0}{1}\" name=\"{0}{1}\"", idPrefix,id);
		if (width > 0)
		{
			form.AppendFormat(" lay-width=\"{0}px\"",width);
		}
		if (!enabled)
		{
			form.Append(" disabled");
		}
		StringBuilder style = new StringBuilder();
		if (!string.IsNullOrEmpty(widthStyle))
		{
			style.AppendFormat("width:{0};", widthStyle);
		}
		else if (width > 0)
		{
			style.AppendFormat("width:{0}px;", width);
		}

		if (!visible)
		{
			style.Append("display:none;");
		}

		if (style.Length > 0)
		{
			form.AppendFormat(" style=\"{0}\"", style);
		}
		if (!string.IsNullOrEmpty(tip))
		{
			form.AppendFormat(" title=\"{0}\"", Program.TransferXml(tip));
		}

		if (props != null && props.Count > 0)
		{
			foreach (KeyValuePair<string, string> kv in props)
			{
				form.AppendFormat(" {0}=\"{1}\"", kv.Key, Program.TransferXml(kv.Value));
			}
		}
		form.Append(">\r\n");
		if (items != null && items.Count > 0)
		{
			form.Append(string.Join("", items.Select(i => string.Format("        <option value=\"{0}\"{2}>{1}</option>\r\n", 
				Program.TransferXml(i.Key), i.Value, i.Key == value ? " selected" : "")).ToArray()));
		}
		form.Append("    </select>\r\n");
	}
}

class VCheckBox : IControl
{
	public int x1 { get; set; }
	public int x2 { get; set; }

	public int y1 { get; set; }
	public int y2 { get; set; }

	public Dictionary<string, string> props { get; set; }

	public string id { get; set; }
	public string text { get; set; }
	public float width { get; set; }
	public string widthStyle { get; set; }
	public bool visible { get; set; }
	public bool selected { get; set; }
	public bool enabled { get; set; }
	public void Render(StringBuilder form, string idPrefix)
	{
		form.AppendFormat("    <input type=\"checkbox\" lay-skin=\"primary\" value=\"1\" class=\"{0}\"", enabled ? "" : "layui-disabled");
		form.AppendFormat(" id=\"{0}{1}\" name=\"{0}{1}\"", idPrefix, id);
		if (!enabled)
		{
			form.Append(" disabled");
		}
		StringBuilder style = new StringBuilder();
		if (!string.IsNullOrEmpty(widthStyle))
		{
			style.AppendFormat("width:{0};", widthStyle);
		}
		else if (width > 0)
		{
			style.AppendFormat("width:{0}px;", width);
		}

		if (!visible)
		{
			style.Append("display:none;");
		}

		if (style.Length > 0)
		{
			form.AppendFormat(" style=\"{0}\"", style);
		}
		if (!string.IsNullOrEmpty(text))
		{
			form.AppendFormat(" title=\"{0}\"", Program.TransferXml(text));
		}
		if (selected)
		{
			form.Append(" checked");
		}
		if (props != null && props.Count > 0)
		{
			foreach (KeyValuePair<string, string> kv in props)
			{
				form.AppendFormat(" {0}=\"{1}\"", kv.Key, Program.TransferXml(kv.Value));
			}
		}
		form.Append(" />\r\n");
	}
}

class VListTable : IControl
{
	public const int COLUMN_MIN_WIDTH = 50;
	public int x1 { get; set; }
	public int x2 { get; set; }

	public int y1 { get; set; }
	public int y2 { get; set; }

	public Dictionary<string, string> props { get; set; }

	public float width { get; set; }
	public string widthStyle { get; set; }

	public List<VListTableColumn> Columns { get; set; }
	public string id { get; set; }
	public float height { get; set; }
	public bool visible { get; set; }
	public void Render(StringBuilder form, string idPrefix)
	{
		form.AppendFormat("    <table class=\"layui-table\" lay-filter=\"{0}{1}\" id=\"{0}{1}\"",
			idPrefix,id, height, COLUMN_MIN_WIDTH);
		StringBuilder style = new StringBuilder();
		/*
		if (height > 0)
		{
			style.AppendFormat("height:{0}px;", height);
		}
		if (width > 0)
		{
			style.AppendFormat("width:{0}px;", width);
		}
		*/
		if (!visible)
		{
			style.Append("display:none");
		}

		if (style.Length > 0)
		{
			form.AppendFormat(" style=\"{0}\"",style);
		}
		if (props != null && props.Count > 0)
		{
			foreach (KeyValuePair<string, string> kv in props)
			{
				form.AppendFormat(" {0}=\"{1}\"",kv.Key,Program.TransferXml(kv.Value));
			}
		}
		form.Append(">\r\n");
		if (Columns != null && Columns.Count > 0)
		{
			form.Append("        <thead>\r\n");
			form.Append("            <tr>\r\n");
			foreach (VListTableColumn col in Columns)
			{
				form.AppendFormat("                <th lay-data=\"{{field:'{0}'{2}{3}}}\">{1}</th>\r\n", col.id, col.text
					, col.width == 0 || !col.visible ? ",hide:true" : string.Format(",width:{0}", Math.Max(col.width, COLUMN_MIN_WIDTH))
					, !string.IsNullOrEmpty(col.align) ? string.Format(",align:'{0}'", col.align) : "");
			}
			form.Append("            </tr>\r\n");
			form.Append("        </thead>\r\n");
		}
		form.Append("        <tbody></tbody>\r\n");
		form.Append("    </table>\r\n");
	}
}

class VListTableColumn
{
	public string text { get; set; }
	public string id{get;set;}
	public float width{get;set;}
	public string align { get; set; }
	public bool visible { get; set; }
}

class ControlComparer : IComparer<IControl>
{
	public static readonly ControlComparer Instance = new ControlComparer();
	public int Compare(IControl x, IControl y)
	{
		return x.x1 - y.x1;
	}
}

class RowComparer : IComparer<Row>
{
	public static readonly RowComparer Instance = new RowComparer();
	public int Compare(Row x, Row y)
	{
		return x.y1 - y.y1;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闪耀星星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值