Skip to content
Snippets Groups Projects
main.js 9.43 KiB
Newer Older
// This script will be run within the webview itself
// It cannot access the main VS Code APIs directly.
(function () {
    const vscode = acquireVsCodeApi();


    console.log("Started");
    /**
     * @type {HTMLSelectElement}
     */
    const toolDropDown = document.getElementById("tools");
    toolDropDown.addEventListener('change', event => {
s1995588's avatar
s1995588 committed
        var oldState = vscode.getState();
        var currentTool = toolDropDown.options[toolDropDown.selectedIndex].value;
        vscode.setState({ tools: oldState.tools, constants: oldState.constants, parameters: oldState.parameters, currentUri: oldState.currentUri, currentTool: currentTool });
        vscode.postMessage({ type: "toolSelected", toolName: currentTool });

    // Handle messages sent from the extension to the webview
    window.addEventListener('message', event => {
        const message = event.data; // The json data that the extension sent
        console.log("Received message");
        switch (message.type) {
            case 'updateConstants':
                    updateConstants(message.constants, message.uri);
s1995588's avatar
s1995588 committed
            case 'updateParameters':
s1995588's avatar
s1995588 committed
                    updateParameters(message.parameters, message.toolName);
            case 'fillTools':
                {
                    fillTools(message.tools);
                    break;
                }
    const runButton = document.getElementById("run-button");
    runButton.addEventListener("click", event => {
        const state = vscode.getState();
        for (const file of state.constants) {
            if (file.uri === state.currentUri) {
                for (const tool of state.parameters) {
                    if (tool.toolName === state.currentTool) {
                        vscode.postMessage({ type: "runTool", uri: state.currentUri, toolName: toolDropDown.options[toolDropDown.selectedIndex].value, constants: file.constants, parameters: tool.parameters });
                        return;
                    }
                }

    function fillTools(tools) {
        const select = document.querySelector("#tools");
        select.innerHTML = "";

        for (const tool of tools) {
            const option = document.createElement("option");
            option.value = tool;
            option.text = tool;
            select.appendChild(option);
        }
s1995588's avatar
s1995588 committed
        const oldState = vscode.getState();
        select.selectedIndex = tools.findIndex(x => x === oldState.currentTool);

s1995588's avatar
s1995588 committed
        vscode.setState({ tools: tools, constants: oldState.constants, parameters: oldState.parameters, currentUri: oldState.currentUri, currentTool: oldState.currentTool });
    function addConstantItem(ul, name, value) {
        const li = document.createElement("li");
        const nameBox = document.createElement("label");
        nameBox.id = "name-" + name;
        nameBox.appendChild(document.createTextNode(name));
        nameBox.classList.add("name");
        li.appendChild(nameBox);
        const valueBox = document.createElement("input");
        valueBox.id = "value-" + name;
        valueBox.type = "text";
        valueBox.value = value;
        valueBox.classList.add("value");
        valueBox.addEventListener("input", event => {
            let oldState = vscode.getState();
            for (const file of oldState.constants) {
                if (file.uri === oldState.currentUri) {
                    for (const constant of file.constants) {
                        if (constant.name === name) {
                            constant.value = valueBox.value;
                            break;
                        }
                    }
                    break;
                }
            }
s1995588's avatar
s1995588 committed
            vscode.setState({ tools: oldState.tools, constants: oldState.constants, parameters: oldState.parameters, currentUri: oldState.currentUri, currentTool: oldState.currentTool });
        li.appendChild(valueBox);
s1995588's avatar
s1995588 committed
    function addParameterItem(ul, name, value) {
        const li = document.createElement("li");
s1995588's avatar
s1995588 committed
        const nameBox = document.createElement("label");
        nameBox.id = "name-" + name;
s1995588's avatar
s1995588 committed
        nameBox.appendChild(document.createTextNode(name));
        nameBox.classList.add("name");

        li.appendChild(nameBox);

        const valueBox = document.createElement("input");
        valueBox.id = "value-" + name;
        valueBox.type = "text";
        valueBox.value = value;
        valueBox.classList.add("value");
s1995588's avatar
s1995588 committed
        valueBox.addEventListener("input", event => {
            let oldState = vscode.getState();
            for (const tool of oldState.parameters) {
                if (tool.toolName === oldState.currentTool) {
                    for (const parameter of tool.parameters) {
                        if (parameter.id === name) {
s1995588's avatar
s1995588 committed
                            parameter.value = valueBox.value;
                            break;
                        }
                    }
                    break;
                }
            }
            vscode.setState({ tools: oldState.tools, constants: oldState.constants, parameters: oldState.parameters, currentUri: oldState.currentUri, currentTool: oldState.currentTool });
        });

        li.appendChild(valueBox);
     *
     * @param {Array<{ uri: string, constants: Array<{ name: string, value: string }> }>} constants
     * @param {string} uri
    function updateConstants(constants, uri) {
        const constantsUl = document.querySelector("#constants");
        const oldState = vscode.getState();

s1995588's avatar
s1995588 committed
        // combines the oldstate constants with the "new" constants.
        for (const file of constants) {
            const index = oldState.constants.findIndex(x => x.uri === file.uri);
            if (index !== -1) {
                for (const constant of file.constants) {
                    const oldConstantIndex = oldState.constants[index].constants.findIndex(x => x.name === constant.name);
                    if (oldConstantIndex !== -1) {
                        constant.value = oldState.constants[index].constants[oldConstantIndex].value;
                    }
                }
            }
        }
        for (const file of oldState.constants) {
            const index = constants.findIndex(x => x.uri === file.uri);
            if (index === -1) {
                constants.push(file);
            }
        }

        constantsUl.innerHTML = "";
s1995588's avatar
s1995588 committed
        // adds the constants to the sidebar.
        for (const file of constants) {
            if (file.uri === uri) {
                for (const constant of file.constants) {
                    addConstantItem(constantsUl, constant.name, constant.value);
                }
                break;
            }
        if (constantsUl.innerHTML === "") {
            constantsUl.innerHTML = "There are no undefined constants.";
        }
s1995588's avatar
s1995588 committed
        vscode.setState({ tools: oldState.tools, constants: constants, parameters: oldState.parameters, currentUri: uri, currentTool: oldState.currentTool });
     * @param {Array<{ toolName: string, parameters: Array<{ id: string, value: string }> }>} parameters
s1995588's avatar
s1995588 committed
     * @param {string} tool
s1995588's avatar
s1995588 committed
    function updateParameters(parameters, toolName) {
        const parametersUl = document.querySelector("#parameters");
        const oldState = vscode.getState();

        // combines the oldstate parameters with the "new" parameters.
        for (const tool of parameters) {
            const index = oldState.parameters.findIndex(x => x.toolName === tool.toolName);
            if (index !== -1) {
                for (const parameter of tool.parameters) {
                    const oldParameterIndex = oldState.parameters[index].parameters.findIndex(x => x.id === parameter.id);
s1995588's avatar
s1995588 committed
                    if (oldParameterIndex !== -1) {
                        parameter.value = oldState.parameters[index].parameters[oldParameterIndex].value;
                    }
                }
            }
        for (const tool of oldState.parameters) {
            const index = parameters.findIndex(x => x.toolName === tool.toolName);
            if (index === -1) {
                parameters.push(tool);
            }
        }

s1995588's avatar
s1995588 committed
        parametersUl.innerHTML = parameters.length === 0 ? "There are no parameters." : "";
        // adds the parameters to the sidebar.
        for (const tool of parameters) {
            if (tool.toolName === toolName) {
                for (const parameter of tool.parameters) {
                    addParameterItem(parametersUl, parameter.id, parameter.value);
s1995588's avatar
s1995588 committed
        vscode.setState({ tools: oldState.tools, constants: oldState.constants, parameters: parameters, currentUri: oldState.currentUri, currentTool: oldState.currentTool });
    }
s1995588's avatar
s1995588 committed
    const oldState = vscode.getState() || { tools: [], constants: [], parameters: [], currentUri: "", currentTool: "" };
    const tools = oldState.tools;
    const constants = oldState.constants;
s1995588's avatar
s1995588 committed
    const parameters = oldState.parameters;
    const currentUri = oldState.currentUri;
s1995588's avatar
s1995588 committed
    const currentTool = oldState.currentTool;

    if (oldState.tools.length === 0) {
        vscode.postMessage({ type: "init" });
    } else {
        fillTools(tools);
s1995588's avatar
s1995588 committed
        updateParameters(parameters, currentTool);
        updateConstants(constants, currentUri);
    }