public override void Process(TagHelperContext context, TagHelperOutput output) { (_htmlHelper as IViewContextAware).Contextualize(ViewContext); var prefix = ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix; output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; if (Width > 0) { output.Attributes.Add("style", $"width:{Width}px"); } var htmlId = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : _htmlHelper.IdForModel() + "_" + ModelItem.Metadata.PropertyName; var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName; var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last(); if (ItemList == null && ItemList2 == null) { var comboDataInfo = GetComboBoxDataSource(ModelItem.ModelExplorer); ItemList = comboDataInfo.ListItems; } var isSelected = false; var sbOptions = new StringBuilder(); if (ItemList != null) { if (ItemList.DataGroupField.IsEmpty()) { foreach (var item in ItemList) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); isSelected = true; } option.InnerHtml.AppendHtml(item.Text); sbOptions.AppendLine(option.GetString()); } } else { var gList = ItemList.Select(d => d.Group).Distinct().OrderBy(d => d.Name).ToList(); foreach (var g in gList) { var optionGroup = new TagBuilder("optgroup"); optionGroup.Attributes.Add("label", g.Name); var gItemList = ItemList.Where(d => d.Group.Name == g.Name).ToList(); foreach (var item in gItemList) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); isSelected = true; } option.InnerHtml.AppendHtml(item.Text); optionGroup.InnerHtml.AppendHtml(option.GetString()); } sbOptions.AppendLine(optionGroup.GetString()); } } } else if (ItemList2 != null) { foreach (var item in ItemList2) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); isSelected = true; } option.InnerHtml.AppendHtml(item.Text); sbOptions.AppendLine(option.GetString()); } } var select = new TagBuilder("select"); select.AddCssClass("form-control form-control-sm"); if (!CssClass.IsEmpty()) { select.AddCssClass(CssClass); } select.Attributes.Add("name", htmlName); select.Attributes.Add("id", htmlId); if (MultiSelect) { select.Attributes.Add("multiple", "multiple"); } if (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) { var requiredAttr = Digger2.GetMetaAttr <RequiredAttribute>(ModelItem.Metadata); var errMsg = ""; if (requiredAttr != null) { errMsg = requiredAttr.ErrorMessage; if (errMsg.IsEmpty() && requiredAttr.ErrorMessageResourceType != null) { var rm = new ResourceManager(requiredAttr.ErrorMessageResourceType); var rsm = rm.GetString(requiredAttr.ErrorMessageResourceName); if (rsm != null && !rsm.IsEmpty()) { errMsg = string.Format(rsm, labelText); } } } select.Attributes.Add("data-val", "true"); select.Attributes.Add("data-val-required", requiredAttr != null ? errMsg : "Gereklidir" ); } select.InnerHtml.AppendHtml(sbOptions.ToString()); var setToNull = isSelected ? "" : @$ "$(" "#{htmlId}" ").val(null); $(" "#{htmlId}" ").trigger(" "change" ");"; string js = DisableJs ? "" : $@" <script type=""text/javascript""> $(function () {{ $(""#{htmlId}"").select2({{ language: ""tr"", placeholder: ""{(PlaceHolderText.IsEmpty() ? "" : PlaceHolderText)}"", {(MultiSelect ? "multiple:true," : "")} {(Tag ? "tags:true," : "")} {(ReadOnly ? "disabled:true," : "")} {(CanClearSelect ? "allowClear: true" : "")} }}); {setToNull} }}); </script>"; if (DisableEditorTemplate) { output.Attributes.Add("id", "div" + htmlId); output.Attributes.Add("class", $"col-sm-{EditorColCount}"); if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <div class=""input-group input-group-sm""> {select.GetString()} <div class=""input-group-append""> <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe'><i class='fas fa-plus'></i></a> </div> {js} </div>"); } else { output.Content.SetHtmlContent($@"{select.GetString()} {js}"); } } else { output.Attributes.Add("class", "form-group row"); output.Attributes.Add("id", "div" + htmlId); var irq = (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) || (IsRequired.HasValue && IsRequired.Value); var requiredHtml = irq ? $"<span class=\"{RequiredCssClass}\">{Symbol}</span>" : ""; var metaHtml = irq ? $"<span class=\"field-validation-valid help-block\" data-valmsg-for=\"{htmlId}\" data-valmsg-replace=\"true\"></span>" : ""; if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <label class=""col-{LabelColCount} col-form-label col-form-label-sm"">{labelText} {requiredHtml}</label> <div class=""col-{EditorColCount}""> <div class=""input-group input-group-sm""> {select.GetString()} <div class=""input-group-append""> <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe'><i class='fas fa-plus'></i></a> </div> </div> {metaHtml} </div> {js}"); } else { output.Content.SetHtmlContent($@" <label class=""col-{LabelColCount} col-form-label col-form-label-sm"">{labelText} {requiredHtml}</label> <div class=""col-{EditorColCount}""> {select.GetString()} {metaHtml} </div> {js}"); } } }
public override void Process(TagHelperContext context, TagHelperOutput output) { (_htmlHelper as IViewContextAware).Contextualize(ViewContext); var prefix = ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix; var htmlId = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : _htmlHelper.IdForModel() != ModelItem.Metadata.PropertyName ? _htmlHelper.IdForModel() + "_" + ModelItem.Metadata.PropertyName : ModelItem.Metadata.PropertyName; var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName; var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last(); if (ItemList == null) { var comboDataInfo = GetComboBoxDataSource(ModelItem.ModelExplorer); ItemList = comboDataInfo.ListItems; if (comboDataInfo.CanSelect) { if (OptionLabel.IsEmpty()) { OptionLabel = UI.Select; } } else { OptionLabel = null; } } var sbOptions = new StringBuilder(); if (!OptionLabel.IsEmpty()) { sbOptions.AppendLine(string.Concat("<option value=\"\">", OptionLabel, "</option>")); } if (ItemList != null) { foreach (var item in ItemList) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); } option.InnerHtml.AppendHtml(item.Text); sbOptions.AppendLine(option.GetString()); } } else if (ItemList2 != null) { foreach (var item in ItemList2) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); } option.InnerHtml.AppendHtml(item.Text); sbOptions.AppendLine(option.GetString()); } } if (DisableEditorTemplate) { output.TagName = "select"; output.TagMode = TagMode.StartTagAndEndTag; if (!DataUrl.IsEmpty()) { output.Attributes.Add("data-url", DataUrl); } output.Attributes.Add("name", htmlName); output.Attributes.Add("id", htmlId); if (ModelItem.Metadata.IsRequired) { var requiredAttr = Digger2.GetMetaAttr <RequiredAttribute>(ModelItem.Metadata); var errMsg = ""; if (requiredAttr != null) { errMsg = requiredAttr.ErrorMessage; if (errMsg.IsEmpty() && requiredAttr.ErrorMessageResourceType != null) { var rm = new ResourceManager(requiredAttr.ErrorMessageResourceType); var rsm = rm.GetString(requiredAttr.ErrorMessageResourceName); if (rsm != null && !rsm.IsEmpty()) { errMsg = string.Format(rsm, labelText); } } } output.Attributes.Add("data-val", "true"); output.Attributes.Add("data-val-required", requiredAttr != null ? errMsg : "Gereklidir" ); } output.Content.SetHtmlContent(sbOptions.ToString()); } else { output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.Add("class", "form-group"); output.Attributes.Add("id", "div" + htmlId); var select = new TagBuilder("select"); if (!DataUrl.IsEmpty()) { select.Attributes.Add("data-url", DataUrl); } select.AddCssClass("form-control input-sm"); if (!CssClass.IsEmpty()) { select.AddCssClass(CssClass); } select.Attributes.Add("name", htmlName); select.Attributes.Add("id", htmlId); var irq = (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) || (IsRequired.HasValue && IsRequired.Value); var requiredHtml = irq ? $"<span class=\"{RequiredCssClass}\">{Symbol}</span>" : ""; var metaHtml = irq ? $"<span class=\"field-validation-valid help-block\" data-valmsg-for=\"{htmlId}\" data-valmsg-replace=\"true\"></span>" : ""; if (irq) { var requiredAttr = Digger2.GetMetaAttr <RequiredAttribute>(ModelItem.Metadata); var errMsg = ""; if (requiredAttr != null) { errMsg = requiredAttr.ErrorMessage; if (errMsg.IsEmpty() && requiredAttr.ErrorMessageResourceType != null) { var rm = new ResourceManager(requiredAttr.ErrorMessageResourceType); var rsm = rm.GetString(requiredAttr.ErrorMessageResourceName); if (rsm != null && !rsm.IsEmpty()) { errMsg = string.Format(rsm, labelText); } } } select.Attributes.Add("data-val", "true"); select.Attributes.Add("data-val-required", requiredAttr != null ? errMsg : "Gereklidir" ); } select.InnerHtml.AppendHtml(sbOptions.ToString()); var addHtml = ""; if (!AddNewUrl.IsEmpty()) { addHtml = $@"<div class='col-xs-1'> <a href='{AddNewUrl}' class='btn btn-xs btn-primary fancyboxAdd fancybox.iframe'><i class='fa fa-plus'></i></a> </div>"; EditorColCount--; } var editorTemplate = $@" <label class=""col-xs-{LabelColCount} control-label"">{labelText} {requiredHtml}</label> <div class=""col-xs-{EditorColCount}""> {select.GetString()} {metaHtml} </div> {addHtml}"; output.Content.SetHtmlContent(editorTemplate); } }
public string GetCssClass() { return(CssClass.IsEmpty() ? "" : " " + BuiltInFunction.EncodeHtmlAttribute(CssClass)); }