Exemple #1
0
        /// <summary>
        /// Method which processes the element and updates the content
        /// if all conditions are met and the given parameters are valid
        /// </summary>
        /// <param name="context">The current tag helper context</param>
        /// <param name="output">The output of this tag helper, the result is written here</param>
        /// <returns>Task indicating when the processing is completed</returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var childContent = await output.GetChildContentAsync();

            ServiceContext serviceContext = ViewContext.ViewBag.ServiceContext;
            RequestContext requestContext = ViewContext.ViewBag.RequestContext;

            if (!string.IsNullOrEmpty(AltinnTextKey))
            {
                if (serviceContext.ServiceText.ContainsKey(AltinnTextKey) &&
                    serviceContext.ServiceText[AltinnTextKey].ContainsKey(serviceContext.CurrentCulture))
                {
                    var href      = output.Attributes["href"];
                    var serverurl = ViewContext.HttpContext.Request.Host;
                    var url       = "/ui/" + serviceContext.ServiceMetaData.Org + "/" + serviceContext.ServiceMetaData.RepositoryName + "/" + requestContext.InstanceId + "/" + output.Attributes["name"].Value;
                    var protocol  = ViewContext.HttpContext.Request.Protocol.Split('/')[0]; // Strips away the protocol version in the last part
                    href = new TagHelperAttribute("href", protocol + "://" + serverurl + url, href.ValueStyle);
                    output.Attributes.SetAttribute(href);

                    var content =
                        ServiceTextHelper.SetTextParams(
                            serviceContext.ServiceText[AltinnTextKey][serviceContext.CurrentCulture], requestContext, serviceContext);
                    output.Content.SetHtmlContent(content);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Method which processes the element and updates the content
        /// if all conditions are met and the given parameters are valid
        /// </summary>
        /// <param name="context">The current tag helper context</param>
        /// <param name="output">The output of this tag helper, the result is written here</param>
        /// <returns>Task indicating when the processing is completed</returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var childContent = await output.GetChildContentAsync();

            ServiceContext serviceContext = ViewContext.ViewBag.ServiceContext;
            RequestContext requestContext = ViewContext.ViewBag.RequestContext;

            if (!string.IsNullOrEmpty(AltinnTextKey))
            {
                if (serviceContext.ServiceText.ContainsKey(AltinnTextKey) &&
                    serviceContext.ServiceText[AltinnTextKey].ContainsKey(serviceContext.CurrentCulture))
                {
                    output.Content.SetHtmlContent(ServiceTextHelper.SetTextParams(serviceContext.ServiceText[AltinnTextKey][serviceContext.CurrentCulture], requestContext, serviceContext));
                }
            }
        }
        /// <inheritdoc />
        /// <remarks>Does nothing if <see cref="For"/> is <c>null</c>.</remarks>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            var tagBuilder = Generator.GenerateLabel(
                ViewContext,
                For.ModelExplorer,
                For.Name,
                labelText: null,
                htmlAttributes: null);

            if (tagBuilder != null)
            {
                output.MergeAttributes(tagBuilder);

                ServiceContext serviceContext = ViewContext.ViewBag.ServiceContext;
                RequestContext requestContext = ViewContext.ViewBag.RequestContext;

                // We check for whitespace to detect scenarios such as:
                // <label for="Name">
                // </label>
                if (!output.IsContentModified)
                {
                    var childContent = await output.GetChildContentAsync();

                    string labelText = string.Empty;
                    string modelpath = serviceContext.RootName + "." + ModelHelper.GetMetadataModelPath(For.Name);

                    bool textSet = false;

                    // Check if specific Text key is used (for reuse of textkey)
                    if (!string.IsNullOrEmpty(AltinnTextKey))
                    {
                        if (serviceContext.ServiceText.ContainsKey(AltinnTextKey) &&
                            serviceContext.ServiceText[AltinnTextKey].ContainsKey(serviceContext.CurrentCulture))
                        {
                            output.Content.SetHtmlContent(serviceContext.ServiceText[AltinnTextKey][serviceContext.CurrentCulture]);
                            textSet = true;
                        }
                    }

                    // First see if the text is defined in the Service Metadata
                    if (!textSet && serviceContext.ServiceMetaData?.Elements?.ContainsKey(modelpath) == true)
                    {
                        ElementMetadata elementMetaData = serviceContext.ServiceMetaData?.Elements[modelpath];
                        if (elementMetaData.Texts?.ContainsKey(TextCategoryType.Label.ToString()) == true)
                        {
                            string textKey = elementMetaData.Texts[TextCategoryType.Label.ToString()];

                            if (serviceContext.ServiceText.ContainsKey(serviceContext.CurrentCulture) &&
                                serviceContext.ServiceText[serviceContext.CurrentCulture].ContainsKey(textKey))
                            {
                                output.Content.SetHtmlContent(ServiceTextHelper.SetTextParams(serviceContext.ServiceText[serviceContext.CurrentCulture][textKey], requestContext, serviceContext));
                                textSet = true;
                            }
                        }
                        else
                        {
                            // Try to collect the text from model path
                            string textKey = TextCategoryType.Label + "." + modelpath;
                            if (serviceContext.ServiceText.ContainsKey(serviceContext.CurrentCulture) &&
                                serviceContext.ServiceText[serviceContext.CurrentCulture].ContainsKey(textKey))
                            {
                                output.Content.SetHtmlContent(ServiceTextHelper.SetTextParams(serviceContext.ServiceText[serviceContext.CurrentCulture][textKey], requestContext, serviceContext));
                                textSet = true;
                            }
                        }
                    }

                    if (!textSet)
                    {
                        if (childContent.IsEmptyOrWhiteSpace)
                        {
                            // Provide default label text since there was nothing useful in the Razor source.
                            output.Content.SetHtmlContent(tagBuilder.InnerHtml);
                        }
                        else
                        {
                            output.Content.SetHtmlContent(childContent);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Builds the validation summary
        /// </summary>
        /// <param name="context">The current tag helper context</param>
        /// <param name="output">The output of the tag helper, is written to when creating the result</param>
        /// <returns>A task</returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "div";
            var childContent = await output.GetChildContentAsync();

            RequestContext requestContext = ViewContext.ViewBag.RequestContext;
            ServiceContext serviceContext = ViewContext.ViewBag.ServiceContext;

            if (requestContext != null && requestContext.ValidationResult != null)
            {
                int numberOfValidationErrors = requestContext.ValidationResult.Where(vs => vs.ValidationStatusType.Equals(ValidationStatusType.Error)).ToList().Count;

                if (numberOfValidationErrors > 0)
                {
                    output.Attributes.Add("class", "card card-inverse card-danger");
                    TagBuilder cardBlockTag = new TagBuilder("div");
                    cardBlockTag.AddCssClass("card-body");

                    TagBuilder cardTitleTag = new TagBuilder("h3");
                    cardTitleTag.InnerHtml.AppendHtml("Du har " + numberOfValidationErrors + " feil");
                    cardTitleTag.AddCssClass("card-title");
                    cardBlockTag.InnerHtml.AppendHtml(cardTitleTag);

                    TagBuilder listBuilder = new TagBuilder("ul");
                    listBuilder.AddCssClass("card-text");
                    foreach (ValidationResult validationResult in requestContext.ValidationResult.OrderBy(w => w.ValidationGroup))
                    {
                        TagBuilder errorListItem = new TagBuilder("li");
                        TagBuilder errorLink     = new TagBuilder("a");

                        string url = "/ui/" + requestContext.InstanceId + "/" + validationResult.ViewID;
                        if (validationResult.CustomParameters != null && validationResult.CustomParameters.Count > 0)
                        {
                            url += "?";
                            string splitter = string.Empty;
                            foreach (KeyValuePair <string, string> kvp in validationResult.CustomParameters)
                            {
                                url     += splitter;
                                url     += kvp.Key + "=" + kvp.Value;
                                splitter = "&";
                            }

                            url += '#' + validationResult.ModelKey.Replace('.', '_').Replace("[", "_").Replace("]", "_");
                        }

                        errorLink.Attributes.Add("href", url);
                        errorLink.InnerHtml.AppendHtml(ServiceTextHelper.SetTextParams(ServiceTextHelper.GetServiceText(validationResult.ValidationMessageKey, serviceContext.ServiceText, validationResult.MessageParams, serviceContext.CurrentCulture), requestContext, serviceContext));

                        errorListItem.InnerHtml.AppendHtml(errorLink);
                        listBuilder.InnerHtml.AppendHtml(errorListItem);
                    }

                    cardBlockTag.InnerHtml.AppendHtml(listBuilder);
                    output.Content.AppendHtml(cardBlockTag);
                }
                else
                {
                    output.Attributes.Add("class", "card card-inverse card-success");
                }
            }
            else
            {
                output.Content.Clear();
            }
        }
Exemple #5
0
        /// <inheritdoc />
        /// <remarks>Does nothing if <see cref="For"/> is <c>null</c>.</remarks>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            if (For != null)
            {
                var tagBuilder = Generator.GenerateValidationMessage(
                    ViewContext,
                    For.ModelExplorer,
                    For.Name,
                    message: null,
                    tag: null,
                    htmlAttributes: null);

                if (tagBuilder != null)
                {
                    RequestContext requestContext = ViewContext.ViewBag.RequestContext;
                    ServiceContext serviceContext = ViewContext.ViewBag.ServiceContext;

                    var fullName = GetFullHtmlFieldName(ViewContext, For.Name);

                    var tryGetModelStateResult = ViewContext.ViewData.ModelState.TryGetValue(fullName, out ModelStateEntry entry);
                    var modelErrors            = tryGetModelStateResult ? entry.Errors : null;

                    ModelError modelError = null;
                    if (modelErrors != null && modelErrors.Count != 0)
                    {
                        modelError = modelErrors[0];
                    }

                    output.MergeAttributes(tagBuilder);

                    // We check for whitespace to detect scenarios such as:
                    // <span validation-for="Name">
                    // </span>
                    if (!output.IsContentModified)
                    {
                        var childContent = await output.GetChildContentAsync();

                        if (modelError != null)
                        {
                            output.Content.SetHtmlContent(ServiceTextHelper.SetTextParams(ServiceTextHelper.GetServiceText(modelError.ErrorMessage, serviceContext.ServiceText, null, serviceContext.CurrentCulture), requestContext, serviceContext));
                        }
                        else if (childContent.IsEmptyOrWhiteSpace)
                        {
                            // Provide default label text since there was nothing useful in the Razor source.
                            output.Content.SetHtmlContent(tagBuilder.InnerHtml);
                        }
                        else
                        {
                            output.Content.SetHtmlContent(childContent);
                        }
                    }
                }
            }
        }