﻿# Script use cases

> [HTML Version](widget-scripts.html)

Widgets are a convenient tool for setting up [dynamic forms](360029574371.md), for instance, if some information needs to be displayed when certain conditions are met.

Let’s look at how scripts can be used in widgets by creating a page with a multiple-question test.

**(widget-scripts-1.png)**

We will configure:

- Expandable panels with questions.

- A pop-up warning that only one answer can be selected.

- A modal window showing whether the test was passed.

## Step 1. Create questions

First, create multiple choice questions for the test:

1. Open the page and go to the [interface designer](360027211631.md).

2. On the **Context** tab, create a [Category](360009707032.md#category) type variable and set the following parameters:

- **Display Name\***. Enter a question to be displayed on the page.

- **Property Name\***. Change the automatically generated property name to \[OBJECT\].

- **Type\***. Select **Category** and choose the **Multiple** option.

- **Values**. Enter possible answers for the question. Change their codes to \[OBJECT\], \[OBJECT\], and \[OBJECT\] and click **Create**.

**(script-use-cases-2.png)**

3. Create a few more questions in the same way.

## Step 2. Configure question display

Now that you’ve created the test questions, determine how they are going to be shown on the page. Let’s make it so that the questions are collapsed by default, but as soon as the user clicks **Start Test**,** **the first question panel expands. To do that:

1. In the [interface designer](360027211631.md), on the **Context** tab, create a variable of the [Yes/No switch](360009707032.md#yes-no-switch) type called **Expand question 1** \[OBJECT\].

2. Add the [Panel with header](panel_with_heading.md) widget to the modeling canvas.

3. In the window that opens, set the following parameters:

	- **Title\***. Enter the panel’s name, **Question 1**.

	- **Collapsible**. Select **Yes**.

	- **Expanded**. Specify the **Expand question 1** variable that you created in step 1. With this variable in the condition, the panel will expand when the user clicks the button that is going to be added to the page.

**(widget-scripts-2.png)**

4. Click **Save**.

5. Add the [Button](button_widget.md) widget to the modeling canvas.

6. In the settings window that opens, set the following parameters:

	- **Title/Tooltip\***. Enter the button’s name, **Start Test**.

	- **Executed script**. Add a script that will change the value of the **Expand question 1** variable \[OBJECT\] to \[OBJECT\]. To do that, click **Create**, enter the function name:** **`startTest`, click** Save**, and then select** Open**.

**(widget-scripts-04.PNG)**

On the **Scripts** tab that opens, write the following script in the created function: 

````
async function startTest(): Promise<void>\{  
    Context.data.showQuestion1 = true   
\}

7. ````
Save the settings.

8. Inside the **Panel with header** widget, place the **Category** type variable with the first question that you created earlier. To do that, on the right-side panel of the interface designer, switch to the **Properties** tab and drag the field to the widget. On the page, the property will be displayed as the [Field](form_line_widget.md) widget.

**(script-use-cases-3.png)**

9. Add as many panels to the modeling canvas as you have questions. Set names for each panel. In the **Collapsible** field, select **Yes**, in the **Expanded** field, **No**. Place the properties with the questions into the panels.

10. Save and publish the changes.

Now all the questions are collapsed when the page opens. When the user clicks **Start Test**, the first question automatically expands.

## Step 3. Configure warning display

In the test, there is only one possible answer for each question. If the user selects several answers, an [Information box](information_block.md) with a warning should be displayed. The box disappears when the user hovers the mouse over it.

To set up the information box, do the following:

1. In the [interface designer](360027211631.md), on the **Context** tab, create a variable of the [Yes/No switch](360009707032.md#yes-no-switch) type called **One answer** \[OBJECT\].

2. Add the **Information box** widget to the modeling canvas. In the settings window that opens, in the **Hide** field, select **Show on condition**. Then specify the **One answer** variable. Whether the information box is shown will depend on the value of this variable.

**(widget-scripts-4.png)**

3. The number of answers selected by the user needs to be checked. To set up the checking, do the following:

	- Select the **Field** widget containing the first question and open its settings by clicking the gear icon:

**(widget-scripts-5.png)**

	- Go to the **Events** tab and add a script that will check the number of selected answers and change the value of the **One answer** variable. To do that, in the **On value change** field, click **Create**, enter the `checkQuestion1` function name, then click **Save** and **Open**.

	- The **Scripts** tab in the interface designer will open. Add a script that will define the value of the **One answer** \[OBJECT\] variable, for example:

````
async function checkQuestion1(): Promise<void> \{  
    if (Context.data. question1 \&\& Context.data. question1.length > 1) \{  
        Context.data.oneAnswer = true  
    \} else \{  
        Context.data.oneAnswer = false;  
    \}  
\}

4. ````
In the settings of the **Information box** widget, go to the **Events** tab and add a script to the **On mouse enter handler**. This script should change the value of the **One answer** \[OBJECT\] variable to \[OBJECT\]:

````
async function closeBlock(): Promise<void> \{  
    Context.data.oneAnswer = false    
\}

5. ````
In the **Question 1** panel, add a **Button** widget, name it **Next Question**, and link a script that checks the answer to the first question. When the button is clicked:

- If more than one answer is selected, the information box appears with its configured text, for example, **Select only one option**.

- If one answer is selected, the first question panel closes and the user moves to the second question.

Script example

````
async function nextQuestion(): Promise<void> \{  
// Checking number of responses to the first question  
    checkQuestion1();  
    if (\!Context.data.oneAnswer) \{  
        // Go to next question  
        Context.data.showQuestion1 = false;  
        Context.data.showQuestion2 = true;  
    \}  
\}    
````

6. ````
Save and publish the changes.

## Step 4. Configure test results display

When the user finishes the test, they click Check Results. A pop-up window displays the test result and includes a button for closing the window.

To configure it:

1. On the **Context** tab of the [interface designer](360027211631.md), create two [Yes/No switch](360009707032.md#yes-no-switch) variables:

- **Correct **`(correct)`stores the test result.

- **Results **`(showResult)` controls whether the modal window with the test result is visible.

2. Add a [Modal window](modal_window.md) widget to the modeling canvas.

3. In **Show window**, select the Results `(showResult)` variable. The value of this variable controls whether the modal window is displayed. 

**(widget-scripts-6.png)**

4. Add two Information box widgets to the modal window. One is shown when the test is passed and the other when it is not. Configure them as follows:

- For a successful result, in the settings of one widget:

	-  On the **Main** tab set **Information type **to **Positive result** and **Title** to **Test passed**.

	- On the **System** tab, set **Visibility** to **Show on condition** and bind the **Correct** `(correct)` variable.

- For an unsuccessful result,  in the settings of another widget:

	- On the **Main** tab, set **Information type** to **Error** and **Title** to **Test not passed**.

	- On the **System** tab, set **Visibility** to **Hide on condition** and bind the same **Correct** `(correct)` variable.

5. To let users hide the results after viewing them, add a [Button](button_widget.md) widget inside the modal window. Enter the button name, set **Action type** to **Script**, and add the following code:

````
async function closeResult(): Promise<void> \{  
    Context.data.showResult = false;  
\}

6. ````
To display the configured modal window with the test results, add another **Button** widget to the modeling canvas. In the button settings, in the **Executed script** field, add code that checks the selected answers and displays one of the information boxes:

- **Test passed** if the correct answers were selected.

- **Test not passed** if an incorrect answer was selected.

Sample script for checking questions and displaying results

````
async function resultTest(): Promise<void> \{  
    if (\!Context.data.question1 || Context.data.question1.length > 1) \{  
        // Question 1 must have one answer  
        Context.data.oneAnswer = true;  
        Context.data.correct = false;  
        return;  
    \}  
  
    // Checking question 1  
    for (let i of Context.data.question1) \{  
        if (i.code == "n1") \{  
            Context.data.correct = true;  
        \}  
        else \{  
            Context.data.correct = false;  
        \}  
    \}  
  
    // Show results  
    showResult();  
\}  
async function showResult(): Promise<void> \{  
    Context.data.showResult = true;  
\}

````


7. Save and publish the changes.