|
| 1 | +import { inject, injectable } from "inversify"; |
| 2 | +import "./constraintMenu.css"; |
| 3 | +import { AbstractUIExtension } from "sprotty"; |
| 4 | +import { calculateTextSize, generateRandomSprottyId } from "../../utils"; |
| 5 | +import { Constraint, ConstraintRegistry } from "./constraintRegistry"; |
| 6 | +import { executeAction } from "../.."; |
| 7 | +import { AnalyzeDiagramAction } from "../serialize/analyze"; |
| 8 | + |
| 9 | +@injectable() |
| 10 | +export class ConstraintMenu extends AbstractUIExtension { |
| 11 | + static readonly ID = "constraint-menu"; |
| 12 | + private selectedConstraint: Constraint | undefined; |
| 13 | + |
| 14 | + constructor(@inject(ConstraintRegistry) private readonly constraintRegistry: ConstraintRegistry) { |
| 15 | + super(); |
| 16 | + this.constraintRegistry = constraintRegistry; |
| 17 | + } |
| 18 | + |
| 19 | + id(): string { |
| 20 | + return ConstraintMenu.ID; |
| 21 | + } |
| 22 | + containerClass(): string { |
| 23 | + return ConstraintMenu.ID; |
| 24 | + } |
| 25 | + protected initializeContents(containerElement: HTMLElement): void { |
| 26 | + containerElement.classList.add("ui-float"); |
| 27 | + containerElement.innerHTML = ` |
| 28 | + <input type="checkbox" id="expand-state-constraint" hidden> |
| 29 | + <label id="constraint-menu-expand-label" for="expand-state-constraint"> |
| 30 | + <div class="expand-button"> |
| 31 | + Constraints |
| 32 | + </div> |
| 33 | + </label> |
| 34 | + `; |
| 35 | + containerElement.appendChild(this.buildConstraintInputWrapper()); |
| 36 | + containerElement.appendChild(this.buildConstraintListWrapper()); |
| 37 | + containerElement.appendChild(this.buildRunButton()); |
| 38 | + } |
| 39 | + |
| 40 | + private buildConstraintInputWrapper(): HTMLElement { |
| 41 | + const wrapper = document.createElement("div"); |
| 42 | + wrapper.id = "constraint-menu-input"; |
| 43 | + wrapper.innerHTML = ` |
| 44 | + <input type="text" id="constraint-input" placeholder="Enter constraint here"> |
| 45 | + `; |
| 46 | + return wrapper; |
| 47 | + } |
| 48 | + |
| 49 | + private buildConstraintListWrapper(): HTMLElement { |
| 50 | + const wrapper = document.createElement("div"); |
| 51 | + wrapper.id = "constraint-menu-list"; |
| 52 | + |
| 53 | + this.rerenderConstraintList(wrapper); |
| 54 | + |
| 55 | + return wrapper; |
| 56 | + } |
| 57 | + |
| 58 | + private buildConstraintListItem(constraint: Constraint): HTMLElement { |
| 59 | + const valueElement = document.createElement("div"); |
| 60 | + valueElement.classList.add("constrain-label"); |
| 61 | + |
| 62 | + valueElement.onclick = () => { |
| 63 | + const elements = document.getElementsByClassName("constraint-label"); |
| 64 | + for (let i = 0; i < elements.length; i++) { |
| 65 | + elements[i].classList.remove("selected"); |
| 66 | + } |
| 67 | + valueElement.classList.add("selected"); |
| 68 | + this.selectConstraintListItem(constraint); |
| 69 | + }; |
| 70 | + |
| 71 | + const valueInput = document.createElement("input"); |
| 72 | + valueInput.value = constraint.name; |
| 73 | + valueInput.placeholder = "Name"; |
| 74 | + this.dynamicallySetInputSize(valueInput); |
| 75 | + valueInput.onchange = () => { |
| 76 | + constraint.name = valueInput.value; |
| 77 | + this.constraintRegistry.constraintChanged(); |
| 78 | + }; |
| 79 | + |
| 80 | + valueElement.appendChild(valueInput); |
| 81 | + |
| 82 | + const deleteButton = document.createElement("button"); |
| 83 | + deleteButton.innerHTML = '<span class="codicon codicon-trash"></span>'; |
| 84 | + deleteButton.onclick = () => { |
| 85 | + this.constraintRegistry.unregisterConstraint(constraint); |
| 86 | + this.rerenderConstraintList(); |
| 87 | + if (this.selectedConstraint === constraint) { |
| 88 | + this.selectConstraintListItem(undefined); |
| 89 | + } |
| 90 | + }; |
| 91 | + valueElement.appendChild(deleteButton); |
| 92 | + return valueElement; |
| 93 | + } |
| 94 | + |
| 95 | + private selectConstraintListItem(constraint?: Constraint): void { |
| 96 | + this.selectedConstraint = constraint; |
| 97 | + const input = document.getElementById("constraint-input") as HTMLInputElement; |
| 98 | + input.value = constraint?.constraint ?? ""; |
| 99 | + } |
| 100 | + |
| 101 | + private rerenderConstraintList(list?: HTMLElement): void { |
| 102 | + if (!list) { |
| 103 | + list = document.getElementById("constraint-menu-list") ?? undefined; |
| 104 | + } |
| 105 | + console.info(list); |
| 106 | + if (!list) return; |
| 107 | + list.innerHTML = ""; |
| 108 | + this.constraintRegistry.getConstraints().forEach((constraint) => { |
| 109 | + list.appendChild(this.buildConstraintListItem(constraint)); |
| 110 | + }); |
| 111 | + |
| 112 | + const addButton = document.createElement("button"); |
| 113 | + addButton.classList.add("constraint-add"); |
| 114 | + addButton.innerHTML = '<span class="codicon codicon-add"></span> Constraint'; |
| 115 | + addButton.onclick = () => { |
| 116 | + /*if (this.editorModeController?.isReadOnly()) { |
| 117 | + return; |
| 118 | + }*/ |
| 119 | + var inputValue = "Test"; |
| 120 | + const inputField = document.getElementById("constraint-input") as HTMLInputElement; |
| 121 | + if (inputField) { |
| 122 | + // Access the value property |
| 123 | + inputValue = inputField.value; |
| 124 | + } |
| 125 | + |
| 126 | + const constraint: Constraint = { |
| 127 | + id: generateRandomSprottyId(), |
| 128 | + name: "", |
| 129 | + constraint: inputValue, |
| 130 | + }; |
| 131 | + this.constraintRegistry.registerConstraint(constraint); |
| 132 | + |
| 133 | + // Insert label type last but before the button |
| 134 | + const newValueElement = this.buildConstraintListItem(constraint); |
| 135 | + list.insertBefore(newValueElement, list.lastChild); |
| 136 | + this.selectConstraintListItem(constraint); |
| 137 | + |
| 138 | + // Select the text input element of the new value to allow entering the value |
| 139 | + newValueElement.querySelector("input")?.focus(); |
| 140 | + }; |
| 141 | + list.appendChild(addButton); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Sets and dynamically updates the size property of the passed input element. |
| 146 | + * When the text is zero the width is set to the placeholder length to make place for it. |
| 147 | + * When the text is changed the size gets updated with the keyup event. |
| 148 | + * @param inputElement the html dom input element to set the size property for |
| 149 | + */ |
| 150 | + private dynamicallySetInputSize(inputElement: HTMLInputElement): void { |
| 151 | + const handleResize = () => { |
| 152 | + const displayText = inputElement.value || inputElement.placeholder; |
| 153 | + const { width } = calculateTextSize(displayText, window.getComputedStyle(inputElement).font); |
| 154 | + |
| 155 | + // Values have higher padding for the rounded border |
| 156 | + const widthPadding = 8; |
| 157 | + const finalWidth = width + widthPadding; |
| 158 | + |
| 159 | + inputElement.style.width = finalWidth + "px"; |
| 160 | + }; |
| 161 | + |
| 162 | + inputElement.onkeyup = handleResize; |
| 163 | + |
| 164 | + // The inputElement is not added to the DOM yet, so we cannot set the size now. |
| 165 | + // Wait for next JS tick, after which the element has been added to the DOM and we can set the initial size |
| 166 | + setTimeout(handleResize, 0); |
| 167 | + } |
| 168 | + |
| 169 | + private buildRunButton(): HTMLElement { |
| 170 | + const wrapper = document.createElement("div"); |
| 171 | + wrapper.id = "run-button-container"; |
| 172 | + |
| 173 | + const button = document.createElement("button"); |
| 174 | + button.id = "run-button"; |
| 175 | + button.innerHTML = "Run"; |
| 176 | + button.onclick = () => { |
| 177 | + executeAction(AnalyzeDiagramAction.create()); |
| 178 | + }; |
| 179 | + |
| 180 | + wrapper.appendChild(button); |
| 181 | + return wrapper; |
| 182 | + } |
| 183 | +} |
0 commit comments