Exemplo n.º 1
0
        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;
            var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName;

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last();

            if (!FieldName.IsEmpty())
            {
                htmlId   = FieldName;
                htmlName = FieldName;
            }
            if (!LabelText.IsEmpty())
            {
                labelText = LabelText;
            }

            var initalJson  = string.Empty;
            var selectionJs = string.Empty;

            if (ItemList != null && ItemList.Count > 0)
            {
                var json     = "[";
                var fistItem = true;
                foreach (var value in ItemList)
                {
                    if (!fistItem)
                    {
                        json += ",";
                    }
                    json    += "{id: " + value.Id + ", text: \"" + value.Name + "\"}";
                    fistItem = false;
                }
                json      += "]";
                initalJson = string.Format("$('#{0}').select2('data', {1});", htmlId, json);
            }
            else
            {
                selectionJs = $@"initSelection: function(element, callback) {{
            var id = $(element).val();
            if (id != '') {{
                $.ajax('{LoaderUrl}/' + id, {{
                    dataType: 'json'
                }}).done(function(data) {{ callback(data); }});
            }}
        }}";
            }

            string js = DisableJs
                ? ""
                : $@"<script type=""text/javascript"">
$(function () {{
    $(""#{htmlId}"").select2({{
        placeholder: ""{PlaceHolderText}"",
        {(CanClearSelect ? "allowClear: true, " : "")}
        {(MultiSelect ? "multiple:true," : "")}
        minimumInputLength: 1,
        ajax: {{
            url: '{Url}',
            dataType: 'json',
            data: function (term, page) {{
                return {{
                    q: term,
                    {(!ParentComboBox.IsEmpty() ? $"id: $('#{ParentComboBox}').val()," : "")}
                }};
            }},
            results: function (data, page) {{
                return {{ results: data }};
            }}
        }},
        {selectionJs}
    }});
    {initalJson}
}});
</script>";

            var valHtml = ModelItem != null ? "value='" + ModelItem.Model + "'" : "";

            if (DisableEditorTemplate)
            {
                if (!AddNewUrl.IsEmpty())
                {
                    var sep = "?";
                    if (AddNewUrl.Contains("?"))
                    {
                        sep = "&";
                    }
                    AddNewUrl += sep + "editorId=" + htmlId;
                    output.Content.SetHtmlContent($@"
<div class=""input-group"">
    <input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} />
    <span class=""input-group-btn"">
        <a href='{AddNewUrl}' class='btn btn-xs btn-primary btnAddPopup fancyboxAdd fancybox.iframe'><i class='fa fa-plus'></i></a>
    </span>
    {js}
</div>");
                }
                else
                {
                    output.Content.SetHtmlContent($@"
<input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} />
{js}");
                }
            }
            else
            {
                if (!DisableFormGroup)
                {
                    output.Attributes.Add("class", "form-group");
                    output.Attributes.Add("id", "div" + htmlId);
                }
                else
                {
                    output.TagName = "span";
                }

                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-xs-{LabelColCount} control-label"">{labelText} {requiredHtml}</label>
<div class=""col-xs-{EditorColCount}"">
    <div class=""input-group"">
        <input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} />
        <span class=""input-group-btn"">
            <a href='{AddNewUrl}' class='btn btn-xs btn-primary btnAddPopup fancyboxAdd fancybox.iframe'><i class='fa fa-plus'></i></a>
        </span>
    </div>
    {metaHtml}
</div>
{js}");
                }
                else
                {
                    output.Content.SetHtmlContent($@"
<label class=""col-xs-{LabelColCount} control-label"">{labelText} {requiredHtml}</label>
<div class=""col-xs-{EditorColCount}"">
    <input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} />
    {metaHtml}
</div>
{js}");
                }
            }
        }
Exemplo n.º 2
0
        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;
            var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName;

            if (ModelParentItem != null)
            {
                var parentHtmlId = prefix.IsEmpty() ? ModelParentItem.Metadata.PropertyName : _htmlHelper.IdForModel() + "_" + ModelParentItem.Metadata.PropertyName;
                ParentComboBox = parentHtmlId;
            }


            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            if (Width > 0)
            {
                output.Attributes.Add("style", $"width:{Width}px");
            }
            var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last();

            var selectionJs = string.Empty;

            if (SelectedValues != null && SelectedValues.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (var s in SelectedValues)
                {
                    sb.AppendLine($@"$('#{htmlId}').append(new Option('{s.Name}', {s.Id}, true, true));");
                }
                sb.AppendLine($@"$('#{htmlId}').trigger('change');");
                selectionJs = sb.ToString();
            }
            else
            {
                var valId = ModelItem != null && ModelItem.Model != null?ModelItem.Model.ToString() : "";

                if (!valId.IsEmpty())
                {
                    selectionJs = $@"
 $.ajax('{LoaderUrl}/{valId}', {{dataType: 'json'}}).done(function(data) {{ 
    var option = new Option(data.text, data.id, true, true);
    $(""#{htmlId}"").append(option).trigger('change');
}});";
                }
            }

            string js = DisableJs
                ? ""
                : $@"<script type=""text/javascript"">
$(function () {{
    $(""#{htmlId}"").select2({{
        placeholder: ""{PlaceHolderText}"",
        language: ""tr"",
        {(CanClearSelect ? "allowClear: true, " : "")}
        {(MultiSelect ? "multiple:true," : "")}
        {(ReadOnly ? "disabled:true," : "")}
        minimumInputLength: 1,
        ajax: {{
            url: '{Url}',
            data: function (params) {{
                return {{
                    q: params.term,
                    {(!ParentComboBox.IsEmpty() ? $"id: $('#{ParentComboBox}').val()," : "")}
                }};
            }},
            processResults: function (data) {{
                return {{ results: data }};
            }}
        }},
    }});
    {selectionJs}
}});
</script>";

            var valHtml = ModelItem != null ? "value='" + ModelItem.Model + "'" : "";

            if (DisableEditorTemplate)
            {
                if (!DisableColSize)
                {
                    output.Attributes.Add("class", $"col-sm-{EditorColCount}");
                }
                output.Attributes.Add("id", "div" + htmlId);
                if (!AddNewUrl.IsEmpty())
                {
                    var sep = "?";
                    if (AddNewUrl.Contains("?"))
                    {
                        sep = "&";
                    }
                    AddNewUrl += sep + "editorId=" + htmlId;
                    output.Content.SetHtmlContent($@"
<div class=""input-group input-group-sm"">
    <select id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml}>
    </select>
    <div class=""input-group-append"">
        <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe {AddNewButtonCssClass}'><i class='fas fa-plus'></i></a>
    </div>
    {js}
</div>");
                }
                else
                {
                    output.Content.SetHtmlContent($@"
<select id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml}>
</select>
{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 id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml}>
        </select>
        <div class=""input-group-append"">
            <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe {AddNewButtonCssClass}'><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 col-form-label-sm"">{labelText} {requiredHtml}</label>
<div class=""col-{EditorColCount}"">
    <select id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml} >
    </select>
    {metaHtml}
</div>
{js}");
                }
            }
        }