golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/relui/static/site.js (about)

     1  /*
     2    Copyright 2022 The Go Authors. All rights reserved.
     3    Use of this source code is governed by a BSD-style
     4    license that can be found in the LICENSE file.
     5  */
     6  
     7  (function () {
     8    /**
     9     * registerTaskListExpandListeners toggles displaying of logs on workflow
    10     * tasks.
    11     *
    12     * For each selector on the page, add a TaskList-Expanded class to this
    13     * element and its next sibling.
    14     *
    15     * @param {string} selector - css selector for target element
    16     */
    17    const registerTaskListExpandListeners = (selector) => {
    18      document.querySelectorAll(selector).forEach((element) => {
    19        element.addEventListener("click", (e) => {
    20          e.stopPropagation();
    21          element.classList.toggle("TaskList-expanded");
    22          element.nextElementSibling.classList.toggle("TaskList-expanded");
    23        });
    24      });
    25    };
    26  
    27    /**
    28     * addSliceRow creates and appends a row to a slice parameter
    29     * for filling in an element.
    30     *
    31     * @param {HTMLElement} container - the container element
    32     * @param {string} paramName - the parameter name
    33     * @param {string} element - the element tag to create
    34     * @param {string} inputType - the type attribute if element is "input"
    35     * @param {string} paramExample - an example value for the parameter
    36     */
    37    const addSliceRow = (container, paramName, element, inputType, paramExample) => {
    38      /*
    39        Create an input element, a button to remove it, group them in a "parameterRow" div:
    40  
    41        <div class="NewWorkflow-parameterRow">
    42          <input name="workflow.params.{{$p.Name}}" placeholder="{{paramExample}}" />
    43          <button title="Remove this row from the slice." onclick="/ * Remove this row. * /">-</button>
    44        </div>
    45      */
    46      const input = document.createElement(element);
    47      input.name = "workflow.params." + paramName;
    48      if (element === "input") {
    49        input.type = inputType;
    50      }
    51      input.placeholder = paramExample;
    52      const removeButton = document.createElement("button");
    53      removeButton.title = "Remove this row from the slice.";
    54      removeButton.addEventListener("click", (e) => {
    55        e.preventDefault();
    56        container.removeChild(div);
    57      });
    58      removeButton.appendChild(document.createTextNode("-"));
    59      const div = document.createElement("div");
    60      div.className = "NewWorkflow-parameterRow";
    61      div.appendChild(input);
    62      div.appendChild(removeButton);
    63  
    64      // Add the "parameterRow" div to the document.
    65      container.appendChild(div);
    66    };
    67  
    68    /** addSliceRowListener registers listeners for addSliceRow.
    69     *
    70     * @param {string} selector - elements to add click listener for addSliceRow.
    71     */
    72    const addSliceRowListener = (selector) => {
    73      document.querySelectorAll(selector).forEach((element) => {
    74        element.addEventListener("click", (e) => {
    75          e.stopPropagation();
    76          addSliceRow(
    77            element.parentElement.parentElement,
    78            element.dataset.paramname,
    79            element.dataset.element,
    80            element.dataset.inputtype,
    81            element.dataset.paramexample
    82          );
    83        });
    84      });
    85    };
    86  
    87    const registerListeners = () => {
    88      registerTaskListExpandListeners(".TaskList-expandableItem");
    89      addSliceRowListener(".NewWorkflow-addSliceRowButton");
    90    };
    91    if (document.readyState === "loading") {
    92      document.addEventListener("DOMContentLoaded", registerListeners);
    93    } else {
    94      registerListeners();
    95    }
    96  })();