Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 1x 1x 1x 1x 6x 6x 2x 5x 2x 6x 6x 14x 6x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { encode } from "html-entities";
import { CustomVariable, InvalidDefinitionError } from "./base";
export class EnumCustomVariable extends CustomVariable {
static definitionName = "dropdown";
private options: string[];
constructor(name: string, label: string, options: string[]) {
super(name, label);
this.options = options;
}
protected inputHTML(): string {
const optionsHtml = this.options.map(o => {
return `<option value="${encode(o)}">${encode(o)}</option>`
}).join("");
return `<select name="${encode(this.name)}" aria-label="${encode(this.label)}">${optionsHtml}</select>`;
}
private static getOptionsFromType(type: string) {
type = type.trim();
if (type.startsWith("dropdown(") && type.endsWith(")")) {
return type.substr(9, type.length - 10).split(",").map(o => o.trim());
}
Iif (type.startsWith("enum(") && type.endsWith(")")) {
return type.substr(5, type.length - 6).split(",").map(o => o.trim());
}
throw new InvalidDefinitionError();
}
static createFromDefinition(name: string, definition: unknown): CustomVariable {
if (typeof definition === "string") {
const options = this.getOptionsFromType(definition);
return new this(name, name, options);
} else if (typeof definition === "object" && definition !== null) {
if ("type" in definition) {
const variableType = definition["type"];
if (typeof variableType === "string") {
const options = this.getOptionsFromType(variableType);
let label = name;
if ("label" in definition && typeof definition["label"] === "string") {
label = definition["label"].trim();
}
return new this(name, label, options);
}
}
}
throw new InvalidDefinitionError();
}
}
|