Example #1
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var metadata      = For.Metadata;
            var modelExplorer = For.ModelExplorer;

            var input = _generator.GenerateHidden(ViewContext, modelExplorer, For.Name, modelExplorer.Model, false, null);

            string outputString;

            using (var writer = new StringWriter())
            {
                input.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                outputString = writer.ToString();
            }

            output.Content.SetHtmlContent(outputString);
            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.SetAttribute("class", "hidden");
        }
Example #2
0
        protected virtual IHtmlContent GenerateHidden(
            ModelExplorer modelExplorer,
            string expression,
            object value,
            bool useViewData,
            object htmlAttributes)
        {
            var tagBuilder = _htmlGenerator.GenerateHidden(
                ViewContext,
                modelExplorer,
                expression,
                value,
                useViewData,
                htmlAttributes);

            if (tagBuilder == null)
            {
                return(HtmlString.Empty);
            }

            return(tagBuilder);
        }
Example #3
0
        protected virtual HtmlString GenerateHidden(
            ModelMetadata metadata,
            string name,
            object value,
            bool useViewData,
            object htmlAttributes)
        {
            var tagBuilder =
                _htmlGenerator.GenerateHidden(
                    ViewContext,
                    metadata,
                    name,
                    value,
                    useViewData,
                    htmlAttributes);

            if (tagBuilder == null)
            {
                return(HtmlString.Empty);
            }

            return(tagBuilder.ToHtmlString(TagRenderMode.SelfClosing));
        }
Example #4
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var api_SearchPath = TmsUtility.GetAPIPath(Type);

            output.TagName = "";
            var host       = Context.HttpContext.Request.Host;
            var mainDivTag = new TagBuilder("div");

            var inputDivTag = new TagBuilder("div");

            inputDivTag.Attributes.Add("style", $"width:{Width}px");
            inputDivTag.Attributes.Add("class", "input-group");

            var dropdownDiv = new TagBuilder("div");

            dropdownDiv.Attributes.Add("class", "drop-down-group");
            dropdownDiv.Attributes.Add("style", "display:none");

            var hiddenElement = _htmlGenerator.GenerateHidden(Context, For.ModelExplorer, For.Name, For.ModelExplorer.Model, false, "");
            var Id            = hiddenElement.Attributes["id"];

            var inputSpan = new TagBuilder("span");

            inputSpan.Attributes.Add("class", "input-group-btn");
            var searchSpan = new TagBuilder("span");

            searchSpan.Attributes.Add("class", "fa fa-search");
            var clearSpan = new TagBuilder("span");

            clearSpan.Attributes.Add("class", "fa fa-eraser");
            var loadingSpan = new TagBuilder("span");

            loadingSpan.Attributes.Add("class", "fas fa-spinner fa-spin");

            var searchBtn = new TagBuilder("button");

            searchBtn.Attributes.Add("class", "btn btn-default");
            searchBtn.Attributes.Add("type", "button");
            searchBtn.Attributes.Add("id", Id + "_imgSearch");
            searchBtn.InnerHtml.AppendHtml(searchSpan);

            var clearBtn = new TagBuilder("button");

            clearBtn.Attributes.Add("class", "btn btn-default");
            clearBtn.Attributes.Add("type", "button");
            clearBtn.Attributes.Add("id", Id + "_imgClear");
            clearBtn.Attributes.Add("style", "display:none");
            clearBtn.InnerHtml.AppendHtml(clearSpan);

            var loadingBtn = new TagBuilder("button");

            loadingBtn.Attributes.Add("class", "btn btn-default");
            loadingBtn.Attributes.Add("type", "button");
            loadingBtn.Attributes.Add("id", Id + "_imgLoading");
            loadingBtn.Attributes.Add("style", "display:none");
            loadingBtn.InnerHtml.AppendHtml(loadingSpan);

            inputSpan.InnerHtml.AppendHtml(searchBtn);
            inputSpan.InnerHtml.AppendHtml(loadingBtn);
            inputSpan.InnerHtml.AppendHtml(clearBtn);

            var inputElement = new TagBuilder("input");

            inputElement.Attributes.Add("class", "form-control");
            inputElement.Attributes.Add("type", "text");
            inputElement.Attributes.Add("id", Id + "_input");
            inputElement.Attributes.Add("value", Input_Value);
            inputElement.Attributes.Add("placeholder", Default_Input);

            inputDivTag.InnerHtml.AppendHtml(inputElement);

            var selectElement = new TagBuilder("select");

            selectElement.Attributes.Add("id", Id + "_ddl");
            selectElement.Attributes.Add("class", "form-control");
            selectElement.Attributes.Add("style", $"width:{Width - 40}px");
            dropdownDiv.InnerHtml.AppendHtml(selectElement);
            inputDivTag.InnerHtml.AppendHtml(dropdownDiv);
            inputDivTag.InnerHtml.AppendHtml(inputSpan);

            mainDivTag.Attributes.Add("id", Id + "_utb");
            mainDivTag.InnerHtml.AppendHtml(inputDivTag);
            mainDivTag.InnerHtml.AppendHtml(hiddenElement);

            var scriptText = $@"
								$(function () {{
									var dd_Activated = false;
									$('#{Id}_imgSearch').click(function () {{
										var searchValue = $('#{Id}_input').val();
										if (searchValue.trim().length === 0) {{
											alert('Search parameter is empty!');
										}}
										else {{
											$('#{Id}_imgSearch').hide();
											$('#{Id}_imgLoading').show();
											$.ajax({{
												url: '{Context.HttpContext.Request.Scheme}://{Context.HttpContext.Request.Host}/{api_SearchPath}'+searchValue,
												dataType: 'json',
											}}).done(function (data) {{
												if (data.length === 0) {{
													alert('No Resuls Found');
													$('#{Id}').val('');
													$('#{Id}_utb').find('#{Id}_input').val('');
													$('#{Id}_imgSearch').show();
													$('#{Id}_imgLoading').hide();
												}}
												else if (data.length === 1) {{
													$('#{Id}_utb').find('#{Id}_input').val(data[0].text);
													$('#{Id}').val(data[0].upn);
													$('#{Id}_imgSearch').hide();
													$('#{Id}_imgLoading').hide();
													$('#{Id}_imgClear').show();
												}}
												else {{
													if (data.length > 100) {{
														alert('There are more than 100 Results.Please modify search parameter');
													}}
													var initialSelect = {{
														id: 0,
														text: 'Select',
													}}
													result = $.map(data, function (x) {{
														return {{
															id: x.value,
															text: x.text,
														}};
													}});
													result.unshift(initialSelect);
													$('#{Id}_ddl').select2({{
														placeholder: 'Select an employee',
														data: result,
													}});
													dd_Activated = true;
													$('#{Id}_utb').find('#{Id}_input').hide();
													$('#{Id}_utb').find('div.drop-down-group').show();
													$('#{Id}_imgSearch').hide();
													$('#{Id}_imgLoading').hide();
													$('#{Id}_imgClear').show();
													$('#{Id}_ddl').on('change.select2', function () {{
														var selectedText = $('#{Id}_ddl').select2('data');
														if (selectedText[0]) {{
															$('#{Id}_utb').find('#{Id}_input').val(selectedText[0].text);
															$('#{Id}').val(selectedText[0].id);
														}}
														else {{
															$('#{Id}_utb').find('#{Id}_input').val('');
															$('#{Id}').val('');
														}}
														$('#{Id}_utb').find('div.drop-down-group').hide();
														$('#{Id}_utb').find('#{Id}_input').show();
														$('#{Id}_imgSearch').hide();
														$('#{Id}_imgLoading').hide();
														$('#{Id}_imgClear').show();
													}})
												}}
											}})
												.fail(function () {{
													alert('An error occured in API request');
													$('#{Id}').val('');
													$('#{Id}_utb').find('#{Id}_input').val('');
													$('#{Id}_imgSearch').show();
													$('#{Id}_imgLoading').hide();
												}});
										}}
									}})

									$('#{Id}_imgClear').click(function () {{
										$('#{Id}').val('');
										$('#{Id}_utb').find('#{Id}_input').val('');
										if (dd_Activated) {{
											$('#{Id}_ddl').html('').select2({{ data: [{{ id: '', text: '' }}] }});
											$('#{Id}_ddl').val([]).trigger('change');
											$('#{Id}_utb').find('div.drop-down-group').hide();
										}}
										$('#{Id}_utb').find('#UPN_input').show();
										$('#{Id}_imgSearch').show();
										$('#{Id}_imgClear').hide();
									}});

									if('{Input_Value}'.length!==0){{
											$('#{Id}_imgSearch').hide();
											$('#{Id}_imgLoading').hide();
											$('#{Id}_imgClear').show();
										}}
								}})
							"                            ;

            output.Content.AppendHtml(mainDivTag);
            _scriptHelper.AddScriptText(scriptText);
        }
        private void WriteTimeRangeBlock(TextWriter writer, bool isHidden = false)
        {
            var dateModelExplorer      = For.ModelExplorer.Properties.Single(p => p.Metadata.PropertyName == nameof(TimeRange.StartDate));
            var dateFieldName          = $"{For.Name}.{nameof(TimeRange.StartDate)}";
            var startTimeModelExplorer = For.ModelExplorer.Properties.Single(p => p.Metadata.PropertyName == nameof(TimeRange.StartTime));
            var startTimeFieldName     = $"{For.Name}.{nameof(TimeRange.StartTime)}";
            var endTimeModelExplorer   = For.ModelExplorer.Properties.Single(p => p.Metadata.PropertyName == nameof(TimeRange.EndTime));
            var endTimeFieldName       = $"{For.Name}.{nameof(TimeRange.EndTime)}";

            object dateValue      = null;
            object startTimeValue = null;
            object endTimeValue   = null;

            if (For.Model != null)
            {
                dateValue      = dateModelExplorer.Model;
                startTimeValue = startTimeModelExplorer.Model;
                endTimeValue   = endTimeModelExplorer.Model;
            }

            if (isHidden)
            {
                //Make three hidden fields
                var tagBuilder = _htmlGenerator.GenerateHidden(ViewContext, dateModelExplorer, dateFieldName, ((DateTime)dateValue).ToSwedishString("yyyy-MM-dd"), false, null);
                tagBuilder.WriteTo(writer, _htmlEncoder);
                var tagBuilder2 = _htmlGenerator.GenerateHidden(ViewContext, startTimeModelExplorer, startTimeFieldName, ((TimeSpan)startTimeValue).ToSwedishString(@"hh\:mm"), false, null);
                tagBuilder2.WriteTo(writer, _htmlEncoder);
                var tagBuilder3 = _htmlGenerator.GenerateHidden(ViewContext, endTimeModelExplorer, endTimeFieldName, ((TimeSpan)endTimeValue).ToSwedishString(@"hh\:mm"), false, null);
                tagBuilder3.WriteTo(writer, _htmlEncoder);
            }
            else
            {
                // Check if label will be displayed
                var type                = For.ModelExplorer.Metadata.ContainerType;
                var property            = type.GetProperty(For.ModelExplorer.Metadata.PropertyName);
                var stayWithinAttribute = property.CustomAttributes.SingleOrDefault(c => c.AttributeType == typeof(StayWithinOriginalRangeAttribute));

                IDictionary <string, string> extraAttributes = null;

                WriteLabelWithoutFor(writer, true);
                var inlineClass = "form-inline";

                if (stayWithinAttribute != null)
                {
                    inlineClass    += " staywithin-fields";
                    extraAttributes = new Dictionary <string, string>
                    {
                        { "data-staywithin-validator", $"{For.Name}_validator" },
                    };
                }
                writer.WriteLine($"<div class=\"{inlineClass}\">");
                WriteDatePickerInput(dateModelExplorer, dateFieldName, dateValue, writer, extraAttributes: extraAttributes);
                WriteTimePickerInput(startTimeModelExplorer, startTimeFieldName, startTimeValue, writer, extraAttributes);
                WriteRightArrowSpan(writer);
                WriteTimePickerInput(endTimeModelExplorer, endTimeFieldName, endTimeValue, writer, extraAttributes);

                writer.WriteLine("</div>"); // form-inline.

                WriteValidation(writer, dateModelExplorer, dateFieldName);
                WriteValidation(writer, startTimeModelExplorer, startTimeFieldName);
                WriteValidation(writer, endTimeModelExplorer, endTimeFieldName);
                if (stayWithinAttribute != null)
                {
                    var hiddenValidationField = _htmlGenerator.GenerateHidden(ViewContext, For.ModelExplorer, $"{For.Name}_validator", null, false, null);
                    hiddenValidationField.Attributes.Add(new KeyValuePair <string, string>("data-rule-staywithin", ".time-range-part"));
                    hiddenValidationField.Attributes.Add(new KeyValuePair <string, string>("data-msg-staywithin", (string)stayWithinAttribute.NamedArguments.Single(a => a.MemberName == "ErrorMessage").TypedValue.Value));
                    hiddenValidationField.Attributes.Add(new KeyValuePair <string, string>("data-rule-otherproperty", (string)stayWithinAttribute.NamedArguments.Single(a => a.MemberName == "OtherRangeProperty").TypedValue.Value));
                    hiddenValidationField.AddCssClass("force-validation");
                    hiddenValidationField.Attributes.Remove("data-val-required");
                    hiddenValidationField.WriteTo(writer, _htmlEncoder);

                    WriteValidation(writer, For.ModelExplorer, $"{For.Name}_validator");
                }
            }
        }
Example #6
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var isLg       = output.Attributes.SingleOrDefault(a => a.Name == "lg") != null;
            var isSm       = output.Attributes.SingleOrDefault(a => a.Name == "sm") != null;
            var isFloating = output.Attributes.SingleOrDefault(a => a.Name == "float") != null;
            var isDisabled = output.Attributes.SingleOrDefault(a => a.Name == "disabled") != null;
            var isReadOnly = output.Attributes.SingleOrDefault(a => a.Name == "readonly") != null;
            var isRequired = output.Attributes.SingleOrDefault(a => a.Name == "required") != null;
            var hasWarning = output.Attributes.SingleOrDefault(a => a.Name == "has-warning") != null;
            var hasSuccess = output.Attributes.SingleOrDefault(a => a.Name == "has-success") != null;
            var hasError   = output.Attributes.SingleOrDefault(a => a.Name == "has-error") != null;

            var icon        = output.Attributes.SingleOrDefault(a => a.Name == "icon");
            var placeholder = output.Attributes.SingleOrDefault(a => a.Name == "placeholder");

            var binding = output.Attributes.SingleOrDefault(a => a.Name == "for");

            output.Attributes.RemoveAll("lg");
            output.Attributes.RemoveAll("sm");
            output.Attributes.RemoveAll("float");
            output.Attributes.RemoveAll("disabled");
            output.Attributes.RemoveAll("readonly");
            output.Attributes.RemoveAll("required");
            output.Attributes.RemoveAll("has-warning");
            output.Attributes.RemoveAll("has-success");
            output.Attributes.RemoveAll("has-error");
            output.Attributes.RemoveAll("icon");
            output.Attributes.RemoveAll("placeholder");
            output.Attributes.RemoveAll("for");

            var formGroupClass = "form-group pmd-textfield";

            var labelAttributes  = new Dictionary <string, object>();
            var selectAttributes = new Dictionary <string, object>
            {
                { "class", "form-control pmd-select2-tags tags" }
            };

            if (isLg)
            {
                formGroupClass = $"{formGroupClass} form-group-lg";
            }
            if (isSm)
            {
                formGroupClass = $"{formGroupClass} form-group-sm";
            }
            if (isFloating)
            {
                formGroupClass = $"{formGroupClass} pmd-textfield-floating-label";
            }
            if (hasWarning)
            {
                formGroupClass = $"{formGroupClass} has-warning";
            }
            if (hasSuccess)
            {
                formGroupClass = $"{formGroupClass} has-sucess";
            }
            if (hasError)
            {
                formGroupClass = $"{formGroupClass} has-error";
            }

            if (isDisabled)
            {
                selectAttributes.Add("disabled", "disabled");
            }
            if (isReadOnly)
            {
                selectAttributes.Add("readonly", "readonly");
            }
            if (isRequired)
            {
                selectAttributes.Add("required", "required");
            }

            var    inputPre          = "";
            var    inputPost         = "";
            var    labelClass        = "control-label";
            string placeholderString = placeholder != null?placeholder.Value.ToString() : "";

            if (icon != null)
            {
                inputPre   = $"{inputPre}<div class=\"input-group\"><div class=\"input-group-addon\"><i class=\"material-icons md-dark pmd-sm\">{icon.Value}</i></div>";
                inputPost  = $"{inputPost}</div>";
                labelClass = $"{labelClass} pmd-input-group-label";
            }

            labelAttributes.Add("class", labelClass);

            var metadata      = For.Metadata;
            var modelExplorer = For.ModelExplorer;

            var items = new List <SelectListItem>();

            if (modelExplorer.Model != null)
            {
                foreach (var item in (string[])modelExplorer.Model)
                {
                    items.Add(new SelectListItem()
                    {
                        Text = item, Value = item
                    });
                }
            }

            var select = _generator.GenerateSelect(ViewContext, modelExplorer, placeholderString, For.Name, items, true, selectAttributes);
            var label  = _generator.GenerateLabel(ViewContext, modelExplorer, For.Name, metadata.DisplayName, labelAttributes);
            var hidden = _generator.GenerateHidden(ViewContext, modelExplorer, For.Name, null, true, null);

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Attributes.SetAttribute("class", formGroupClass);

            string selectOutput;

            using (var writer = new StringWriter())
            {
                label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                writer.Write(inputPre);
                select.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                writer.Write(inputPost);
                //hidden.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                selectOutput = writer.ToString();
            }

            output.Content.SetHtmlContent(selectOutput);
        }
Example #7
0
 public TagBuilder GenerateHidden(ViewContext viewContext, ModelExplorer modelExplorer, string expression, object value, bool useViewData, object htmlAttributes)
 {
     return(_htmlGenerator.GenerateHidden(viewContext, modelExplorer, expression, value, useViewData, htmlAttributes));
 }
Example #8
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var placeholder = output.HasAttribute("placeholder") ? output.GetAttribute("placeholder") : "";

            output.AddClass("form-group pmd-textfield");

            var labelAttributes  = new Dictionary <string, object>();
            var selectAttributes = new Dictionary <string, object>
            {
                { "class", "select-simple form-control pmd-select2" }
            };

            if (IsLg)
            {
                output.AddClass("form-group-lg");
            }
            if (IsSm)
            {
                output.AddClass("form-group-sm");
            }
            if (IsFloating)
            {
                output.AddClass("pmd-textfield-floating-label");
            }
            if (HasWarning)
            {
                output.AddClass("has-warning");
            }
            if (HasSuccess)
            {
                output.AddClass("has-success");
            }
            if (HasError)
            {
                output.AddClass("has-error");
            }

            if (Disabled)
            {
                selectAttributes.Add("disabled", "disabled");
            }
            if (ReadOnly)
            {
                selectAttributes.Add("readonly", "readonly");
            }
            if (Required)
            {
                selectAttributes.Add("required", "required");
            }

            var inputPre   = "";
            var inputPost  = "";
            var labelClass = "control-label";

            if (Icon != null)
            {
                inputPre   = $"{inputPre}<div class=\"input-group\"><div class=\"input-group-addon\"><i class=\"material-icons md-dark pmd-sm\">{Icon}</i></div>";
                inputPost  = $"{inputPost}</div>";
                labelClass = $"{labelClass} pmd-input-group-label";
            }

            labelAttributes.Add("class", labelClass);

            var metadata      = For.Metadata;
            var modelExplorer = For.ModelExplorer;

            var modelType = For.ModelExplorer.ModelType;

            if (Nullable.GetUnderlyingType(For.ModelExplorer.ModelType) != null)
            {
                modelType = Nullable.GetUnderlyingType(For.ModelExplorer.ModelType);
            }

            if (modelType.IsEnum)
            {
                var selectListItems = new List <SelectListItem>();
                var enumNames       = Enum.GetNames(modelType);
                var enumValues      = Enum.GetValues(modelType);
                var attributes      = modelType.GetCustomAttribute <DisplayAttribute>();

                int i = 0;
                foreach (var name in enumNames)
                {
                    var display = modelType.GetMember(name).First().GetCustomAttribute <DisplayAttribute>();
                    selectListItems.Add(new SelectListItem()
                    {
                        Text  = display == null ? name : display.Name,
                        Value = enumValues.GetValue(i).ToString()
                    });

                    i++;
                }

                Items = selectListItems;
            }

            var select = _generator.GenerateSelect(ViewContext, modelExplorer, placeholder, For.Name, Items, Multiple, selectAttributes);
            var label  = _generator.GenerateLabel(ViewContext, modelExplorer, For.Name, metadata.DisplayName, labelAttributes);
            var hidden = _generator.GenerateHidden(ViewContext, modelExplorer, For.Name, null, true, null);

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;

            string selectOutput;

            using (var writer = new StringWriter())
            {
                label.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                writer.Write(inputPre);
                select.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                writer.Write(inputPost);
                hidden.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
                selectOutput = writer.ToString();
            }

            output.Content.SetHtmlContent(selectOutput);
        }