Ondrej-Douda commented on code in PR #6079: URL: https://github.com/apache/netbeans/pull/6079#discussion_r1234268867
########## java/java.lsp.server/vscode/src/propertiesView/script.ts: ########## @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. + */ +import { provideVSCodeDesignSystem, vsCodeButton, vsCodeTextField, vsCodeDivider, vsCodeCheckbox, Button, TextField, Checkbox } from "@vscode/webview-ui-toolkit"; +import { isError, asClass, isClass } from "../typesUtil"; +import { CommandKey, Message, MessageProp } from "./controlTypes"; + +provideVSCodeDesignSystem().register(vsCodeButton(), vsCodeTextField(), vsCodeDivider(), vsCodeCheckbox()); + +const vscode = acquireVsCodeApi(); +document.addEventListener("DOMContentLoaded", () => { + try { + asClass(Button, document.getElementById('save'), "Not save.") + .addEventListener('click', () => { + try { + if (validate()) + sendMessage({ _type: CommandKey.Save, properties: getProperties() }); + } catch (e: unknown) { + handleError(e); + } + }); + asClass(Button, document.getElementById('cancel'), "Not cancel.") + .addEventListener('click', () => { + sendMessage({ _type: CommandKey.Cancel }); + }); + } catch (e: unknown) { + handleError(e); + } +}); + +function handleError(error: unknown) { + if (isError(error)) + sendMessage({ _type: CommandKey.Error, error: error.message, stack: error.stack }); + else + sendMessage({ _type: CommandKey.Error, error: JSON.stringify(error) }); +} + +function sendMessage(message: Message) { + vscode.postMessage(message); +} + +function getProperties(): MessageProp[] { + const out: MessageProp[] = []; + const elements = document.getElementsByName("input"); + for (let i = 0; i < elements.length; ++i) { + const element = elements.item(i); + if (element) + out.push(getProperty(element)); + } + return out; +} + +function getProperty(element: HTMLElement): MessageProp { + if (isClass(TextField, element)) { + return makeProperty(element.value, element?.id); + } else if (isClass(Checkbox, element)) { + return makeProperty(element.checked, element?.id); + } else if (isClass(HTMLTableElement, element)) { + return makeProperty(parseProperties(element), element?.id); + } + return { name: element?.id || "", value: element?.nodeValue || "" }; +} + +function makeProperty(value: string | boolean | Record<string, string>, name?: string): MessageProp { + return { name: name || "NoID", value: value }; +} + +function parseProperties(table: HTMLTableElement): Record<string, string> { + const out: Record<string, string> = {}; + for (let i = 0; i < table.rows.length; ++i) { + readProperty(out, table.rows.item(i)?.cells); + } + return out; +} + +function readProperty(out: Record<string, string>, cells?: HTMLCollectionOf<HTMLTableCellElement> | null) { + out[asClass(TextField, cells?.item(0)?.getElementsByClassName("name").item(0)).value] Review Comment: It will throw an exception... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
