Ejemplo n.º 1
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return;
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return;
            }

            #region << Init Prepend and Append >>
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var prependTaghelper = htmlDoc.DocumentNode.Descendants("wv-field-prepend");
            var appendTagHelper  = htmlDoc.DocumentNode.Descendants("wv-field-append");

            foreach (var node in prependTaghelper)
            {
                PrependHtml.Add(node.InnerHtml.ToString());
            }

            foreach (var node in appendTagHelper)
            {
                AppendHtml.Add(node.InnerHtml.ToString());
            }

            #endregion


            #region << Validate Options >>
            if (Options == null)
            {
                var divEl = new TagBuilder("div");
                divEl.AddCssClass("form-control-plaintext erp-plain-text");

                var errorListEl = new TagBuilder("ul");
                errorListEl.AddCssClass("erp-error-list list-unstyled");

                var errorEl = new TagBuilder("li");
                errorEl.AddCssClass("go-red");

                var iconEl = new TagBuilder("span");
                iconEl.AddCssClass("fa fa-fw fa-exclamation");

                errorEl.InnerHtml.AppendHtml(iconEl);
                errorEl.InnerHtml.Append($"Error:Select options cannot be null");

                errorListEl.InnerHtml.AppendHtml(errorEl);
                divEl.InnerHtml.AppendHtml(errorListEl);
                output.Content.AppendHtml(divEl);
                //Finally
                if (SubInputEl != null)
                {
                    output.PostContent.AppendHtml(SubInputEl);
                }
                return;
            }
            #endregion

            #endregion

            #region << Render >>
            if (Mode == FieldRenderMode.Form)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group");
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        foreach (var htmlString in PrependHtml)
                        {
                            prependEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);
                    }
                    //Hidden Control to be posted when no option is selected
                    var dummyHiddenInput = new TagBuilder("input");
                    dummyHiddenInput.Attributes.Add("type", "hidden");
                    dummyHiddenInput.Attributes.Add("id", $"dummy-{FieldId}");
                    if (Value == null || ((List <string>)Value).Count == 0)
                    {
                        dummyHiddenInput.Attributes.Add("name", $"{Name}");
                    }
                    dummyHiddenInput.Attributes.Add("value", "");
                    inputGroupEl.InnerHtml.AppendHtml(dummyHiddenInput);

                    //Control
                    var selectEl = new TagBuilder("select");
                    selectEl.Attributes.Add("id", $"input-{FieldId}");
                    //Name will be attached and removed depending on if there is selected values or not, in order for the dummy to post
                    if (Value != null && ((List <string>)Value).Count > 0)
                    {
                        selectEl.Attributes.Add("name", $"{Name}");
                    }
                    selectEl.Attributes.Add("data-field-name", $"{Name}");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-multiselect invisible");
                    if (ValidationErrors.Count > 0)
                    {
                        inputElCssClassList.Add("is-invalid");
                    }
                    selectEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));
                    if (Required)
                    {
                        selectEl.Attributes.Add("required", null);
                    }

                    selectEl.Attributes.Add("multiple", "multiple");

                    foreach (var option in Options)
                    {
                        var optionEl = new TagBuilder("option");
                        optionEl.Attributes.Add("value", option.Value);
                        if (Value != null && ((List <string>)Value).Any(x => x == option.Value))
                        {
                            optionEl.Attributes.Add("selected", null);
                        }
                        optionEl.InnerHtml.Append(option.Label);
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    inputGroupEl.InnerHtml.AppendHtml(selectEl);
                    //Append
                    if (AppendHtml.Count > 0)
                    {
                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        foreach (var htmlString in AppendHtml)
                        {
                            appendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    }

                    output.Content.AppendHtml(inputGroupEl);

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldMultiSelect) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldMultiSelect) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("form.js", "WebVella.Erp.Web.TagHelpers.WvFieldMultiSelect");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        //scriptEl.InnerHtml.AppendHtml(scriptContent);
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldMultiSelect) + "-form"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							MultiSelectFormInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);
                    scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName);
                    scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString());

                    var fieldConfig = new WvFieldMultiSelectConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == FieldAccess.FullAndCreate ? true : false
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion


                    //TODO Implement CanAddValues
                    //@if (config.CanAddValues ?? false)
                    //{
                    //	<div id="*****@*****.**" class="modal" data-backdrop="true">
                    //		<div class="modal-dialog" name="add-option">
                    //			<div class="modal-content">
                    //				<div class="modal-header">
                    //					<h5 class="modal-title">Add @(fieldMeta.Label)</h5>
                    //				</div>
                    //				<div class="modal-body">
                    //					<div class="alert alert-danger d-none"></div>
                    //					<div class="form-group">
                    //						<label class="control-label">New value</label>
                    //						<input class="form-control erp-multiselect add-option-input" value="" required/>
                    //					</div>
                    //				</div>
                    //				<div class="modal-footer">
                    //					<button class="btn btn-primary btn-sm" type="submit"><i class="fa fa-plus-circle"></i> Add</button>
                    //					<button class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
                    //				</div>
                    //			</div>
                    //		</div>
                    //	</div>

                    //}
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    //Have to render it as a normal select as readonly prop does not work with select 2. Also in order for the select not to work it should be disabled,
                    //which will not pass the value, this the hidden input

                    var hiddenInput = new TagBuilder("input");
                    hiddenInput.Attributes.Add("type", "hidden");
                    hiddenInput.Attributes.Add("id", $"input-{FieldId}");
                    hiddenInput.Attributes.Add("name", $"{Name}");
                    hiddenInput.Attributes.Add("value", (Value ?? "").ToString());
                    output.Content.AppendHtml(hiddenInput);


                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group");
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        foreach (var htmlString in PrependHtml)
                        {
                            prependEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);
                    }


                    //Control
                    var selectEl = new TagBuilder("select");
                    selectEl.Attributes.Add("id", $"select-{FieldId}");
                    selectEl.Attributes.Add("readonly", null);
                    selectEl.Attributes.Add("disabled", $"disabled");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-multiselect invisible");
                    if (ValidationErrors.Count > 0)
                    {
                        inputElCssClassList.Add("is-invalid");
                    }
                    selectEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));
                    if (Required)
                    {
                        selectEl.Attributes.Add("required", null);
                    }

                    selectEl.Attributes.Add("multiple", "multiple");

                    foreach (var option in Options)
                    {
                        var optionEl = new TagBuilder("option");
                        optionEl.Attributes.Add("value", option.Value);
                        if (Value != null && ((List <string>)Value).Any(x => x == option.Value))
                        {
                            optionEl.Attributes.Add("selected", null);
                        }
                        optionEl.InnerHtml.Append(option.Label);
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    inputGroupEl.InnerHtml.AppendHtml(selectEl);
                    //Append
                    if (AppendHtml.Count > 0)
                    {
                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        foreach (var htmlString in AppendHtml)
                        {
                            appendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    }

                    output.Content.AppendHtml(inputGroupEl);
                }
            }
            else if (Mode == FieldRenderMode.Display)
            {
                if (((List <string>)Value).Count > 0)
                {
                    var select2ContainerEl = new TagBuilder("span");
                    select2ContainerEl.Attributes.Add("id", $"input-{FieldId}");
                    select2ContainerEl.AddCssClass("select2 select2-container select2-container--default d-block disabled clean");

                    var select2SelectionEl = new TagBuilder("span");
                    select2SelectionEl.AddCssClass("selection");

                    var select2SelectionInnerEl = new TagBuilder("span");
                    select2SelectionInnerEl.AddCssClass("select2-selection select2-selection--multiple");

                    var select2SelectionUlEl = new TagBuilder("ul");
                    select2SelectionUlEl.AddCssClass("select2-selection__rendered");

                    foreach (var dataKey in Value)
                    {
                        var optionEl = new TagBuilder("li");
                        var option   = Options.FirstOrDefault(x => x.Value == dataKey);
                        if (option == null)
                        {
                            optionEl.AddCssClass("select2-selection__choice missing");
                            optionEl.Attributes.Add("title", dataKey);
                            optionEl.Attributes.Add("data-key", dataKey);
                            optionEl.InnerHtml.Append(dataKey);
                            if (Value != null && (Value.ToString() != ""))
                            {
                                var optionElIcon = new TagBuilder("span");
                                optionElIcon.AddCssClass("fa fa-fw fa-exclamation-circle go-red");
                                optionElIcon.Attributes.Add("title", "the value is not supported by this field anymore");
                                optionEl.InnerHtml.AppendHtml(optionElIcon);
                            }
                        }
                        else
                        {
                            optionEl.AddCssClass("select2-selection__choice");
                            optionEl.Attributes.Add("title", option.Label);
                            optionEl.Attributes.Add("data-key", dataKey);
                            optionEl.InnerHtml.Append(option.Label);
                        }
                        select2SelectionUlEl.InnerHtml.AppendHtml(optionEl);
                    }

                    select2SelectionInnerEl.InnerHtml.AppendHtml(select2SelectionUlEl);
                    select2SelectionEl.InnerHtml.AppendHtml(select2SelectionInnerEl);
                    select2ContainerEl.InnerHtml.AppendHtml(select2SelectionEl);

                    output.Content.AppendHtml(select2ContainerEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == FieldRenderMode.Simple)
            {
                output.SuppressOutput();
                var valueLabels = new List <string>();
                foreach (var dataKey in (List <string>)Value)
                {
                    var option = Options.FirstOrDefault(x => x.Value == dataKey);
                    if (option == null)
                    {
                        valueLabels.Add(dataKey);
                    }
                    else
                    {
                        valueLabels.Add(option.Label);
                    }
                }

                output.Content.AppendHtml(String.Join(", ", valueLabels));
                return;
            }
            else if (Mode == FieldRenderMode.InlineEdit)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    #region << View Wrapper >>
                    {
                        var viewWrapperEl = new TagBuilder("div");
                        viewWrapperEl.AddCssClass("input-group view-wrapper");
                        viewWrapperEl.Attributes.Add("title", "double click to edit");
                        viewWrapperEl.Attributes.Add("id", $"view-{FieldId}");

                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var viewInputPrepend = new TagBuilder("span");
                            viewInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);
                        }
                        //Control
                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-multiselect");

                        var select2ContainerEl = new TagBuilder("span");
                        select2ContainerEl.AddCssClass("select2 select2-container select2-container--default d-block disabled");

                        var select2SelectionEl = new TagBuilder("span");
                        select2SelectionEl.AddCssClass("selection");

                        var select2SelectionInnerEl = new TagBuilder("span");
                        select2SelectionInnerEl.AddCssClass("select2-selection select2-selection--multiple");

                        var select2SelectionUlEl = new TagBuilder("ul");
                        select2SelectionUlEl.AddCssClass("select2-selection__rendered");

                        foreach (var dataKey in Value)
                        {
                            var optionEl = new TagBuilder("li");
                            var option   = Options.FirstOrDefault(x => x.Value == dataKey);
                            if (option == null)
                            {
                                optionEl.AddCssClass("select2-selection__choice missing");
                                optionEl.Attributes.Add("title", dataKey);
                                optionEl.Attributes.Add("data-key", dataKey);
                                optionEl.InnerHtml.Append(dataKey);
                                if (Value != null && (Value.ToString() != ""))
                                {
                                    var optionElIcon = new TagBuilder("span");
                                    optionElIcon.AddCssClass("fa fa-fw fa-exclamation-circle go-red");
                                    optionElIcon.Attributes.Add("title", "the value is not supported by this field anymore");
                                    optionEl.InnerHtml.AppendHtml(optionElIcon);
                                }
                            }
                            else
                            {
                                optionEl.AddCssClass("select2-selection__choice");
                                optionEl.Attributes.Add("title", option.Label);
                                optionEl.Attributes.Add("data-key", dataKey);
                                optionEl.InnerHtml.Append(option.Label);
                            }
                            select2SelectionUlEl.InnerHtml.AppendHtml(optionEl);
                        }

                        select2SelectionInnerEl.InnerHtml.AppendHtml(select2SelectionUlEl);
                        select2SelectionEl.InnerHtml.AppendHtml(select2SelectionInnerEl);
                        select2ContainerEl.InnerHtml.AppendHtml(select2SelectionEl);
                        viewFormControlEl.InnerHtml.AppendHtml(select2ContainerEl);
                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        //Append
                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append");
                        foreach (var htmlString in AppendHtml)
                        {
                            viewInputActionEl.InnerHtml.AppendHtml(htmlString);
                        }
                        viewInputActionEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white action' title='edit'><i class='fa fa-fw fa-pencil-alt'></i></button>");
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl);
                        output.Content.AppendHtml(viewWrapperEl);
                    }
                    #endregion

                    #region << Edit Wrapper>>
                    {
                        var editWrapperEl = new TagBuilder("div");
                        editWrapperEl.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapperEl.Attributes.Add("style", $"display:none;");
                        editWrapperEl.AddCssClass("edit-wrapper");

                        var editInputGroupEl = new TagBuilder("div");
                        editInputGroupEl.AddCssClass("input-group");
                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var editInputPrepend = new TagBuilder("span");
                            editInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                editInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            editInputGroupEl.InnerHtml.AppendHtml(editInputPrepend);
                        }
                        //Control
                        var formControl         = new TagBuilder("div");
                        var inputElCssClassList = new List <string>();
                        inputElCssClassList.Add("form-control erp-multiselect");
                        if (ValidationErrors.Count > 0)
                        {
                            inputElCssClassList.Add("is-invalid");
                        }
                        formControl.Attributes.Add("class", String.Join(' ', inputElCssClassList));

                        var selectEl = new TagBuilder("select");
                        selectEl.Attributes.Add("id", $"input-{FieldId}");
                        selectEl.Attributes.Add("name", $"{Name}");

                        if (Required)
                        {
                            selectEl.Attributes.Add("required", null);
                        }
                        selectEl.Attributes.Add("multiple", "multiple");
                        selectEl.Attributes.Add("data-original-value", JsonConvert.SerializeObject((Value ?? "").ToString()));


                        foreach (var option in Options)
                        {
                            var optionEl = new TagBuilder("option");
                            optionEl.Attributes.Add("value", option.Value);
                            if (((List <string>)Value).Any(x => x == option.Value))
                            {
                                optionEl.Attributes.Add("selected", null);
                            }
                            optionEl.InnerHtml.Append(option.Label);
                            selectEl.InnerHtml.AppendHtml(optionEl);
                        }

                        formControl.InnerHtml.AppendHtml(selectEl);
                        editInputGroupEl.InnerHtml.AppendHtml(formControl);

                        //Append
                        var editInputGroupAppendEl = new TagBuilder("span");
                        editInputGroupAppendEl.AddCssClass("input-group-append");

                        foreach (var htmlString in AppendHtml)
                        {
                            editInputGroupAppendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white save' title='save'><i class='fa fa-fw fa-check go-green'></i></button>");
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white cancel' title='cancel'><i class='fa fa-fw fa-times go-gray'></i></button>");
                        editInputGroupEl.InnerHtml.AppendHtml(editInputGroupAppendEl);

                        editWrapperEl.InnerHtml.AppendHtml(editInputGroupEl);

                        output.Content.AppendHtml(editWrapperEl);
                    }
                    #endregion

                    var jsCompressor = new JavaScriptCompressor();
                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldMultiSelect) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldMultiSelect) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("inline-edit.js", "WebVella.Erp.Web.TagHelpers.WvFieldMultiSelect");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldMultiSelect) + "-inline-edit"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							MultiSelectInlineEditInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",""{{RecordId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);
                    scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName);
                    scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString());

                    var fieldConfig = new WvFieldMultiSelectConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == FieldAccess.FullAndCreate ? true : false
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    var divEl = new TagBuilder("div");
                    divEl.AddCssClass("input-group");
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend");
                        foreach (var htmlString in PrependHtml)
                        {
                            viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                        }
                        divEl.InnerHtml.AppendHtml(viewInputPrepend);
                    }
                    //Control
                    var viewFormControlEl = new TagBuilder("div");
                    viewFormControlEl.AddCssClass("form-control erp-multiselect");

                    var select2ContainerEl = new TagBuilder("span");
                    select2ContainerEl.AddCssClass("select2 select2-container select2-container--default d-block disabled");

                    var select2SelectionEl = new TagBuilder("span");
                    select2SelectionEl.AddCssClass("selection");

                    var select2SelectionInnerEl = new TagBuilder("span");
                    select2SelectionInnerEl.AddCssClass("select2-selection select2-selection--multiple");

                    var select2SelectionUlEl = new TagBuilder("ul");
                    select2SelectionUlEl.AddCssClass("select2-selection__rendered");

                    foreach (var dataKey in Value)
                    {
                        var optionEl = new TagBuilder("li");
                        var option   = Options.FirstOrDefault(x => x.Value == dataKey);
                        if (option == null)
                        {
                            optionEl.AddCssClass("select2-selection__choice missing");
                            optionEl.Attributes.Add("title", dataKey);
                            optionEl.Attributes.Add("data-key", dataKey);
                            optionEl.InnerHtml.Append(dataKey);
                            if (Value != null && (Value.ToString() != ""))
                            {
                                var optionElIcon = new TagBuilder("span");
                                optionElIcon.AddCssClass("fa fa-fw fa-exclamation-circle go-red");
                                optionElIcon.Attributes.Add("title", "the value is not supported by this field anymore");
                                optionEl.InnerHtml.AppendHtml(optionElIcon);
                            }
                        }
                        else
                        {
                            optionEl.AddCssClass("select2-selection__choice");
                            optionEl.Attributes.Add("title", option.Label);
                            optionEl.Attributes.Add("data-key", dataKey);
                            optionEl.InnerHtml.Append(option.Label);
                        }
                        select2SelectionUlEl.InnerHtml.AppendHtml(optionEl);
                    }
                    select2SelectionInnerEl.InnerHtml.AppendHtml(select2SelectionUlEl);
                    select2SelectionEl.InnerHtml.AppendHtml(select2SelectionInnerEl);
                    select2ContainerEl.InnerHtml.AppendHtml(select2SelectionEl);
                    viewFormControlEl.InnerHtml.AppendHtml(select2ContainerEl);
                    divEl.InnerHtml.AppendHtml(viewFormControlEl);

                    //Append
                    var appendActionSpan = new TagBuilder("span");
                    appendActionSpan.AddCssClass("input-group-append");
                    foreach (var htmlString in AppendHtml)
                    {
                        appendActionSpan.InnerHtml.AppendHtml(htmlString);
                    }
                    appendActionSpan.InnerHtml.AppendHtml("<button type=\"button\" disabled class='btn btn-white action' title='locked'><i class='fa fa-fw fa-lock'></i></button>");
                    divEl.InnerHtml.AppendHtml(appendActionSpan);

                    output.Content.AppendHtml(divEl);
                }
            }
            #endregion


            //Finally
            if (SubInputEl != null)
            {
                output.PostContent.AppendHtml(SubInputEl);
            }

            return;
        }
Ejemplo n.º 2
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return;
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return;
            }

            #region << Init Prepend and Append >>
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var prependTaghelper = htmlDoc.DocumentNode.Descendants("wv-field-prepend");
            var appendTagHelper  = htmlDoc.DocumentNode.Descendants("wv-field-append");

            foreach (var node in prependTaghelper)
            {
                PrependHtml.Add(node.InnerHtml.ToString());
            }

            foreach (var node in appendTagHelper)
            {
                AppendHtml.Add(node.InnerHtml.ToString());
            }

            #endregion

            //Apply currency dec places
            if (Value != null)
            {
                Value = Math.Round(Convert.ToDecimal(Value), DecimalDigits + 2);
            }

            if (Step == null)
            {
                Step = (decimal)0.01;
            }

            #endregion

            #region << Render >>
            if (Mode == FieldRenderMode.Form)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group");

                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        foreach (var htmlString in PrependHtml)
                        {
                            prependEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);
                    }
                    //Control
                    var inputEl             = new TagBuilder("input");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-percent");
                    inputEl.Attributes.Add("type", "number");
                    var fakeValueString = "";
                    if (Value != null)
                    {
                        fakeValueString = (((decimal)Value) * 100).ToString("N" + DecimalDigits);
                    }
                    inputEl.Attributes.Add("value", fakeValueString);
                    inputEl.Attributes.Add("id", $"fake-{FieldId}");
                    if (Required)
                    {
                        inputEl.Attributes.Add("required", null);
                    }
                    if (!String.IsNullOrWhiteSpace(Placeholder))
                    {
                        inputEl.Attributes.Add("placeholder", Placeholder);
                    }
                    if (Min != null)
                    {
                        inputEl.Attributes.Add("min", Min.ToString());
                    }
                    if (Max != null)
                    {
                        inputEl.Attributes.Add("max", Max.ToString());
                    }
                    if (Step != null)
                    {
                        inputEl.Attributes.Add("step", Step.ToString());
                    }

                    if (ValidationErrors.Count > 0)
                    {
                        inputElCssClassList.Add("is-invalid");
                    }
                    inputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));
                    inputGroupEl.InnerHtml.AppendHtml(inputEl);

                    //Append
                    if (AppendHtml.Count > 0)
                    {
                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        foreach (var htmlString in AppendHtml)
                        {
                            appendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    }

                    output.Content.AppendHtml(inputGroupEl);

                    var hiddenInputEl = new TagBuilder("input");
                    hiddenInputEl.Attributes.Add("name", Name);
                    hiddenInputEl.Attributes.Add("id", $"input-{FieldId}");
                    hiddenInputEl.Attributes.Add("type", "hidden");
                    var hiddenValueString = "";
                    if (Value != null)
                    {
                        hiddenValueString = ((decimal)Value).ToString("N" + (DecimalDigits + 2));
                    }
                    hiddenInputEl.Attributes.Add("value", hiddenValueString);
                    output.Content.AppendHtml(hiddenInputEl);


                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldPercent) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldPercent) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("form.js", "WebVella.Erp.Web.TagHelpers.WvFieldPercent");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldPercent) + "-form"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							PercentFormInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",""{{RecordId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);
                    scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName);
                    scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString());

                    var fieldConfig = new WvFieldPercentConfig()
                    {
                        ApiUrl        = ApiUrl,
                        CanAddValues  = Access == FieldAccess.FullAndCreate ? true : false,
                        DecimalDigits = DecimalDigits
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group");

                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        foreach (var htmlString in PrependHtml)
                        {
                            prependEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);
                    }
                    //Control
                    var inputEl = new TagBuilder("input");
                    inputEl.AddCssClass("form-control erp-percent");
                    inputEl.Attributes.Add("type", "number");
                    inputEl.Attributes.Add("readonly", null);
                    var fakeValueString = "";
                    if (Value != null)
                    {
                        fakeValueString = (((decimal)Value) * 100).ToString("N" + DecimalDigits);
                    }
                    inputEl.Attributes.Add("value", fakeValueString);
                    inputGroupEl.InnerHtml.AppendHtml(inputEl);
                    //Append
                    if (AppendHtml.Count > 0)
                    {
                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        foreach (var htmlString in AppendHtml)
                        {
                            appendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    }
                    output.Content.AppendHtml(inputGroupEl);
                }
            }
            else if (Mode == FieldRenderMode.Display)
            {
                if (Value != null)
                {
                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-percent");
                    var fakeValueString = "";
                    if (Value != null)
                    {
                        fakeValueString = ((decimal)Value).ToString("P" + DecimalDigits, Culture);
                    }
                    divEl.InnerHtml.Append(fakeValueString);
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == FieldRenderMode.Simple)
            {
                if (Value != null)
                {
                    output.SuppressOutput();
                    var fakeValueString = "";
                    if (Value != null)
                    {
                        fakeValueString = ((decimal)Value).ToString("P" + DecimalDigits, Culture);
                    }
                    output.Content.Append(fakeValueString);
                }
                else
                {
                    output.Content.Append("");
                }
                return;
            }
            else if (Mode == FieldRenderMode.InlineEdit)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    #region << View Wrapper >>
                    {
                        var viewWrapperEl = new TagBuilder("div");
                        viewWrapperEl.AddCssClass("input-group view-wrapper");
                        viewWrapperEl.Attributes.Add("title", "double click to edit");
                        viewWrapperEl.Attributes.Add("id", $"view-{FieldId}");

                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var viewInputPrepend = new TagBuilder("span");
                            viewInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);
                        }
                        //Control
                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-percent");
                        var fakeValueString = "";
                        if (Value != null)
                        {
                            fakeValueString = (((decimal)Value) * 100).ToString("N" + DecimalDigits);
                        }
                        viewFormControlEl.InnerHtml.Append(fakeValueString);
                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        //Append
                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append");
                        foreach (var htmlString in AppendHtml)
                        {
                            viewInputActionEl.InnerHtml.AppendHtml(htmlString);
                        }
                        viewInputActionEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white action' title='edit'><i class='fa fa-fw fa-pencil-alt'></i></button>");
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl);

                        output.Content.AppendHtml(viewWrapperEl);
                    }
                    #endregion

                    #region << Edit Wrapper>>
                    {
                        var editWrapperEl = new TagBuilder("div");
                        editWrapperEl.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapperEl.Attributes.Add("style", $"display:none;");
                        editWrapperEl.AddCssClass("edit-wrapper");

                        var editInputGroupEl = new TagBuilder("div");
                        editInputGroupEl.AddCssClass("input-group");

                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var editInputPrepend = new TagBuilder("span");
                            editInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                editInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            editInputGroupEl.InnerHtml.AppendHtml(editInputPrepend);
                        }
                        //Control
                        var editInputEl = new TagBuilder("input");
                        editInputEl.AddCssClass("form-control erp-percent");
                        editInputEl.Attributes.Add("id", $"fake-{FieldId}");
                        editInputEl.Attributes.Add("type", "number");
                        if (Required)
                        {
                            editInputEl.Attributes.Add("required", null);
                        }
                        if (!String.IsNullOrWhiteSpace(Placeholder))
                        {
                            editInputEl.Attributes.Add("placeholder", Placeholder);
                        }
                        if (Min != null)
                        {
                            editInputEl.Attributes.Add("min", Min.ToString());
                        }
                        if (Max != null)
                        {
                            editInputEl.Attributes.Add("max", Max.ToString());
                        }
                        if (Step != null)
                        {
                            editInputEl.Attributes.Add("step", Step.ToString());
                        }
                        var fakeValueString = "";
                        if (Value != null)
                        {
                            fakeValueString = (((decimal)Value) * 100).ToString("N" + DecimalDigits);
                        }
                        editInputEl.Attributes.Add("value", fakeValueString);
                        editInputGroupEl.InnerHtml.AppendHtml(editInputEl);

                        //Append
                        var editInputGroupAppendEl = new TagBuilder("span");
                        editInputGroupAppendEl.AddCssClass("input-group-append");

                        foreach (var htmlString in AppendHtml)
                        {
                            editInputGroupAppendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white save' title='save'><i class='fa fa-fw fa-check go-green'></i></button>");
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white cancel' title='cancel'><i class='fa fa-fw fa-times go-gray'></i></button>");
                        editInputGroupEl.InnerHtml.AppendHtml(editInputGroupAppendEl);

                        editWrapperEl.InnerHtml.AppendHtml(editInputGroupEl);

                        var hiddenInputEl = new TagBuilder("input");
                        hiddenInputEl.Attributes.Add("id", $"input-{FieldId}");
                        hiddenInputEl.Attributes.Add("type", "hidden");
                        var hiddenValueString = "";
                        if (Value != null)
                        {
                            hiddenValueString = ((decimal)Value).ToString("N" + (DecimalDigits + 2));
                        }
                        hiddenInputEl.Attributes.Add("value", hiddenValueString);
                        hiddenInputEl.Attributes.Add("data-original-value", hiddenValueString);
                        editWrapperEl.InnerHtml.AppendHtml(hiddenInputEl);

                        output.Content.AppendHtml(editWrapperEl);
                    }
                    #endregion

                    var jsCompressor = new JavaScriptCompressor();
                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldPercent) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldPercent) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("inline-edit.js", "WebVella.Erp.Web.TagHelpers.WvFieldPercent");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldPercent) + "-inline-edit"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							PercentInlineEditInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",""{{RecordId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);
                    scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName);
                    scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString());

                    var fieldConfig = new WvFieldPercentConfig()
                    {
                        ApiUrl        = ApiUrl,
                        CanAddValues  = Access == FieldAccess.FullAndCreate ? true : false,
                        DecimalDigits = DecimalDigits
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    var divEl = new TagBuilder("div");
                    divEl.AddCssClass("input-group");

                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend");
                        foreach (var htmlString in PrependHtml)
                        {
                            viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                        }
                        divEl.InnerHtml.AppendHtml(viewInputPrepend);
                    }
                    //Control
                    var inputEl = new TagBuilder("input");
                    inputEl.AddCssClass("form-control erp-percent");
                    inputEl.Attributes.Add("type", "text");
                    var fakeValueString = "";
                    if (Value != null)
                    {
                        fakeValueString = (((decimal)Value) * 100).ToString("N" + DecimalDigits);
                    }
                    inputEl.Attributes.Add("value", fakeValueString);
                    inputEl.Attributes.Add("readonly", null);
                    divEl.InnerHtml.AppendHtml(inputEl);
                    //Append
                    var appendActionSpan = new TagBuilder("span");
                    appendActionSpan.AddCssClass("input-group-append");
                    foreach (var htmlString in AppendHtml)
                    {
                        appendActionSpan.InnerHtml.AppendHtml(htmlString);
                    }
                    appendActionSpan.InnerHtml.AppendHtml("<button type=\"button\" disabled class='btn btn-white action' title='locked'><i class='fa fa-fw fa-lock'></i></button>");
                    divEl.InnerHtml.AppendHtml(appendActionSpan);
                    output.Content.AppendHtml(divEl);
                }
            }
            #endregion


            //Finally
            if (SubInputEl != null)
            {
                output.PostContent.AppendHtml(SubInputEl);
            }

            return;
        }
Ejemplo n.º 3
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return;
            }
            #region << Init Prepend and Append >>
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var prependTaghelper = htmlDoc.DocumentNode.Descendants("wv-field-prepend");
            var appendTagHelper  = htmlDoc.DocumentNode.Descendants("wv-field-append");

            foreach (var node in prependTaghelper)
            {
                PrependHtml.Add(node.InnerHtml.ToString());
            }

            foreach (var node in appendTagHelper)
            {
                AppendHtml.Add(node.InnerHtml.ToString());
            }

            #endregion
            #endregion

            #region << Render >>
            if (Mode == FieldRenderMode.Form)
            {
                var inputReadOnlyValue = "";
                var inputEditValue     = "";
                if (Value != null)
                {
                    inputReadOnlyValue = (Value as DateTime? ?? (DateTime?)DateTime.Now).ConvertToAppDate().Value.ToString("dd MMM yyyy", Culture);
                    inputEditValue     = (Value ?? DateTime.Now).ToString("yyyy-MM-ddT00:00:00.000");
                }


                var inputGroupEl = new TagBuilder("div");
                inputGroupEl.AddCssClass("input-group");
                //Prepend
                if (PrependHtml.Count > 0)
                {
                    var prependEl = new TagBuilder("span");
                    prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    foreach (var htmlString in PrependHtml)
                    {
                        prependEl.InnerHtml.AppendHtml(htmlString);
                    }
                    inputGroupEl.InnerHtml.AppendHtml(prependEl);
                }
                //Control
                var inputEl             = new TagBuilder("input");
                var inputElCssClassList = new List <string>();
                inputElCssClassList.Add("form-control erp-date");

                inputEl.Attributes.Add("type", "text");
                inputEl.Attributes.Add("id", $"input-{FieldId}");
                inputEl.Attributes.Add("name", Name);

                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    if (Required)
                    {
                        inputEl.Attributes.Add("required", null);
                    }
                    if (!String.IsNullOrWhiteSpace(Placeholder))
                    {
                        inputEl.Attributes.Add("placeholder", Placeholder);
                    }

                    inputEl.Attributes.Add("value", inputEditValue);
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    inputEl.Attributes.Add("value", inputReadOnlyValue);
                    inputEl.Attributes.Add("readonly", null);
                }

                if (ValidationErrors.Count > 0)
                {
                    inputElCssClassList.Add("is-invalid");
                }

                inputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));

                inputGroupEl.InnerHtml.AppendHtml(inputEl);
                //Append
                if (AppendHtml.Count > 0)
                {
                    var appendEl = new TagBuilder("span");
                    appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                    foreach (var htmlString in AppendHtml)
                    {
                        appendEl.InnerHtml.AppendHtml(htmlString);
                    }
                    inputGroupEl.InnerHtml.AppendHtml(appendEl);
                }

                output.Content.AppendHtml(inputGroupEl);

                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldDate) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldDate) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("form.js", "WebVella.Erp.Web.TagHelpers.WvFieldDate");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldDate) + "-form"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							InitFlatPickrDate(""{{FieldId}}"");
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
            }
            else if (Mode == FieldRenderMode.Display)
            {
                if (Value != null)
                {
                    var inputReadOnlyValue = (Value as DateTime? ?? (DateTime?)DateTime.Now).ConvertToAppDate().Value.ToString("dd MMM yyyy", Culture);

                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-date");
                    divEl.InnerHtml.Append(inputReadOnlyValue);
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == FieldRenderMode.Simple)
            {
                if (Value != null)
                {
                    output.SuppressOutput();
                    var inputReadOnlyValue = (Value as DateTime? ?? (DateTime?)DateTime.Now).ConvertToAppDate().Value.ToString("dd MMM yyyy", Culture);
                    output.Content.AppendHtml(inputReadOnlyValue);
                }
                else
                {
                    output.Content.AppendHtml("");
                }
                return;
            }
            else if (Mode == FieldRenderMode.InlineEdit)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    var editInpuValue      = "";
                    var readOnlyInputValue = "";
                    if (Value != null)
                    {
                        readOnlyInputValue = (Value as DateTime? ?? (DateTime?)DateTime.Now).ConvertToAppDate().Value.ToString("dd MMM yyyy", Culture);
                        editInpuValue      = (Value ?? DateTime.Now).ToString("yyyy-MM-ddT00:00:00.000");
                    }

                    #region << View Wrapper >>
                    {
                        var viewWrapperEl = new TagBuilder("div");
                        viewWrapperEl.AddCssClass("input-group view-wrapper");
                        viewWrapperEl.Attributes.Add("title", "double click to edit");
                        viewWrapperEl.Attributes.Add("id", $"view-{FieldId}");
                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var viewInputPrepend = new TagBuilder("span");
                            viewInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);
                        }
                        //Control
                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-date");
                        viewFormControlEl.InnerHtml.Append(readOnlyInputValue);
                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        //Append
                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append");
                        foreach (var htmlString in AppendHtml)
                        {
                            viewInputActionEl.InnerHtml.AppendHtml(htmlString);
                        }
                        viewInputActionEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white action' title='edit'><i class='fa fa-fw fa-pencil-alt'></i></button>");
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl);
                        output.Content.AppendHtml(viewWrapperEl);
                    }
                    #endregion

                    #region << Edit Wrapper>>
                    {
                        var editWrapperEl = new TagBuilder("div");
                        editWrapperEl.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapperEl.Attributes.Add("style", $"display:none;");
                        editWrapperEl.Attributes.Add("data-default-date", editInpuValue);                         //There is a problem with double initializing flatpickr which this solves
                        editWrapperEl.AddCssClass("edit-wrapper");

                        var editInputGroupEl = new TagBuilder("div");
                        editInputGroupEl.AddCssClass("input-group");
                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var editInputPrepend = new TagBuilder("span");
                            editInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                editInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            editInputGroupEl.InnerHtml.AppendHtml(editInputPrepend);
                        }
                        //Control
                        var editInputEl = new TagBuilder("input");
                        editInputEl.AddCssClass("form-control erp-date");
                        editInputEl.Attributes.Add("type", "text");
                        if (Required)
                        {
                            editInputEl.Attributes.Add("required", null);
                        }
                        if (!String.IsNullOrWhiteSpace(Placeholder))
                        {
                            editInputEl.Attributes.Add("placeholder", Placeholder);
                        }

                        editInputEl.Attributes.Add("value", editInpuValue);
                        editInputGroupEl.InnerHtml.AppendHtml(editInputEl);

                        //Append
                        var editInputGroupAppendEl = new TagBuilder("span");
                        editInputGroupAppendEl.AddCssClass("input-group-append");

                        foreach (var htmlString in AppendHtml)
                        {
                            editInputGroupAppendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white save' title='save'><i class='fa fa-fw fa-check go-green'></i></button>");
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white cancel' title='cancel'><i class='fa fa-fw fa-times go-gray'></i></button>");
                        editInputGroupEl.InnerHtml.AppendHtml(editInputGroupAppendEl);

                        editWrapperEl.InnerHtml.AppendHtml(editInputGroupEl);

                        output.Content.AppendHtml(editWrapperEl);
                    }
                    #endregion

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldDate) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldDate) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("inline-edit.js", "WebVella.Erp.Web.TagHelpers.WvFieldDate");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldDate) + "-inline-edit"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							DateInlineEditInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",""{{RecordId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);
                    scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName);
                    scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString());

                    var fieldConfig = new WvFieldDateConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == FieldAccess.FullAndCreate ? true : false
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    var readOnlyInputValue = "";
                    if (Value != null)
                    {
                        readOnlyInputValue = (Value as DateTime? ?? (DateTime?)DateTime.Now).ConvertToAppDate().Value.ToString("dd MMM yyyy", Culture);
                    }

                    var divEl = new TagBuilder("div");
                    divEl.AddCssClass("input-group");

                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend");
                        foreach (var htmlString in PrependHtml)
                        {
                            viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                        }
                        divEl.InnerHtml.AppendHtml(viewInputPrepend);
                    }
                    //Control
                    var inputEl = new TagBuilder("input");
                    inputEl.AddCssClass("form-control erp-date");
                    inputEl.Attributes.Add("type", "text");
                    inputEl.Attributes.Add("value", readOnlyInputValue);
                    inputEl.Attributes.Add("readonly", null);
                    divEl.InnerHtml.AppendHtml(inputEl);
                    //Append
                    var appendActionSpan = new TagBuilder("span");
                    appendActionSpan.AddCssClass("input-group-append");
                    foreach (var htmlString in AppendHtml)
                    {
                        appendActionSpan.InnerHtml.AppendHtml(htmlString);
                    }
                    appendActionSpan.InnerHtml.AppendHtml("<button type=\"button\" disabled class='btn btn-white action' title='locked'><i class='fa fa-fw fa-lock'></i></button>");
                    divEl.InnerHtml.AppendHtml(appendActionSpan);

                    output.Content.AppendHtml(divEl);
                }
            }
            #endregion

            //Finally
            if (SubInputEl != null)
            {
                output.PostContent.AppendHtml(SubInputEl);
            }

            return;
        }
Ejemplo n.º 4
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return;
            }

            #region << Init Prepend and Append >>
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var prependTaghelper = htmlDoc.DocumentNode.Descendants("wv-field-prepend");
            var appendTagHelper  = htmlDoc.DocumentNode.Descendants("wv-field-append");

            foreach (var node in prependTaghelper)
            {
                PrependHtml.Add(node.InnerHtml.ToString());
            }

            foreach (var node in appendTagHelper)
            {
                AppendHtml.Add(node.InnerHtml.ToString());
            }

            #endregion

            #endregion

            #region << Render >>
            if (Mode == FieldRenderMode.Form)
            {
                var inputGroupEl = new TagBuilder("div");
                inputGroupEl.AddCssClass("input-group");
                //Prepend
                if (PrependHtml.Count > 0)
                {
                    var prependEl = new TagBuilder("span");
                    prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    foreach (var htmlString in PrependHtml)
                    {
                        prependEl.InnerHtml.AppendHtml(htmlString);
                    }
                    inputGroupEl.InnerHtml.AppendHtml(prependEl);
                }
                //Control
                var inputEl             = new TagBuilder("input");
                var inputElCssClassList = new List <string>();
                inputElCssClassList.Add("form-control erp-text");

                inputEl.Attributes.Add("type", "text");
                inputEl.Attributes.Add("value", (Value ?? "").ToString());
                inputEl.Attributes.Add("id", $"input-{FieldId}");
                inputEl.Attributes.Add("name", Name);
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    if (Required)
                    {
                        inputEl.Attributes.Add("required", null);
                    }
                    if (!String.IsNullOrWhiteSpace(Placeholder))
                    {
                        inputEl.Attributes.Add("placeholder", Placeholder);
                    }
                    if (MaxLength != null && MaxLength != 0)
                    {
                        inputEl.Attributes.Add("maxlength", MaxLength.ToString());
                    }

                    if (AutoComplete != null)
                    {
                        if (AutoComplete == true)
                        {
                            var autoCompleteValue = "on";
                            if ((new List <string>()
                            {
                                "name"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "name";
                            }
                            else if ((new List <string>()
                            {
                                "fname", "firstname", "first_name", "first-name", "givenname", "given_name", "given-name"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "given-name";
                            }
                            else if ((new List <string>()
                            {
                                "mname", "middlename", "middle_name", "middle-name"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "additional-name";
                            }
                            else if ((new List <string>()
                            {
                                "lname", "lastname", "last_name", "last-name", "familyname", "family_name", "family-name"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "family-name";
                            }
                            else if ((new List <string>()
                            {
                                "address", "street", "streetaddress", "street_address", "street-address"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "street-address";
                            }
                            else if ((new List <string>()
                            {
                                "region", "province", "state"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "address-level1";
                            }
                            else if ((new List <string>()
                            {
                                "city", "town", "place"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "address-level2";
                            }
                            else if ((new List <string>()
                            {
                                "zip", "zip2", "postal", "postalcode", "postal_code", "postal-code"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "postal-code";
                            }
                            else if ((new List <string>()
                            {
                                "country"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "country";
                            }
                            else if ((new List <string>()
                            {
                                "username"
                            }).Contains(Name.ToLowerInvariant()))
                            {
                                autoCompleteValue = "username";
                            }

                            inputEl.Attributes.Add("autocomplete", autoCompleteValue);
                        }
                        else
                        {
                            inputEl.Attributes.Add("autocomplete", "off");
                        }
                    }
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    inputEl.Attributes.Add("readonly", null);
                }

                if (ValidationErrors.Count > 0)
                {
                    inputElCssClassList.Add("is-invalid");
                }
                inputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));
                inputGroupEl.InnerHtml.AppendHtml(inputEl);
                //Append
                if (AppendHtml.Count > 0)
                {
                    var appendEl = new TagBuilder("span");
                    appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                    foreach (var htmlString in AppendHtml)
                    {
                        appendEl.InnerHtml.AppendHtml(htmlString);
                    }
                    inputGroupEl.InnerHtml.AppendHtml(appendEl);
                }

                output.Content.AppendHtml(inputGroupEl);
            }
            else if (Mode == FieldRenderMode.Display)
            {
                if (!String.IsNullOrWhiteSpace(Value))
                {
                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-text");
                    divEl.InnerHtml.Append((Value ?? "").ToString());
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == FieldRenderMode.Simple)
            {
                output.SuppressOutput();
                output.Content.AppendHtml((Value ?? "").ToString());
                return;
            }
            else if (Mode == FieldRenderMode.InlineEdit)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    #region << View Wrapper >>
                    {
                        var viewWrapperEl = new TagBuilder("div");
                        viewWrapperEl.AddCssClass("input-group view-wrapper");
                        viewWrapperEl.Attributes.Add("title", "double click to edit");
                        viewWrapperEl.Attributes.Add("id", $"view-{FieldId}");
                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var viewInputPrepend = new TagBuilder("span");
                            viewInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);
                        }
                        //Control
                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-text");
                        viewFormControlEl.InnerHtml.Append((Value ?? "").ToString());
                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        //Append
                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append");
                        foreach (var htmlString in AppendHtml)
                        {
                            viewInputActionEl.InnerHtml.AppendHtml(htmlString);
                        }
                        viewInputActionEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white action' title='edit'><i class='fa fa-fw fa-pencil-alt'></i></button>");
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl);

                        output.Content.AppendHtml(viewWrapperEl);
                    }
                    #endregion

                    #region << Edit Wrapper>>
                    {
                        var editWrapperEl = new TagBuilder("div");
                        editWrapperEl.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapperEl.Attributes.Add("style", $"display:none;");
                        editWrapperEl.AddCssClass("edit-wrapper");

                        var editInputGroupEl = new TagBuilder("div");
                        editInputGroupEl.AddCssClass("input-group");
                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var editInputPrepend = new TagBuilder("span");
                            editInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                editInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            editInputGroupEl.InnerHtml.AppendHtml(editInputPrepend);
                        }
                        //Control
                        var editInputEl = new TagBuilder("input");
                        editInputEl.AddCssClass("form-control erp-text");
                        if (Required)
                        {
                            editInputEl.Attributes.Add("required", null);
                        }
                        if (!String.IsNullOrWhiteSpace(Placeholder))
                        {
                            editInputEl.Attributes.Add("placeholder", Placeholder);
                        }
                        if (MaxLength != null && MaxLength != 0)
                        {
                            editInputEl.Attributes.Add("maxlength", MaxLength.ToString());
                        }
                        editInputEl.Attributes.Add("value", (Value ?? "").ToString());
                        editInputGroupEl.InnerHtml.AppendHtml(editInputEl);

                        //Append
                        var editInputGroupAppendEl = new TagBuilder("span");
                        editInputGroupAppendEl.AddCssClass("input-group-append");

                        foreach (var htmlString in AppendHtml)
                        {
                            editInputGroupAppendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white save' title='save'><i class='fa fa-fw fa-check go-green'></i></button>");
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white cancel' title='cancel'><i class='fa fa-fw fa-times go-gray'></i></button>");

                        editInputGroupEl.InnerHtml.AppendHtml(editInputGroupAppendEl);
                        editWrapperEl.InnerHtml.AppendHtml(editInputGroupEl);

                        output.Content.AppendHtml(editWrapperEl);
                    }
                    #endregion

                    var jsCompressor = new JavaScriptCompressor();
                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldText) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldText) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("inline-edit.js", "WebVella.Erp.Web.TagHelpers.WvFieldText");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldText) + "-inline-edit"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							TextInlineEditInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",""{{RecordId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);
                    scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName);
                    scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString());

                    var fieldConfig = new WvFieldTextConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == FieldAccess.FullAndCreate ? true : false
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    var divEl = new TagBuilder("div");
                    divEl.AddCssClass("input-group");
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend");
                        foreach (var htmlString in PrependHtml)
                        {
                            viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                        }
                        divEl.InnerHtml.AppendHtml(viewInputPrepend);
                    }
                    //Control
                    var inputEl = new TagBuilder("input");
                    inputEl.AddCssClass("form-control erp-text");
                    inputEl.Attributes.Add("type", "text");
                    inputEl.Attributes.Add("value", (Value ?? "").ToString());
                    inputEl.Attributes.Add("readonly", null);
                    divEl.InnerHtml.AppendHtml(inputEl);
                    //Append
                    var appendActionSpan = new TagBuilder("span");
                    appendActionSpan.AddCssClass("input-group-append");
                    foreach (var htmlString in AppendHtml)
                    {
                        appendActionSpan.InnerHtml.AppendHtml(htmlString);
                    }
                    appendActionSpan.InnerHtml.AppendHtml("<button type=\"button\" disabled class='btn btn-white action' title='locked'><i class='fa fa-fw fa-lock'></i></button>");
                    divEl.InnerHtml.AppendHtml(appendActionSpan);

                    output.Content.AppendHtml(divEl);
                }
            }
            #endregion


            //Finally
            if (SubInputEl != null)
            {
                output.Content.AppendHtml(SubInputEl);
            }

            return;
        }
Ejemplo n.º 5
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return;
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return;
            }

            if (Currency == null)
            {
                Currency = WvHelpers.GetCurrencyType(CurrencyCode);
                if (Currency == null)
                {
                    throw new Exception($"Currency with code '{CurrencyCode}' is not found.");
                }
            }
            //Apply currency decimal places
            if (Value != null)
            {
                var decimalPlaces = Convert.ToInt32(Currency.DecimalDigits);
                Value = Math.Round(Convert.ToDecimal(Value), decimalPlaces);
            }

            #endregion

            #region << Init Prepend and Append >>
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var prependTaghelper = htmlDoc.DocumentNode.Descendants("wv-field-prepend");
            var appendTagHelper  = htmlDoc.DocumentNode.Descendants("wv-field-append");

            foreach (var node in prependTaghelper)
            {
                PrependHtml.Add(node.InnerHtml.ToString());
            }

            foreach (var node in appendTagHelper)
            {
                AppendHtml.Add(node.InnerHtml.ToString());
            }

            #endregion

            #region << Render >>
            if (Mode == WvFieldRenderMode.Form)
            {
                var inputGroupEl = new TagBuilder("div");
                inputGroupEl.AddCssClass("input-group");
                //Prepend
                if (PrependHtml.Count > 0)
                {
                    var prependEl = new TagBuilder("span");
                    prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    foreach (var htmlString in PrependHtml)
                    {
                        prependEl.InnerHtml.AppendHtml(htmlString);
                    }
                    inputGroupEl.InnerHtml.AppendHtml(prependEl);
                }

                var inputEl             = new TagBuilder("input");
                var inputElCssClassList = new List <string>();
                inputElCssClassList.Add("form-control erp-currency");

                inputEl.Attributes.Add("type", "number");
                inputEl.Attributes.Add("value", (Value ?? "").ToString());
                inputEl.Attributes.Add("id", $"input-{FieldId}");
                inputEl.Attributes.Add("name", Name);

                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    if (Required)
                    {
                        inputEl.Attributes.Add("required", null);
                    }
                    if (!String.IsNullOrWhiteSpace(Placeholder))
                    {
                        inputEl.Attributes.Add("placeholder", Placeholder);
                    }
                    if (Min != null)
                    {
                        inputEl.Attributes.Add("min", Min.ToString());
                    }
                    if (Max != null)
                    {
                        inputEl.Attributes.Add("max", Max.ToString());
                    }
                    if (Step != null)
                    {
                        inputEl.Attributes.Add("step", Step.ToString());
                    }
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    inputEl.Attributes.Add("readonly", null);
                }

                if (ValidationErrors.Count > 0)
                {
                    inputElCssClassList.Add("is-invalid");
                }

                inputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));

                inputGroupEl.InnerHtml.AppendHtml(inputEl);

                //Append
                if (AppendHtml.Count > 0)
                {
                    var appendEl = new TagBuilder("span");
                    appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                    foreach (var htmlString in AppendHtml)
                    {
                        appendEl.InnerHtml.AppendHtml(htmlString);
                    }
                    inputGroupEl.InnerHtml.AppendHtml(appendEl);
                }

                output.Content.AppendHtml(inputGroupEl);
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                if (Value != null)
                {
                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-currency");
                    var valueString = Value.ToString("N" + Currency.DecimalDigits, Culture);
                    if (Currency.SymbolPlacement == CurrencySymbolPlacement.After)
                    {
                        divEl.InnerHtml.Append($"{valueString} {Currency.SymbolNative}");
                    }
                    else
                    {
                        divEl.InnerHtml.Append($"{Currency.SymbolNative} {valueString}");
                    }
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                if (Value != null)
                {
                    output.SuppressOutput();
                    var valueString = Value.ToString("N" + Currency.DecimalDigits, Culture);
                    if (Currency.SymbolPlacement == CurrencySymbolPlacement.After)
                    {
                        output.Content.AppendHtml($"{valueString} {Currency.SymbolNative}");
                    }
                    else
                    {
                        output.Content.AppendHtml($"{Currency.SymbolNative} {valueString}");
                    }
                }
                else
                {
                    output.SuppressOutput();
                    output.Content.AppendHtml("");
                }
                return;
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    #region << View Wrapper >>
                    {
                        var viewWrapperEl = new TagBuilder("div");
                        viewWrapperEl.AddCssClass("input-group view-wrapper");
                        viewWrapperEl.Attributes.Add("title", "double click to edit");
                        viewWrapperEl.Attributes.Add("id", $"view-{FieldId}");

                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var viewInputPrepend = new TagBuilder("span");
                            viewInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);
                        }

                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-currency");
                        viewFormControlEl.InnerHtml.Append((Value ?? "").ToString());
                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append action");
                        viewInputActionEl.Attributes.Add("title", "edit");

                        var viewInputActionLinkEl = new TagBuilder("button");
                        viewInputActionLinkEl.Attributes.Add("type", "button");
                        viewInputActionLinkEl.AddCssClass("btn btn-white");

                        var viewInputActionIconEl = new TagBuilder("span");
                        viewInputActionIconEl.AddCssClass("fa fa-fw fa-pencil-alt");
                        viewInputActionLinkEl.InnerHtml.AppendHtml(viewInputActionIconEl);
                        viewInputActionEl.InnerHtml.AppendHtml(viewInputActionLinkEl);
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl);

                        output.Content.AppendHtml(viewWrapperEl);
                    }
                    #endregion

                    #region << Edit Wrapper>>
                    {
                        var editWrapperEl = new TagBuilder("div");
                        editWrapperEl.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapperEl.Attributes.Add("style", $"display:none;");
                        editWrapperEl.AddCssClass("edit-wrapper");

                        var editInputGroupEl = new TagBuilder("div");
                        editInputGroupEl.AddCssClass("input-group");

                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var editInputPrepend = new TagBuilder("span");
                            editInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                editInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            editInputGroupEl.InnerHtml.AppendHtml(editInputPrepend);
                        }


                        var editInputEl = new TagBuilder("input");
                        editInputEl.AddCssClass("form-control erp-currency");
                        editInputEl.Attributes.Add("type", "number");
                        if (Required)
                        {
                            editInputEl.Attributes.Add("required", null);
                        }
                        if (!String.IsNullOrWhiteSpace(Placeholder))
                        {
                            editInputEl.Attributes.Add("placeholder", Placeholder);
                        }
                        if (Min != null)
                        {
                            editInputEl.Attributes.Add("min", Min.ToString());
                        }
                        if (Max != null)
                        {
                            editInputEl.Attributes.Add("max", Max.ToString());
                        }
                        if (Step != null)
                        {
                            editInputEl.Attributes.Add("step", Step.ToString());
                        }

                        editInputEl.Attributes.Add("value", (Value ?? "").ToString());
                        editInputGroupEl.InnerHtml.AppendHtml(editInputEl);

                        var editInputGroupAppendEl = new TagBuilder("span");
                        editInputGroupAppendEl.AddCssClass("input-group-append");

                        var editSaveBtnEl = new TagBuilder("button");
                        editSaveBtnEl.Attributes.Add("type", "submit");
                        editSaveBtnEl.AddCssClass("btn btn-white save");
                        editSaveBtnEl.Attributes.Add("title", "save");

                        var editSaveIconEl = new TagBuilder("span");
                        editSaveIconEl.AddCssClass("fa fa-fw fa-check go-green");
                        editSaveBtnEl.InnerHtml.AppendHtml(editSaveIconEl);
                        editInputGroupAppendEl.InnerHtml.AppendHtml(editSaveBtnEl);

                        var editCancelBtnEl = new TagBuilder("button");
                        editCancelBtnEl.Attributes.Add("type", "submit");
                        editCancelBtnEl.AddCssClass("btn btn-white cancel");
                        editCancelBtnEl.Attributes.Add("title", "cancel");

                        var editCancelIconEl = new TagBuilder("span");
                        editCancelIconEl.AddCssClass("fa fa-fw fa-times go-gray");
                        editCancelBtnEl.InnerHtml.AppendHtml(editCancelIconEl);
                        editInputGroupAppendEl.InnerHtml.AppendHtml(editCancelBtnEl);

                        editInputGroupEl.InnerHtml.AppendHtml(editInputGroupAppendEl);
                        editWrapperEl.InnerHtml.AppendHtml(editInputGroupEl);

                        output.Content.AppendHtml(editWrapperEl);
                    }
                    #endregion

                    var jsCompressor = new JavaScriptCompressor();
                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldCurrency) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldCurrency) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldCurrency", "WebVella.TagHelpers");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldCurrency) + "-inline-edit"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							CurrencyInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                    var fieldConfig = new WvFieldCurrencyConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == WvFieldAccess.FullAndCreate ? true : false
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    //initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));
                    initScript.InnerHtml.AppendHtml(scriptTemplate);
                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    var divEl = new TagBuilder("div");
                    divEl.AddCssClass("input-group");

                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend");
                        foreach (var htmlString in PrependHtml)
                        {
                            viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                        }
                        divEl.InnerHtml.AppendHtml(viewInputPrepend);
                    }


                    var inputEl = new TagBuilder("input");
                    inputEl.AddCssClass("form-control erp-currency");
                    inputEl.Attributes.Add("type", "text");
                    inputEl.Attributes.Add("value", (Value ?? "").ToString());
                    inputEl.Attributes.Add("readonly", null);

                    //Append
                    var appendActionSpan = new TagBuilder("span");
                    appendActionSpan.AddCssClass("input-group-append action");
                    foreach (var htmlString in AppendHtml)
                    {
                        appendActionSpan.InnerHtml.AppendHtml(htmlString);
                    }

                    var appendIconSpan = new TagBuilder("span");
                    appendIconSpan.AddCssClass("fa fa-fw fa-lock");

                    appendActionSpan.InnerHtml.AppendHtml(appendIconSpan);


                    divEl.InnerHtml.AppendHtml(inputEl);
                    divEl.InnerHtml.AppendHtml(appendActionSpan);
                    output.Content.AppendHtml(divEl);
                }
            }
            #endregion


            //Finally
            if (SubInputEl != null)
            {
                output.PostContent.AppendHtml(SubInputEl);
            }

            return;
        }
Ejemplo n.º 6
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return;
            }

            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return;
            }

            if (Options == null)
            {
                Options = new List <WvSelectOption>();
            }

            if (Options.Count == 0 && AjaxDatasource != null && AjaxDatasource.InitOptions.Count > 0)
            {
                Options = AjaxDatasource.InitOptions;
            }

            if (AjaxDatasource != null && String.IsNullOrWhiteSpace(AjaxDatasourceApi))
            {
                if (!AjaxDatasource.UseSelectApi)
                {
                    //old fashion call
                    AjaxDatasourceApi = "/api/v3/en_US/eql-ds";
                }
                else
                {
                    //The new api
                    AjaxDatasourceApi = "/api/v3/en_US/eql-ds-select2";
                }
            }


            #region << Init Prepend and Append >>
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var prependTaghelper = htmlDoc.DocumentNode.Descendants("wv-field-prepend");
            var appendTagHelper  = htmlDoc.DocumentNode.Descendants("wv-field-append");

            foreach (var node in prependTaghelper)
            {
                PrependHtml.Add(node.InnerHtml.ToString());
            }

            foreach (var node in appendTagHelper)
            {
                AppendHtml.Add(node.InnerHtml.ToString());
            }

            #endregion

            #region << Validate Options >>
            if (Options == null)
            {
                var divEl = new TagBuilder("div");
                divEl.AddCssClass("form-control-plaintext erp-plain-text");

                var errorListEl = new TagBuilder("ul");
                errorListEl.AddCssClass("erp-error-list list-unstyled");

                var errorEl = new TagBuilder("li");
                errorEl.AddCssClass("go-red");

                var iconEl = new TagBuilder("span");
                iconEl.AddCssClass("fa fa-fw fa-exclamation");

                errorEl.InnerHtml.AppendHtml(iconEl);
                errorEl.InnerHtml.Append($"Error: Select options cannot be null");

                errorListEl.InnerHtml.AppendHtml(errorEl);
                divEl.InnerHtml.AppendHtml(errorListEl);
                output.Content.AppendHtml(divEl);
                //Finally
                if (SubInputEl != null)
                {
                    output.PostContent.AppendHtml(SubInputEl);
                }
                return;
            }
            #endregion

            #endregion



            #region << Render >>
            if (Mode == WvFieldRenderMode.Form)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    var inputGroupEl = new TagBuilder("div");
                    if (PrependHtml.Count > 0 || AppendHtml.Count > 0)
                    {
                        inputGroupEl.AddCssClass("input-group");
                    }
                    else
                    {
                        inputGroupEl.AddCssClass("");
                    }
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        foreach (var htmlString in PrependHtml)
                        {
                            prependEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);
                    }
                    //Control
                    var selectEl = new TagBuilder("select");
                    selectEl.Attributes.Add("id", $"input-{FieldId}");
                    selectEl.Attributes.Add("name", $"{Name}");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-select");
                    if (ValidationErrors.Count > 0)
                    {
                        inputElCssClassList.Add("is-invalid");
                    }
                    selectEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));
                    var emptyOptionAdded = false;
                    if (Required)
                    {
                        selectEl.Attributes.Add("required", null);

                        if (String.IsNullOrWhiteSpace(Value))
                        {
                            var defaultOption = Options.FirstOrDefault(x => x.Value == DefaultValue);

                            if (defaultOption != null)
                            {
                                Value = DefaultValue;
                            }
                        }
                    }
                    else
                    {
                        var optionEl = new TagBuilder("option");
                        // Should work only with <option></option> and the select2 placeholder to be presented
                        selectEl.InnerHtml.AppendHtml(optionEl);
                        emptyOptionAdded = true;
                    }

                    foreach (var option in Options)
                    {
                        var optionEl = new TagBuilder("option");
                        optionEl.Attributes.Add("value", option.Value);
                        if (option.Value == (Value ?? "").ToString())
                        {
                            optionEl.Attributes.Add("selected", null);
                        }
                        optionEl.Attributes.Add("data-icon", option.IconClass);
                        optionEl.Attributes.Add("data-color", option.Color);
                        optionEl.InnerHtml.Append(option.Label);
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    //At least one option should be in the select so it can submit
                    if (!emptyOptionAdded && Options.Count == 0)
                    {
                        var optionEl = new TagBuilder("option");
                        // Should work only with <option></option> and the select2 placeholder to be presented
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    inputGroupEl.InnerHtml.AppendHtml(selectEl);
                    //Append
                    if (AppendHtml.Count > 0)
                    {
                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        foreach (var htmlString in AppendHtml)
                        {
                            appendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    }

                    output.Content.AppendHtml(inputGroupEl);

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Select2 >>
                    {
                        var wvLibraryInitialized = false;
                        var libraryItemsKey      = "WebVella-" + "select2";
                        if (ViewContext.HttpContext.Items.ContainsKey(libraryItemsKey))
                        {
                            var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[libraryItemsKey];
                            wvLibraryInitialized = tagHelperContext.Initialized;
                        }

                        if (!wvLibraryInitialized)
                        {
                            {
                                var libCssEl = new TagBuilder("link");
                                libCssEl.Attributes.Add("href", "/_content/WebVella.TagHelpers/lib/select2/css/select2.min.css");
                                libCssEl.Attributes.Add("type", "text/css");
                                libCssEl.Attributes.Add("rel", "stylesheet");
                                output.PostContent.AppendHtml(libCssEl);
                                output.PostContent.AppendHtml("\r\n\t");
                            }
                            {
                                var libCssEl = new TagBuilder("link");
                                libCssEl.Attributes.Add("href", "/_content/WebVella.TagHelpers/lib/select2-bootstrap-theme/select2-bootstrap4.css");
                                libCssEl.Attributes.Add("type", "text/css");
                                libCssEl.Attributes.Add("rel", "stylesheet");
                                output.PostContent.AppendHtml(libCssEl);
                                output.PostContent.AppendHtml("\r\n\t");
                            }

                            var libJsEl = new TagBuilder("script");
                            libJsEl.Attributes.Add("type", "text/javascript");
                            libJsEl.Attributes.Add("src", "/_content/WebVella.TagHelpers/lib/select2/js/select2.min.js");
                            output.PostContent.AppendHtml(libJsEl);
                            output.PostContent.AppendHtml("\r\n\t");

                            ViewContext.HttpContext.Items[libraryItemsKey] = new WvTagHelperContext()
                            {
                                Initialized = true
                            };
                        }
                    }
                    #endregion


                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldSelect) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldSelect) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldSelect", "WebVella.TagHelpers");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        //scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        scriptEl.InnerHtml.AppendHtml(scriptContent);
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldSelect) + "-form"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							SelectFormInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                    var fieldConfig = new WvFieldSelectConfig()
                    {
                        ApiUrl            = ApiUrl,
                        CanAddValues      = Access == WvFieldAccess.FullAndCreate ? true : false,
                        IsInvalid         = ValidationErrors.Count > 0,
                        AjaxDatasource    = AjaxDatasource,
                        AjaxDatasourceApi = AjaxDatasourceApi,
                        SelectMatchType   = SelectMatchType,
                        Placeholder       = Placeholder
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion


                    //TODO Implement CanAddValues
                    //@if (config.CanAddValues ?? false)
                    //{
                    //<div id="*****@*****.**" class="modal" data-backdrop="true">
                    //	<div class="modal-dialog" name="add-option">
                    //		<div class="modal-content">
                    //			<div class="modal-header">
                    //				<h5 class="modal-title">Add @(fieldMeta.Label)</h5>
                    //			</div>
                    //			<div class="modal-body">
                    //				<div class="alert alert-danger d-none"></div>
                    //				<div class="form-group">
                    //					<label class="control-label">New value</label>
                    //					<input class="form-control erp-select add-option-input" value="" required />
                    //				</div>
                    //			</div>
                    //			<div class="modal-footer">
                    //				<button class="btn btn-primary btn-sm" type="submit"><i class="fa fa-plus-circle"></i> Add</button>
                    //				<button class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
                    //			</div>
                    //		</div>
                    //	</div>
                    //</div>

                    //}
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    //Have to render it as a normal select as readonly prop does not work with select 2. Also in order for the select not to work it should be disabled,
                    //which will not pass the value, this the hidden input

                    //Hidden input
                    var hiddenEl = new TagBuilder("input");
                    hiddenEl.Attributes.Add("id", $"input-{FieldId}");
                    hiddenEl.Attributes.Add("name", $"{Name}");
                    hiddenEl.Attributes.Add("value", (Value ?? "").ToString());
                    hiddenEl.Attributes.Add("type", "hidden");
                    output.Content.AppendHtml(hiddenEl);

                    var inputGroupEl = new TagBuilder("div");
                    if (PrependHtml.Count > 0 || AppendHtml.Count > 0)
                    {
                        inputGroupEl.AddCssClass("input-group");
                    }
                    else
                    {
                        inputGroupEl.AddCssClass("d-flex");
                    }
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        foreach (var htmlString in PrependHtml)
                        {
                            prependEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);
                    }

                    //Dummy disabled select
                    var selectEl = new TagBuilder("select");
                    selectEl.Attributes.Add("id", $"select-{FieldId}");
                    selectEl.Attributes.Add("readonly", null);
                    selectEl.Attributes.Add("disabled", "disabled");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-select");
                    if (ValidationErrors.Count > 0)
                    {
                        inputElCssClassList.Add("is-invalid");
                    }
                    selectEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));
                    if (Required)
                    {
                        selectEl.Attributes.Add("required", null);

                        if (String.IsNullOrWhiteSpace(Value))
                        {
                            var defaultOption = Options.FirstOrDefault(x => x.Value == DefaultValue);

                            if (defaultOption != null)
                            {
                                Value = DefaultValue;
                            }
                        }
                    }
                    else
                    {
                        var optionEl = new TagBuilder("option");
                        // Should work only with <option></option> and the select2 placeholder to be presented
                        //optionEl.Attributes.Add("value", "");
                        //if (String.IsNullOrWhiteSpace(Value))
                        //{
                        //    optionEl.Attributes.Add("selected", null);
                        //}
                        //optionEl.InnerHtml.Append("not selected");
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    foreach (var option in Options)
                    {
                        var optionEl = new TagBuilder("option");
                        optionEl.Attributes.Add("value", option.Value);
                        if (option.Value == (Value ?? "").ToString())
                        {
                            optionEl.Attributes.Add("selected", null);
                        }
                        optionEl.Attributes.Add("data-icon", option.IconClass);
                        optionEl.Attributes.Add("data-color", option.Color);
                        optionEl.InnerHtml.Append(option.Label);
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }
                    inputGroupEl.InnerHtml.AppendHtml(selectEl);
                    //Append
                    if (AppendHtml.Count > 0)
                    {
                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass($"input-group-append {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        foreach (var htmlString in AppendHtml)
                        {
                            appendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    }

                    output.Content.AppendHtml(inputGroupEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                var valueString = (Value ?? "").ToString();
                if (!String.IsNullOrWhiteSpace(valueString))
                {
                    var formControlEl = new TagBuilder("div");
                    formControlEl.Attributes.Add("id", $"input-{FieldId}");
                    formControlEl.AddCssClass("form-control-plaintext erp-select");

                    var optionEl       = new TagBuilder("span");
                    var selectedOption = Options.FirstOrDefault(x => x.Value == valueString);
                    if (selectedOption == null)
                    {
                        optionEl.InnerHtml.Append(valueString);
                        if (Value != null && valueString)
                        {
                            var optionElIcon = new TagBuilder("span");
                            optionElIcon.AddCssClass("fa fa-fw fa-exclamation-circle go-red");
                            optionElIcon.Attributes.Add("title", "the value is not supported by this field anymore");
                            optionEl.InnerHtml.AppendHtml(optionElIcon);
                        }
                    }
                    else
                    {
                        optionEl.Attributes.Add("title", selectedOption.Label);
                        optionEl.Attributes.Add("data-key", valueString);
                        if (String.IsNullOrWhiteSpace(selectedOption.IconClass))
                        {
                            optionEl.InnerHtml.Append(selectedOption.Label);
                        }
                        else
                        {
                            var color = "#999";
                            if (!String.IsNullOrWhiteSpace(selectedOption.Color))
                            {
                                color = selectedOption.Color;
                            }

                            optionEl.InnerHtml.AppendHtml($"<i class=\"{selectedOption.IconClass}\" style=\"color:{color}\"></i> {selectedOption.Label}");
                        }
                    }

                    formControlEl.InnerHtml.AppendHtml(optionEl);

                    output.Content.AppendHtml(formControlEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                var valueString = (Value ?? "").ToString();

                if (String.IsNullOrWhiteSpace(valueString))
                {
                    output.Content.AppendHtml("");
                }
                else
                {
                    var selectedOption = Options.FirstOrDefault(x => x.Value == valueString);
                    if (selectedOption == null)
                    {
                        output.Content.Append(valueString);
                        // in simple mode exclamation mark should not be rendered
                        //if (Value != null && (Value.ToString() != ""))
                        //{
                        //	var optionElIcon = new TagBuilder("span");
                        //	optionElIcon.AddCssClass("fa fa-fw fa-exclamation-circle go-red");
                        //	optionElIcon.Attributes.Add("title", "the value is not supported by this field anymore");
                        //	output.Content.AppendHtml(optionElIcon);
                        //}
                    }
                    else
                    {
                        if (String.IsNullOrWhiteSpace(selectedOption.IconClass))
                        {
                            output.Content.Append(selectedOption.Label);
                        }
                        else
                        {
                            var color = "#999";
                            if (!String.IsNullOrWhiteSpace(selectedOption.Color))
                            {
                                color = selectedOption.Color;
                            }

                            output.Content.AppendHtml($"<i class=\"{selectedOption.IconClass}\" style=\"color:{color}\"></i> {selectedOption.Label}");
                        }
                    }
                }
                return;
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    #region << View Wrapper >>
                    {
                        var viewWrapperEl = new TagBuilder("div");
                        viewWrapperEl.AddCssClass("input-group view-wrapper");
                        viewWrapperEl.Attributes.Add("title", "double click to edit");
                        viewWrapperEl.Attributes.Add("id", $"view-{FieldId}");

                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var viewInputPrepend = new TagBuilder("span");
                            viewInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);
                        }
                        //Control
                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-select");

                        var selectedOption = Options.FirstOrDefault(x => x.Value == (Value ?? "").ToString());
                        if (selectedOption == null)
                        {
                            viewFormControlEl.InnerHtml.Append((Value ?? "").ToString());
                            if (Value != null && (Value.ToString() != ""))
                            {
                                var optionElIcon = new TagBuilder("span");
                                optionElIcon.AddCssClass("fa fa-fw fa-exclamation-circle go-red");
                                optionElIcon.Attributes.Add("title", "the value is not supported by this field anymore");
                                viewFormControlEl.InnerHtml.AppendHtml(optionElIcon);
                            }
                        }
                        else
                        {
                            if (String.IsNullOrWhiteSpace(selectedOption.IconClass))
                            {
                                viewFormControlEl.InnerHtml.Append(selectedOption.Label);
                            }
                            else
                            {
                                var color = "#999";
                                if (!String.IsNullOrWhiteSpace(selectedOption.Color))
                                {
                                    color = selectedOption.Color;
                                }

                                viewFormControlEl.InnerHtml.AppendHtml($"<i class=\"{selectedOption.IconClass}\" style=\"color:{color}\"></i> {selectedOption.Label}");
                            }
                        }

                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        //Append
                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append action");
                        foreach (var htmlString in AppendHtml)
                        {
                            viewInputActionEl.InnerHtml.AppendHtml(htmlString);
                        }
                        viewInputActionEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white' title='edit'><i class='fa fa-fw fa-pencil-alt'></i></button>");
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl);

                        output.Content.AppendHtml(viewWrapperEl);
                    }
                    #endregion

                    #region << Edit Wrapper>>
                    {
                        var editWrapperEl = new TagBuilder("div");
                        editWrapperEl.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapperEl.Attributes.Add("style", $"display:none;");
                        editWrapperEl.AddCssClass("edit-wrapper");

                        var editInputGroupEl = new TagBuilder("div");
                        editInputGroupEl.AddCssClass("input-group");

                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var editInputPrepend = new TagBuilder("span");
                            editInputPrepend.AddCssClass("input-group-prepend");
                            foreach (var htmlString in PrependHtml)
                            {
                                editInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            editInputGroupEl.InnerHtml.AppendHtml(editInputPrepend);
                        }
                        //Control
                        var formControl         = new TagBuilder("div");
                        var inputElCssClassList = new List <string>();
                        inputElCssClassList.Add("form-control erp-select");
                        if (ValidationErrors.Count > 0)
                        {
                            inputElCssClassList.Add("is-invalid");
                        }
                        formControl.Attributes.Add("class", String.Join(' ', inputElCssClassList));

                        var selectEl = new TagBuilder("select");
                        selectEl.Attributes.Add("id", $"input-{FieldId}");
                        selectEl.Attributes.Add("name", $"{Name}");

                        var emptyOptionAdded = false;
                        if (Required)
                        {
                            selectEl.Attributes.Add("required", null);
                        }
                        else
                        {
                            var optionEl = new TagBuilder("option");
                            // Should work only with <option></option> and the select2 placeholder to be presented
                            selectEl.InnerHtml.AppendHtml(optionEl);
                            emptyOptionAdded = true;
                        }

                        selectEl.Attributes.Add("data-original-value", JsonConvert.SerializeObject((Value ?? "").ToString()));

                        foreach (var option in Options)
                        {
                            var optionEl = new TagBuilder("option");
                            optionEl.Attributes.Add("value", option.Value);
                            optionEl.Attributes.Add("data-icon", option.IconClass);
                            optionEl.Attributes.Add("data-color", option.Color);
                            if (option.Value == (Value ?? "").ToString())
                            {
                                optionEl.Attributes.Add("selected", null);
                            }
                            optionEl.InnerHtml.Append(option.Label);
                            selectEl.InnerHtml.AppendHtml(optionEl);
                        }

                        //At least one option should be in the select so it can submit
                        if (!emptyOptionAdded && Options.Count == 0)
                        {
                            var optionEl = new TagBuilder("option");
                            // Should work only with <option></option> and the select2 placeholder to be presented
                            selectEl.InnerHtml.AppendHtml(optionEl);
                        }

                        formControl.InnerHtml.AppendHtml(selectEl);
                        editInputGroupEl.InnerHtml.AppendHtml(formControl);

                        //Append
                        var editInputGroupAppendEl = new TagBuilder("span");
                        editInputGroupAppendEl.AddCssClass("input-group-append");

                        foreach (var htmlString in AppendHtml)
                        {
                            editInputGroupAppendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white save' title='save'><i class='fa fa-fw fa-check go-green'></i></button>");
                        editInputGroupAppendEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white cancel' title='cancel'><i class='fa fa-fw fa-times go-gray'></i></button>");
                        editInputGroupEl.InnerHtml.AppendHtml(editInputGroupAppendEl);

                        editWrapperEl.InnerHtml.AppendHtml(editInputGroupEl);

                        output.Content.AppendHtml(editWrapperEl);
                    }
                    #endregion

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Select2 >>
                    {
                        var wvLibraryInitialized = false;
                        var libraryItemsKey      = "WebVella-" + "select2";
                        if (ViewContext.HttpContext.Items.ContainsKey(libraryItemsKey))
                        {
                            var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[libraryItemsKey];
                            wvLibraryInitialized = tagHelperContext.Initialized;
                        }

                        if (!wvLibraryInitialized)
                        {
                            {
                                var libCssEl = new TagBuilder("link");
                                libCssEl.Attributes.Add("href", "/_content/WebVella.TagHelpers/lib/select2/css/select2.min.css");
                                libCssEl.Attributes.Add("type", "text/css");
                                libCssEl.Attributes.Add("rel", "stylesheet");
                                output.PostContent.AppendHtml(libCssEl);
                                output.PostContent.AppendHtml("\r\n\t");
                            }
                            {
                                var libCssEl = new TagBuilder("link");
                                libCssEl.Attributes.Add("href", "/_content/WebVella.TagHelpers/lib/select2-bootstrap-theme/select2-bootstrap4.css");
                                libCssEl.Attributes.Add("type", "text/css");
                                libCssEl.Attributes.Add("rel", "stylesheet");
                                output.PostContent.AppendHtml(libCssEl);
                                output.PostContent.AppendHtml("\r\n\t");
                            }

                            var libJsEl = new TagBuilder("script");
                            libJsEl.Attributes.Add("type", "text/javascript");
                            libJsEl.Attributes.Add("src", "/_content/WebVella.TagHelpers/lib/select2/js/select2.min.js");
                            output.PostContent.AppendHtml(libJsEl);
                            output.PostContent.AppendHtml("\r\n\t");

                            ViewContext.HttpContext.Items[libraryItemsKey] = new WvTagHelperContext()
                            {
                                Initialized = true
                            };
                        }
                    }
                    #endregion


                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldSelect) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldSelect) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldSelect", "WebVella.TagHelpers");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        //scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        scriptEl.InnerHtml.AppendHtml(scriptContent);
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldSelect) + "-inline-edit"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							SelectInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);


                    var fieldConfig = new WvFieldSelectConfig()
                    {
                        ApiUrl            = ApiUrl,
                        CanAddValues      = Access == WvFieldAccess.FullAndCreate ? true : false,
                        IsInvalid         = ValidationErrors.Count > 0,
                        AjaxDatasource    = AjaxDatasource,
                        AjaxDatasourceApi = AjaxDatasourceApi,
                        SelectMatchType   = SelectMatchType,
                        Placeholder       = Placeholder
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    var divEl = new TagBuilder("div");
                    divEl.AddCssClass("input-group");

                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend");
                        foreach (var htmlString in PrependHtml)
                        {
                            viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                        }
                        divEl.InnerHtml.AppendHtml(viewInputPrepend);
                    }
                    //Control
                    var formControlEl = new TagBuilder("div");
                    formControlEl.AddCssClass("form-control erp-select");

                    var optionEl = new TagBuilder("span");
                    optionEl.Attributes.Add("selected", null);
                    var selectedOption = Options.FirstOrDefault(x => x.Value == (Value ?? "").ToString());
                    if (selectedOption == null)
                    {
                        optionEl.InnerHtml.Append((Value ?? "").ToString());
                        if (Value != null && (Value.ToString() != ""))
                        {
                            var optionElIcon = new TagBuilder("span");
                            optionElIcon.AddCssClass("fa fa-fw fa-exclamation-circle go-red");
                            optionElIcon.Attributes.Add("title", "the value is not supported by this field anymore");
                            optionEl.InnerHtml.AppendHtml(optionElIcon);
                        }
                    }
                    else
                    {
                        optionEl.Attributes.Add("title", selectedOption.Label);
                        optionEl.Attributes.Add("data-key", (Value ?? "").ToString());
                        if (String.IsNullOrWhiteSpace(selectedOption.IconClass))
                        {
                            optionEl.InnerHtml.Append(selectedOption.Label);
                        }
                        else
                        {
                            var color = "#999";
                            if (!String.IsNullOrWhiteSpace(selectedOption.Color))
                            {
                                color = selectedOption.Color;
                            }

                            optionEl.InnerHtml.AppendHtml($"<i class=\"{selectedOption.IconClass}\" style=\"color:{color}\"></i> {selectedOption.Label}");
                        }
                    }
                    formControlEl.InnerHtml.AppendHtml(optionEl);

                    divEl.InnerHtml.AppendHtml(formControlEl);
                    //Append
                    var appendActionSpan = new TagBuilder("span");
                    appendActionSpan.AddCssClass("input-group-append");
                    foreach (var htmlString in AppendHtml)
                    {
                        appendActionSpan.InnerHtml.AppendHtml(htmlString);
                    }
                    appendActionSpan.InnerHtml.AppendHtml("<button type=\"button\" disabled class='btn btn-white' title='locked'><i class='fa fa-fw fa-lock'></i></button>");
                    divEl.InnerHtml.AppendHtml(appendActionSpan);

                    output.Content.AppendHtml(divEl);
                }
            }
            #endregion


            //Finally
            if (SubInputEl != null)
            {
                output.PostContent.AppendHtml(SubInputEl);
            }

            return;
        }
Ejemplo n.º 7
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return;
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return;
            }

            #region << Init Prepend and Append >>
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var prependTaghelper = htmlDoc.DocumentNode.Descendants("wv-field-prepend");
            var appendTagHelper  = htmlDoc.DocumentNode.Descendants("wv-field-append");

            foreach (var node in prependTaghelper)
            {
                PrependHtml.Add(node.InnerHtml.ToString());
            }

            foreach (var node in appendTagHelper)
            {
                AppendHtml.Add(node.InnerHtml.ToString());
            }

            #endregion

            #endregion



            #region << Render >>
            if (Mode == FieldRenderMode.Form)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    var inputEl = new TagBuilder("textarea");
                    inputEl.Attributes.Add("id", $"input-{FieldId}");
                    inputEl.Attributes.Add("name", Name);
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-html");

                    if (Required)
                    {
                        inputEl.Attributes.Add("required", null);
                    }

                    if (ValidationErrors.Count > 0)
                    {
                        inputElCssClassList.Add("is-invalid");
                    }

                    inputEl.InnerHtml.Append((Value ?? "").ToString());

                    inputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList));

                    output.Content.AppendHtml(inputEl);

                    var jsCompressor = new JavaScriptCompressor();
                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldHtml) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldHtml) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("form.js", "WebVella.Erp.Web.TagHelpers.WvFieldHtml");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        //scriptEl.InnerHtml.AppendHtml(scriptContent);
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldHtml) + "-form"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							InitHtmlFieldCKEditor(""{{FieldId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());

                    var fieldConfig = new WvFieldHtmlConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == FieldAccess.FullAndCreate ? true : false,
                        UploadMode   = UploadMode,
                        ToolbarMode  = ToolbarMode
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group");
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend erp-multilinetext {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        foreach (var htmlString in PrependHtml)
                        {
                            prependEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);
                    }
                    //Control
                    var inputEl = new TagBuilder("div");
                    inputEl.Attributes.Add("id", $"plaintext-{FieldId}");
                    inputEl.AddCssClass("form-control erp-html disabled");
                    inputEl.Attributes.Add("style", "height:auto;");
                    inputEl.InnerHtml.AppendHtml((Value ?? "").ToString());
                    inputGroupEl.InnerHtml.AppendHtml(inputEl);
                    //Append
                    if (AppendHtml.Count > 0)
                    {
                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass($"input-group-append erp-multilinetext {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        foreach (var htmlString in AppendHtml)
                        {
                            appendEl.InnerHtml.AppendHtml(htmlString);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    }
                    //Hidden input with the value
                    var hiddenInput = new TagBuilder("input");
                    hiddenInput.Attributes.Add("type", "hidden");
                    hiddenInput.Attributes.Add("id", $"input-{FieldId}");
                    hiddenInput.Attributes.Add("name", Name);
                    hiddenInput.Attributes.Add("value", (Value ?? "").ToString());
                    output.Content.AppendHtml(hiddenInput);

                    output.Content.AppendHtml(inputGroupEl);
                }
            }
            else if (Mode == FieldRenderMode.Display)
            {
                if (!String.IsNullOrWhiteSpace(Value))
                {
                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-html");
                    divEl.InnerHtml.AppendHtml((Value ?? "").ToString());
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == FieldRenderMode.Simple)
            {
                output.SuppressOutput();
                output.Content.AppendHtml((Value ?? "").ToString());
                return;
            }
            else if (Mode == FieldRenderMode.InlineEdit)
            {
                if (Access == FieldAccess.Full || Access == FieldAccess.FullAndCreate)
                {
                    #region << View Wrapper >>
                    {
                        var viewWrapperEl = new TagBuilder("div");
                        viewWrapperEl.AddCssClass("input-group view-wrapper");
                        viewWrapperEl.Attributes.Add("title", "double click to edit");
                        viewWrapperEl.Attributes.Add("id", $"view-{FieldId}");
                        //Prepend
                        if (PrependHtml.Count > 0)
                        {
                            var viewInputPrepend = new TagBuilder("span");
                            viewInputPrepend.AddCssClass("input-group-prepend erp-multilinetext");
                            foreach (var htmlString in PrependHtml)
                            {
                                viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                            }
                            viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);
                        }
                        //Control

                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-html");
                        viewFormControlEl.Attributes.Add("style", "height:auto;");
                        viewFormControlEl.InnerHtml.AppendHtml((Value ?? "").ToString());
                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        //Append
                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append erp-multilinetext");
                        foreach (var htmlString in AppendHtml)
                        {
                            viewInputActionEl.InnerHtml.AppendHtml(htmlString);
                        }
                        viewInputActionEl.InnerHtml.AppendHtml("<button type=\"button\" class='btn btn-white action' title='edit'><i class='fa fa-fw fa-pencil-alt'></i></button>");
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl);
                        output.Content.AppendHtml(viewWrapperEl);
                    }
                    #endregion

                    #region << Edit Modal >>
                    {
                        var editModalEl = new TagBuilder("div");
                        editModalEl.AddCssClass("modal");
                        editModalEl.Attributes.Add("id", $"edit-{FieldId}");
                        editModalEl.Attributes.Add("tabindex", "-1");
                        var editModalDialog = new TagBuilder("div");
                        editModalDialog.AddCssClass("modal-dialog modal-lg");
                        var editModalContent = new TagBuilder("div");
                        editModalContent.AddCssClass("modal-content");

                        var editModalHeader = new TagBuilder("div");
                        editModalHeader.AddCssClass("modal-header");
                        var editModalHeaderTitle = new TagBuilder("h5");
                        editModalHeaderTitle.AddCssClass("modal-title");
                        editModalHeaderTitle.InnerHtml.Append("edit field ");
                        var editModalHeaderTitleSpan = new TagBuilder("span");
                        editModalHeaderTitleSpan.AddCssClass("go-green");
                        editModalHeaderTitleSpan.InnerHtml.Append(Name);
                        editModalHeaderTitle.InnerHtml.AppendHtml(editModalHeaderTitleSpan);
                        editModalHeader.InnerHtml.AppendHtml(editModalHeaderTitle);
                        var editModalHeaderButton = new TagBuilder("button");
                        editModalHeaderButton.Attributes.Add("type", "button");
                        editModalHeaderButton.AddCssClass("close");
                        editModalHeaderButton.Attributes.Add("data-dismiss", "modal");
                        editModalHeaderButton.InnerHtml.AppendHtml(new TagBuilder("span").InnerHtml.AppendHtml("&times;"));
                        editModalHeader.InnerHtml.AppendHtml(editModalHeaderButton);
                        editModalContent.InnerHtml.AppendHtml(editModalHeader);

                        var editModalBody = new TagBuilder("div");
                        editModalBody.AddCssClass("modal-body");
                        var editModalBodyTextArea = new TagBuilder("textarea");
                        editModalBodyTextArea.Attributes.Add("id", $"input-{FieldId}");
                        editModalBodyTextArea.AddCssClass("form-control erp-html");
                        if (Required)
                        {
                            editModalBodyTextArea.Attributes.Add("required", null);
                        }
                        editModalBodyTextArea.InnerHtml.AppendHtml((Value ?? "").ToString());
                        editModalBody.InnerHtml.AppendHtml(editModalBodyTextArea);
                        editModalContent.InnerHtml.AppendHtml(editModalBody);

                        var editModalFooter = new TagBuilder("div");
                        editModalFooter.AddCssClass("modal-footer");
                        var editModalFooterSave = new TagBuilder("button");
                        editModalFooterSave.Attributes.Add("type", "button");
                        editModalFooterSave.AddCssClass("btn btn-primary save");
                        var editModalFooterSaveIcon = new TagBuilder("span");
                        editModalFooterSaveIcon.AddCssClass("fa fa-check");
                        editModalFooterSave.InnerHtml.AppendHtml(editModalFooterSaveIcon);
                        editModalFooterSave.InnerHtml.AppendHtml("save");
                        editModalFooter.InnerHtml.AppendHtml(editModalFooterSave);
                        var editModalFooterCancel = new TagBuilder("button");
                        editModalFooterCancel.Attributes.Add("type", "button");
                        editModalFooterCancel.AddCssClass("btn btn-secondary cancel");
                        editModalFooterCancel.InnerHtml.Append("cancel");
                        editModalFooter.InnerHtml.AppendHtml(editModalFooterCancel);
                        editModalContent.InnerHtml.AppendHtml(editModalFooter);

                        editModalDialog.InnerHtml.AppendHtml(editModalContent);
                        editModalEl.InnerHtml.AppendHtml(editModalDialog);

                        output.Content.AppendHtml(editModalEl);
                    }
                    #endregion
                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldHtml) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldHtml) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = FileService.GetEmbeddedTextResource("inline-edit.js", "WebVella.Erp.Web.TagHelpers.WvFieldHtml");
                        var scriptEl      = new TagBuilder("script");
                        scriptEl.Attributes.Add("type", "text/javascript");
                        scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent));
                        output.PostContent.AppendHtml(scriptEl);

                        ViewContext.HttpContext.Items[typeof(WvFieldHtml) + "-inline-edit"] = new WvTagHelperContext()
                        {
                            Initialized = true
                        };
                    }
                    #endregion

                    #region << Add Inline Init Script for this instance >>
                    var initScript = new TagBuilder("script");
                    initScript.Attributes.Add("type", "text/javascript");
                    var scriptTemplate = @"
						$(function(){
							HtmlInlineEditInit(""{{FieldId}}"",""{{Name}}"",""{{EntityName}}"",""{{RecordId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);
                    scriptTemplate = scriptTemplate.Replace("{{EntityName}}", EntityName);
                    scriptTemplate = scriptTemplate.Replace("{{RecordId}}", (RecordId ?? null).ToString());

                    var fieldConfig = new WvFieldHtmlConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == FieldAccess.FullAndCreate ? true : false
                    };

                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

                    initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate));

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == FieldAccess.ReadOnly)
                {
                    var divEl = new TagBuilder("div");
                    divEl.AddCssClass("input-group");
                    //Prepend
                    if (PrependHtml.Count > 0)
                    {
                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend  erp-multilinetext");
                        foreach (var htmlString in PrependHtml)
                        {
                            viewInputPrepend.InnerHtml.AppendHtml(htmlString);
                        }
                        divEl.InnerHtml.AppendHtml(viewInputPrepend);
                    }
                    //Control
                    var inputEl = new TagBuilder("div");
                    inputEl.AddCssClass("form-control erp-html disabled");
                    inputEl.InnerHtml.AppendHtml((Value ?? "").ToString());
                    divEl.InnerHtml.AppendHtml(inputEl);
                    //Append
                    var appendActionSpan = new TagBuilder("span");
                    appendActionSpan.AddCssClass("input-group-append  erp-multilinetext");
                    foreach (var htmlString in AppendHtml)
                    {
                        appendActionSpan.InnerHtml.AppendHtml(htmlString);
                    }
                    appendActionSpan.InnerHtml.AppendHtml("<button type=\"button\" disabled class='btn btn-white action' title='locked'><i class='fa fa-fw fa-lock'></i></button>");
                    divEl.InnerHtml.AppendHtml(appendActionSpan);
                    output.Content.AppendHtml(divEl);
                }
            }
            #endregion


            //Finally
            if (SubInputEl != null)
            {
                output.PostContent.AppendHtml(SubInputEl);
            }

            return;
        }