public void ChoiceSet_RoundTrip()
        {
            var foo = new ChoiceSet()
            {
                new Choice()
                {
                    Value = "test1"
                },
                new Choice()
                {
                    Value = "test2"
                },
                new Choice()
                {
                    Value = "test3"
                }
            };

            var bar = JsonConvert.DeserializeObject <ChoiceSet>(JsonConvert.SerializeObject(foo));

            for (int i = 0; i < foo.Count; i++)
            {
                Assert.AreEqual(foo[i].Value, bar[i].Value);
            }
        }
Esempio n. 2
0
        private void SetChoiceSets()
        {
            List <int> choices = new List <int>();

            for (int i = 0; i < OrderedAdaptors.Count - 1; i++)
            {
                choices.Add(OrderedAdaptors[i]);

                if (OrderedAdaptors[i + 1] == OrderedAdaptors[i] + 3)
                {
                    ChoiceSet.Add(choices);
                    choices = new List <int>();
                }
            }
        }
        private static List <AdaptiveColumnSet> CreateHorizontalAdaptiveColumnSet(ChoiceSet choices)
        {
            var idx = 0;

            var adaptiveColumns = new List <AdaptiveColumnSet>();

            foreach (var choice in choices)
            {
                var columnSet = CreateAdaptiveColumnSet();
                columnSet.Columns.Add(CreateToggleInput(choice.Value, idx));
                idx++;
                adaptiveColumns.Add(columnSet);
            }

            return(adaptiveColumns);
        }
        private static List <AdaptiveColumnSet> CreateVerticalAdaptiveColumnSet(ChoiceSet choices)
        {
            var columnSet = CreateAdaptiveColumnSet();

            var idx = 0;

            foreach (var choice in choices)
            {
                columnSet.Columns.Add(CreateToggleInput(choice.Value, idx));
                idx++;
            }

            return(new List <AdaptiveColumnSet>()
            {
                columnSet
            });
        }
Esempio n. 5
0
        protected static HtmlTag ChoiceSetRender(TypedElement element, RenderContext context)
        {
            ChoiceSet choiceSet  = (ChoiceSet)element;
            var       choiceText = GetFallbackText(choiceSet);

            if (choiceText == null)
            {
                var choices = choiceSet.Choices.Select(choice => choice.Title).ToList();
                if (choiceSet.Style == ChoiceInputStyle.Compact)
                {
                    if (choiceSet.IsMultiSelect)
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " and ")}";
                    }
                    else
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " or ")}";
                    }
                }
                else // if (this.Style == ChoiceInputStyle.Expanded)
                {
                    choiceText = $"* {RendererUtilities.JoinString(choices, "\n* ", "\n* ")}";
                }
            }
            var container = new Container {
                Separation = choiceSet.Separation
            };

            container.Items.Add(new TextBlock
            {
                Text = choiceText,
                Wrap = true
            });
            container.Items.Add(new TextBlock
            {
                Text =
                    RendererUtilities.JoinString(choiceSet.Choices.Where(c => c.IsSelected).Select(c => c.Title).ToList(), ", ", " and "),
                Color = TextColor.Accent,
                Wrap  = true
            });
            return(context.Render(container));
        }
        public List <AdaptiveColumnSet> PrepareControl(Orientation orientation, ChoiceSet choices)
        {
            _orientationStyle = orientation;

            switch (orientation)
            {
            case Orientation.Horizontal:
                _adaptiveColumnSets = CreateHorizontalAdaptiveColumnSet(choices);
                break;

            case Orientation.Vertical:
                _adaptiveColumnSets = CreateVerticalAdaptiveColumnSet(choices);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null);
            }

            return(_adaptiveColumnSets);
        }
        private static void ReplyAdaptiveCard(IDialogContext context, Activity activity, Activity reply)
        {
            var card = new AdaptiveCard();

            var choices = new List <Choice>();

            choices.Add(new Choice()
            {
                Title = "Category 1",
                Value = "c1"
            });
            choices.Add(new Choice()
            {
                Title = "Category 2",
                Value = "c2"
            });
            var choiceSet = new ChoiceSet()
            {
                IsMultiSelect = false,
                Choices       = choices,
                Style         = ChoiceInputStyle.Compact,
                Id            = "Category"
            };

            card.Body.Add(choiceSet);
            //object o = "wewqeriwq[poefljk"
            card.Actions.Add(new SubmitAction()
            {
                Title = "Select Category",
                Data  = Newtonsoft.Json.Linq.JObject.FromObject(new { button = "select" })
            });

            reply.Attachments.Add(new Attachment()
            {
                Content     = card,
                ContentType = AdaptiveCard.ContentType,
                Name        = $"Card"
            });
        }
Esempio n. 8
0
        /// <summary>
        /// 1. IsMultiSelect == false && IsCompact == true => render as a drop down select element
        /// 2. IsMultiSelect == false && IsCompact == false => render as a list of radio buttons
        /// 3. IsMultiSelect == true => render as a list of toggle inputs
        /// </summary>
        protected static HtmlTag ChoiceSetRender(TypedElement element, RenderContext context)
        {
            ChoiceSet choiceSet = (ChoiceSet)element;

            if (!choiceSet.IsMultiSelect)
            {
                if (choiceSet.IsCompact)
                {
                    var uiSelectElement = new HtmlTag("select")
                                          .Attr("name", choiceSet.Id)
                                          .AddClass("ac-input")
                                          .AddClass("ac-multichoiceInput")
                                          .Style("width", "100%");

                    foreach (var choice in choiceSet.Choices)
                    {
                        var option = new HtmlTag("option")
                        {
                            Text = choice.Title
                        }
                        .Attr("value", choice.Value);

                        if (choice.Value == choiceSet.Value)
                        {
                            option.Attr("selected", string.Empty);
                        }
                        uiSelectElement.Append(option);
                    }

                    return(uiSelectElement);
                }
                else
                {
                    // render as a series of radio buttons
                    var uiElement = new HtmlTag("div")
                                    .AddClass("ac-input")
                                    .Style("width", "100%");

                    foreach (var choice in choiceSet.Choices)
                    {
                        var uiRadioInput = new HtmlTag("input")
                                           .Attr("type", "radio")
                                           .Attr("name", choiceSet.Id)
                                           .Attr("value", choice.Value)
                                           .Style("margin", "0px")
                                           .Style("display", "inline-block")
                                           .Style("vertical-align", "middle");

                        if (choice.Value == choiceSet.Value)
                        {
                            uiRadioInput.Attr("checked", string.Empty);
                        }

                        var uiLabel = context.Render(new TextBlock()
                        {
                            Text = choice.Title
                        })
                                      .Style("display", "inline-block")
                                      .Style("margin-left", "6px")
                                      .Style("vertical-align", "middle");

                        var compoundInputElement = new HtmlTag("div")
                                                   .Append(uiRadioInput)
                                                   .Append(uiLabel);

                        uiElement.Append(compoundInputElement);
                    }

                    return(uiElement);
                }
            }
            else
            {
                // the default values are specified by a comma separated string input.value
                var defaultValues = choiceSet.Value?.Split(',').Select(p => p.Trim()).ToList() ?? new List <string>();

                // render as a list of toggle inputs
                var uiElement = new HtmlTag("div")
                                .AddClass("ac-input")
                                .Attr("width", "100%");

                foreach (var choice in choiceSet.Choices)
                {
                    var uiCheckboxInput = new HtmlTag("input")
                                          .Attr("type", "checkbox")
                                          .Attr("name", choiceSet.Id)
                                          .Attr("value", choice.Value)
                                          .Style("margin", "0px")
                                          .Style("display", "inline-block")
                                          .Style("vertical-align", "middle");

                    if (defaultValues.Contains(choice.Value))
                    {
                        uiCheckboxInput.Attr("checked", string.Empty);
                    }

                    var uiLabel = context.Render(new TextBlock()
                    {
                        Text = choice.Title
                    })
                                  .Style("display", "inline-block")
                                  .Style("margin-left", "6px")
                                  .Style("vertical-align", "middle");

                    var compoundInputElement = new HtmlTag("div")
                                               .Append(uiCheckboxInput)
                                               .Append(uiLabel);

                    uiElement.Append(compoundInputElement);
                }

                return(uiElement);
            }
        }
Esempio n. 9
0
        private void LoadDialogs()
        {
            System.Diagnostics.Trace.TraceInformation("Loading resources...");

            var rootDialog = new AdaptiveDialog()
            {
                AutoEndDialog = false,
            };
            var choiceInput = new ChoiceInput()
            {
                Prompt       = new ActivityTemplate("What declarative sample do you want to run?"),
                Property     = "conversation.dialogChoice",
                AlwaysPrompt = true,
                Choices      = new ChoiceSet(new List <Choice>())
            };

            var handleChoice = new SwitchCondition()
            {
                Condition = "conversation.dialogChoice",
                Cases     = new List <Case>()
            };

            Dialog lastDialog = null;
            var    choices    = new ChoiceSet();

            foreach (var resource in this.resourceExplorer.GetResources(".dialog").Where(r => r.Id.EndsWith(".main.dialog")))
            {
                try
                {
                    var name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(resource.Id));
                    choices.Add(new Choice(name));
                    var dialog = resourceExplorer.LoadType <Dialog>(resource);
                    lastDialog = dialog;
                    handleChoice.Cases.Add(new Case($"{name}", new List <Dialog>()
                    {
                        dialog
                    }));
                }
                catch (SyntaxErrorException err)
                {
                    Trace.TraceError($"{err.Source}: Error: {err.Message}");
                }
                catch (Exception err)
                {
                    Trace.TraceError(err.Message);
                }
            }

            if (handleChoice.Cases.Count() == 1)
            {
                rootDialog.Triggers.Add(new OnBeginDialog()
                {
                    Actions = new List <Dialog>()
                    {
                        lastDialog,
                        new RepeatDialog()
                    }
                });
            }
            else
            {
                choiceInput.Choices = choices;
                choiceInput.Style   = ListStyle.Auto;
                rootDialog.Triggers.Add(new OnBeginDialog()
                {
                    Actions = new List <Dialog>()
                    {
                        choiceInput,
                        new SendActivity("# Running ${conversation.dialogChoice}.main.dialog"),
                        handleChoice,
                        new RepeatDialog()
                    }
                });
            }

            this.dialogManager = new DialogManager(rootDialog)
                                 .UseResourceExplorer(this.resourceExplorer)
                                 .UseLanguageGeneration();

            Trace.TraceInformation("Done loading resources.");
        }
Esempio n. 10
0
 public virtual void Visit(ChoiceSet choiceSet)
 {
 }
Esempio n. 11
0
        private async Task SurveyQuestionsCard(IDialogContext context)
        {
            AdaptiveCard card = new AdaptiveCard()
            {
                Body = new List <CardElement>()
                {
                    new Container()
                    {
                        Items = new List <CardElement>()
                        {
                            new ColumnSet()
                            {
                                Columns = new List <Column>()
                                {
                                    new Column()
                                    {
                                        Size  = ColumnSize.Stretch,
                                        Items = new List <CardElement>()
                                        {
                                            new TextBlock()
                                            {
                                                Text     = context.UserData.GetValue <string>(ContextConstants.Today) + ", " + context.UserData.GetValue <string>(ContextConstants.EventName),
                                                Weight   = TextWeight.Bolder,
                                                IsSubtle = true,
                                                Wrap     = true
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                Actions = new List <ActionBase>()
                {
                    new SubmitAction()
                    {
                        Title    = "Submit Survey",
                        Speak    = "<s>Submit Survey</s>",
                        DataJson = "{ \"Type\": \"SubmitSurvey\" }"
                    }
                }
            };

            //Generate cards based on the questions
            for (int i = 0; i < question.Count; i++)
            {
                card.Body.Add(new TextBlock()
                {
                    Text = "Q" + (i + 1) + ". " + question[i]
                });

                if (questionType[i] == "2") //Multiple-Choice
                {
                    var choices = new List <Choice>();

                    for (int j = 0; j < answerList[i].Count; j++)
                    {
                        choices.Add(new Choice()
                        {
                            Title = answerList[i][j],
                            Value = answerList[i][j]
                        });
                    }

                    var choiceSet = new ChoiceSet()
                    {
                        IsMultiSelect = false,
                        Choices       = choices,
                        Style         = ChoiceInputStyle.Compact,
                        Id            = "ans" + i
                    };
                    card.Body.Add(choiceSet);
                }
                else    //OpenEnded Answer (1)
                {
                    card.Body.Add(new TextInput()
                    {
                        Id          = "ans" + i,
                        Placeholder = "Please enter a comment",
                        Style       = TextInputStyle.Text,
                        IsMultiline = true
                    });
                }
            }

            Attachment attachment = new Attachment()
            {
                ContentType = AdaptiveCard.ContentType,
                Content     = card
            };

            var reply = context.MakeMessage();

            reply.Attachments.Add(attachment);
            await context.PostAsync(reply, CancellationToken.None);

            context.Wait(MessageReceivedAsync);
        }
Esempio n. 12
0
        public Attachment ToCard(Session session)
        {
            var card = new AdaptiveCard();

            card.Body.Add(new TextBlock()
            {
                Text = session.Title, Weight = TextWeight.Bolder, Size = TextSize.Medium, Wrap = true
            });

            card.Body.Add(new TextBlock()
            {
                Text = $"{session.Speaker} - {session.Day} at {session.Time}", Size = TextSize.Medium
            });
            //card.Body.Add(new TextBlock() { Text = session.Description, Wrap = true });


            ShowCardAction showCardAction = new ShowCardAction();

            showCardAction.Title = "Rate this session";
            card.Actions.Add(showCardAction);

            var reviewCard = new AdaptiveCard();
            var ratings    = new ChoiceSet()
            {
                Style         = ChoiceInputStyle.Compact,
                IsMultiSelect = false,

                Choices = new List <Choice>()
                {
                    new Choice()
                    {
                        Title = "1", Value = "1"
                    },
                    new Choice()
                    {
                        Title = "2", Value = "2"
                    },
                    new Choice()
                    {
                        Title = "3", Value = "3"
                    },
                    new Choice()
                    {
                        Title = "4", Value = "4"
                    },
                    new Choice()
                    {
                        Title = "5", Value = "5"
                    },
                }
            };

            reviewCard.Body.Add(new TextBlock()
            {
                Text = "Let us know what you thought of this session"
            });
            reviewCard.Body.Add(ratings);
            var data = new { session.Title, Action = "ReviewSession" };

            reviewCard.Actions.Add(new SubmitAction()
            {
                Title = "Submit feedback", Data = data, DataJson = JsonConvert.SerializeObject(data)
            });

            showCardAction.Card = reviewCard;

            Attachment attachment = new Attachment()

            {
                ContentType = AdaptiveCard.ContentType,

                Content = card
            };

            return(attachment);
        }
        private void LoadDialogs()
        {
            System.Diagnostics.Trace.TraceInformation("Loading resources...");

            // Create a non-used dialog just to make sure the target assembly is referred so that
            // the target assembly's component registration can be used to deserialize declarative components
            var qnaDialog = new QnAMakerDialog();

            System.Diagnostics.Trace.TraceInformation($"Touch ${qnaDialog.GetType().ToString()} to make sure assembly is referred.");

            var rootDialog = new AdaptiveDialog()
            {
                AutoEndDialog = false,
            };
            var choiceInput = new ChoiceInput()
            {
                Prompt       = new ActivityTemplate("What declarative sample do you want to run?"),
                Property     = "conversation.dialogChoice",
                AlwaysPrompt = true,
                Choices      = new ChoiceSet(new List <Choice>())
            };

            var handleChoice = new SwitchCondition()
            {
                Condition = "conversation.dialogChoice",
                Cases     = new List <Case>()
            };

            Dialog lastDialog = null;
            var    choices    = new ChoiceSet();
            var    dialogName = configuration["dialog"];

            foreach (var resource in this.resourceExplorer.GetResources(".dialog").Where(r => dialogName != null ? r.Id == dialogName : r.Id.EndsWith(".main.dialog")))
            {
                try
                {
                    var name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(resource.Id));
                    choices.Add(new Choice(name));
                    var dialog = resourceExplorer.LoadType <Dialog>(resource);
                    lastDialog = dialog;
                    handleChoice.Cases.Add(new Case($"{name}", new List <Dialog>()
                    {
                        dialog
                    }));
                }
                catch (SyntaxErrorException err)
                {
                    Trace.TraceError($"{err.Source}: Error: {err.Message}");
                }
                catch (Exception err)
                {
                    Trace.TraceError(err.Message);
                }
            }

            if (handleChoice.Cases.Count() == 1)
            {
                rootDialog.Triggers.Add(new OnBeginDialog
                {
                    Actions = new List <Dialog>
                    {
                        lastDialog,
                        new RepeatDialog()
                    }
                });
            }
            else
            {
                choiceInput.Choices = choices;
                choiceInput.Style   = ListStyle.Auto;
                rootDialog.Triggers.Add(new OnBeginDialog()
                {
                    Actions = new List <Dialog>()
                    {
                        choiceInput,
                        new SendActivity("# Running ${conversation.dialogChoice}.main.dialog"),
                        handleChoice,
                        new RepeatDialog()
                    }
                });
            }

            this.dialogManager = new DialogManager(rootDialog)
                                 .UseResourceExplorer(this.resourceExplorer)
                                 .UseLanguageGeneration();

            Trace.TraceInformation("Done loading resources.");
        }
Esempio n. 14
0
        public static FrameworkElement Render(ChoiceSet choiceSet, RenderContext context)
        {
            var chosen = choiceSet.Value?.Split(',').Select(p => p.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList() ?? new List <string>();

#if WPF
            if (context.Config.SupportsInteractivity)
            {
                var uiGrid = new Grid();
                uiGrid.RowDefinitions.Add(new RowDefinition()
                {
                    Height = GridLength.Auto
                });
                uiGrid.RowDefinitions.Add(new RowDefinition()
                {
                    Height = new GridLength(1, GridUnitType.Star)
                });

                var uiComboBox = new ComboBox();
                uiComboBox.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.ComboBox");
                uiComboBox.DataContext = choiceSet;

                var uiChoices = new ListBox();
                ScrollViewer.SetHorizontalScrollBarVisibility(uiChoices, ScrollBarVisibility.Disabled);
                var itemsPanelTemplate = new ItemsPanelTemplate();
                var factory            = new FrameworkElementFactory(typeof(WrapPanel));
                itemsPanelTemplate.VisualTree = factory;
                uiChoices.ItemsPanel          = itemsPanelTemplate;
                uiChoices.DataContext         = choiceSet;
                uiChoices.Style = context.GetStyle("Adaptive.Input.ChoiceSet");

                foreach (var choice in choiceSet.Choices)
                {
                    if (choiceSet.IsMultiSelect == true)
                    {
                        var uiCheckbox = new CheckBox();
                        uiCheckbox.Content     = choice.Title;
                        uiCheckbox.IsChecked   = chosen.Contains(choice.Value);
                        uiCheckbox.DataContext = choice;
                        uiCheckbox.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.CheckBox");
                        uiChoices.Items.Add(uiCheckbox);
                    }
                    else
                    {
                        if (choiceSet.Style == ChoiceInputStyle.Compact)
                        {
                            var uiComboItem = new ComboBoxItem();
                            uiComboItem.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.ComboBoxItem");
                            uiComboItem.Content     = choice.Title;
                            uiComboItem.DataContext = choice;
                            uiComboBox.Items.Add(uiComboItem);
                            if (chosen.Contains(choice.Value))
                            {
                                uiComboBox.SelectedItem = uiComboItem;
                            }
                        }
                        else
                        {
                            var uiRadio = new RadioButton();
                            uiRadio.Content     = choice.Title;
                            uiRadio.IsChecked   = chosen.Contains(choice.Value);
                            uiRadio.GroupName   = choiceSet.Id;
                            uiRadio.DataContext = choice;
                            uiRadio.Style       = context.GetStyle("Adaptive.Input.ChoiceSet.Radio");
                            uiChoices.Items.Add(uiRadio);
                        }
                    }
                }
                context.InputBindings.Add(choiceSet.Id, () =>
                {
                    if (choiceSet.IsMultiSelect == true)
                    {
                        string values = string.Empty;
                        foreach (var item in uiChoices.Items)
                        {
                            CheckBox checkBox = (CheckBox)item;
                            Choice choice     = checkBox.DataContext as Choice;
                            if (checkBox.IsChecked == true)
                            {
                                values += (values == string.Empty ? "" : ",") + choice.Value;
                            }
                        }
                        return(values);
                    }
                    else
                    {
                        if (choiceSet.Style == ChoiceInputStyle.Compact)
                        {
                            ComboBoxItem item = uiComboBox.SelectedItem as ComboBoxItem;
                            if (item != null)
                            {
                                Choice choice = item.DataContext as Choice;
                                return(choice.Value);
                            }
                            return(null);
                        }
                        else
                        {
                            foreach (var item in uiChoices.Items)
                            {
                                RadioButton radioBox = (RadioButton)item;
                                Choice choice        = radioBox.DataContext as Choice;
                                if (radioBox.IsChecked == true)
                                {
                                    return(choice.Value);
                                }
                            }
                            return(null);
                        }
                    }
                });
                if (choiceSet.Style == ChoiceInputStyle.Compact)
                {
                    Grid.SetRow(uiComboBox, 1);
                    uiGrid.Children.Add(uiComboBox);
                    return(uiGrid);
                }
                else
                {
                    Grid.SetRow(uiChoices, 1);
                    uiGrid.Children.Add(uiChoices);
                    return(uiGrid);
                }
            }
#endif

            string choiceText = XamlUtilities.GetFallbackText(choiceSet);
            if (choiceText == null)
            {
                List <string> choices = choiceSet.Choices.Select(choice => choice.Title).ToList();
                if (choiceSet.Style == ChoiceInputStyle.Compact)
                {
                    if (choiceSet.IsMultiSelect)
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " and ")}";
                    }
                    else
                    {
                        choiceText = $"Choices: {RendererUtilities.JoinString(choices, ", ", " or ")}";
                    }
                }
                else // if (choiceSet.Style == ChoiceInputStyle.Expanded)
                {
                    choiceText = $"* {RendererUtilities.JoinString(choices, "\n* ", "\n* ")}";
                }
            }
            Container container = TypedElementConverter.CreateElement <Container>();
            container.Separation = choiceSet.Separation;
            TextBlock textBlock = TypedElementConverter.CreateElement <TextBlock>();
            textBlock.Text = choiceText;
            textBlock.Wrap = true;
            container.Items.Add(textBlock);

            textBlock       = TypedElementConverter.CreateElement <TextBlock>();
            textBlock.Text  = RendererUtilities.JoinString(choiceSet.Choices.Where(c => chosen.Contains(c.Value)).Select(c => c.Title).ToList(), ", ", " and ");
            textBlock.Color = TextColor.Accent;
            textBlock.Wrap  = true;
            container.Items.Add(textBlock);
            return(context.Render(container));
        }