예제 #1
0
        public TagBuilder GenerateDisplayHtml(List <dynamic> fileObjects, string getHandlerPrefix)
        {
            var resultEl = new TagBuilder("div");

            if (fileObjects.Count == 0)
            {
                resultEl.AddCssClass("form-control-plaintext");
                resultEl.AddCssClass("go-gray");
                resultEl.InnerHtml.AppendHtml("No data");
                return(resultEl);
            }

            resultEl.AddCssClass("wv-field-file-multiple form-control-plaintext");
            foreach (var fileObject in fileObjects)
            {
                var fileRowEl = new TagBuilder("a");
                fileRowEl.AddCssClass("filerow");
                fileRowEl.Attributes.Add("href", getHandlerPrefix + fileObject.path);
                fileRowEl.Attributes.Add("target", "_blank");
                fileRowEl.Attributes.Add("title", getHandlerPrefix + fileObject.path);
                if (!String.IsNullOrWhiteSpace(fileObject.icon))
                {
                    fileRowEl.InnerHtml.AppendHtml($"<div class='icon'><i class='{fileObject.icon}'></i></div>");
                }

                var fileRowMetaEl = new TagBuilder("div");
                fileRowMetaEl.AddCssClass("meta");
                fileRowMetaEl.InnerHtml.AppendHtml($"<span class='link go-blue'>{(String.IsNullOrWhiteSpace(fileObject.name) ? "unknown name" : fileObject.name)}</span>");

                if (((int)fileObject.size) > 0 || ((DateTime?)fileObject.timestamp) != null || String.IsNullOrWhiteSpace((string)fileObject.author))
                {
                    var fileRowMetaDetailsEl = new TagBuilder("div");
                    fileRowMetaDetailsEl.AddCssClass("details");

                    if (((int)fileObject.size) > 0)
                    {
                        var sizeString = WvHelpers.GetSizeStringFromSize((int)fileObject.size);
                        fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='size'>{sizeString}</span>");
                    }
                    if (((DateTime?)fileObject.timestamp) != null)
                    {
                        var timestampString = ((DateTime)fileObject.timestamp).ToString("dd MMM yyyy HH:mm", Culture);
                        fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='timestamp'>{timestampString}</span>");
                    }
                    if (!String.IsNullOrWhiteSpace((string)fileObject.author))
                    {
                        fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='author'>{(string)fileObject.author}</span>");
                    }

                    fileRowMetaEl.InnerHtml.AppendHtml(fileRowMetaDetailsEl);
                }

                fileRowEl.InnerHtml.AppendHtml(fileRowMetaEl);
                resultEl.InnerHtml.AppendHtml(fileRowEl);
            }
            return(resultEl);
        }
예제 #2
0
        private List <dynamic> GetFileObjectsFromStringList(List <string> filePaths)
        {
            var resultFiles = new List <dynamic>();

            foreach (var filePath in filePaths)
            {
                var     iconClass = "fa " + WvHelpers.GetPathTypeIcon(filePath);
                var     fileName  = WvHelpers.GetFileNameFromPath(filePath);
                dynamic file      = new
                {
                    path      = filePath,
                    size      = 0,
                    name      = fileName,
                    icon      = iconClass,
                    timestamp = (DateTime?)null,
                    author    = ""
                };
                resultFiles.Add(file);
            }

            return(resultFiles);
        }
예제 #3
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");
                    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));

                    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("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.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(WvFieldMultiSelect) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldMultiSelect) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldMultiSelect", "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(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}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                    var fieldConfig = new WvFieldMultiSelectConfig()
                    {
                        ApiUrl            = ApiUrl,
                        CanAddValues      = Access == WvFieldAccess.FullAndCreate ? true : false,
                        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-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 == 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

                    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.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)
            {
                if (((List <string>)Value).Count > 0)
                {
                    var select2ContainerEl = new TagBuilder("span");
                    select2ContainerEl.Attributes.Add("id", $"input-{FieldId}");
                    select2ContainerEl.AddCssClass("select2 select2-container select2-container--bootstrap4 d-block disabled clean");

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

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

                    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);
                            if (String.IsNullOrWhiteSpace(option.IconClass))
                            {
                                optionEl.InnerHtml.Append(option.Label);
                            }
                            else
                            {
                                var color = "#999";
                                if (!String.IsNullOrWhiteSpace(option.Color))
                                {
                                    color = option.Color;
                                }

                                optionEl.InnerHtml.AppendHtml($"<i class=\"{option.IconClass}\" style=\"color:{color}\"></i> {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 == WvFieldRenderMode.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
                    {
                        if (String.IsNullOrWhiteSpace(option.IconClass))
                        {
                            valueLabels.Add(option.Label);
                        }
                        else
                        {
                            var color = "#999";
                            if (!String.IsNullOrWhiteSpace(option.Color))
                            {
                                color = option.Color;
                            }

                            valueLabels.Add($"<i class=\"{option.IconClass}\" style=\"color:{color}\"></i> {option.Label}");
                        }
                    }
                }

                output.Content.AppendHtml(String.Join(", ", valueLabels));
                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-multiselect");

                        var select2ContainerEl = new TagBuilder("span");
                        select2ContainerEl.AddCssClass("select2 select2-container select2-container--bootstrap4 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);
                                if (String.IsNullOrWhiteSpace(option.IconClass))
                                {
                                    optionEl.InnerHtml.Append(option.Label);
                                }
                                else
                                {
                                    var color = "#999";
                                    if (!String.IsNullOrWhiteSpace(option.Color))
                                    {
                                        color = option.Color;
                                    }

                                    optionEl.InnerHtml.AppendHtml($"<i class=\"{option.IconClass}\" style=\"color:{color}\"></i> {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 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-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}");
                        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("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.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);
                        }
                        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(WvFieldMultiSelect) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldMultiSelect) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldMultiSelect", "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(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}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                    var fieldConfig = new WvFieldMultiSelectConfig()
                    {
                        ApiUrl            = ApiUrl,
                        CanAddValues      = Access == WvFieldAccess.FullAndCreate ? true : false,
                        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 viewFormControlEl = new TagBuilder("div");
                    viewFormControlEl.AddCssClass("form-control erp-multiselect");

                    var select2ContainerEl = new TagBuilder("span");
                    select2ContainerEl.AddCssClass("select2 select2-container select2-container--bootstrap4 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);
                            if (String.IsNullOrWhiteSpace(option.IconClass))
                            {
                                optionEl.InnerHtml.Append(option.Label);
                            }
                            else
                            {
                                var color = "#999";
                                if (!String.IsNullOrWhiteSpace(option.Color))
                                {
                                    color = option.Color;
                                }

                                optionEl.InnerHtml.AppendHtml($"<i class=\"{option.IconClass}\" style=\"color:{color}\"></i> {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 action");
                    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;
        }
예제 #4
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }

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

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            #endregion

            #region << Render >>

            if (!String.IsNullOrWhiteSpace(Value))
            {
                if (IsEncoded)
                {
                    Value = HttpUtility.HtmlDecode(Value);
                }
            }
            else
            {
                Value = "";
            }


            var editorWrapperEl     = new TagBuilder("div");
            var wrapperCssClassList = new List <string>();
            wrapperCssClassList.Add("form-control-plaintext erp-code");
            if (ValidationErrors.Count > 0)
            {
                wrapperCssClassList.Add("is-invalid");
            }
            editorWrapperEl.Attributes.Add("class", String.Join(' ', wrapperCssClassList));

            var editorWrapper = new TagBuilder("div");
            editorWrapper.Attributes.Add("id", $"ace-{FieldId}");
            editorWrapper.Attributes.Add("style", $"height:{Height}");
            editorWrapperEl.InnerHtml.AppendHtml(editorWrapper);

            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());
            editorWrapperEl.InnerHtml.AppendHtml(hiddenInput);

            output.Content.AppendHtml(editorWrapperEl);

            var jsCompressor = new JavaScriptCompressor();

            #region << Add Ace lib >>
            {
                var tagHelperInitialized = false;
                var fileName             = "ace.js";
                if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldCode) + fileName))
                {
                    var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldCode) + fileName];
                    tagHelperInitialized = tagHelperContext.Initialized;
                }
                if (!tagHelperInitialized)
                {
                    var scriptEl = new TagBuilder("script");
                    scriptEl.Attributes.Add("type", "text/javascript");
                    scriptEl.Attributes.Add("src", "/webvella-taghelpers/lib/ace/ace.js");
                    output.PostContent.AppendHtml(scriptEl);

                    ViewContext.HttpContext.Items[typeof(WvFieldCode) + fileName] = new WvTagHelperContext()
                    {
                        Initialized = true
                    };
                }
            }
            #endregion

            #region << Init Ace script >>
            {
                var tagHelperInitialized = false;
                var fileName             = "form.js";
                if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldCode) + fileName))
                {
                    var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldCode) + fileName];
                    tagHelperInitialized = tagHelperContext.Initialized;
                }
                if (!tagHelperInitialized)
                {
                    var scriptContent = WvHelpers.GetEmbeddedTextResource(fileName, "WebVella.TagHelpers.TagHelpers.WvFieldCode", "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(WvFieldCode) + fileName] = 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(){
						CodeFormInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
					});"                    ;

            scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
            scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

            var fieldConfig = new WvFieldCodeConfig()
            {
                ApiUrl       = ApiUrl,
                CanAddValues = Access == WvFieldAccess.FullAndCreate ? true : false,
                Language     = Language,
                Theme        = Theme,
                ReadOnly     = Mode == WvFieldRenderMode.Display
            };
            scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

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

            output.PostContent.AppendHtml(initScript);
            #endregion


            #endregion



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

            return(Task.CompletedTask);
        }
예제 #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;
            }

            #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 == 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);
                }
                //Control
                var inputEl             = new TagBuilder("input");
                var inputElCssClassList = new List <string>();
                inputElCssClassList.Add("form-control erp-guid");

                inputEl.Attributes.Add("type", "text");
                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);
                    }
                }
                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-guid");
                    divEl.InnerHtml.Append((Value ?? "").ToString());
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                output.Content.Append((Value ?? "").ToString());
                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-guid");
                        viewFormControlEl.InnerHtml.Append((Value ?? "").ToString());
                        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 editInputEl = new TagBuilder("input");
                        editInputEl.AddCssClass("form-control erp-guid");
                        if (Required)
                        {
                            editInputEl.Attributes.Add("required", null);
                        }
                        if (!String.IsNullOrWhiteSpace(Placeholder))
                        {
                            editInputEl.Attributes.Add("placeholder", Placeholder);
                        }
                        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(WvFieldGuid) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldGuid) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldGuid", "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(WvFieldGuid) + "-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(){
							GuidInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

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

                    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 inputEl = new TagBuilder("input");
                    inputEl.AddCssClass("form-control erp-guid");
                    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' 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;
        }
예제 #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;
            }
            #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 == WvFieldRenderMode.Form)
            {
                var inputReadOnlyValue = "";
                var inputEditValue     = "";
                if (Value != null)
                {
                    inputReadOnlyValue = (Value as DateTime? ?? DateTime.Now).WvConvertToTZDate(TimezoneName).WvClearKind().ToString("dd MMM yyyy HH:mm", Culture);
                    inputEditValue     = (Value as DateTime? ?? DateTime.Now).WvConvertToTZDate(TimezoneName).WvClearKind().ToString("yyyy-MM-ddTHH:mm:ss");                //.ToString("yyyy-MM-ddTHH:mm:ssZ");
                }


                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-datetime");

                inputEl.Attributes.Add("type", "text");
                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);
                    }

                    inputEl.Attributes.Add("value", inputEditValue);
                }
                else if (Access == WvFieldAccess.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 == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Libraries >>
                    var wvLibraryInitialized = false;
                    var libraryItemsKey      = "WebVella-" + "flatpickr";
                    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", "/webvella-taghelpers/lib/flatpickr/flatpickr.min.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", "/webvella-taghelpers/lib/flatpickr/flatpickr.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(WvFieldDateTime) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldDateTime) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }

                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldDateTime", "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(WvFieldDateTime) + "-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(){
							InitFlatPickrDateTime(""{{FieldId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());

                    var fieldConfig = new WvFieldDateTimeConfig()
                    {
                        IsRequired = Required
                    };
                    scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig));

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                if (Value != null)
                {
                    var inputReadOnlyValue = (Value as DateTime? ?? DateTime.Now).WvConvertToTZDate(TimezoneName).WvClearKind().ToString("dd MMM yyyy HH:mm", Culture);

                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-datetime");
                    divEl.InnerHtml.Append(inputReadOnlyValue);
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                if (Value != null)
                {
                    output.SuppressOutput();
                    var inputReadOnlyValue = (Value as DateTime? ?? DateTime.Now).WvConvertToTZDate(TimezoneName).WvClearKind().ToString("dd MMM yyyy HH:mm", Culture);
                    output.Content.AppendHtml(inputReadOnlyValue);
                }
                else
                {
                    output.SuppressOutput();
                    output.Content.AppendHtml("");
                }
                return;
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    var editInpuValue      = "";
                    var readOnlyInputValue = "";
                    if (Value != null)
                    {
                        readOnlyInputValue = (Value as DateTime? ?? DateTime.Now).WvConvertToTZDate(TimezoneName).WvClearKind().ToString("dd MMM yyyy HH:mm", Culture);
                        editInpuValue      = (Value as DateTime? ?? DateTime.Now).WvConvertToTZDate(TimezoneName).WvClearKind().ToString("yyyy-MM-ddTHH:mm:ss");                    //.ToString("yyyy-MM-ddTHH:mm:ssZ");
                    }

                    #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-datetime");
                        viewFormControlEl.InnerHtml.Append(readOnlyInputValue);
                        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.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-datetime");
                        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 Libraries >>
                    var wvLibraryInitialized = false;
                    var libraryItemsKey      = "WebVella-" + "flatpickr";
                    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", "/webvella-taghelpers/lib/flatpickr/flatpickr.min.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", "/webvella-taghelpers/lib/flatpickr/flatpickr.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(WvFieldDateTime) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldDateTime) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldDateTime", "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(WvFieldDateTime) + "-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(){
							DateTimeInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

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

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

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    var readOnlyInputValue = "";
                    if (Value != null)
                    {
                        readOnlyInputValue = (Value as DateTime? ?? DateTime.Now).WvConvertToTZDate(TimezoneName).WvClearKind().ToString("dd MMM yyyy HH:mm", 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-datetime");
                    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 action");
                    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;
        }
예제 #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;
            }

            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).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;
        }
예제 #8
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }

            ElementId = Guid.NewGuid();

            output.TagName = "div";
            output.AddCssClass($"wv-echarts {CssClass}");

            var wrapperEl = new TagBuilder("div");

            wrapperEl.Attributes.Add("id", "echarts-" + ElementId);
            wrapperEl.AddCssClass("echart");
            var style = "";

            if (!String.IsNullOrWhiteSpace(Width))
            {
                style += $"width:{Width};";
            }
            if (!String.IsNullOrWhiteSpace(Height))
            {
                style += $"height:{Height};";
            }
            wrapperEl.Attributes.Add("style", style);
            output.Content.AppendHtml(wrapperEl);

            dynamic options = null;

            if (Mode == "map")
            {
                options = new
                {
                    series = new List <dynamic> {
                        new {
                            data = new List <dynamic> {
                                new {
                                    name      = "California",
                                    value     = 1,
                                    itemStyle = new {
                                        normal = new {
                                            color = "#009688"
                                        }
                                    }
                                },
                                new {
                                    name  = "Florida",
                                    value = 0
                                }
                            },
                            type      = "map",
                            mapType   = "USA",
                            top       = 0,
                            left      = 0,
                            right     = 0,
                            markPoint = new {
                                symbol = "none"
                            },
                            zoom       = 1,
                            scaleLimit = new {
                                min = 1,
                                max = 4
                            },
                            roam      = true,
                            itemStyle = new {
                                emphasis = new {
                                    areaColor = "#ccc",
                                    color     = "#ccc",
                                    label     = new {
                                        color = "#333"
                                    }
                                }
                            }
                        }
                    }
                };
            }
            else if (Mode == "match")
            {
                options = new
                {
                    radar = new
                    {
                        indicator = new List <dynamic> {
                            new{
                                name = "profile",
                                max  = 100
                            },
                            new{
                                name = "time",
                                max  = 100
                            },
                            new{
                                name = "location",
                                max  = 100
                            },
                            new{
                                name = "experience",
                                max  = 100
                            },
                            new{
                                name = "certification",
                                max  = 100
                            },
                        }
                    },
                    series = new List <dynamic> {
                        new {
                            type  = "radar",
                            top   = 0,
                            left  = 0,
                            right = 0,
                            data  = new List <dynamic> {
                                new{
                                    areaStyle = new { color = "#009688", opacity = 0.25 },
                                    lineStyle = new { color = "#009688" },
                                    value     = new List <int> {
                                        55, 25, 5, 75, 34
                                    }
                                }
                            }
                        }
                    }
                };
            }
            else if (Mode == "expertise")
            {
                options = new
                {
                    legend = new {
                        bottom = 0,
                        top    = "auto",
                        data   = new List <dynamic> {
                            new {
                                name = "certification",
                                icon = "circle"
                            },
                            new {
                                name = "experience",
                                icon = "circle"
                            },
                            new {
                                name = "selection",
                                icon = "circle"
                            }
                        }
                    },
                    radar = new
                    {
                        indicator = new List <dynamic> {
                            new{
                                name = "Securely Provision",
                                max  = 100
                            },
                            new{
                                name = "Operate and Maintain",
                                max  = 100
                            },
                            new{
                                name = "Oversee and Govern",
                                max  = 100
                            },
                            new{
                                name = "Protect and Defend",
                                max  = 100
                            },
                            new{
                                name = "Analyze",
                                max  = 100
                            },
                            new{
                                name = "Collect and Operate",
                                max  = 100
                            },
                            new{
                                name = "Investigate",
                                max  = 100
                            },
                        }
                    },
                    series = new List <dynamic> {
                        new {
                            type  = "radar",
                            top   = 0,
                            left  = 0,
                            right = 0,
                            data  = new List <dynamic> {
                                new{
                                    areaStyle = new { color = "#009688", opacity = 0.25 },
                                    lineStyle = new { color = "#009688" },
                                    itemStyle = new { color = "#009688" },
                                    value     = new List <int> {
                                        55, 0, 0, 0, 34, 34, 0
                                    },
                                    name = "certification"
                                },
                                new{
                                    areaStyle = new { color = "#E91E63", opacity = 0.25 },
                                    lineStyle = new { color = "#E91E63" },
                                    itemStyle = new { color = "#E91E63" },
                                    value     = new List <int> {
                                        12, 33, 55, 12, 1, 33, 45
                                    },
                                    name = "experience"
                                },
                                new{
                                    areaStyle = new { color = "#2196F3", opacity = 0.25 },
                                    lineStyle = new { color = "#2196F3" },
                                    itemStyle = new { color = "#2196F3" },
                                    value     = new List <int> {
                                        85, 45, 34, 0, 0, 33, 44
                                    },
                                    name = "selection"
                                }
                            }
                        }
                    }
                };
            }
            else if (Mode == "calendar")
            {
                var calendarData = new List <dynamic>();
                for (int i = 0; i < 190; i++)
                {
                    Random r      = new Random();
                    var    number = r.Next(0, 3);
                    var    date   = DateTime.ParseExact("2019-10-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture).AddDays(i);
                    calendarData.Add(new List <dynamic> {
                        date.ToString("yyyy-MM-dd"), number
                    });
                }

                options = new
                {
                    visualMap = new {
                        show  = false,
                        min   = 0,
                        max   = 2,
                        color = new List <string> {
                            "#E57373", "#FFB74D", "#eee"
                        }
                    },
                    calendar = new {
                        range = new List <string> {
                            "2019-10", "2020-4"
                        },
                        splitLine = new {
                            lineStyle = new {
                                color      = "#fff",
                                width      = 3,
                                shadowBlur = 0,
                                opacity    = 1
                            }
                        },
                        itemStyle = new {
                            borderColor = "#fff",
                            borderWidth = 1,
                            shadowBlur  = 0,
                            opacity     = 1
                        }
                    },
                    series = new List <dynamic> {
                        new {
                            type             = "heatmap",
                            coordinateSystem = "calendar",
                            left             = 0,
                            right            = 0,
                            data             = calendarData
                        }
                    }
                };
            }
            else if (Mode == "hits")
            {
                var calendarData   = new List <dynamic>();
                var calendaryDays  = new List <string>();
                var calendarValues = new List <int>();
                for (int i = 0; i < 190; i++)
                {
                    Random r      = new Random();
                    var    number = r.Next(0, 3);
                    var    date   = DateTime.ParseExact("2019-10-01", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture).AddDays(i);
                    calendarData.Add(new List <dynamic> {
                        date.ToString("yyyy-MM-dd"), number
                    });

                    calendaryDays.Add(date.ToString("MM/dd"));
                    calendarValues.Add(r.Next(0, 300));
                }

                options = new
                {
                    visualMap = new {
                        show  = false,
                        min   = 0,
                        max   = 300,
                        color = new List <string> {
                            "#009688", "#E0F2F1"
                        }
                    },
                    xAxis = new List <dynamic> {
                        new {
                            data = calendaryDays
                        }
                    },
                    yAxis = new List <dynamic> {
                        new {
                            splitLine = new { show = false }
                        }
                    },
                    series = new List <dynamic> {
                        new {
                            type       = "line",
                            showSymbol = false,
                            data       = calendarValues
                        }
                    }
                };
            }

            var jsCompressor = new JavaScriptCompressor();

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

                if (!wvLibraryInitialized)
                {
                    {
                        var libJsEl = new TagBuilder("script");
                        libJsEl.Attributes.Add("type", "text/javascript");
                        libJsEl.Attributes.Add("src", "/webvella-taghelpers/lib/echarts/echarts-en.js");
                        output.PostContent.AppendHtml(libJsEl);
                        output.PostContent.AppendHtml("\r\n\t");
                    }

                    ViewContext.HttpContext.Items[libraryItemsKey] = 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 = "";
            if (Mode == "map")
            {
                scriptTemplate = @"
						$(function(){
	var myChart = echarts.init(document.getElementById(""echarts-{{ELEMENTID}}""));

	var usaJson = {{USAJSON}}

    echarts.registerMap('USA', usaJson, {
        Alaska: {              
            left: -131,
            top: 25,
            width: 15
        },
        Hawaii: {
            left: -110,        
            top: 28,
            width: 5
        },
        'Puerto Rico': {       
            left: -76,
            top: 26,
            width: 2
        }
    });

    myChart.setOption({{OPTIONS}});

myChart.on('click', function (params) {
    var stateName = params.name;
	var options = myChart.getOption();
	var stateIndex = _.findIndex(options.series[0].data,function(state){return state.name === stateName});
	if(stateIndex > -1){
		var state = options.series[0].data[stateIndex];
		if(state.value === 1){
			state.value = 0;
			delete state.itemStyle;
		}
		else{
			state.value = 1;
			state.itemStyle = {};
			state.itemStyle.normal = {};
			state.itemStyle.normal.color = '#009688';
		}
	}
	else{
		var state = {
			name: stateName,
			value: 1,
			itemStyle:{
				normal:{
					color: '#009688'
				}
			}
		}
		options.series[0].data.push(state);
	}
	myChart.setOption(options);
});

	$(window).on('resize', function(){
        if(myChart != null && myChart != undefined){
            myChart.resize();
        }
    });

	});"    ;
            }
            else if (Mode == "match" || Mode == "expertise" || Mode == "calendar" || Mode == "hits")
            {
                scriptTemplate = @"
						$(function(){
	var myChart = echarts.init(document.getElementById(""echarts-{{ELEMENTID}}""));

    myChart.setOption({{OPTIONS}});



	$(window).on('resize', function(){
        if(myChart != null && myChart != undefined){
            myChart.resize();
        }
    });

	});"    ;
            }


            var fieldConfig = new WvFieldSelectConfig()
            {
            };
            var usaJson = WvHelpers.GetEmbeddedTextResource("usa.json", "WebVella.TagHelpers.TagHelpers.WvECharts", "WebVella.TagHelpers");
            scriptTemplate = scriptTemplate.Replace("{{OPTIONS}}", JsonConvert.SerializeObject(options));
            scriptTemplate = scriptTemplate.Replace("{{USAJSON}}", usaJson);
            scriptTemplate = scriptTemplate.Replace("{{ELEMENTID}}", ElementId.ToString());

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

            output.PostContent.AppendHtml(initScript);
            #endregion


            return(Task.CompletedTask);
        }
예제 #9
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            if (String.IsNullOrWhiteSpace(TextTrue))
            {
                TextTrue = "selected";
            }

            if (String.IsNullOrWhiteSpace(TextFalse))
            {
                TextFalse = "not selected";
            }

            #endregion


            #region << Render >>
            if (Mode == WvFieldRenderMode.Form)
            {
                var wrapper1 = new TagBuilder("div");
                wrapper1.AddCssClass("form-control-plaintext erp-checkbox");
                var wrapper2 = new TagBuilder("div");
                wrapper2.AddCssClass("form-check");
                var labelWrapper = new TagBuilder("label");
                labelWrapper.AddCssClass("form-check-label");

                var inputHidden = new TagBuilder("input");
                inputHidden.Attributes.Add("data-source-id", $"input-{FieldId}");
                inputHidden.Attributes.Add("type", "hidden");
                inputHidden.Attributes.Add("name", Name);
                inputHidden.Attributes.Add("value", (Value ?? "").ToString().ToLowerInvariant());
                labelWrapper.InnerHtml.AppendHtml(inputHidden);

                var inputFake = new TagBuilder("input");
                inputFake.Attributes.Add("id", $"input-{FieldId}");
                inputFake.Attributes.Add("type", "checkbox");
                inputFake.Attributes.Add("value", "true");
                inputFake.Attributes.Add("data-field-name", Name);
                if (Access == WvFieldAccess.ReadOnly)
                {
                    inputFake.Attributes.Add("readonly", null);
                }

                inputFake.AddCssClass($"form-check-input {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                if (Value != null && Value)
                {
                    inputFake.Attributes.Add("checked", "checked");
                }
                labelWrapper.InnerHtml.AppendHtml(inputFake);

                labelWrapper.InnerHtml.AppendHtml(TextTrue);
                wrapper2.InnerHtml.AppendHtml(labelWrapper);
                wrapper1.InnerHtml.AppendHtml(wrapper2);

                output.Content.AppendHtml(wrapper1);


                var jsCompressor = new JavaScriptCompressor();
                #region << Init Scripts >>
                var tagHelperInitialized = false;
                if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldCheckbox) + "-form"))
                {
                    var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldCheckbox) + "-form"];
                    tagHelperInitialized = tagHelperContext.Initialized;
                }
                if (!tagHelperInitialized)
                {
                    var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldCheckbox", "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(WvFieldCheckbox) + "-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(){
							CheckboxFormInit(""{{FieldId}}"");
						});"                        ;
                scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));

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

                output.PostContent.AppendHtml(initScript);
                #endregion
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                var wrapper = new TagBuilder("div");
                wrapper.Attributes.Add("id", $"input-{FieldId}");
                wrapper.AddCssClass("form-control-plaintext erp-checkbox");
                if (!String.IsNullOrWhiteSpace(Name))
                {
                    wrapper.Attributes.Add("data-field-name", Name);
                }
                wrapper.Attributes.Add("data-field-value", (Value ?? "").ToString().ToLowerInvariant());

                if (Value != null)
                {
                    var iconEl = new TagBuilder("span");
                    iconEl.AddCssClass($"go-gray mr-1 fa fa-{(Value ? "check" : "times")}");
                    wrapper.InnerHtml.AppendHtml(iconEl);
                    wrapper.InnerHtml.AppendHtml(Value ? TextTrue : TextFalse);
                }
                output.Content.AppendHtml(wrapper);
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                if (Value != null)
                {
                    output.SuppressOutput();
                    var iconEl = new TagBuilder("span");
                    iconEl.InnerHtml.AppendHtml($"{(Value ? TextTrue : TextFalse)}");
                    output.Content.AppendHtml(iconEl);
                }
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    //View
                    {
                        var viewWrapper = new TagBuilder("div");
                        viewWrapper.Attributes.Add("id", $"view-{FieldId}");
                        viewWrapper.AddCssClass("input-group view-wrapper");

                        var prependWrapper = new TagBuilder("span");
                        prependWrapper.AddCssClass("input-group-prepend");

                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass("input-group-text");

                        var prependIcon = new TagBuilder("span");
                        if (Value != null)
                        {
                            prependIcon.AddCssClass($"fa fa-fw fa-{(Value ? "check" : "times")}");
                        }
                        prependEl.InnerHtml.AppendHtml(prependIcon);
                        prependWrapper.InnerHtml.AppendHtml(prependEl);
                        viewWrapper.InnerHtml.AppendHtml(prependWrapper);

                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass($"form-control erp-checkbox {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        viewFormControlEl.Attributes.Add("title", "double click to edit");
                        if (Value != null)
                        {
                            viewFormControlEl.InnerHtml.AppendHtml(Value ? TextTrue : TextFalse);
                        }
                        viewWrapper.InnerHtml.AppendHtml(viewFormControlEl);


                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass("input-group-append action");
                        var appendButton = new TagBuilder("button");
                        appendButton.Attributes.Add("type", "button");
                        appendButton.Attributes.Add("title", "edit");
                        appendButton.AddCssClass("btn btn-white ");
                        var appendIcon = new TagBuilder("span");
                        appendIcon.AddCssClass("fa fa-fw fa-pencil-alt");
                        appendButton.InnerHtml.AppendHtml(appendIcon);
                        appendEl.InnerHtml.AppendHtml(appendButton);
                        viewWrapper.InnerHtml.AppendHtml(appendEl);

                        output.Content.AppendHtml(viewWrapper);
                    }

                    //Edit
                    {
                        var editWrapper = new TagBuilder("div");
                        editWrapper.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapper.AddCssClass("edit-wrapper");
                        editWrapper.Attributes.Add("style", "display:none;");
                        var inputGroupEl = new TagBuilder("div");
                        inputGroupEl.AddCssClass("input-group erp-checkbox");
                        var editFormControlEl = new TagBuilder("div");
                        editFormControlEl.AddCssClass("form-control erp-checkbox");
                        var editFormCheckEl = new TagBuilder("div");
                        editFormCheckEl.AddCssClass("form-check");
                        var editFormLabelEl = new TagBuilder("label");
                        editFormLabelEl.AddCssClass("form-check-label");
                        var editInputEl = new TagBuilder("input");
                        editInputEl.Attributes.Add("type", "checkbox");
                        editInputEl.AddCssClass("form-check-input");
                        if (Value != null && Value)
                        {
                            editInputEl.Attributes.Add("checked", "checked");
                        }
                        editInputEl.Attributes.Add("value", "true");
                        editFormLabelEl.InnerHtml.AppendHtml(editInputEl);
                        if (Value != null)
                        {
                            editFormLabelEl.InnerHtml.AppendHtml(TextTrue);
                        }
                        editFormCheckEl.InnerHtml.AppendHtml(editFormLabelEl);
                        editFormControlEl.InnerHtml.AppendHtml(editFormCheckEl);
                        inputGroupEl.InnerHtml.AppendHtml(editFormControlEl);

                        var editInputAppend = new TagBuilder("span");
                        editInputAppend.AddCssClass("input-group-append");
                        var editSaveBtn = new TagBuilder("button");
                        editSaveBtn.Attributes.Add("type", "submit");
                        editSaveBtn.AddCssClass("btn btn-white save");
                        editSaveBtn.Attributes.Add("title", "Save");
                        var editSaveIcon = new TagBuilder("span");
                        editSaveIcon.AddCssClass("fa fa-fw fa-check go-green");
                        editSaveBtn.InnerHtml.AppendHtml(editSaveIcon);
                        editInputAppend.InnerHtml.AppendHtml(editSaveBtn);

                        var editCancelBtn = new TagBuilder("button");
                        editCancelBtn.Attributes.Add("type", "submit");
                        editCancelBtn.AddCssClass("btn btn-white cancel");
                        editCancelBtn.Attributes.Add("title", "Cancel");
                        var editCancelIcon = new TagBuilder("span");
                        editCancelIcon.AddCssClass("fa fa-fw fa-times go-gray");
                        editCancelBtn.InnerHtml.AppendHtml(editCancelIcon);
                        editInputAppend.InnerHtml.AppendHtml(editCancelBtn);

                        inputGroupEl.InnerHtml.AppendHtml(editInputAppend);
                        editWrapper.InnerHtml.AppendHtml(inputGroupEl);
                        output.Content.AppendHtml(editWrapper);



                        var jsCompressor = new JavaScriptCompressor();
                        #region << Init Scripts >>
                        var tagHelperInitialized = false;
                        if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldCheckbox) + "-inline-edit"))
                        {
                            var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldCheckbox) + "-inline-edit"];
                            tagHelperInitialized = tagHelperContext.Initialized;
                        }
                        if (!tagHelperInitialized)
                        {
                            var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldCheckbox", "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(WvFieldCheckbox) + "-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(){
							CheckboxInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                        scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                        scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                        var fieldConfig = new WvFieldCheckboxConfig()
                        {
                            ApiUrl       = ApiUrl,
                            CanAddValues = Access == WvFieldAccess.FullAndCreate ? true : false,
                            TrueLabel    = TextTrue,
                            FalseLabel   = TextFalse
                        };

                        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");

                    var prependWrapper = new TagBuilder("span");
                    prependWrapper.AddCssClass("input-group-prepend");

                    var prependEl = new TagBuilder("span");
                    prependEl.AddCssClass("input-group-text");

                    var prependIcon = new TagBuilder("span");
                    prependIcon.AddCssClass($"fa fa-fw fa-{(Value ? "check" : "times")}");
                    prependEl.InnerHtml.AppendHtml(prependIcon);
                    prependWrapper.InnerHtml.AppendHtml(prependEl);
                    divEl.InnerHtml.AppendHtml(prependWrapper);



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

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

                    var appendTextSpan = new TagBuilder("span");
                    appendTextSpan.AddCssClass("input-group-text");

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

                    appendTextSpan.InnerHtml.AppendHtml(appendIconSpan);

                    appendActionSpan.InnerHtml.AppendHtml(appendTextSpan);

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



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

            return(Task.CompletedTask);
        }
예제 #10
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }

            //ValueGuid
            if (Value is string && ((string)Value).StartsWith("["))
            {
                ValueGrid = JsonConvert.DeserializeObject <List <WvKeyStringList> >((string)Value);
            }
            else
            {
                ValueGrid = new List <WvKeyStringList>();
            }


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

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            if (String.IsNullOrWhiteSpace(TextTrue))
            {
                TextTrue = "selected";
            }

            if (String.IsNullOrWhiteSpace(TextFalse))
            {
                TextFalse = "not selected";
            }

            #endregion


            #region << Render >>
            //Check if all columns or rows are with empty string as label
            var allColumnsWithEmptyLabels = true;
            var allRowsWithEmptyLabels    = true;
            foreach (var column in Columns)
            {
                if (!String.IsNullOrWhiteSpace(column.Label))
                {
                    allColumnsWithEmptyLabels = false;
                    break;
                }
            }
            foreach (var row in Rows)
            {
                if (!String.IsNullOrWhiteSpace(row.Label))
                {
                    allRowsWithEmptyLabels = false;
                    break;
                }
            }

            if (Mode == WvFieldRenderMode.Form)
            {
                var wrapper1 = new TagBuilder("div");
                wrapper1.AddCssClass("form-control-plaintext erp-checkbox-grid erp-list");

                var tableEl = new TagBuilder("table");
                tableEl.AddCssClass("table table-bordered table-hover table-sm mb-0");

                if (!allColumnsWithEmptyLabels)
                {
                    var tableHead        = new TagBuilder("thead");
                    var tableHeadTr      = new TagBuilder("tr");
                    var tableHeadFirstTh = new TagBuilder("th");
                    tableHeadTr.InnerHtml.AppendHtml(tableHeadFirstTh);

                    foreach (var column in Columns)
                    {
                        var tableHeadTh = new TagBuilder("th");
                        tableHeadTh.InnerHtml.Append(column.Label);
                        tableHeadTr.InnerHtml.AppendHtml(tableHeadTh);
                    }
                    tableHead.InnerHtml.AppendHtml(tableHeadTr);
                    tableEl.InnerHtml.AppendHtml(tableHead);
                }
                var tableBodyEl = new TagBuilder("tbody");
                foreach (var row in Rows)
                {
                    var rowValuesIndex = ValueGrid.FindIndex(x => x.Key == row.Value);
                    var rowValues      = new List <string>();
                    if (rowValuesIndex > -1)
                    {
                        rowValues = ValueGrid[rowValuesIndex].Values;
                    }
                    var rowTrEl = new TagBuilder("tr");
                    if (!allRowsWithEmptyLabels)
                    {
                        var rowFirstTdEl = new TagBuilder("td");
                        rowFirstTdEl.InnerHtml.Append(row.Label);
                        rowTrEl.InnerHtml.AppendHtml(rowFirstTdEl);
                    }
                    foreach (var column in Columns)
                    {
                        var isChecked  = rowValues.Contains(column.Value);
                        var columnTdEl = new TagBuilder("td");

                        var wrapper2 = new TagBuilder("div");
                        wrapper2.AddCssClass("form-check");
                        var labelWrapper = new TagBuilder("label");
                        labelWrapper.AddCssClass("form-check-label");

                        var inputFake = new TagBuilder("input");
                        inputFake.Attributes.Add("data-row-key", $"{row.Value}");
                        inputFake.Attributes.Add("type", "checkbox");
                        inputFake.AddCssClass($"gchk-{FieldId}");
                        inputFake.Attributes.Add("value", $"{column.Value}");
                        if (Access == WvFieldAccess.ReadOnly)
                        {
                            inputFake.Attributes.Add("readonly", null);
                        }

                        inputFake.AddCssClass($"form-check-input {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        if (isChecked)
                        {
                            inputFake.Attributes.Add("checked", "checked");
                        }
                        labelWrapper.InnerHtml.AppendHtml(inputFake);

                        labelWrapper.InnerHtml.AppendHtml(TextTrue);
                        wrapper2.InnerHtml.AppendHtml(labelWrapper);

                        columnTdEl.InnerHtml.AppendHtml(wrapper2);
                        rowTrEl.InnerHtml.AppendHtml(columnTdEl);
                    }
                    tableBodyEl.InnerHtml.AppendHtml(rowTrEl);
                }

                tableEl.InnerHtml.AppendHtml(tableBodyEl);

                wrapper1.InnerHtml.AppendHtml(tableEl);

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

                output.Content.AppendHtml(wrapper1);


                var jsCompressor = new JavaScriptCompressor();
                #region << Init Scripts >>
                var tagHelperInitialized = false;
                var fileName             = "form.js";
                if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldCheckboxGrid) + fileName))
                {
                    var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldCheckboxGrid) + fileName];
                    tagHelperInitialized = tagHelperContext.Initialized;
                }
                if (!tagHelperInitialized)
                {
                    var scriptContent = WvHelpers.GetEmbeddedTextResource(fileName, "WebVella.TagHelpers.TagHelpers.WvFieldCheckboxGrid", "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(WvFieldCheckboxGrid) + fileName] = 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(){
							CheckboxGridFormInit(""{{FieldId}}"");
						});"                        ;
                scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));

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

                output.PostContent.AppendHtml(initScript);
                #endregion
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                var wrapper1 = new TagBuilder("div");
                wrapper1.Attributes.Add("id", $"input-{FieldId}");
                wrapper1.AddCssClass("form-control-plaintext erp-checkbox-grid erp-list");

                var tableEl = new TagBuilder("table");
                tableEl.AddCssClass("table table-bordered table-hover table-sm mb-0");

                if (!allColumnsWithEmptyLabels)
                {
                    var tableHead        = new TagBuilder("thead");
                    var tableHeadTr      = new TagBuilder("tr");
                    var tableHeadFirstTh = new TagBuilder("th");
                    tableHeadTr.InnerHtml.AppendHtml(tableHeadFirstTh);

                    foreach (var column in Columns)
                    {
                        var tableHeadTh = new TagBuilder("th");
                        tableHeadTh.InnerHtml.Append(column.Label);
                        tableHeadTr.InnerHtml.AppendHtml(tableHeadTh);
                    }
                    tableHead.InnerHtml.AppendHtml(tableHeadTr);
                    tableEl.InnerHtml.AppendHtml(tableHead);
                }
                var tableBodyEl = new TagBuilder("tbody");
                foreach (var row in Rows)
                {
                    var rowValuesIndex = ValueGrid.FindIndex(x => x.Key == row.Value);
                    var rowValues      = new List <string>();
                    if (rowValuesIndex > -1)
                    {
                        rowValues = ValueGrid[rowValuesIndex].Values;
                    }
                    var rowTrEl = new TagBuilder("tr");
                    if (!allRowsWithEmptyLabels)
                    {
                        var rowFirstTdEl = new TagBuilder("td");
                        rowFirstTdEl.InnerHtml.Append(row.Label);
                        rowTrEl.InnerHtml.AppendHtml(rowFirstTdEl);
                    }
                    foreach (var column in Columns)
                    {
                        var isChecked  = rowValues.Contains(column.Value);
                        var columnTdEl = new TagBuilder("td");

                        var iconEl = new TagBuilder("span");
                        iconEl.AddCssClass($"{(isChecked ? "fa fa-check" : "go-gray")}");
                        iconEl.InnerHtml.Append($"{(isChecked ? "" : "-")}");

                        columnTdEl.InnerHtml.AppendHtml(iconEl);
                        rowTrEl.InnerHtml.AppendHtml(columnTdEl);
                    }
                    tableBodyEl.InnerHtml.AppendHtml(rowTrEl);
                }

                tableEl.InnerHtml.AppendHtml(tableBodyEl);

                wrapper1.InnerHtml.AppendHtml(tableEl);

                output.Content.AppendHtml(wrapper1);
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                var alertEl = new TagBuilder("div");
                alertEl.AddCssClass($"alert alert-danger");
                alertEl.InnerHtml.Append($"Not Implemented yet");
                output.Content.AppendHtml(alertEl);
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                var alertEl = new TagBuilder("div");
                alertEl.AddCssClass($"alert alert-danger");
                alertEl.InnerHtml.Append($"Not Implemented yet");
                output.Content.AppendHtml(alertEl);
            }
            #endregion



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

            return(Task.CompletedTask);
        }
예제 #11
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 == WvFieldRenderMode.Form)
            {
                var inputGroupEl = new TagBuilder("div");
                inputGroupEl.AddCssClass("input-group");

                if (Size == WvCssSize.Large)
                {
                    inputGroupEl.AddCssClass("input-group-lg");
                }
                if (Size == WvCssSize.Small)
                {
                    inputGroupEl.AddCssClass("input-group-sm");
                }

                //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 == WvFieldAccess.Full || Access == WvFieldAccess.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 == 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 (!String.IsNullOrWhiteSpace(Value))
                {
                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-text");
                    if (Size == WvCssSize.Large)
                    {
                        divEl.AddCssClass("input-group-lg");
                    }
                    if (Size == WvCssSize.Small)
                    {
                        divEl.AddCssClass("input-group-sm");
                    }

                    /*
                     * Append an anchor field with the provided link value, if it is present
                     * Feature: LInkable Text Field
                     * Author: Amarjeet-L
                     */
                    if (Link != null && Link != "")
                    {
                        divEl.InnerHtml.AppendHtml("<a href='" + Link + "' target='_blank'>" + (Value ?? "").ToString() + "</a>");
                    }
                    else
                    {
                        divEl.InnerHtml.Append((Value ?? "").ToString());
                    }
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();

                /*
                 * Append an anchor field with the provided link value, if it is present
                 * Feature: LInkable Text Field
                 * Author: Amarjeet-L
                 */
                if (Link != null && Link != "")
                {
                    output.Content.AppendHtml("<a href='" + Link + "' target='_blank'>" + (Value ?? "").ToString() + "</a>");
                }
                else
                {
                    output.Content.Append((Value ?? "").ToString());
                }
                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");
                        if (Size == WvCssSize.Large)
                        {
                            viewWrapperEl.AddCssClass("input-group-lg");
                        }
                        if (Size == WvCssSize.Small)
                        {
                            viewWrapperEl.AddCssClass("input-group-sm");
                        }

                        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 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);

                        /*
                         * Append an anchor field with the provided link value, if it is present
                         * Feature: LInkable Text Field
                         * Author: Amarjeet-L
                         */
                        if (Link != "" && Link != null)
                        {
                            var linkInputActionEl = new TagBuilder("span");
                            linkInputActionEl.AddCssClass("input-group-append");
                            foreach (var htmlString in AppendHtml)
                            {
                                linkInputActionEl.InnerHtml.AppendHtml(htmlString);
                            }
                            linkInputActionEl.InnerHtml.AppendHtml("<a href='" + Link + "' target='_blank' class='btn btn-white' title='details'><i class='fas fa-external-link-alt'></i></a>");
                            viewWrapperEl.InnerHtml.AppendHtml(linkInputActionEl);
                        }
                        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");
                        if (Size == WvCssSize.Large)
                        {
                            editInputGroupEl.AddCssClass("input-group-lg");
                        }
                        if (Size == WvCssSize.Small)
                        {
                            editInputGroupEl.AddCssClass("input-group-sm");
                        }

                        //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 = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldText", "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(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}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

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

                    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");
                    if (Size == WvCssSize.Large)
                    {
                        divEl.AddCssClass("input-group-lg");
                    }
                    if (Size == WvCssSize.Small)
                    {
                        divEl.AddCssClass("input-group-sm");
                    }
                    //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' 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;
        }
예제 #12
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }

            if (String.IsNullOrWhiteSpace(Id))
            {
                Id = Guid.NewGuid().ToString();
            }

            #region << Init >>

            if (LabelMode == WvLabelRenderMode.Undefined)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey(typeof(WvLabelRenderMode)))
                {
                    LabelMode = (WvLabelRenderMode)context.Items[typeof(WvLabelRenderMode)];
                }
                else
                {
                    LabelMode = WvLabelRenderMode.Stacked;
                }
            }

            if (FieldMode == WvFieldRenderMode.Undefined)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey(typeof(WvFieldRenderMode)))
                {
                    FieldMode = (WvFieldRenderMode)context.Items[typeof(WvFieldRenderMode)];
                }
                else
                {
                    FieldMode = WvFieldRenderMode.Form;
                }
            }

            if (Size == WvCssSize.Inherit)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey(typeof(WvCssSize)))
                {
                    Size = (WvCssSize)context.Items[typeof(WvCssSize)];
                }
                else
                {
                    Size = WvCssSize.Normal;
                }
            }
            #endregion

            if (!IsCard)
            {
                output.TagName = "div";
                output.AddCssClass("lns");
                output.AddCssClass(Class);
                output.Attributes.Add("id", Id);

                if (!String.IsNullOrWhiteSpace(Title))
                {
                    var titleEl = new TagBuilder(TitleTag);
                    titleEl.AddCssClass($"lns-header");

                    if (!IsCollapsable)
                    {
                        output.AddCssClass("lns-nc");
                        titleEl.InnerHtml.AppendHtml("<span class=\"title\"><i class=\"icon fas fa-fw fa-circle\"></i>" + Title + "</span>");
                    }
                    else
                    {
                        output.AddCssClass("lns-c");
                        var collapseLink = new TagBuilder("a");
                        collapseLink.Attributes.Add("data-toggle", "collapse");
                        collapseLink.Attributes.Add("href", "#collapse-" + Id);
                        var iconEl = new TagBuilder("span");
                        if (IsCollapsed)
                        {
                            iconEl.AddCssClass("icon fas fa-fw fa-caret-right");
                            collapseLink.AddCssClass("collapsed");
                            collapseLink.Attributes.Add("aria-expanded", "false");
                        }
                        else
                        {
                            iconEl.AddCssClass("icon fas fa-fw fa-caret-down");
                            collapseLink.Attributes.Add("aria-expanded", "true");
                        }
                        collapseLink.InnerHtml.AppendHtml(iconEl);
                        collapseLink.InnerHtml.AppendHtml(Title);
                        titleEl.InnerHtml.AppendHtml(collapseLink);
                    }
                    output.PreContent.AppendHtml(titleEl);
                }

                if (IsCollapsable)
                {
                    var collapseWrapper = new TagBuilder("div");
                    collapseWrapper.AddCssClass("collapse multi-collapse");
                    collapseWrapper.AddCssClass(BodyClass);
                    if (!IsCollapsed)
                    {
                        collapseWrapper.AddCssClass("show");
                    }
                    collapseWrapper.Attributes.Add("id", "collapse-" + Id);
                    output.PreContent.AppendHtml(collapseWrapper.RenderStartTag());
                    output.PostContent.AppendHtml(collapseWrapper.RenderEndTag());
                }
            }
            else
            {
                //Collapsable card not implemented - check bootstrap for future implementation guidence
                output.TagName = "div";
                output.AddCssClass("card lns");
                output.AddCssClass(Class);
                output.Attributes.Add("id", Id);

                if (!String.IsNullOrWhiteSpace(Title))
                {
                    if (!IsCollapsable)
                    {
                        output.AddCssClass("lns-nc");
                        var cardHeader = new TagBuilder("div");
                        cardHeader.AddCssClass($"card-header {TitleClass}");

                        //cardHeader.InnerHtml.AppendHtml("<div class='float-right'>test</div>");

                        var titleEl = new TagBuilder(TitleTag);
                        titleEl.AddCssClass("lns-header m-0");
                        titleEl.InnerHtml.AppendHtml(Title);
                        cardHeader.InnerHtml.AppendHtml(titleEl);
                        output.PreContent.AppendHtml(cardHeader);
                    }
                    else
                    {
                        output.AddCssClass("lns-c");
                        var cardHeader = new TagBuilder("a");
                        cardHeader.AddCssClass($"card-header pl-1 lns-header {TitleClass}");
                        cardHeader.Attributes.Add("data-toggle", "collapse");
                        cardHeader.Attributes.Add("href", "#collapse-" + Id);
                        var iconEl = new TagBuilder("span");
                        if (IsCollapsed)
                        {
                            iconEl.AddCssClass("icon fa fa-fw fa-caret-right");
                            cardHeader.AddCssClass("collapsed");
                            cardHeader.Attributes.Add("aria-expanded", "false");
                        }
                        else
                        {
                            iconEl.AddCssClass("icon fa fa-fw fa-caret-down");
                            cardHeader.Attributes.Add("aria-expanded", "true");
                        }
                        cardHeader.InnerHtml.AppendHtml(iconEl);


                        var titleEl = new TagBuilder(TitleTag);
                        titleEl.AddCssClass("m-0 d-inline-block");
                        titleEl.InnerHtml.AppendHtml(Title);
                        cardHeader.InnerHtml.AppendHtml(titleEl);
                        output.PreContent.AppendHtml(cardHeader);
                    }
                }

                var cardBodyEl = new TagBuilder("div");
                cardBodyEl.AddCssClass("card-body");
                cardBodyEl.AddCssClass(BodyClass);

                var collapseWrapper = new TagBuilder("div");
                if (IsCollapsable)
                {
                    collapseWrapper.AddCssClass("collapse multi-collapse");
                    if (!IsCollapsed)
                    {
                        collapseWrapper.AddCssClass("show");
                    }
                    collapseWrapper.Attributes.Add("id", "collapse-" + Id);

                    output.PreContent.AppendHtml(collapseWrapper.RenderStartTag());
                }

                output.PreContent.AppendHtml(cardBodyEl.RenderStartTag());
                output.PostContent.AppendHtml(cardBodyEl.RenderEndTag());

                if (IsCollapsable)
                {
                    output.PostContent.AppendHtml(collapseWrapper.RenderEndTag());
                }
            }


            var jsCompressor = new JavaScriptCompressor();
            #region << Init Scripts >>
            var tagHelperInitialized = false;
            var fileName             = "service.js";
            if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvSection) + fileName))
            {
                var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvSection) + fileName];
                tagHelperInitialized = tagHelperContext.Initialized;
            }
            if (!tagHelperInitialized)
            {
                var scriptContent = WvHelpers.GetEmbeddedTextResource(fileName, "WebVella.TagHelpers.TagHelpers.WvSection", "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(WvSection) + fileName] = 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(){
                        WvSectionInit(""{{SectionId}}"");
                    });"    ;
            scriptTemplate = scriptTemplate.Replace("{{SectionId}}", Id.ToString());

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

            output.PostContent.AppendHtml(initScript);
            #endregion



            context.Items[typeof(WvLabelRenderMode)] = LabelMode;
            context.Items[typeof(WvFieldRenderMode)] = FieldMode;

            return(Task.CompletedTask);
        }
예제 #13
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }

            if (Width == 0)
            {
                Width = null;
            }

            if (Height == 0)
            {
                Height = null;
            }

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

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            if (!((string)Value).StartsWith("http"))
            {
                if (Height != null || Width != null)
                {
                    QueryCommandsList.Add($"action=resize");
                    QueryCommandsList.Add($"mode={ResizeAction}");
                }

                if (Height != null)
                {
                    StylesList.Add($"height:{Height}px");
                    QueryCommandsList.Add($"height={Height}");
                }

                if (Width != null)
                {
                    StylesList.Add($"width:{Width}px");
                    QueryCommandsList.Add($"width={Width}");
                }
            }

            WrapperStyle = String.Join(";", StylesList);
            ImageQuery   = "";
            if (QueryCommandsList.Count > 0)
            {
                ImageQuery = "?" + String.Join("&", QueryCommandsList);
            }
            #endregion

            #region << Render >>

            if (Mode == WvFieldRenderMode.Form)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    var outsideWrapper = new TagBuilder("div");

                    var viewWrapper = new TagBuilder("div");
                    viewWrapper.Attributes.Add("id", $"view-{FieldId}");
                    viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper  {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    viewWrapper.Attributes.Add("style", WrapperStyle);

                    var viewImage = new TagBuilder("img");
                    viewImage.AddCssClass("wrapper-image");
                    if (!String.IsNullOrWhiteSpace(Value))
                    {
                        viewImage.Attributes.Add("src", $"{SrcPrefix}{Value}{ImageQuery}");
                    }
                    else
                    {
                        viewImage.Attributes.Add("src", $"");
                    }

                    viewImage.Attributes.Add("title", $"{FileName}");
                    viewWrapper.InnerHtml.AppendHtml(viewImage);

                    var viewRemoveLink = new TagBuilder("a");
                    viewRemoveLink.Attributes.Add("href", "javascript:void(0)");
                    viewRemoveLink.AddCssClass("action remove");
                    viewRemoveLink.InnerHtml.AppendHtml(TextRemove);
                    viewWrapper.InnerHtml.AppendHtml(viewRemoveLink);

                    outsideWrapper.InnerHtml.AppendHtml(viewWrapper);


                    var editWrapper = new TagBuilder("div");
                    editWrapper.Attributes.Add("id", $"edit-{FieldId}");
                    editWrapper.AddCssClass($"image-wrapper erp-image edit-wrapper  {(String.IsNullOrWhiteSpace(Value) ? "" : "d-none")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    editWrapper.Attributes.Add("style", WrapperStyle);

                    var editInput = new TagBuilder("input");
                    editInput.Attributes.Add("type", "file");
                    editInput.Attributes.Add("value", "");
                    editInput.Attributes.Add("id", $"file-{FieldId}");
                    editInput.Attributes.Add("accept", $"{Accept}");
                    editWrapper.InnerHtml.AppendHtml(editInput);

                    var editWrapperIconDiv = new TagBuilder("div");
                    editWrapperIconDiv.AddCssClass("wrapper-icon");
                    var editWrapperIconSpan = new TagBuilder("span");
                    editWrapperIconSpan.AddCssClass("fa fa-image fa-fw");
                    editWrapperIconDiv.InnerHtml.AppendHtml(editWrapperIconSpan);
                    editWrapper.InnerHtml.AppendHtml(editWrapperIconDiv);

                    var editWrapperTextDiv = new TagBuilder("div");
                    editWrapperTextDiv.AddCssClass("wrapper-text");
                    editWrapperTextDiv.Attributes.Add("id", $"fake-{Name}-{FieldId}");
                    var editWrapperTextLink = new TagBuilder("button");
                    editWrapperTextLink.Attributes.Add("type", "button");
                    editWrapperTextLink.AddCssClass("btn btn-secondary btn-sm");
                    editWrapperTextLink.InnerHtml.AppendHtml(TextSelect);
                    editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextLink);
                    var editWrapperTextSpan = new TagBuilder("span");
                    editWrapperTextSpan.AddCssClass("d-none");
                    editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextSpan);
                    editWrapper.InnerHtml.AppendHtml(editWrapperTextDiv);

                    outsideWrapper.InnerHtml.AppendHtml(editWrapper);

                    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());
                    hiddenInput.Attributes.Add("data-original-value", (Value ?? "").ToString());
                    hiddenInput.Attributes.Add("data-original-filename", Name);
                    outsideWrapper.InnerHtml.AppendHtml(hiddenInput);

                    output.Content.AppendHtml(outsideWrapper);

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldImage) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldImage) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldImage", "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(WvFieldImage) + "-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(){
							ImageFormInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                    var fieldConfig = new WvFieldImageConfig()
                    {
                        ApiUrl        = ApiUrl,
                        CanAddValues  = Access == WvFieldAccess.FullAndCreate ? true : false,
                        Accept        = Accept,
                        Width         = Width,
                        Height        = Height,
                        ResizeAction  = ResizeAction,
                        FileUploadApi = FileUploadApi,
                        SrcPrefix     = SrcPrefix
                    };

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

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    var outerWrapper = new TagBuilder("div");
                    var viewWrapper  = new TagBuilder("div");
                    viewWrapper.Attributes.Add("id", $"view-{FieldId}");
                    viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper  {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    viewWrapper.Attributes.Add("style", $"{WrapperStyle}");

                    var viewImage = new TagBuilder("img");
                    viewImage.AddCssClass("wrapper-image");
                    viewImage.Attributes.Add("src", $"{SrcPrefix}{Value}{ImageQuery}");
                    viewImage.Attributes.Add("title", $"{FileName}");
                    viewWrapper.InnerHtml.AppendHtml(viewImage);

                    outerWrapper.InnerHtml.AppendHtml(viewWrapper);
                    //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.PostContent.AppendHtml(hiddenInput);


                    output.PostContent.AppendHtml(outerWrapper);
                }
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                if (!String.IsNullOrWhiteSpace(Value))
                {
                    var outerWrapper = new TagBuilder("div");
                    var viewWrapper  = new TagBuilder("div");
                    viewWrapper.Attributes.Add("id", $"view-{FieldId}");
                    viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper  {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    viewWrapper.Attributes.Add("style", $"{WrapperStyle}");

                    var viewImage = new TagBuilder("img");
                    viewImage.AddCssClass("wrapper-image");
                    viewImage.Attributes.Add("src", $"{SrcPrefix}{Value}{ImageQuery}");
                    viewImage.Attributes.Add("title", $"{FileName}");
                    viewWrapper.InnerHtml.AppendHtml(viewImage);

                    outerWrapper.InnerHtml.AppendHtml(viewWrapper);
                    output.PostContent.AppendHtml(outerWrapper);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                var viewImage = new TagBuilder("img");
                viewImage.AddCssClass("img-fluid");
                viewImage.Attributes.Add("src", $"{SrcPrefix}{Value}{ImageQuery}");
                viewImage.Attributes.Add("title", $"{FileName}");
                output.Content.AppendHtml(viewImage);
                return(Task.CompletedTask);
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    var outsideWrapper = new TagBuilder("div");

                    var viewWrapper = new TagBuilder("div");
                    viewWrapper.Attributes.Add("id", $"view-{FieldId}");
                    viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper  {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    viewWrapper.Attributes.Add("style", WrapperStyle);

                    var viewImage = new TagBuilder("img");
                    viewImage.AddCssClass("wrapper-image");
                    viewImage.Attributes.Add("src", $"{SrcPrefix}{Value}{ImageQuery}");
                    viewImage.Attributes.Add("title", $"{FileName}");
                    viewWrapper.InnerHtml.AppendHtml(viewImage);

                    var viewRemoveLink = new TagBuilder("a");
                    viewRemoveLink.Attributes.Add("href", "javascript:void(0)");
                    viewRemoveLink.AddCssClass("action remove");
                    viewRemoveLink.InnerHtml.AppendHtml(TextRemove);
                    viewWrapper.InnerHtml.AppendHtml(viewRemoveLink);

                    outsideWrapper.InnerHtml.AppendHtml(viewWrapper);


                    var editWrapper = new TagBuilder("div");
                    editWrapper.Attributes.Add("id", $"edit-{FieldId}");
                    editWrapper.AddCssClass($"image-wrapper erp-image edit-wrapper  {(String.IsNullOrWhiteSpace(Value) ? "" : "d-none")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    editWrapper.Attributes.Add("style", WrapperStyle);

                    var editInput = new TagBuilder("input");
                    editInput.Attributes.Add("type", "file");
                    editInput.Attributes.Add("value", "");
                    editInput.Attributes.Add("id", $"file-{FieldId}");
                    editInput.Attributes.Add("accept", $"{Accept}");
                    editWrapper.InnerHtml.AppendHtml(editInput);

                    var editWrapperIconDiv = new TagBuilder("div");
                    editWrapperIconDiv.AddCssClass("wrapper-icon");
                    var editWrapperIconSpan = new TagBuilder("span");
                    editWrapperIconSpan.AddCssClass("fa fa-image fa-fw");
                    editWrapperIconDiv.InnerHtml.AppendHtml(editWrapperIconSpan);
                    editWrapper.InnerHtml.AppendHtml(editWrapperIconDiv);

                    var editWrapperTextDiv = new TagBuilder("div");
                    editWrapperTextDiv.AddCssClass("wrapper-text");
                    editWrapperTextDiv.Attributes.Add("id", $"fake-{Name}-{FieldId}");
                    var editWrapperTextLink = new TagBuilder("button");
                    editWrapperTextLink.Attributes.Add("type", "button");
                    editWrapperTextLink.AddCssClass("btn btn-secondary btn-sm");
                    editWrapperTextLink.InnerHtml.AppendHtml(TextSelect);
                    editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextLink);
                    var editWrapperTextSpan = new TagBuilder("span");
                    editWrapperTextSpan.AddCssClass("d-none");
                    editWrapperTextDiv.InnerHtml.AppendHtml(editWrapperTextSpan);
                    editWrapper.InnerHtml.AppendHtml(editWrapperTextDiv);

                    outsideWrapper.InnerHtml.AppendHtml(editWrapper);

                    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());
                    hiddenInput.Attributes.Add("data-original-value", (Value ?? "").ToString());
                    hiddenInput.Attributes.Add("data-original-filename", Name);
                    outsideWrapper.InnerHtml.AppendHtml(hiddenInput);

                    output.Content.AppendHtml(outsideWrapper);

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldImage) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldImage) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldImage", "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(WvFieldImage) + "-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(){
							ImageInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                    var fieldConfig = new WvFieldImageConfig()
                    {
                        ApiUrl        = ApiUrl,
                        CanAddValues  = Access == WvFieldAccess.FullAndCreate ? true : false,
                        Accept        = Accept,
                        Width         = Width,
                        Height        = Height,
                        ResizeAction  = ResizeAction,
                        FileUploadApi = FileUploadApi,
                        SrcPrefix     = SrcPrefix
                    };

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

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    var outerWrapper = new TagBuilder("div");
                    var viewWrapper  = new TagBuilder("div");
                    viewWrapper.Attributes.Add("id", $"view-{FieldId}");
                    viewWrapper.AddCssClass($"image-wrapper erp-image view-wrapper  {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")} {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    viewWrapper.Attributes.Add("style", $"{WrapperStyle}");

                    var viewImage = new TagBuilder("img");
                    viewImage.AddCssClass("wrapper-image");
                    viewImage.Attributes.Add("src", $"{SrcPrefix}{Value}{ImageQuery}");
                    viewImage.Attributes.Add("title", $"{FileName}");
                    viewWrapper.InnerHtml.AppendHtml(viewImage);

                    outerWrapper.InnerHtml.AppendHtml(viewWrapper);
                    output.PostContent.AppendHtml(outerWrapper);
                }
            }
            #endregion


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

            return(Task.CompletedTask);
        }
예제 #14
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            if (Value is string && Value == "")
            {
                Value = null;
            }

            if (Value != null && !(Value is List <string>))
            {
                output.Content.AppendHtml("<div class='go-red'>'value' property should be 'List&lt;string>'</div>");
                return(Task.CompletedTask);
            }

            if (Value != null && Value is List <string> )
            {
                FilePaths = (List <string>)Value;
            }

            #endregion

            #region << Render >>
            if (Mode == WvFieldRenderMode.Form)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    #region << Hidden input for posting >>
                    output.Content.AppendHtml($"<input type='hidden' id='input-{FieldId}' name='{Name}' value='{String.Join(',', FilePaths)}'/>");
                    #endregion

                    #region << fake upload >>
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group erp-file-multiple-input");

                    var fakeInputEl         = new TagBuilder("div");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-file with-progress");
                    fakeInputEl.Attributes.Add("id", $"fake-{FieldId}");

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

                    var fakeInputProgress = new TagBuilder("div");
                    fakeInputProgress.AddCssClass("form-control-progress");
                    fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress);

                    inputGroupEl.InnerHtml.AppendHtml(fakeInputEl);

                    var appendEl = new TagBuilder("span");
                    appendEl.AddCssClass("input-group-append action erp-file");
                    var selectFileLink = new TagBuilder("button");
                    selectFileLink.Attributes.Add("type", $"button");
                    selectFileLink.AddCssClass("btn btn-white");
                    selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();");
                    selectFileLink.InnerHtml.AppendHtml("browse");
                    appendEl.InnerHtml.AppendHtml(selectFileLink);
                    inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    output.Content.AppendHtml(inputGroupEl);

                    var realHiddenFileInput = new TagBuilder("input");
                    realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}");
                    realHiddenFileInput.Attributes.Add("type", $"file");
                    realHiddenFileInput.Attributes.Add("multiple", $"true");
                    realHiddenFileInput.AddCssClass("d-none");
                    realHiddenFileInput.Attributes.Add("value", $"");
                    if (!String.IsNullOrWhiteSpace(Accept))
                    {
                        realHiddenFileInput.Attributes.Add("accept", $"{Accept}");
                    }
                    output.Content.AppendHtml(realHiddenFileInput);

                    #endregion

                    #region << Files list element >>

                    var filesListEl = new TagBuilder("div");
                    filesListEl.AddCssClass("form-control erp-file-multiple-list form");
                    filesListEl.Attributes.Add("id", $"fake-list-{FieldId}");

                    if (FilePaths.Count == 0)
                    {
                        filesListEl.AddCssClass("d-none");
                    }


                    //Generate the files list
                    foreach (var filePath in FilePaths)
                    {
                        var fileRow = new TagBuilder("div");
                        fileRow.AddCssClass("filerow");
                        fileRow.Attributes.Add("data-file-path", filePath);
                        //Append icon
                        var iconClass = WvHelpers.GetPathTypeIcon(filePath);
                        fileRow.InnerHtml.AppendHtml($"<div class='icon'><i class='fa {iconClass}'></i></div>");

                        //Append meta
                        var rowMeta = new TagBuilder("div");
                        rowMeta.AddCssClass("meta");

                        //Append file
                        var fileName = WvHelpers.GetFileNameFromPath(filePath);
                        rowMeta.InnerHtml.AppendHtml($"<a class='link' href='{filePath}' target='_blank' title='{filePath}'>{fileName}<em></em></a>");

                        fileRow.InnerHtml.AppendHtml(rowMeta);

                        //Action
                        var rowAction = new TagBuilder("div");
                        rowAction.AddCssClass("action remove");
                        rowAction.InnerHtml.AppendHtml($"<a class='link' href='#'><i class='fa fa-times-circle'></i></a>");
                        //rowAction.InnerHtml.AppendHtml($"<span class='progress d-none'>0%</span>");
                        //rowAction.InnerHtml.AppendHtml($"<span class='error go-red d-none'><i class='fas fa-exclamation-circle'></i></span>");

                        fileRow.InnerHtml.AppendHtml(rowAction);
                        filesListEl.InnerHtml.AppendHtml(fileRow);
                    }

                    output.Content.AppendHtml(filesListEl);

                    #endregion


                    var jsCompressor = new JavaScriptCompressor();

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

                            ViewContext.HttpContext.Items[typeof(WvFieldFileMultiple) + fileName] = 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(){
							FieldMultiFileFormInit(""{{FieldId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());

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

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

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    output.Content.AppendHtml("Not implemented yet");
                }
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                output.Content.AppendHtml("Not implemented yet");
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.Content.AppendHtml("Not implemented yet");
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                output.Content.AppendHtml("Not implemented yet");
            }
            #endregion


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

            return(Task.CompletedTask);
        }
예제 #15
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return;
            }
            #region << Init >>
            if (String.IsNullOrWhiteSpace(Id))
            {
                Id = "wv-" + Guid.NewGuid();
            }

            //Records Count
            if (VerticalAlign != WvVerticalAlignmentType.None)
            {
                context.Items[typeof(WvVerticalAlignmentType)] = VerticalAlign;
            }
            var content = await output.GetChildContentAsync();

            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(content.GetContent());
            var actionTagHelperList = htmlDoc.DocumentNode.Descendants("tr");
            RecordsCount = actionTagHelperList.Count();

            #endregion

            #region << Setup wrapper >>
            var wrapperEl = new TagBuilder("div");
            var classList = new List <string>();
            classList.Add("erp-list");

            wrapperEl.Attributes.Add("id", Id);

            if (ResponsiveBreakpoint != WvCssBreakpoint.None)
            {
                var cssClass = $"{(ResponsiveBreakpoint == WvCssBreakpoint.XSmall ? "table-responsive" : $"table-responsive-{ResponsiveBreakpoint.GetLabel()}")}";
                classList.Add(cssClass);
            }

            if (!String.IsNullOrWhiteSpace(Name))
            {
                classList.Add($"erp-list-{Name}");
            }

            if (!String.IsNullOrWhiteSpace(Prefix))
            {
                wrapperEl.Attributes.Add("data-list-query-prefix", $"{Prefix}");
            }

            wrapperEl.Attributes.Add("class", String.Join(" ", classList));

            output.PreElement.AppendHtml(wrapperEl.RenderStartTag());
            output.PostElement.AppendHtml(wrapperEl.RenderEndTag());
            #endregion

            output.TagName = "table";

            #region << Table >>
            output.AddCssClass("table");

            #region << Css classes >>
            if (Striped)
            {
                output.AddCssClass("table-striped");
            }

            if (Small)
            {
                output.AddCssClass("table-sm");
            }

            if (Bordered)
            {
                output.AddCssClass("table-bordered");
            }

            if (Borderless)
            {
                output.AddCssClass("table-borderless");
            }

            if (Hover)
            {
                output.AddCssClass("table-hover");
            }

            if (!String.IsNullOrWhiteSpace(Class))
            {
                output.AddCssClass(Class);
            }

            if (Id != null)
            {
                output.Attributes.Add("id", $"list-table-{Id}");
            }

            #endregion

            #endregion

            #region << Thead >>
            if (HasThead)
            {
                var theadEl = new TagBuilder("thead");
                var trEl    = new TagBuilder("tr");

                foreach (var column in Columns)
                {
                    var thEl          = new TagBuilder("th");
                    var columnCssList = new List <string>();
                    if (!String.IsNullOrWhiteSpace(column.Class))
                    {
                        columnCssList.Add(column.Class);
                    }
                    thEl.Attributes.Add("data-filter-name", $"{column.Name}");
                    if (!String.IsNullOrEmpty(column.Width))
                    {
                        thEl.Attributes.Add("style", $"width:{column.Width};");
                    }
                    thEl.AddCssClass(String.Join(" ", columnCssList));

                    var sortedColumn = "";
                    if (ViewContext.HttpContext.Request.Query.ContainsKey((String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringSortBy))
                    {
                        sortedColumn = ViewContext.HttpContext.Request.Query[(String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringSortBy];
                    }
                    var sortOrder = "";
                    if (ViewContext.HttpContext.Request.Query.ContainsKey((String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringSortOrder))
                    {
                        sortOrder = ViewContext.HttpContext.Request.Query[(String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringSortOrder];
                    }
                    thEl.InnerHtml.AppendHtml(column.Label);
                    if (column.Sortable)
                    {
                        var columnSortOrder = "";
                        if (sortedColumn == column.Name)
                        {
                            columnSortOrder = sortOrder;
                        }

                        var sortLink = new TagBuilder("a");
                        sortLink.AddCssClass("sort-link");
                        sortLink.Attributes.Add("href", "javascript:void(0)");
                        sortLink.Attributes.Add("data-dataname", $"{column.Name}");
                        var sortMarkerEl = new TagBuilder("span");
                        sortMarkerEl.AddCssClass($"ml-1 sort-marker {columnSortOrder}");
                        var caretUpIcon = new TagBuilder("span");
                        caretUpIcon.AddCssClass("fa fa-caret-up");
                        sortMarkerEl.InnerHtml.AppendHtml(caretUpIcon);
                        var caretDownIcon = new TagBuilder("span");
                        caretDownIcon.AddCssClass("fa fa-caret-down");
                        sortMarkerEl.InnerHtml.AppendHtml(caretDownIcon);
                        sortLink.InnerHtml.AppendHtml(sortMarkerEl);
                        sortLink.InnerHtml.AppendHtml(new TagBuilder("em"));

                        thEl.InnerHtml.AppendHtml(sortLink);
                    }
                    trEl.InnerHtml.AppendHtml(thEl);
                }

                theadEl.InnerHtml.AppendHtml(trEl);
                output.PreContent.AppendHtml(theadEl);
            }
            #endregion

            #region << Tbody >>
            var tbodyEl = new TagBuilder("tbody");
            output.PreContent.AppendHtml(tbodyEl.RenderStartTag());

            output.PostContent.AppendHtml(tbodyEl.RenderEndTag());
            #endregion

            #region << Tfoot >>
            if (HasTfoot)
            {
                var tfootEl = new TagBuilder("tfoot");
                var trEl    = new TagBuilder("tr");
                var tdEl    = new TagBuilder("td");
                tdEl.Attributes.Add("colspan", Columns.Count.ToString());

                if (TotalCount > (PageSize * Page) || Page != 1)
                {
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group float-right input-group-sm pager-goto");
                    var inputPrependEl = new TagBuilder("span");
                    inputPrependEl.AddCssClass("input-group-prepend");
                    var inputPrependTextEl = new TagBuilder("span");
                    inputPrependTextEl.AddCssClass("input-group-text");
                    inputPrependTextEl.InnerHtml.Append("page");
                    inputPrependEl.InnerHtml.AppendHtml(inputPrependTextEl);
                    inputGroupEl.InnerHtml.AppendHtml(inputPrependEl);

                    var inputCtrEl = new TagBuilder("input");
                    inputCtrEl.AddCssClass("form-control");
                    inputCtrEl.Attributes.Add("value", Page.ToString());
                    inputCtrEl.Attributes.Add("id", $"list-pager-form-{Id}");
                    inputGroupEl.InnerHtml.AppendHtml(inputCtrEl);

                    tdEl.InnerHtml.AppendHtml(inputGroupEl);
                }
                else
                {
                    var floatEl = new TagBuilder("div");
                    floatEl.AddCssClass("pager-info float-right");
                    var divider = new TagBuilder("span");
                    divider.AddCssClass("divider go-gray mr-2 ml-2");
                    divider.InnerHtml.Append("|");
                    floatEl.InnerHtml.AppendHtml(divider);
                    var spanEl = new TagBuilder("span");
                    spanEl.InnerHtml.AppendHtml("Single page");
                    floatEl.InnerHtml.AppendHtml(spanEl);
                    tdEl.InnerHtml.AppendHtml(floatEl);
                }

                if (TotalCount != 0)
                {
                    var upperRecordCount = Page * PageSize;
                    if (TotalCount < upperRecordCount)
                    {
                        upperRecordCount = TotalCount;
                    }
                    var floatEl = new TagBuilder("div");
                    floatEl.AddCssClass("pager-info float-right");
                    var pageString = $"{(Page - 1) * PageSize + 1}-{upperRecordCount} of {TotalCount}";
                    floatEl.InnerHtml.Append(pageString);
                    tdEl.InnerHtml.AppendHtml(floatEl);
                }

                if (PageSize == RecordsCount || Page != 1)
                {
                    var pagePrevDisabled = Page == 1;
                    var pageNextDisabled = (TotalCount <= (PageSize * Page));

                    var btnGroupEl = new TagBuilder("div");
                    btnGroupEl.AddCssClass("btn-group float-left pager");

                    var prevBtnEl = new TagBuilder("button");
                    prevBtnEl.Attributes.Add("type", "button");
                    prevBtnEl.Attributes.Add("title", "Previous page");
                    prevBtnEl.AddCssClass($"btn btn-sm btn-outline-secondary {(pagePrevDisabled ? "disabled" : "")}");
                    if (pagePrevDisabled)
                    {
                        prevBtnEl.Attributes.Add("disabled", "disabled");
                    }
                    prevBtnEl.Attributes.Add("onclick", $"ErpListChangePage('{(String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringPage}',{Page - 1})");
                    var prevBtnIconEl = new TagBuilder("span");
                    prevBtnIconEl.AddCssClass("fa fa-fw fa-angle-left mr-1");
                    prevBtnEl.InnerHtml.AppendHtml(prevBtnIconEl);
                    prevBtnEl.InnerHtml.Append("Prev");
                    btnGroupEl.InnerHtml.AppendHtml(prevBtnEl);

                    var nextBtnEl = new TagBuilder("button");
                    nextBtnEl.Attributes.Add("type", "button");
                    nextBtnEl.Attributes.Add("title", "Next page");
                    nextBtnEl.AddCssClass($"btn btn-sm btn-outline-secondary {(pageNextDisabled ? "disabled" : "")}");
                    if (pageNextDisabled)
                    {
                        nextBtnEl.Attributes.Add("disabled", "disabled");
                    }
                    nextBtnEl.Attributes.Add("onclick", $"ErpListChangePage('{(String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringPage}',{Page + 1})");
                    nextBtnEl.InnerHtml.Append("Next");
                    var nextBtnIconEl = new TagBuilder("span");
                    nextBtnIconEl.AddCssClass("fa fa-fw fa-angle-right ml-1");
                    nextBtnEl.InnerHtml.AppendHtml(nextBtnIconEl);
                    btnGroupEl.InnerHtml.AppendHtml(nextBtnEl);

                    tdEl.InnerHtml.AppendHtml(btnGroupEl);
                }
                trEl.InnerHtml.AppendHtml(tdEl);
                tfootEl.InnerHtml.AppendHtml(trEl);
                output.PostContent.AppendHtml(tfootEl);
            }
            #endregion

            var jsCompressor = new JavaScriptCompressor();

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

                ViewContext.HttpContext.Items[typeof(WvGrid) + "-sort"] = 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(){
							ErpListPagerInputSubmit(""list-pager-form-{{Id}}"",""{{QueryStringPage}}"");
							ErpListSortInit(""{{Id}}"",""{{QueryStringSortBy}}"",""{{QueryStringSortOrder}}"");
						});"                        ;
            scriptTemplate = scriptTemplate.Replace("{{Id}}", Id.ToString());
            scriptTemplate = scriptTemplate.Replace("{{QueryStringSortBy}}", (String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringSortBy);
            scriptTemplate = scriptTemplate.Replace("{{QueryStringSortOrder}}", (String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringSortOrder);
            scriptTemplate = scriptTemplate.Replace("{{QueryStringPage}}", (String.IsNullOrWhiteSpace(Prefix) ? "" : Prefix) + QueryStringPage);

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

            output.PostElement.AppendHtml(initScript);
            #endregion


            context.Items["Id"]                   = Id;
            context.Items["Culture"]              = Culture;
            context.Items["QueryStringSortBy"]    = QueryStringSortBy;
            context.Items["QueryStringSortOrder"] = QueryStringSortOrder;
            context.Items["QueryStringPage"]      = QueryStringPage;
            context.Items["HasThead"]             = HasThead;

            if (!String.IsNullOrWhiteSpace(Prefix))
            {
                context.Items["Prefix"] = Prefix;
            }
            if (!String.IsNullOrWhiteSpace(Name))
            {
                context.Items["Name"] = Name;
            }

            if (Columns.Any())
            {
                context.Items["Columns"] = Columns;
            }
            if (VerticalAlign != WvVerticalAlignmentType.None)
            {
                context.Items[typeof(WvVerticalAlignmentType)] = VerticalAlign;
            }
            //return Task.CompletedTask;
        }
예제 #16
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;
            }

            #endregion



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

                    var prependIcon = new TagBuilder("div");
                    prependIcon.AddCssClass("input-group-prepend");
                    prependIcon.InnerHtml.AppendHtml($"<span class=\"input-group-text\"><i class=\"fa-fw {(Value != null ? Value.ToString() : "")}\"></i></span>");
                    inputGroupEl.InnerHtml.AppendHtml(prependIcon);


                    //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));
                    if (Required)
                    {
                        selectEl.Attributes.Add("required", null);
                    }
                    else
                    {
                        var optionEl = new TagBuilder("option");
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    if (!String.IsNullOrWhiteSpace((string)Value))
                    {
                        var optionEl = new TagBuilder("option");
                        optionEl.Attributes.Add("value", Value.ToString());
                        optionEl.Attributes.Add("selected", null);
                        optionEl.InnerHtml.Append(Value.ToString());
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    inputGroupEl.InnerHtml.AppendHtml(selectEl);

                    output.Content.AppendHtml(inputGroupEl);

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init FA Icons >>
                    {
                        var wvFAIconsLibraryInitialized = false;
                        var libraryItemsKey             = "WebVella-" + "fontawesome-icon-json";
                        if (ViewContext.HttpContext.Items.ContainsKey(libraryItemsKey))
                        {
                            var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[libraryItemsKey];
                            wvFAIconsLibraryInitialized = tagHelperContext.Initialized;
                        }

                        if (!wvFAIconsLibraryInitialized)
                        {
                            var libJsEl = new TagBuilder("script");
                            libJsEl.Attributes.Add("type", "text/javascript");
                            var scriptIconTemplate = @"
								WvFontAwesomeIcons = {{FAIcons}}
							"                            ;
                            scriptIconTemplate = scriptIconTemplate.Replace("{{FAIcons}}", JsonConvert.SerializeObject(WvHelpers.GetAllFontAwesomeIcons()));
                            libJsEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptIconTemplate));

                            output.PostContent.AppendHtml(libJsEl);
                            output.PostContent.AppendHtml("\r\n\t");

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

                    #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(WvFieldIcon) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldIcon) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldIcon", "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(WvFieldIcon) + "-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(){
							IconFieldFormInit(""{{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
                    };

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

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                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");
                    inputGroupEl.AddCssClass("input-group");

                    var prependIcon = new TagBuilder("div");
                    prependIcon.AddCssClass("input-group-prepend");
                    prependIcon.InnerHtml.AppendHtml($"<span class=\"input-group-text\"><i class=\"fa-fw {(Value != null ? Value.ToString() : "")}\"></i></span>");
                    inputGroupEl.InnerHtml.AppendHtml(prependIcon);

                    //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 (Value != null)
                    {
                        var optionEl = new TagBuilder("option");
                        optionEl.Attributes.Add("value", Value.ToString());
                        optionEl.Attributes.Add("selected", null);
                        optionEl.InnerHtml.Append(Value.ToString());
                        selectEl.InnerHtml.AppendHtml(optionEl);
                    }

                    inputGroupEl.InnerHtml.AppendHtml(selectEl);

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

                    var optionEl = new TagBuilder("span");
                    optionEl.Attributes.Add("data-key", (Value ?? "").ToString());
                    var color = "#333";
                    optionEl.InnerHtml.AppendHtml($"<i class=\"fa-fw {Value.ToString()}\" style=\"color:{color}\"></i> {Value.ToString()}");

                    formControlEl.InnerHtml.AppendHtml(optionEl);

                    output.Content.AppendHtml(formControlEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                if (Value == null)
                {
                    output.Content.AppendHtml("");
                }
                else
                {
                    var color = "#333";
                    output.Content.AppendHtml($"<i class=\"fa-fw {Value.ToString()}\" style=\"color:{color}\"></i> {Value.ToString()}");
                }
                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}");

                        var prependIcon = new TagBuilder("div");
                        prependIcon.AddCssClass("input-group-prepend");
                        prependIcon.InnerHtml.AppendHtml($"<span class=\"input-group-text\"><i class=\"fa-fw {(Value != null ? Value.ToString() : "")}\"></i></span>");
                        viewWrapperEl.InnerHtml.AppendHtml(prependIcon);

                        //Control
                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-select");

                        if (Value != null)
                        {
                            viewFormControlEl.InnerHtml.AppendHtml($"{Value.ToString()}");
                        }

                        viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl);

                        //Append
                        var viewInputActionEl = new TagBuilder("span");
                        viewInputActionEl.AddCssClass("input-group-append action");
                        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");

                        var prependIcon = new TagBuilder("div");
                        prependIcon.AddCssClass("input-group-prepend");
                        prependIcon.InnerHtml.AppendHtml($"<span class=\"input-group-text\"><i class=\"fa-fw {(Value != null ? Value.ToString() : "")}\"></i></span>");
                        editInputGroupEl.InnerHtml.AppendHtml(prependIcon);


                        //Control
                        var formControl         = new TagBuilder("select");
                        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));
                        formControl.Attributes.Add("id", $"input-{FieldId}");

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

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

                        if (Value != null)
                        {
                            var optionEl = new TagBuilder("option");
                            optionEl.Attributes.Add("value", Value.ToString());
                            optionEl.Attributes.Add("selected", null);
                            optionEl.InnerHtml.Append(Value.ToString());
                            formControl.InnerHtml.AppendHtml(optionEl);
                        }

                        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 Libraries >>
                    {
                        var wvLibraryInitialized = false;
                        var libraryItemsKey      = "WebVella-" + "fontawesome-icon-json";
                        if (ViewContext.HttpContext.Items.ContainsKey(libraryItemsKey))
                        {
                            var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[libraryItemsKey];
                            wvLibraryInitialized = tagHelperContext.Initialized;
                        }

                        if (!wvLibraryInitialized)
                        {
                            var libJsEl = new TagBuilder("script");
                            libJsEl.Attributes.Add("type", "text/javascript");
                            var scriptIconTemplate = @"
								var WvFontAwesomeIcons = {{FAIcons}}
							"                            ;
                            scriptIconTemplate = scriptIconTemplate.Replace("{{FAIcons}}", JsonConvert.SerializeObject(WvHelpers.GetAllFontAwesomeIcons()));
                            libJsEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptIconTemplate));

                            output.PostContent.AppendHtml(libJsEl);
                            output.PostContent.AppendHtml("\r\n\t");

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

                    #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(WvFieldIcon) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldIcon) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldIcon", "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(WvFieldIcon) + "-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(){
							IconFieldInlineEditInit(""{{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
                    };

                    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");

                    var prependIcon = new TagBuilder("div");
                    prependIcon.AddCssClass("input-group-prepend");
                    prependIcon.InnerHtml.AppendHtml($"<span class=\"input-group-text\"><i class=\"fa-fw {(Value != null ? Value.ToString() : "")}\"></i></span>");
                    divEl.InnerHtml.AppendHtml(prependIcon);

                    //Control
                    var formControlEl = new TagBuilder("div");
                    formControlEl.AddCssClass("form-control erp-select");


                    if (Value != null)
                    {
                        var optionEl = new TagBuilder("option");
                        optionEl.Attributes.Add("value", Value.ToString());
                        optionEl.Attributes.Add("selected", null);
                        optionEl.InnerHtml.Append(Value.ToString());
                        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;
        }
예제 #17
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            if (Value != null && !String.IsNullOrWhiteSpace((Value ?? "").ToString()))
            {
                PathTypeIcon = WvHelpers.GetPathTypeIcon(Value);
                FileName     = Path.GetFileName(Value);
            }

            #endregion

            #region << Render >>
            if (Mode == WvFieldRenderMode.Form)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group");
                    var prependEl = new TagBuilder("span");
                    prependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    prependEl.Attributes.Add("title", $"{SrcPrefix}{Value}");
                    var prependText = new TagBuilder("span");
                    prependText.AddCssClass("input-group-text");
                    var prependIcon = new TagBuilder("span");
                    prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}");
                    prependText.InnerHtml.AppendHtml(prependIcon);
                    prependEl.InnerHtml.AppendHtml(prependText);
                    inputGroupEl.InnerHtml.AppendHtml(prependEl);

                    var fakeInputEl         = new TagBuilder("div");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-file with-progress ");

                    fakeInputEl.Attributes.Add("id", $"fake-{FieldId}");

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

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

                    var fakeInputFileLinkEl = new TagBuilder("a");
                    fakeInputFileLinkEl.Attributes.Add("href", $"{SrcPrefix}{Value}");
                    fakeInputFileLinkEl.Attributes.Add("target", "_blank");
                    fakeInputFileLinkEl.Attributes.Add("title", $"{SrcPrefix}{Value}");
                    fakeInputFileLinkEl.InnerHtml.Append(FileName);
                    fakeInputEl.InnerHtml.AppendHtml(fakeInputFileLinkEl);
                    var fakeInputProgress = new TagBuilder("div");
                    fakeInputProgress.AddCssClass("form-control-progress");
                    fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress);
                    inputGroupEl.InnerHtml.AppendHtml(fakeInputEl);

                    var appendEl = new TagBuilder("span");
                    appendEl.AddCssClass("input-group-append action erp-file");
                    if (!Required)
                    {
                        var appendDeleteLink = new TagBuilder("button");
                        appendDeleteLink.Attributes.Add("type", $"button");
                        appendDeleteLink.Attributes.Add("id", $"remove-{FieldId}");
                        appendDeleteLink.AddCssClass($"btn btn-white remove {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}");
                        appendDeleteLink.Attributes.Add("title", "select as undefined");
                        var appendDeleteLinkIcon = new TagBuilder("span");
                        appendDeleteLinkIcon.AddCssClass("fa fa-fw fa-trash go-red");
                        appendDeleteLink.InnerHtml.AppendHtml(appendDeleteLinkIcon);
                        appendEl.InnerHtml.AppendHtml(appendDeleteLink);
                    }

                    var selectFileLink = new TagBuilder("button");
                    selectFileLink.Attributes.Add("type", $"button");
                    selectFileLink.AddCssClass("btn btn-white");
                    selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();");
                    selectFileLink.InnerHtml.AppendHtml("browse");
                    appendEl.InnerHtml.AppendHtml(selectFileLink);

                    inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    output.Content.AppendHtml(inputGroupEl);

                    var realHiddenFileInput = new TagBuilder("input");
                    realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}");
                    realHiddenFileInput.Attributes.Add("type", $"file");
                    realHiddenFileInput.AddCssClass("d-none");
                    realHiddenFileInput.Attributes.Add("value", $"");
                    if (!String.IsNullOrWhiteSpace(Accept))
                    {
                        realHiddenFileInput.Attributes.Add("accept", $"{Accept}");
                    }
                    output.Content.AppendHtml(realHiddenFileInput);

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

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldFile) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldFile) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldFile", "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(WvFieldFile) + "-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(){
							FileFormInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

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

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

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    if (!String.IsNullOrWhiteSpace(Value))
                    {
                        var inputGroupEl = new TagBuilder("div");
                        inputGroupEl.AddCssClass("input-group");
                        var prependEl = new TagBuilder("span");
                        prependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        prependEl.Attributes.Add("title", $"{SrcPrefix}{Value}");
                        var prependText = new TagBuilder("span");
                        prependText.AddCssClass("input-group-text");
                        var prependIcon = new TagBuilder("span");
                        prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}");
                        prependText.InnerHtml.AppendHtml(prependIcon);
                        prependEl.InnerHtml.AppendHtml(prependText);
                        inputGroupEl.InnerHtml.AppendHtml(prependEl);

                        var inputEl = new TagBuilder("div");
                        inputEl.AddCssClass("form-control erp-file disabled");
                        var inputElLink = new TagBuilder("a");
                        inputElLink.Attributes.Add("href", $"{SrcPrefix}{Value}");
                        inputElLink.Attributes.Add("target", "_blank");
                        inputElLink.Attributes.Add("title", $"{SrcPrefix}{Value}");
                        inputElLink.InnerHtml.Append(FileName);
                        inputEl.InnerHtml.AppendHtml(inputElLink);
                        inputGroupEl.InnerHtml.AppendHtml(inputEl);
                        output.Content.AppendHtml(inputGroupEl);

                        //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);
                    }
                    else
                    {
                        var inputEl = new TagBuilder("input");
                        inputEl.Attributes.Add("readonly", null);
                        inputEl.AddCssClass("form-control erp-file");
                        inputEl.Attributes.Add("value", "");
                        inputEl.Attributes.Add("name", Name);
                        output.Content.AppendHtml(inputEl);
                    }
                }
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                if (!String.IsNullOrWhiteSpace(Value))
                {
                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-file");
                    var iconEl = new TagBuilder("span");
                    iconEl.AddCssClass($"fa fa-fw {PathTypeIcon}");
                    divEl.InnerHtml.AppendHtml(iconEl);
                    var linkEl = new TagBuilder("a");
                    linkEl.Attributes.Add("href", $"{SrcPrefix}{Value}");
                    linkEl.Attributes.Add("target", $"_blank");
                    linkEl.InnerHtml.Append(FileName);
                    divEl.InnerHtml.AppendHtml(linkEl);
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                var linkEl = new TagBuilder("a");
                linkEl.Attributes.Add("href", $"{SrcPrefix}{Value}");
                linkEl.Attributes.Add("target", $"_blank");
                linkEl.InnerHtml.Append(FileName);
                output.Content.AppendHtml(linkEl);
                return(Task.CompletedTask);
            }
            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}");

                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass($"input-group-prepend icon-addon");
                        viewInputPrepend.Attributes.Add("title", $"{SrcPrefix}{Value}");
                        var viewInputPrependText = new TagBuilder("span");
                        viewInputPrependText.AddCssClass("input-group-text");
                        var prependIcon = new TagBuilder("span");
                        prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}");
                        viewInputPrependText.InnerHtml.AppendHtml(prependIcon);
                        viewInputPrepend.InnerHtml.AppendHtml(viewInputPrependText);
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);

                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-file");

                        var viewFormControlLinkEl = new TagBuilder("a");
                        viewFormControlLinkEl.Attributes.Add("href", $"{SrcPrefix}{Value}");
                        viewFormControlLinkEl.Attributes.Add("target", "_blank");
                        viewFormControlLinkEl.Attributes.Add("title", $"{SrcPrefix}{Value}");
                        viewFormControlLinkEl.InnerHtml.Append(FileName);
                        viewFormControlEl.InnerHtml.AppendHtml(viewFormControlLinkEl);

                        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");

                        var editWrapperPrependEl = new TagBuilder("span");
                        editWrapperPrependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        editWrapperPrependEl.Attributes.Add("title", $"{SrcPrefix}{Value}");
                        var editWrapperPrependText = new TagBuilder("span");
                        editWrapperPrependText.AddCssClass("input-group-text");
                        var editWrapperPrependIcon = new TagBuilder("span");
                        editWrapperPrependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}");
                        editWrapperPrependText.InnerHtml.AppendHtml(editWrapperPrependIcon);
                        editWrapperPrependEl.InnerHtml.AppendHtml(editWrapperPrependText);
                        editInputGroupEl.InnerHtml.AppendHtml(editWrapperPrependEl);


                        var fakeInputEl         = new TagBuilder("div");
                        var inputElCssClassList = new List <string>();
                        inputElCssClassList.Add("form-control erp-file with-progress ");

                        fakeInputEl.Attributes.Add("id", $"fake-{FieldId}");

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

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

                        var fakeInputFileLinkEl = new TagBuilder("a");
                        fakeInputFileLinkEl.Attributes.Add("href", $"{SrcPrefix}{Value}");
                        fakeInputFileLinkEl.Attributes.Add("target", "_blank");
                        fakeInputFileLinkEl.Attributes.Add("title", $"{SrcPrefix}{Value}");
                        fakeInputFileLinkEl.InnerHtml.Append(FileName);
                        fakeInputEl.InnerHtml.AppendHtml(fakeInputFileLinkEl);
                        var fakeInputProgress = new TagBuilder("div");
                        fakeInputProgress.AddCssClass("form-control-progress");
                        fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress);
                        editInputGroupEl.InnerHtml.AppendHtml(fakeInputEl);


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

                        if (!Required)
                        {
                            var appendDeleteLink = new TagBuilder("button");
                            appendDeleteLink.Attributes.Add("type", $"button");
                            appendDeleteLink.Attributes.Add("id", $"remove-{FieldId}");
                            appendDeleteLink.AddCssClass($"btn btn-white remove {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}");
                            appendDeleteLink.Attributes.Add("title", "select as undefined");
                            var appendDeleteLinkIcon = new TagBuilder("span");
                            appendDeleteLinkIcon.AddCssClass("fa fa-fw fa-trash go-red");
                            appendDeleteLink.InnerHtml.AppendHtml(appendDeleteLinkIcon);
                            editInputGroupAppendEl.InnerHtml.AppendHtml(appendDeleteLink);
                        }

                        var selectFileLink = new TagBuilder("button");
                        selectFileLink.Attributes.Add("type", $"button");
                        selectFileLink.AddCssClass("btn btn-white");
                        selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();");
                        selectFileLink.InnerHtml.AppendHtml("select");
                        editInputGroupAppendEl.InnerHtml.AppendHtml(selectFileLink);


                        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);

                        var realHiddenFileInput = new TagBuilder("input");
                        realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}");
                        realHiddenFileInput.Attributes.Add("type", $"file");
                        realHiddenFileInput.AddCssClass("d-none");
                        realHiddenFileInput.Attributes.Add("value", $"");
                        if (!String.IsNullOrWhiteSpace(Accept))
                        {
                            realHiddenFileInput.Attributes.Add("accept", $"{Accept}");
                        }
                        output.Content.AppendHtml(realHiddenFileInput);

                        var realSubmitInput = new TagBuilder("input");
                        realSubmitInput.Attributes.Add("id", $"input-{FieldId}");
                        realSubmitInput.Attributes.Add("type", $"hidden");
                        realSubmitInput.Attributes.Add("value", $"{Value}");
                        realSubmitInput.Attributes.Add("data-newfilepath", $"{Value}");
                        realSubmitInput.Attributes.Add("data-filename", $"{FileName}");
                        realSubmitInput.Attributes.Add("data-newfilename", $"{FileName}");
                        output.Content.AppendHtml(realSubmitInput);
                    }
                    #endregion

                    var jsCompressor = new JavaScriptCompressor();

                    #region << Init Scripts >>
                    var tagHelperInitialized = false;
                    if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldFile) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldFile) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldFile", "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(WvFieldFile) + "-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(){
							FileInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

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

                    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");

                    var prependEl = new TagBuilder("span");
                    prependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                    prependEl.Attributes.Add("title", $"{SrcPrefix}{Value}");
                    var prependText = new TagBuilder("span");
                    prependText.AddCssClass("input-group-text");
                    var prependIcon = new TagBuilder("span");
                    prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}");
                    prependText.InnerHtml.AppendHtml(prependIcon);
                    prependEl.InnerHtml.AppendHtml(prependText);
                    divEl.InnerHtml.AppendHtml(prependEl);

                    var inputEl = new TagBuilder("div");
                    inputEl.AddCssClass("form-control erp-file disabled");
                    var inputElLink = new TagBuilder("a");
                    inputElLink.Attributes.Add("href", $"{SrcPrefix}{Value}");
                    inputElLink.Attributes.Add("target", "_blank");
                    inputElLink.Attributes.Add("title", $"{SrcPrefix}{Value}");
                    inputElLink.InnerHtml.Append(FileName);
                    inputEl.InnerHtml.AppendHtml(inputElLink);
                    divEl.InnerHtml.AppendHtml(inputEl);

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

                    var appendTextSpan = new TagBuilder("span");
                    appendTextSpan.AddCssClass("input-group-text");

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

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


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

            return(Task.CompletedTask);
        }
예제 #18
0
        public bool InitField(TagHelperContext context, TagHelperOutput output)
        {
            var isSuccess = true;

            #region << Init >>
            if (FieldId == null)
            {
                FieldId = Guid.NewGuid();
            }

            if (String.IsNullOrWhiteSpace(ApiUrl) && Mode == WvFieldRenderMode.InlineEdit)
            {
                InitErrors.Add("In 'inlineEdit' when 'api-url' is not defined, entityName and recordId are required!");
            }

            if (LabelMode == WvLabelRenderMode.Undefined)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey(typeof(WvLabelRenderMode)))
                {
                    LabelMode = (WvLabelRenderMode)context.Items[typeof(WvLabelRenderMode)];
                }
                else
                {
                    LabelMode = WvLabelRenderMode.Stacked;
                }
            }

            if (Mode == WvFieldRenderMode.Undefined)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey(typeof(WvFieldRenderMode)))
                {
                    Mode = (WvFieldRenderMode)context.Items[typeof(WvFieldRenderMode)];
                }
                else
                {
                    Mode = WvFieldRenderMode.Form;
                }
            }

            if (!String.IsNullOrWhiteSpace(Locale))
            {
                Culture = new CultureInfo(Locale);
            }

            if (String.IsNullOrWhiteSpace(Name) && (Mode == WvFieldRenderMode.Form || Mode == WvFieldRenderMode.InlineEdit) &&
                context.TagName != "wv-field-plaintext" && (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate))
            {
                InitErrors.Add("In InlineEdit or Form the attribute 'name' is required when with full access");
            }
            if (ValidationErrors.Count > 0)
            {
                ValidationErrors = ValidationErrors.FindAll(x => (x.Key ?? "").ToLowerInvariant() == (Name ?? "").ToLowerInvariant()).ToList();
            }

            if (ValidationErrors.Count == 0)
            {
                //Check if it is defined in form group
                if (context.Items.ContainsKey("WvTagHelperValidationErrors") && context.Items["WvTagHelperValidationErrors"] != null)
                {
                    var validationErrors = (List <KeyValuePair <string, string> >)context.Items["WvTagHelperValidationErrors"];
                    if (validationErrors != null & validationErrors.Count > 0)
                    {
                        ValidationErrors = validationErrors.FindAll(x => (x.Key ?? "").ToLowerInvariant() == (Name ?? "").ToLowerInvariant());
                    }
                }
            }

            if (AutoComplete == null)
            {
                if (context.Items.ContainsKey("FromAutocomplete"))
                {
                    AutoComplete = (bool)context.Items["FromAutocomplete"];
                }
            }

            #region << Init Value >>
            var         tagName   = context.TagName;
            var         errorList = new List <string>();
            WvFieldType?fieldType = WvFieldType.TextField;
            switch (context.TagName)
            {
            case "wv-field-autonumber":
                fieldType = WvFieldType.AutoNumberField;
                break;

            case "wv-field-checkbox":
                fieldType = WvFieldType.CheckboxField;
                break;

            case "wv-field-checkbox-grid":
            {
                fieldType = WvFieldType.TextField;
            }
            break;

            case "wv-field-currency":
                fieldType = WvFieldType.CurrencyField;
                break;

            case "wv-field-date":
                fieldType = WvFieldType.DateField;
                break;

            case "wv-field-datetime":
            case "wv-field-time":
                fieldType = WvFieldType.DateTimeField;
                break;

            case "wv-field-email":
                fieldType = WvFieldType.EmailField;
                break;

            case "wv-field-file":
                fieldType = WvFieldType.FileField;
                break;

            case "wv-field-html":
                fieldType = WvFieldType.HtmlField;
                break;

            case "wv-field-image":
                fieldType = WvFieldType.ImageField;
                break;

            case "wv-field-textarea":
            case "wv-field-data-csv":
                fieldType = WvFieldType.MultiLineTextField;
                break;

            case "wv-field-multiselect":
            case "wv-field-checkbox-list":
                fieldType = WvFieldType.MultiSelectField;
                if (Value == null)
                {
                    Value = new List <string>();
                }
                else if (Value is List <string> )
                {
                }
                else if (Value is List <WvSelectOption> )
                {
                    var newListString = new List <string>();
                    foreach (var option in (List <WvSelectOption>)Value)
                    {
                        newListString.Add(option.Value.ToLowerInvariant());
                    }
                    Value = newListString;
                }
                else if (Value is string)
                {
                    var stringValue = Value.ToString().ToLowerInvariant();

                    Value = new List <string>()
                    {
                        stringValue
                    };
                }
                else
                {
                    throw new Exception("Expected multiselect value is List<string> or List<SelectOption>");
                }
                break;

            case "wv-field-number":
                fieldType = WvFieldType.NumberField;
                break;

            case "wv-field-password":
                fieldType = WvFieldType.PasswordField;
                break;

            case "wv-field-percent":
                fieldType = WvFieldType.PercentField;
                break;

            case "wv-field-phone":
                fieldType = WvFieldType.PhoneField;
                break;

            case "wv-field-guid":
                fieldType = WvFieldType.GuidField;
                break;

            case "wv-field-select":
                fieldType = WvFieldType.SelectField;
                if (Value == null)
                {
                    Value = "";
                }
                else if (Value is string && !String.IsNullOrWhiteSpace(Value.ToString()))
                {
                    Value = Value.ToString().ToLowerInvariant();
                }
                break;

            case "wv-field-text":
                fieldType = WvFieldType.TextField;
                break;

            case "wv-field-url":
                fieldType = WvFieldType.UrlField;
                break;

            default:
                fieldType = null;
                break;
            }
            dynamic valueResult = null;
            WvHelpers.ValidateValueToFieldType(fieldType, Value, out valueResult, out errorList);
            Value = valueResult;
            dynamic defaultValueResult = null;
            WvHelpers.ValidateValueToFieldType(fieldType, DefaultValue, out defaultValueResult, out errorList);
            DefaultValue = defaultValueResult;

            if (errorList.Count > 0)
            {
                foreach (var error in errorList)
                {
                    InitErrors.Add(error);
                }
            }
            #endregion

            #endregion

            #region << Render Field Group / Output tag >>
            output.TagName = "div";
            if (!String.IsNullOrWhiteSpace(Class))
            {
                CssClassList.Add(Class);
            }
            CssClassList.Add("form-group");
            CssClassList.Add("wv-field");
            if (LabelMode == WvLabelRenderMode.Horizontal)
            {
                CssClassList.Add("label-horizontal");
            }
            if (!String.IsNullOrWhiteSpace(Id))
            {
                output.Attributes.Add("id", Id);
            }


            switch (LabelMode)
            {
            case WvLabelRenderMode.Hidden:
                CssClassList.Add("label-hidden");
                break;

            case WvLabelRenderMode.Horizontal:
                CssClassList.Add("row no-gutters");
                break;
            }

            switch (Mode)
            {
            case WvFieldRenderMode.Display:
                CssClassList.Add("display");
                break;

            case WvFieldRenderMode.Form:
                CssClassList.Add("form");
                break;

            case WvFieldRenderMode.InlineEdit:
                CssClassList.Add("inline-edit");
                break;

            case WvFieldRenderMode.Simple:
                CssClassList.Add("list");
                break;
            }

            CssClassList.Add(context.TagName);

            output.Attributes.SetAttribute("class", String.Join(' ', CssClassList));

            #endregion

            #region << Render Label >>
            if (LabelMode != WvLabelRenderMode.Hidden)
            {
                var labelEl = new TagBuilder("label");
                //Set label attributes
                if (FieldId != null)
                {
                    labelEl.Attributes.Add("for", "input-" + FieldId);
                }
                if (LabelMode == WvLabelRenderMode.Horizontal)
                {
                    labelEl.Attributes.Add("class", "col-12 col-sm-auto col-form-label label-horizontal pr-0 pr-sm-2");
                }
                else
                {
                    labelEl.Attributes.Add("class", "control-label label-stacked");
                }

                if (Size == WvCssSize.Large)
                {
                    labelEl.AddCssClass("control-label-lg");
                }
                if (Size == WvCssSize.Small)
                {
                    labelEl.AddCssClass("control-label-sm");
                }

                //Set Required
                if (Required && Mode == WvFieldRenderMode.Form)
                {
                    var requiredEl = new TagBuilder("abbr");
                    requiredEl.MergeAttribute("class", "go-red");
                    requiredEl.MergeAttribute("title", "required");
                    requiredEl.InnerHtml.AppendHtml("<i class='fas fa-asterisk'></i>");
                    labelEl.InnerHtml.AppendHtml(requiredEl);
                }

                //Set Label text
                if (LabelMode != WvLabelRenderMode.Horizontal)
                {
                    labelEl.InnerHtml.AppendHtml(LabelText);
                }
                else
                {
                    labelEl.InnerHtml.AppendHtml(LabelText + ":");
                }

                //Set Help
                if (!String.IsNullOrWhiteSpace(LabelHelpText))
                {
                    var helpEl = new TagBuilder("i");
                    helpEl.MergeAttribute("class", "fa fa-fw fa-question-circle field-help");
                    helpEl.MergeAttribute("data-toggle", "tooltip");
                    helpEl.MergeAttribute("data-placement", "top");
                    helpEl.MergeAttribute("data-html", "true");
                    helpEl.MergeAttribute("id", "help-message-" + FieldId);
                    helpEl.MergeAttribute("title", LabelHelpText);
                    labelEl.InnerHtml.AppendHtml(helpEl);
                    var scriptEl = new TagBuilder("script");
                    var script   = $"$(function () {{$('#help-message-{FieldId}').tooltip();}})";
                    scriptEl.InnerHtml.AppendHtml(script);
                    labelEl.InnerHtml.AppendHtml(scriptEl);
                }
                ;

                //Set Warning
                if (!String.IsNullOrWhiteSpace(LabelWarningText))
                {
                    var helpEl = new TagBuilder("i");
                    helpEl.MergeAttribute("class", "fa fa-fw fa-exclamation-triangle field-warning-message");
                    helpEl.MergeAttribute("data-toggle", "tooltip");
                    helpEl.MergeAttribute("data-placement", "top");
                    helpEl.MergeAttribute("data-html", "true");
                    helpEl.MergeAttribute("id", "warning-message-" + FieldId);
                    helpEl.MergeAttribute("data-delay", "{ \"show\": 100, \"hide\": 3000 }");
                    helpEl.MergeAttribute("title", LabelWarningText);
                    labelEl.InnerHtml.AppendHtml(helpEl);
                    var scriptEl = new TagBuilder("script");
                    var script   = $"$(function () {{$('#warning-message-{FieldId}').tooltip();}})";
                    scriptEl.InnerHtml.AppendHtml(script);
                    labelEl.InnerHtml.AppendHtml(scriptEl);
                }
                ;

                //Set Error
                if (!String.IsNullOrWhiteSpace(LabelErrorText))
                {
                    var helpEl = new TagBuilder("i");
                    helpEl.MergeAttribute("class", "fa fa-fw fa-exclamation-circle field-error-message");
                    helpEl.MergeAttribute("data-toggle", "tooltip");
                    helpEl.MergeAttribute("data-placement", "top");
                    helpEl.MergeAttribute("data-html", "true");
                    helpEl.MergeAttribute("id", "error-message-" + FieldId);
                    helpEl.MergeAttribute("data-delay", "{ \"show\": 100, \"hide\": 3000 }");
                    helpEl.MergeAttribute("title", LabelErrorText);
                    labelEl.InnerHtml.AppendHtml(helpEl);
                    var scriptEl = new TagBuilder("script");
                    var script   = $"$(function () {{$('#error-message-{FieldId}').tooltip();}})";
                    scriptEl.InnerHtml.AppendHtml(script);
                    labelEl.InnerHtml.AppendHtml(scriptEl);
                }
                ;

                output.PreContent.AppendHtml(labelEl);
            }

            #endregion

            #region << Render description >>
            if (!String.IsNullOrWhiteSpace(Description))
            {
                var descriptionEl = new TagBuilder("div");
                descriptionEl.AddCssClass("field-description post-label");
                descriptionEl.InnerHtml.AppendHtml(Description);
                output.PreContent.AppendHtml(descriptionEl);
            }
            #endregion

            #region << Field Outer Wrapper tag - StartTag >>
            var fieldWrapper = new TagBuilder("div");
            fieldWrapper.AddCssClass("col");

            if (LabelMode == WvLabelRenderMode.Horizontal)
            {
                output.PreContent.AppendHtml(fieldWrapper.RenderStartTag());
            }
            #endregion

            #region << if Init Errors >>
            if (InitErrors.Count > 0)
            {
                var errorListEl = new TagBuilder("ul");
                errorListEl.AddCssClass("erp-error-list list-unstyled");
                foreach (var error in InitErrors)
                {
                    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: {error}");

                    errorListEl.InnerHtml.AppendHtml(errorEl);
                }
                output.PostContent.AppendHtml(errorListEl);
                return(false);
            }
            #endregion

            #region << if Forbidden >>
            if (Access == WvFieldAccess.Forbidden)
            {
                if (Mode != WvFieldRenderMode.Simple)
                {
                    var forbiddenEl = new TagBuilder("div");
                    forbiddenEl.AddCssClass("form-control-plaintext");

                    var innerSpan = new TagBuilder("span");
                    innerSpan.AddCssClass("go-gray");

                    var innerIcon = new TagBuilder("span");
                    innerIcon.AddCssClass("fa fa-lock mr-1");

                    var innerEm = new TagBuilder("em");
                    innerEm.InnerHtml.Append(AccessDeniedMessage);

                    innerSpan.InnerHtml.AppendHtml(innerIcon);
                    innerSpan.InnerHtml.AppendHtml(innerEm);
                    forbiddenEl.InnerHtml.AppendHtml(innerSpan);

                    output.PostContent.AppendHtml(forbiddenEl);
                    return(false);
                }
                else
                {
                    var innerSpan = new TagBuilder("span");
                    innerSpan.AddCssClass("go-gray");

                    var innerIcon = new TagBuilder("span");
                    innerIcon.AddCssClass("fa fa-lock mr-1");

                    var innerEm = new TagBuilder("em");
                    innerEm.InnerHtml.Append(AccessDeniedMessage);
                    innerSpan.InnerHtml.AppendHtml(innerIcon);
                    innerSpan.InnerHtml.AppendHtml(innerEm);

                    output.SuppressOutput();
                    output.PostContent.AppendHtml(innerSpan);

                    return(false);
                }
            }
            #endregion

            #region << Field Outer Wrapper tag - End Tag >>
            if (LabelMode == WvLabelRenderMode.Horizontal)
            {
                output.PostContent.AppendHtml(fieldWrapper.RenderEndTag());
            }
            #endregion

            #region << Init SubInputEl >>

            if (ValidationErrors.Count == 1)
            {
                SubInputEl = new TagBuilder("div");
                SubInputEl.AddCssClass("invalid-feedback d-block");
                SubInputEl.InnerHtml.AppendHtml(ValidationErrors.First().Value);
            }
            else if (ValidationErrors.Count > 1)
            {
                SubInputEl = new TagBuilder("div");
                SubInputEl.AddCssClass("invalid-feedback d-block");
                var errorListEl = new TagBuilder("ul");
                foreach (var error in ValidationErrors)
                {
                    var errorEl = new TagBuilder("li");
                    errorEl.InnerHtml.AppendHtml(error.Value);
                    errorListEl.InnerHtml.AppendHtml(errorEl);
                }
                SubInputEl.InnerHtml.AppendHtml(errorListEl);
            }
            //else if (!String.IsNullOrWhiteSpace(Description))
            //{
            //	SubInputEl = new TagBuilder("div");
            //	SubInputEl.AddCssClass("field-description form-text text-muted");
            //	SubInputEl.InnerHtml.AppendHtml(Description);
            //}
            #endregion

            #region << Init  EmptyValEl>>
            {
                EmptyValEl = new TagBuilder("div");
                EmptyValEl.AddCssClass("form-control-plaintext");

                var innerSpan = new TagBuilder("span");
                innerSpan.AddCssClass("go-gray");

                var innerEm = new TagBuilder("em");
                innerEm.InnerHtml.Append(EmptyValueMessage);

                innerSpan.InnerHtml.AppendHtml(innerEm);
                EmptyValEl.InnerHtml.AppendHtml(innerSpan);
            }
            #endregion

            return(isSuccess);
        }
예제 #19
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            if (Value == null)
            {
                Value = new List <dynamic>();
            }
            else if (Value is string)
            {
                var stringValue = (string)Value;
                if (String.IsNullOrWhiteSpace(stringValue))
                {
                    Value = new List <dynamic>();
                }
                else if (stringValue.Contains(","))
                {
                    var filePaths = stringValue.Split(",", StringSplitOptions.RemoveEmptyEntries).ToList();
                    Value = GetFileObjectsFromStringList(filePaths);
                }
                else
                {
                    var filePaths = new List <string> {
                        stringValue
                    };
                    Value = GetFileObjectsFromStringList(filePaths);
                }
            }

            else if (Value is List <string> )
            {
                var filePaths = (List <string>)Value;
                Value = GetFileObjectsFromStringList(filePaths);
            }
            else if (Value is List <object> )
            {
                var valueObjects = (List <object>)Value;
                var resultFiles  = new List <dynamic>();
                foreach (var fileObject in valueObjects)
                {
                    var      type          = fileObject.GetType();
                    var      filePath      = "";
                    var      fileSize      = 0;
                    var      fileIcon      = "";
                    var      fileName      = "";
                    DateTime?fileTimestamp = null;
                    var      fileAuthor    = "";

                    if (!String.IsNullOrWhiteSpace(PathPropName) && type.GetProperty(PathPropName) != null && type.GetProperty(PathPropName).GetValue(fileObject) != null)
                    {
                        var propValue = type.GetProperty(PathPropName).GetValue(fileObject);
                        if (propValue is string)
                        {
                            filePath = propValue.ToString();
                        }
                    }

                    if (!String.IsNullOrWhiteSpace(SizePropName) && type.GetProperty(SizePropName) != null && type.GetProperty(SizePropName).GetValue(fileObject) != null)
                    {
                        var propValue = type.GetProperty(SizePropName).GetValue(fileObject);
                        if (propValue is int || propValue is decimal || propValue is double)
                        {
                            fileSize = (int)propValue;
                        }
                    }

                    if (!String.IsNullOrWhiteSpace(IconPropName) && type.GetProperty(IconPropName) != null && type.GetProperty(IconPropName).GetValue(fileObject) != null)
                    {
                        var propValue = type.GetProperty(IconPropName).GetValue(fileObject);
                        if (propValue is string)
                        {
                            fileIcon = propValue.ToString();
                        }
                    }
                    if (!String.IsNullOrWhiteSpace(filePath) && String.IsNullOrWhiteSpace(fileIcon))
                    {
                        fileIcon = "fa " + WvHelpers.GetPathTypeIcon(filePath);
                    }

                    if (!String.IsNullOrWhiteSpace(NamePropName) && type.GetProperty(NamePropName) != null && type.GetProperty(NamePropName).GetValue(fileObject) != null)
                    {
                        var propValue = type.GetProperty(NamePropName).GetValue(fileObject);
                        if (propValue is string)
                        {
                            fileName = propValue.ToString();
                        }
                    }
                    if (!String.IsNullOrWhiteSpace(filePath) && String.IsNullOrWhiteSpace(fileName))
                    {
                        fileName = WvHelpers.GetFileNameFromPath(filePath);
                    }

                    if (!String.IsNullOrWhiteSpace(TimestampPropName) && type.GetProperty(TimestampPropName) != null && type.GetProperty(TimestampPropName).GetValue(fileObject) != null)
                    {
                        var propValue = type.GetProperty(TimestampPropName).GetValue(fileObject);
                        if (propValue is DateTime)
                        {
                            fileTimestamp = (DateTime)propValue;
                        }
                    }

                    if (!String.IsNullOrWhiteSpace(AuthorPropName) && type.GetProperty(AuthorPropName) != null && type.GetProperty(AuthorPropName).GetValue(fileObject) != null)
                    {
                        var propValue = type.GetProperty(AuthorPropName).GetValue(fileObject);
                        if (propValue is string)
                        {
                            fileAuthor = propValue.ToString();
                        }
                    }

                    dynamic file = new
                    {
                        path      = filePath,
                        size      = fileSize,
                        name      = fileName,
                        icon      = fileIcon,
                        timestamp = fileTimestamp,
                        author    = fileAuthor
                    };
                    resultFiles.Add(file);
                }
                Value = resultFiles;
            }
            else
            {
                output.Content.AppendHtml("<div class='go-red'>'value' property type is not supported</div>");
                return(Task.CompletedTask);
            }

            #endregion

            #region << Render >>
            var fileObjects = (List <dynamic>)Value;
            if (Mode == WvFieldRenderMode.Form)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    #region << Hidden input for posting >>
                    output.Content.AppendHtml($"<input type='hidden' id='input-{FieldId}' name='{Name}' value='{String.Join(',', fileObjects.Select(x => x.path).ToList())}'/>");
                    #endregion

                    #region << fake upload >>
                    var inputGroupEl = new TagBuilder("div");
                    inputGroupEl.AddCssClass("input-group erp-file-multiple-input");

                    var fakeInputEl         = new TagBuilder("div");
                    var inputElCssClassList = new List <string>();
                    inputElCssClassList.Add("form-control erp-file with-progress");
                    fakeInputEl.Attributes.Add("id", $"fake-{FieldId}");
                    fakeInputEl.Attributes.Add("style", "cursor:pointer");
                    inputElCssClassList.Add("go-gray");
                    fakeInputEl.InnerHtml.AppendHtml("Activate 'Paste Image' from clipboard");


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

                    var fakeInputProgress = new TagBuilder("div");
                    fakeInputProgress.AddCssClass("form-control-progress");
                    fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress);

                    fakeInputEl.Attributes.Add("data-handler-prefix", GetHandlerPrefix);
                    fakeInputEl.Attributes.Add("data-file-upload-api", FileUploadApi);
                    fakeInputEl.Attributes.Add("data-path-name", PathPropName);
                    fakeInputEl.Attributes.Add("data-size-name", SizePropName);
                    fakeInputEl.Attributes.Add("data-name-name", NamePropName);
                    fakeInputEl.Attributes.Add("data-icon-name", IconPropName);
                    fakeInputEl.Attributes.Add("data-timestamp-name", TimestampPropName);
                    fakeInputEl.Attributes.Add("data-author-name", AuthorPropName);
                    fakeInputEl.Attributes.Add("data-field-id", (FieldId != null ? FieldId.Value.ToString() : ""));


                    inputGroupEl.InnerHtml.AppendHtml(fakeInputEl);

                    var appendEl = new TagBuilder("span");
                    appendEl.AddCssClass("input-group-append action erp-file");
                    var selectFileLink = new TagBuilder("button");
                    selectFileLink.Attributes.Add("type", $"button");
                    selectFileLink.AddCssClass("btn btn-white");
                    selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();");
                    selectFileLink.InnerHtml.AppendHtml("browse");
                    appendEl.InnerHtml.AppendHtml(selectFileLink);
                    inputGroupEl.InnerHtml.AppendHtml(appendEl);
                    output.Content.AppendHtml(inputGroupEl);

                    var realHiddenFileInput = new TagBuilder("input");
                    realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}");
                    realHiddenFileInput.Attributes.Add("type", $"file");
                    realHiddenFileInput.Attributes.Add("multiple", $"true");
                    realHiddenFileInput.AddCssClass("d-none");
                    realHiddenFileInput.Attributes.Add("value", $"");
                    if (!String.IsNullOrWhiteSpace(Accept))
                    {
                        realHiddenFileInput.Attributes.Add("accept", $"{Accept}");
                    }
                    output.Content.AppendHtml(realHiddenFileInput);

                    #endregion

                    #region << Files list element >>

                    var filesListEl = new TagBuilder("div");
                    filesListEl.AddCssClass("form-control erp-file-multiple-list form");
                    filesListEl.Attributes.Add("id", $"fake-list-{FieldId}");

                    if (fileObjects.Count == 0)
                    {
                        filesListEl.AddCssClass("d-none");
                    }


                    //Generate the files list
                    foreach (var fileObject in fileObjects)
                    {
                        var fileRow = new TagBuilder("div");
                        fileRow.AddCssClass("filerow");
                        fileRow.Attributes.Add("data-file-path", fileObject.path);
                        fileRow.Attributes.Add("data-field-id", (FieldId != null ? FieldId.Value.ToString() : ""));

                        //Append icon
                        fileRow.InnerHtml.AppendHtml($"<div class='icon'><i class='fa {fileObject.icon}'></i></div>");

                        //Append meta
                        var rowMeta = new TagBuilder("div");
                        rowMeta.AddCssClass("meta");

                        //Append file
                        rowMeta.InnerHtml.AppendHtml($"<a class='link' href='{GetHandlerPrefix}{fileObject.path}' target='_blank' title='{GetHandlerPrefix}{fileObject.path}'>{fileObject.name}<em></em></a>");

                        if (((int)fileObject.size) > 0 || ((DateTime?)fileObject.timestamp) != null || String.IsNullOrWhiteSpace((string)fileObject.author))
                        {
                            var fileRowMetaDetailsEl = new TagBuilder("div");
                            fileRowMetaDetailsEl.AddCssClass("details");

                            if (((int)fileObject.size) > 0)
                            {
                                var sizeString = WvHelpers.GetSizeStringFromSize((int)fileObject.size);
                                fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='size'>{sizeString}</span>");
                            }
                            if (((DateTime?)fileObject.timestamp) != null)
                            {
                                var timestampString = ((DateTime)fileObject.timestamp).ToString("dd MMM yyyy HH:mm", Culture);
                                fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='timestamp'>{timestampString}</span>");
                            }
                            if (!String.IsNullOrWhiteSpace((string)fileObject.author))
                            {
                                fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='author'>{(string)fileObject.author}</span>");
                            }

                            rowMeta.InnerHtml.AppendHtml(fileRowMetaDetailsEl);
                        }

                        fileRow.InnerHtml.AppendHtml(rowMeta);

                        //Action
                        var rowAction = new TagBuilder("div");
                        rowAction.AddCssClass("action remove");
                        rowAction.InnerHtml.AppendHtml($"<a class='link' href='#'><i class='fa fa-times-circle'></i></a>");

                        fileRow.InnerHtml.AppendHtml(rowAction);
                        filesListEl.InnerHtml.AppendHtml(fileRow);
                    }

                    output.Content.AppendHtml(filesListEl);

                    #endregion


                    var jsCompressor = new JavaScriptCompressor();

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

                            ViewContext.HttpContext.Items[typeof(WvFieldFileMultiple) + fileName] = 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(){
							FieldMultiFileFormInit(""{{FieldId}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));

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

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

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

                    output.PostContent.AppendHtml(initScript);
                    #endregion
                }
                else if (Access == WvFieldAccess.ReadOnly)
                {
                    output.Content.AppendHtml(GenerateDisplayHtml(fileObjects, GetHandlerPrefix));
                }
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                output.Content.AppendHtml(GenerateDisplayHtml(fileObjects, GetHandlerPrefix));
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                foreach (var fileObject in fileObjects)
                {
                    var fileSpan = new TagBuilder("span");
                    fileSpan.AddCssClass("mr-2");
                    if (!String.IsNullOrWhiteSpace(fileObject.icon))
                    {
                        fileSpan.InnerHtml.AppendHtml($"<i class='{fileObject.icon}'></i> ");
                    }
                    var fileLink = new TagBuilder("a");
                    fileLink.Attributes.Add("href", GetHandlerPrefix + fileObject.path);
                    fileLink.Attributes.Add("title", GetHandlerPrefix + fileObject.path);
                    fileLink.Attributes.Add("target", "_blank");

                    fileLink.InnerHtml.Append(fileObject.name);

                    fileSpan.InnerHtml.AppendHtml(fileLink);

                    output.Content.AppendHtml(fileSpan);
                }
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                output.Content.AppendHtml("Not implemented yet");
            }
            #endregion


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

            return(Task.CompletedTask);
        }
예제 #20
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }
            #endregion


            #region << Render >>
            if (Mode == WvFieldRenderMode.Form)
            {
                if (!IsButtonGroup)
                {
                    var wrapper1 = new TagBuilder("div");
                    wrapper1.AddCssClass("form-control-plaintext erp-radio-list");
                    wrapper1.Attributes.Add("id", $"input-" + FieldId);
                    foreach (var selectOption in Options)
                    {
                        var wrapper2 = new TagBuilder("div");
                        wrapper2.AddCssClass("form-check form-check-inline ml-1");
                        var labelWrapper = new TagBuilder("label");
                        labelWrapper.AddCssClass("form-check-label");

                        var inputChkb = new TagBuilder("input");
                        inputChkb.Attributes.Add("type", "radio");
                        inputChkb.Attributes.Add("value", selectOption.Value);
                        inputChkb.Attributes.Add("name", Name);
                        if (Access == WvFieldAccess.ReadOnly)
                        {
                            inputChkb.Attributes.Add("readonly", null);
                        }

                        inputChkb.AddCssClass($"form-check-input {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        if (Value != null && Value == selectOption.Value)
                        {
                            inputChkb.Attributes.Add("checked", "checked");
                        }
                        labelWrapper.InnerHtml.AppendHtml(inputChkb);

                        labelWrapper.InnerHtml.AppendHtml(selectOption.Label);
                        wrapper2.InnerHtml.AppendHtml(labelWrapper);
                        wrapper1.InnerHtml.AppendHtml(wrapper2);
                    }

                    output.Content.AppendHtml(wrapper1);
                }
                else
                {
                    var wrapper1 = new TagBuilder("div");
                    wrapper1.AddCssClass("form-control-plaintext erp-radio-list is-buttons");
                    wrapper1.Attributes.Add("id", $"input-" + FieldId);
                    var wrapper2 = new TagBuilder("div");
                    wrapper2.AddCssClass("btn-group btn-group-toggle");
                    foreach (var selectOption in Options)
                    {
                        var labelWrapper = new TagBuilder("label");
                        if (Value != null && Value == selectOption.Value)
                        {
                            labelWrapper.AddCssClass($"btn {ButtonClassChecked}");
                        }
                        else
                        {
                            labelWrapper.AddCssClass($"btn {ButtonClassUnchecked}");
                        }
                        var inputChkb = new TagBuilder("input");
                        inputChkb.Attributes.Add("type", "radio");
                        inputChkb.Attributes.Add("value", selectOption.Value);
                        inputChkb.Attributes.Add("name", Name);
                        if (Access == WvFieldAccess.ReadOnly)
                        {
                            inputChkb.Attributes.Add("readonly", null);
                        }
                        inputChkb.Attributes.Add("autocomplete", "off");
                        inputChkb.AddCssClass($"form-check-input {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");

                        if (Value != null && Value == selectOption.Value)
                        {
                            inputChkb.Attributes.Add("checked", "checked");
                        }
                        labelWrapper.InnerHtml.AppendHtml(inputChkb);

                        labelWrapper.InnerHtml.AppendHtml(selectOption.Label);
                        wrapper2.InnerHtml.AppendHtml(labelWrapper);
                    }
                    wrapper1.InnerHtml.AppendHtml(wrapper2);
                    output.Content.AppendHtml(wrapper1);
                }

                var jsCompressor = new JavaScriptCompressor();

                #region << Init Scripts >>
                var tagHelperInitialized = false;
                var fileName             = "form.js";
                if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldRadioList) + fileName))
                {
                    var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldRadioList) + fileName];
                    tagHelperInitialized = tagHelperContext.Initialized;
                }
                if (!tagHelperInitialized)
                {
                    var scriptContent = WvHelpers.GetEmbeddedTextResource(fileName, "WebVella.TagHelpers.TagHelpers.WvFieldRadioList", "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(WvFieldRadioList) + fileName] = 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(){
							RadioListFormInit(""{{FieldId}}"",{{IsButtonGroup}},""{{ButtonClassUnchecked}}"",""{{ButtonClassChecked}}"");
						});"                        ;
                scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                scriptTemplate = scriptTemplate.Replace("{{IsButtonGroup}}", IsButtonGroup.ToString().ToLowerInvariant());
                scriptTemplate = scriptTemplate.Replace("{{ButtonClassUnchecked}}", ButtonClassUnchecked);
                scriptTemplate = scriptTemplate.Replace("{{ButtonClassChecked}}", ButtonClassChecked);

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

                output.PostContent.AppendHtml(initScript);
                #endregion
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                var wrapper = new TagBuilder("div");
                wrapper.Attributes.Add("id", $"input-{FieldId}");
                wrapper.AddCssClass("form-control-plaintext erp-radio-list");
                if (!String.IsNullOrWhiteSpace(Name))
                {
                    wrapper.Attributes.Add("data-field-name", Name);
                }
                wrapper.Attributes.Add("data-field-value", (Value ?? "").ToString().ToLowerInvariant());

                var selectedOption = Options.FirstOrDefault(x => x.Value == Value);
                var renderedValue  = Value;
                if (selectedOption != null)
                {
                    renderedValue = selectedOption.Label;
                }

                wrapper.InnerHtml.AppendHtml(renderedValue);
                output.Content.AppendHtml(wrapper);
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                var selectedOption = Options.FirstOrDefault(x => x.Value == Value);
                var renderedValue  = Value;
                if (selectedOption != null)
                {
                    renderedValue = selectedOption.Label;
                }

                output.Content.AppendHtml(renderedValue);
            }
            else if (Mode == WvFieldRenderMode.InlineEdit)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                    //View
                    {
                        var viewWrapper = new TagBuilder("div");
                        viewWrapper.Attributes.Add("id", $"view-{FieldId}");
                        viewWrapper.AddCssClass("input-group view-wrapper");


                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass($"form-control erp-radio-list {(ValidationErrors.Count > 0 ? "is-invalid" : "")}");
                        viewFormControlEl.Attributes.Add("title", "double click to edit");

                        var selectedOption = Options.FirstOrDefault(x => x.Value == Value);
                        var renderedValue  = Value;
                        if (selectedOption != null)
                        {
                            renderedValue = selectedOption.Label;
                        }

                        viewFormControlEl.InnerHtml.AppendHtml(renderedValue);
                        viewWrapper.InnerHtml.AppendHtml(viewFormControlEl);


                        var appendEl = new TagBuilder("span");
                        appendEl.AddCssClass("input-group-append action");
                        var appendButton = new TagBuilder("button");
                        appendButton.Attributes.Add("type", "button");
                        appendButton.Attributes.Add("title", "edit");
                        appendButton.AddCssClass("btn btn-secondary");
                        var appendIcon = new TagBuilder("span");
                        appendIcon.AddCssClass("fa fa-fw fa-pencil-alt");
                        appendButton.InnerHtml.AppendHtml(appendIcon);
                        appendEl.InnerHtml.AppendHtml(appendButton);
                        viewWrapper.InnerHtml.AppendHtml(appendEl);

                        output.Content.AppendHtml(viewWrapper);
                    }

                    //Edit
                    {
                        var editWrapper = new TagBuilder("div");
                        editWrapper.Attributes.Add("id", $"edit-{FieldId}");
                        editWrapper.AddCssClass("edit-wrapper");
                        editWrapper.Attributes.Add("style", "display:none;");
                        var inputGroupEl = new TagBuilder("div");
                        inputGroupEl.AddCssClass("input-group erp-checkbox-list");
                        var editFormControlEl = new TagBuilder("div");
                        editFormControlEl.AddCssClass("form-control");
                        foreach (var selectOption in Options)
                        {
                            var editFormCheckEl = new TagBuilder("div");
                            editFormCheckEl.AddCssClass("form-check form-check-inline ml-1");
                            var editFormLabelEl = new TagBuilder("label");
                            editFormLabelEl.AddCssClass("form-check-label");

                            var editInputEl = new TagBuilder("input");
                            editInputEl.Attributes.Add("type", "radio");
                            editInputEl.Attributes.Add("value", selectOption.Value);
                            editInputEl.AddCssClass("form-check-input");
                            editInputEl.Attributes.Add("name", Name);
                            if (Value != null && Value == selectOption.Value)
                            {
                                editInputEl.Attributes.Add("checked", "checked");
                            }

                            editFormLabelEl.InnerHtml.AppendHtml(editInputEl);
                            editFormLabelEl.InnerHtml.AppendHtml(selectOption.Label);
                            editFormCheckEl.InnerHtml.AppendHtml(editFormLabelEl);
                            editFormControlEl.InnerHtml.AppendHtml(editFormCheckEl);
                        }
                        inputGroupEl.InnerHtml.AppendHtml(editFormControlEl);

                        var editInputAppend = new TagBuilder("span");
                        editInputAppend.AddCssClass("input-group-append");
                        var editSaveBtn = new TagBuilder("button");
                        editSaveBtn.Attributes.Add("type", "submit");
                        editSaveBtn.AddCssClass("btn btn-secondary save");
                        editSaveBtn.Attributes.Add("title", "Save");
                        var editSaveIcon = new TagBuilder("span");
                        editSaveIcon.AddCssClass("fa fa-fw fa-check go-green");
                        editSaveBtn.InnerHtml.AppendHtml(editSaveIcon);
                        editInputAppend.InnerHtml.AppendHtml(editSaveBtn);

                        var editCancelBtn = new TagBuilder("button");
                        editCancelBtn.Attributes.Add("type", "submit");
                        editCancelBtn.AddCssClass("btn btn-secondary cancel");
                        editCancelBtn.Attributes.Add("title", "Cancel");
                        var editCancelIcon = new TagBuilder("span");
                        editCancelIcon.AddCssClass("fa fa-fw fa-times go-gray");
                        editCancelBtn.InnerHtml.AppendHtml(editCancelIcon);
                        editInputAppend.InnerHtml.AppendHtml(editCancelBtn);

                        inputGroupEl.InnerHtml.AppendHtml(editInputAppend);
                        editWrapper.InnerHtml.AppendHtml(inputGroupEl);
                        output.Content.AppendHtml(editWrapper);



                        var jsCompressor = new JavaScriptCompressor();
                        #region << Init Scripts >>
                        var tagHelperInitialized = false;
                        if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldRadioList) + "-inline-edit"))
                        {
                            var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldRadioList) + "-inline-edit"];
                            tagHelperInitialized = tagHelperContext.Initialized;
                        }
                        if (!tagHelperInitialized)
                        {
                            var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldRadioList", "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(WvFieldRadioList) + "-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(){
							RadioListInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                        scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : ""));
                        scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);


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

                        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");

                    var prependWrapper = new TagBuilder("span");
                    prependWrapper.AddCssClass("input-group-prepend");

                    var prependEl = new TagBuilder("span");
                    prependEl.AddCssClass("input-group-text");

                    var prependIcon = new TagBuilder("span");
                    prependIcon.AddCssClass($"fa fa-fw fa-{(Value ? "check" : "times")}");
                    prependEl.InnerHtml.AppendHtml(prependIcon);
                    prependWrapper.InnerHtml.AppendHtml(prependEl);
                    divEl.InnerHtml.AppendHtml(prependWrapper);

                    var selectedOption = Options.FirstOrDefault(x => x.Value == Value);
                    var renderedValue  = Value;
                    if (selectedOption != null)
                    {
                        renderedValue = selectedOption.Label;
                    }

                    var inputEl = new TagBuilder("input");
                    inputEl.AddCssClass("form-control erp-radio-list");
                    inputEl.Attributes.Add("type", "text");
                    inputEl.Attributes.Add("value", renderedValue);
                    inputEl.Attributes.Add("readonly", null);

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

                    var appendTextSpan = new TagBuilder("span");
                    appendTextSpan.AddCssClass("input-group-text");

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

                    appendTextSpan.InnerHtml.AppendHtml(appendIconSpan);

                    appendActionSpan.InnerHtml.AppendHtml(appendTextSpan);

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

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

            return(Task.CompletedTask);
        }
예제 #21
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 == WvFieldRenderMode.Form)
            {
                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.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 Libraries >>
                    var wvLibraryInitialized = false;
                    var libraryItemsKey      = "WebVella-" + "ckeditor";
                    if (ViewContext.HttpContext.Items.ContainsKey(libraryItemsKey))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[libraryItemsKey];
                        wvLibraryInitialized = tagHelperContext.Initialized;
                    }

                    if (!wvLibraryInitialized)
                    {
                        var libJsEl = new TagBuilder("script");
                        libJsEl.Attributes.Add("type", "text/javascript");
                        libJsEl.Attributes.Add("src", "/webvella-taghelpers/lib/ckeditor/ckeditor.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(WvFieldHtml) + "-form"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldHtml) + "-form"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldHtml", "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(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 == WvFieldAccess.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 == WvFieldAccess.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 == WvFieldRenderMode.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 == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                output.Content.AppendHtml((Value ?? "").ToString());
                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 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 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 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 btn-sm");
                        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 btn-sm");
                        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 Libraries >>
                    var wvLibraryInitialized = false;
                    var libraryItemsKey      = "WebVella-" + "ckeditor";
                    if (ViewContext.HttpContext.Items.ContainsKey(libraryItemsKey))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[libraryItemsKey];
                        wvLibraryInitialized = tagHelperContext.Initialized;
                    }

                    if (!wvLibraryInitialized)
                    {
                        var libJsEl = new TagBuilder("script");
                        libJsEl.Attributes.Add("type", "text/javascript");
                        libJsEl.Attributes.Add("src", "/webvella-taghelpers/lib/ckeditor/ckeditor.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(WvFieldHtml) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldHtml) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldHtml", "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(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}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

                    var fieldConfig = new WvFieldHtmlConfig()
                    {
                        ApiUrl       = ApiUrl,
                        CanAddValues = Access == WvFieldAccess.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 == 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  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;
        }
예제 #22
0
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (!isVisible)
            {
                output.SuppressOutput();
                return(Task.CompletedTask);
            }
            #region << Init >>
            var initSuccess = InitField(context, output);

            if (!initSuccess)
            {
                return(Task.CompletedTask);
            }

            #endregion

            #region << Render >>
            if (Mode == WvFieldRenderMode.Form)
            {
                var wrapperEl = new TagBuilder("div");
                wrapperEl.AddCssClass("form-control-plaintext erp-color");
                var inputEl             = new TagBuilder("input");
                var inputElCssClassList = new List <string>();
                inputElCssClassList.Add("d-none");

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

                if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate)
                {
                }
                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));

                wrapperEl.InnerHtml.AppendHtml(inputEl);

                output.Content.AppendHtml(wrapperEl);

                var jsCompressor = new JavaScriptCompressor();

                #region << Init Libraries >>
                var wvLibraryInitialized = false;
                var libraryItemsKey      = "WebVella-" + "spectrum";
                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", "/webvella-taghelpers/lib/spectrum/spectrum.min.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", "/webvella-taghelpers/lib/spectrum/spectrum.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;
                var fileName             = "form.js";
                if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldColor) + fileName))
                {
                    var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldColor) + fileName];
                    tagHelperInitialized = tagHelperContext.Initialized;
                }
                if (!tagHelperInitialized)
                {
                    var scriptContent = WvHelpers.GetEmbeddedTextResource(fileName, "WebVella.TagHelpers.TagHelpers.WvFieldColor", "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(WvFieldColor) + fileName] = 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(){
							ColorFormInit(""{{FieldId}}"");
						});"                        ;
                scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());

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

                output.PostContent.AppendHtml(initScript);
                #endregion
            }
            else if (Mode == WvFieldRenderMode.Display)
            {
                if (!String.IsNullOrWhiteSpace(Value))
                {
                    var divEl = new TagBuilder("div");
                    divEl.Attributes.Add("id", $"input-{FieldId}");
                    divEl.AddCssClass("form-control-plaintext erp-color");
                    var colorDiv = new TagBuilder("div");
                    colorDiv.AddCssClass("color-box");
                    colorDiv.Attributes.Add("style", $"background-color:{(Value ?? "").ToString()}");
                    divEl.InnerHtml.AppendHtml(colorDiv);
                    divEl.InnerHtml.AppendHtml((Value ?? "").ToString());
                    output.Content.AppendHtml(divEl);
                }
                else
                {
                    output.Content.AppendHtml(EmptyValEl);
                }
            }
            else if (Mode == WvFieldRenderMode.Simple)
            {
                output.SuppressOutput();
                output.Content.AppendHtml((Value ?? "").ToString());
                return(Task.CompletedTask);
            }
            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}");

                        var viewInputPrepend = new TagBuilder("span");
                        viewInputPrepend.AddCssClass("input-group-prepend");
                        var viewInputPrependText = new TagBuilder("span");
                        viewInputPrependText.AddCssClass("input-group-text");
                        viewInputPrependText.InnerHtml.AppendHtml($"<i class='fas fa-fw fa-square' style='color:{(Value ?? "#ffffff").ToString()}'></i>");
                        viewInputPrepend.InnerHtml.AppendHtml(viewInputPrependText);
                        viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend);

                        var viewFormControlEl = new TagBuilder("div");
                        viewFormControlEl.AddCssClass("form-control erp-color");
                        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");

                        var editInputEl = new TagBuilder("input");
                        editInputEl.AddCssClass("form-control erp-color");
                        editInputEl.Attributes.Add("type", "color");
                        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", "button");
                        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", "button");
                        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 Libraries >>
                    var wvLibraryInitialized = false;
                    var libraryItemsKey      = "WebVella-" + "spectrum";
                    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", "/webvella-taghelpers/lib/spectrum/spectrum.min.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", "/webvella-taghelpers/lib/spectrum/spectrum.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(WvFieldColor) + "-inline-edit"))
                    {
                        var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldColor) + "-inline-edit"];
                        tagHelperInitialized = tagHelperContext.Initialized;
                    }
                    if (!tagHelperInitialized)
                    {
                        var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldColor", "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(WvFieldColor) + "-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(){
							ColorInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}});
						});"                        ;
                    scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString());
                    scriptTemplate = scriptTemplate.Replace("{{Name}}", Name);

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

                    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");

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

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

                    var appendTextSpan = new TagBuilder("span");
                    appendTextSpan.AddCssClass("input-group-text");

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

                    appendTextSpan.InnerHtml.AppendHtml(appendIconSpan);

                    appendActionSpan.InnerHtml.AppendHtml(appendTextSpan);

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


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

            return(Task.CompletedTask);
        }