Ejemplo n.º 1
0
        protected static HtmlTag AdaptiveCardRender(AdaptiveCard card, AdaptiveRendererContext context)
        {
            var uiCard = new DivTag()
                         .AddClass($"ac-{card.Type.ToLower()}")
                         .Style("width", "100%")
                         .Style("background-color", context.GetRGBColor(context.Config.ContainerStyles.Default.BackgroundColor))
                         .Style("padding", $"{context.Config.Spacing.Padding}px")
                         .Style("box-sizing", "border-box");

            if (!string.IsNullOrEmpty(context.Config.FontFamily))
            {
                uiCard.Style("font-family", context.Config.FontFamily);
            }

            if (card.BackgroundImage != null)
            {
                uiCard = uiCard.Style("background-image", $"url('{card.BackgroundImage}')")
                         .Style("background-repeat", "no-repeat")
                         .Style("background-size", "cover");
            }

            AddContainerElements(uiCard, card.Body, card.Actions, context);

            return(uiCard);
        }
Ejemplo n.º 2
0
        protected static HtmlTag AdaptiveCardRender(AdaptiveCard card, AdaptiveRenderContext context)
        {
            var uiCard = new DivTag()
                         .AddClass($"ac-{card.Type.ToLower()}")
                         .Style("width", "100%")
                         .Style("background-color", context.GetRGBColor(context.Config.ContainerStyles.Default.BackgroundColor))
                         .Style("padding", $"{context.Config.Spacing.Padding}px")
                         .Style("box-sizing", "border-box");

            if (!string.IsNullOrEmpty(context.Config.FontFamily))
            {
                uiCard.Style("font-family", context.Config.FontFamily);
            }

            if (card.BackgroundImage != null)
            {
                uiCard.Style("background-image", $"url('{context.Config.ResolveFinalAbsoluteUri(card.BackgroundImage)}')")
                .Style("background-repeat", "no-repeat")
                .Style("background-size", "cover");
            }

            AddContainerElements(uiCard, card.Body, card.Actions, context);

            AddSelectAction(uiCard, card.SelectAction, context);

            // Add all accumulated selectAction show cards
            foreach (var showCard in context.ShowCardTags)
            {
                uiCard.Children.Add(showCard);
            }

            return(uiCard);
        }
Ejemplo n.º 3
0
        protected static HtmlTag ContainerRender(AdaptiveContainer container, AdaptiveRenderContext context)
        {
            var uiContainer = new DivTag()
                              .AddClass($"ac-{container.Type.Replace(".", "").ToLower()}");

            if (container.Style != null)
            {
                // Apply background color
                var containerStyle = context.Config.ContainerStyles.Default;
                if (container.Style == AdaptiveContainerStyle.Emphasis)
                {
                    containerStyle = context.Config.ContainerStyles.Emphasis;
                }

                uiContainer.Style("background-color", context.GetRGBColor(containerStyle.BackgroundColor));
            }

            AddContainerElements(uiContainer, container.Items, null, context);

            AddSelectAction(uiContainer, container.SelectAction, context);

            return(uiContainer);
        }
Ejemplo n.º 4
0
        protected static HtmlTag ImageRender(AdaptiveImage image, AdaptiveRendererContext context)
        {
            var uiDiv = new DivTag()
                        .AddClass($"ac-{image.Type.Replace(".", "").ToLower()}")
                        .Style("display", "block")
                        .Style("box-sizing", "border-box");

            switch (image.Size)
            {
            case AdaptiveImageSize.Auto:
                uiDiv = uiDiv.Style("max-width", $"100%");
                break;

            case AdaptiveImageSize.Small:
                uiDiv = uiDiv.Style("max-width", $"{context.Config.ImageSizes.Small}px");
                break;

            case AdaptiveImageSize.Medium:
                uiDiv = uiDiv.Style("max-width", $"{context.Config.ImageSizes.Medium}px");
                break;

            case AdaptiveImageSize.Large:
                uiDiv = uiDiv.Style("max-width", $"{context.Config.ImageSizes.Large}px");
                break;

            case AdaptiveImageSize.Stretch:
                uiDiv = uiDiv.Style("width", $"100%");
                break;
            }

            var uiImage = new HtmlTag("img")
                          .Style("width", "100%")
                          .Attr("alt", image.AltText ?? "card image")
                          .Attr("src", image.Url.ToString());

            switch (image.Style)
            {
            case AdaptiveImageStyle.Default:
                break;

            case AdaptiveImageStyle.Person:
                uiImage = uiImage.Style("background-position", "50% 50%")
                          .Style("border-radius", "50%")
                          .Style("background-repeat", "no-repeat");
                break;
            }


            switch (image.HorizontalAlignment)
            {
            case AdaptiveHorizontalAlignment.Left:
                uiDiv = uiDiv.Style("overflow", "hidden")
                        .Style("display", "block");
                break;

            case AdaptiveHorizontalAlignment.Center:
                uiDiv = uiDiv.Style("overflow", "hidden")
                        .Style("margin-right", "auto")
                        .Style("margin-left", "auto")
                        .Style("display", "block");
                break;

            case AdaptiveHorizontalAlignment.Right:
                uiDiv = uiDiv.Style("overflow", "hidden")
                        .Style("margin-left", "auto")
                        .Style("display", "block");
                break;
            }
            uiDiv.Children.Add(uiImage);

            if (context.Config.SupportsInteractivity && image.SelectAction != null)
            {
                uiDiv.AddClass("ac-tap");
            }
            return(uiDiv);
        }
Ejemplo n.º 5
0
        protected static void AddContainerElements(HtmlTag uiContainer, IList <AdaptiveElement> elements, IList <AdaptiveAction> actions, AdaptiveRendererContext context)
        {
            if (elements != null)
            {
                foreach (var cardElement in elements)
                {
                    // each element has a row
                    var uiElement = context.Render(cardElement);
                    if (uiElement != null)
                    {
                        if (uiContainer.Children.Any())
                        {
                            AddSeparator(uiContainer, cardElement, context);
                        }

                        uiContainer.Children.Add(uiElement);
                    }
                }
            }

            if (context.Config.SupportsInteractivity && actions != null)
            {
                var uiButtonStrip = new DivTag()
                                    .AddClass("ac-actionset")
                                    .Style("display", "flex");

                // contains ShowCardAction.AdaptiveCard
                var uiShowCardStrip = new DivTag()
                                      .Style("margin-top", context.Config.Actions.ShowCard.InlineTopMargin + "px");

                if (context.Config.Actions.ActionsOrientation == ActionsOrientation.Horizontal)
                {
                    uiButtonStrip.Style("flex-direction", "row");

                    switch (context.Config.Actions.ActionAlignment)
                    {
                    case AdaptiveHorizontalAlignment.Center:
                        uiButtonStrip.Style("justify-content", "center");
                        break;

                    case AdaptiveHorizontalAlignment.Right:
                        uiButtonStrip.Style("justify-content", "flex-end");
                        break;

                    default:
                        uiButtonStrip.Style("justify-content", "flex-start");
                        break;
                    }
                }
                else
                {
                    uiButtonStrip.Style("flex-direction", "column");
                    switch (context.Config.Actions.ActionAlignment)
                    {
                    case AdaptiveHorizontalAlignment.Center:
                        uiButtonStrip.Style("align-items", "center");
                        break;

                    case AdaptiveHorizontalAlignment.Right:
                        uiButtonStrip.Style("align-items", "flex-end");
                        break;

                    case AdaptiveHorizontalAlignment.Stretch:
                        uiButtonStrip.Style("align-items", "stretch");
                        break;

                    default:
                        uiButtonStrip.Style("align-items", "flex-start");
                        break;
                    }
                }

                var maxActions = Math.Min(context.Config.Actions.MaxActions, actions.Count);
                for (var i = 0; i < maxActions; i++)
                {
                    // add actions
                    var uiAction = context.Render(actions[i]);
                    if (uiAction != null)
                    {
                        if (actions[i] is AdaptiveShowCardAction showCardAction)
                        {
                            // add button-card mapping for clients to implement showcard action
                            //var cardId = "ac-showCard" + i;
                            //uiAction.Attr("ac-cardId", cardId);

                            var cardId = uiAction.Attributes["data-ac-showCardId"];

                            var uiCard = context.Render(showCardAction.Card);
                            if (uiCard != null)
                            {
                                uiCard.Attr("id", cardId)
                                .AddClass("ac-showCard")
                                .Style("display", "none");
                                uiShowCardStrip.Children.Add(uiCard);
                            }
                        }
                        uiButtonStrip.Children.Add(uiAction);
                    }

                    // add spacer between buttons according to config
                    if (i < maxActions - 1 && context.Config.Actions.ButtonSpacing > 0)
                    {
                        var uiSpacer = new DivTag();

                        if (context.Config.Actions.ActionsOrientation == ActionsOrientation.Horizontal)
                        {
                            uiSpacer.Style("flex", "0 0 auto");
                            uiSpacer.Style("width", context.Config.Actions.ButtonSpacing + "px");
                        }
                        else
                        {
                            uiSpacer.Style("height", context.Config.Actions.ButtonSpacing + "px");
                        }
                        uiButtonStrip.Children.Add(uiSpacer);
                    }
                }

                if (uiButtonStrip.Children.Any())
                {
                    AdaptiveCardRenderer.AddSeparator(uiContainer, new AdaptiveContainer(), context);
                    uiContainer.Children.Add(uiButtonStrip);
                }

                if (uiShowCardStrip.Children.Any())
                {
                    uiContainer.Children.Add(uiShowCardStrip);
                }
            }
        }
Ejemplo n.º 6
0
        protected static HtmlTag ImageRender(AdaptiveImage image, AdaptiveRenderContext context)
        {
            var uiDiv = new DivTag()
                        .AddClass($"ac-{image.Type.Replace(".", "").ToLower()}")
                        .Style("display", "block")
                        .Style("box-sizing", "border-box");

            switch (image.Size)
            {
            case AdaptiveImageSize.Auto:
                uiDiv = uiDiv.Style("max-width", $"100%");
                break;

            case AdaptiveImageSize.Small:
                uiDiv = uiDiv.Style("max-width", $"{context.Config.ImageSizes.Small}px");
                break;

            case AdaptiveImageSize.Medium:
                uiDiv = uiDiv.Style("max-width", $"{context.Config.ImageSizes.Medium}px");
                break;

            case AdaptiveImageSize.Large:
                uiDiv = uiDiv.Style("max-width", $"{context.Config.ImageSizes.Large}px");
                break;

            case AdaptiveImageSize.Stretch:
                uiDiv = uiDiv.Style("width", $"100%");
                break;
            }

            var uiImage = new HtmlTag("img")
                          .Style("width", "100%")
                          .Attr("alt", image.AltText ?? "card image")
                          .Attr("src", context.Config.ResolveFinalAbsoluteUri(image.Url));

            switch (image.Style)
            {
            case AdaptiveImageStyle.Default:
                break;

            case AdaptiveImageStyle.Person:
                uiImage = uiImage.Style("background-position", "50% 50%")
                          .Style("border-radius", "50%")
                          .Style("background-repeat", "no-repeat");
                break;
            }

            switch (image.HorizontalAlignment)
            {
            case AdaptiveHorizontalAlignment.Left:
                uiDiv = uiDiv.Style("overflow", "hidden")
                        .Style("display", "block");
                break;

            case AdaptiveHorizontalAlignment.Center:
                uiDiv = uiDiv.Style("overflow", "hidden")
                        .Style("margin-right", "auto")
                        .Style("margin-left", "auto")
                        .Style("display", "block");
                break;

            case AdaptiveHorizontalAlignment.Right:
                uiDiv = uiDiv.Style("overflow", "hidden")
                        .Style("margin-left", "auto")
                        .Style("display", "block");
                break;
            }

            if (!string.IsNullOrEmpty(image.BackgroundColor))
            {
                uiImage.Style("background-color", context.GetRGBColor(image.BackgroundColor));
            }

            uiDiv.Children.Add(uiImage);

            AddSelectAction(uiDiv, image.SelectAction, context);
            return(uiDiv);
        }
Ejemplo n.º 7
0
        protected static void AddContainerElements(HtmlTag uiContainer, IList <AdaptiveElement> elements, IList <AdaptiveAction> actions, AdaptiveRenderContext context)
        {
            if (elements != null)
            {
                foreach (var cardElement in elements)
                {
                    // each element has a row
                    var uiElement = context.Render(cardElement);
                    if (uiElement != null)
                    {
                        if (uiContainer.Children.Any())
                        {
                            AddSeparator(uiContainer, cardElement, context);
                        }

                        uiContainer.Children.Add(uiElement);
                    }
                }
            }

            if (context.Config.SupportsInteractivity && actions != null)
            {
                var uiButtonStrip = new DivTag()
                                    .AddClass("ac-actionset")
                                    .Style("display", "flex");
                var actionsConfig = context.Config.Actions;

                // TODO: This top marging is currently being double applied, will have to investigate later
                //.Style("margin-top", $"{context.Config.GetSpacing(context.Config.Actions.Spacing)}px");

                // contains ShowCardAction.AdaptiveCard
                var showCards = new List <HtmlTag>();

                if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                {
                    uiButtonStrip.Style("flex-direction", "row");

                    switch (actionsConfig.ActionAlignment)
                    {
                    case AdaptiveHorizontalAlignment.Center:
                        uiButtonStrip.Style("justify-content", "center");
                        break;

                    case AdaptiveHorizontalAlignment.Right:
                        uiButtonStrip.Style("justify-content", "flex-end");
                        break;

                    default:
                        uiButtonStrip.Style("justify-content", "flex-start");
                        break;
                    }
                }
                else
                {
                    uiButtonStrip.Style("flex-direction", "column");
                    switch (actionsConfig.ActionAlignment)
                    {
                    case AdaptiveHorizontalAlignment.Center:
                        uiButtonStrip.Style("align-items", "center");
                        break;

                    case AdaptiveHorizontalAlignment.Right:
                        uiButtonStrip.Style("align-items", "flex-end");
                        break;

                    case AdaptiveHorizontalAlignment.Stretch:
                        uiButtonStrip.Style("align-items", "stretch");
                        break;

                    default:
                        uiButtonStrip.Style("align-items", "flex-start");
                        break;
                    }
                }

                var maxActions = Math.Min(actionsConfig.MaxActions, actions.Count);
                // See if all actions have icons, otherwise force the icon placement to the left
                var  oldConfigIconPlacement = actionsConfig.IconPlacement;
                bool allActionsHaveIcons    = true;
                for (var i = 0; i < maxActions; i++)
                {
                    if (string.IsNullOrEmpty(actions[i].IconUrl))
                    {
                        allActionsHaveIcons = false;
                        break;
                    }
                }

                if (!allActionsHaveIcons)
                {
                    actionsConfig.IconPlacement = IconPlacement.LeftOfTitle;
                }

                for (var i = 0; i < maxActions; i++)
                {
                    // add actions
                    var uiAction = context.Render(actions[i]);
                    if (uiAction != null)
                    {
                        if (actions[i] is AdaptiveShowCardAction showCardAction)
                        {
                            var cardId = uiAction.Attributes["data-ac-showCardId"];

                            var uiCard = context.Render(showCardAction.Card);
                            if (uiCard != null)
                            {
                                uiCard.Attr("id", cardId)
                                .AddClass("ac-showCard")
                                .Style("padding", "0")
                                .Style("display", "none")
                                .Style("margin-top", $"{actionsConfig.ShowCard.InlineTopMargin}px");

                                showCards.Add(uiCard);
                            }
                        }
                        uiButtonStrip.Children.Add(uiAction);
                    }

                    // add spacer between buttons according to config
                    if (i < maxActions - 1 && actionsConfig.ButtonSpacing > 0)
                    {
                        var uiSpacer = new DivTag();

                        if (actionsConfig.ActionsOrientation == ActionsOrientation.Horizontal)
                        {
                            uiSpacer.Style("flex", "0 0 auto");
                            uiSpacer.Style("width", actionsConfig.ButtonSpacing + "px");
                        }
                        else
                        {
                            uiSpacer.Style("height", actionsConfig.ButtonSpacing + "px");
                        }
                        uiButtonStrip.Children.Add(uiSpacer);
                    }
                }

                if (uiButtonStrip.Children.Any())
                {
                    AdaptiveCardRenderer.AddSeparator(uiContainer, new AdaptiveContainer(), context);
                    uiContainer.Children.Add(uiButtonStrip);
                }

                foreach (var showCard in showCards)
                {
                    uiContainer.Children.Add(showCard);
                }

                // Restore the iconPlacement for the context.
                actionsConfig.IconPlacement = oldConfigIconPlacement;
            }
        }