Files
KSP-MGA-Planner/dist/main/editor/selector.js
Krafpy 824af087c1 Modified file structure.
Modified the file structure to have the `index.html` at the root
of the repository. Needed for Github Pages.
2021-08-15 21:31:25 +02:00

43 lines
1.1 KiB
JavaScript

export class Selector {
constructor(id) {
this._selector = document.getElementById(id);
}
fill(options) {
this.clear();
for (const option of options) {
appendOption(this._selector, option);
}
}
clear() {
this._selector.innerHTML = "";
}
change(onChange) {
this._onChange = onChange;
this._selector.onchange = () => {
const index = this._selector.selectedIndex;
const value = this._selector.value;
this._onChange(value, index);
};
}
select(index) {
this._selector.selectedIndex = index;
if (this._onChange)
this._onChange(this._selector.value, this._selector.selectedIndex);
return this._selector.value;
}
get selected() {
return this._selector.value;
}
enable() {
this._selector.disabled = false;
}
disable() {
this._selector.disabled = true;
}
}
function appendOption(selector, option) {
const optionElt = document.createElement("option");
optionElt.innerHTML = option;
selector.appendChild(optionElt);
}