forked from MapComplete/MapComplete
New question system
This commit is contained in:
parent
1738fc4252
commit
d1f8080c24
45 changed files with 1391 additions and 689 deletions
|
@ -1,24 +1,32 @@
|
|||
import {UIElement} from "../UIElement";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {UIInputElement} from "./UIInputElement";
|
||||
|
||||
|
||||
export class TextField extends UIElement {
|
||||
export class TextField<T> extends UIInputElement<T> {
|
||||
|
||||
public value = new UIEventSource("");
|
||||
public value: UIEventSource<string> = new UIEventSource<string>("");
|
||||
/**
|
||||
* Pings and has the value data
|
||||
*/
|
||||
public enterPressed = new UIEventSource<string>(undefined);
|
||||
private _placeholder: UIEventSource<string>;
|
||||
private _mapping: (string) => T;
|
||||
|
||||
constructor(placeholder : UIEventSource<string>) {
|
||||
constructor(placeholder: UIEventSource<string>,
|
||||
mapping: ((string) => T)) {
|
||||
super(placeholder);
|
||||
this._placeholder = placeholder;
|
||||
this._mapping = mapping;
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<T> {
|
||||
return this.value.map(this._mapping);
|
||||
}
|
||||
|
||||
protected InnerRender(): string {
|
||||
return "<form onSubmit='return false' class='form-text-field'>" +
|
||||
"<input type='text' placeholder='"+this._placeholder.data+"' id='text-" + this.id + "'>" +
|
||||
"<input type='text' placeholder='" + (this._placeholder.data ?? "") + "' id='text-" + this.id + "'>" +
|
||||
"</form>";
|
||||
}
|
||||
|
||||
|
@ -27,19 +35,24 @@ export class TextField extends UIElement {
|
|||
const field = document.getElementById('text-' + this.id);
|
||||
const self = this;
|
||||
field.oninput = () => {
|
||||
// @ts-ignore
|
||||
self.value.setData(field.value);
|
||||
};
|
||||
|
||||
field.addEventListener("keyup", function (event) {
|
||||
if (event.key === "Enter") {
|
||||
// @ts-ignore
|
||||
self.enterPressed.setData(field.value);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
Clear() {
|
||||
const field = document.getElementById('text-' + this.id);
|
||||
if (field !== undefined) {
|
||||
// @ts-ignore
|
||||
field.value = "";
|
||||
}
|
||||
}
|
||||
|
|
8
UI/Base/UIInputElement.ts
Normal file
8
UI/Base/UIInputElement.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import {UIElement} from "../UIElement";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
|
||||
export abstract class UIInputElement<T> extends UIElement{
|
||||
|
||||
abstract GetValue() : UIEventSource<T>;
|
||||
|
||||
}
|
|
@ -1,30 +1,33 @@
|
|||
import {UIElement} from "../UIElement";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {FixedUiElement} from "./FixedUiElement";
|
||||
import $ from "jquery"
|
||||
import {UIInputElement} from "./UIInputElement";
|
||||
|
||||
export class UIRadioButton extends UIElement {
|
||||
export class UIRadioButton<T> extends UIInputElement<T> {
|
||||
|
||||
public readonly SelectedElementIndex: UIEventSource<{ index: number, value: string }>
|
||||
= new UIEventSource<{ index: number, value: string }>(null);
|
||||
public readonly SelectedElementIndex: UIEventSource<number>
|
||||
= new UIEventSource<number>(null);
|
||||
|
||||
private readonly _elements: UIEventSource<{ element: UIElement, value: string }[]>
|
||||
private readonly _elements: UIEventSource<UIElement[]>
|
||||
private _selectFirstAsDefault: boolean;
|
||||
private _valueMapping: (i: number) => T;
|
||||
|
||||
constructor(elements: UIEventSource<{ element: UIElement, value: string }[]>) {
|
||||
constructor(elements: UIEventSource<UIElement[]>,
|
||||
valueMapping: ((i: number) => T),
|
||||
selectFirstAsDefault = true) {
|
||||
super(elements);
|
||||
this._elements = elements;
|
||||
this._selectFirstAsDefault = selectFirstAsDefault;
|
||||
const self = this;
|
||||
this._valueMapping = valueMapping;
|
||||
this.SelectedElementIndex.addCallback(() => {
|
||||
self.InnerUpdate(undefined);
|
||||
})
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<T> {
|
||||
return this.SelectedElementIndex.map(this._valueMapping);
|
||||
}
|
||||
|
||||
static FromStrings(choices: string[]): UIRadioButton {
|
||||
const wrapped = [];
|
||||
for (const choice of choices) {
|
||||
wrapped.push({
|
||||
element: new FixedUiElement(choice),
|
||||
value: choice
|
||||
});
|
||||
}
|
||||
return new UIRadioButton(new UIEventSource<{ element: UIElement, value: string }[]>(wrapped))
|
||||
}
|
||||
|
||||
private IdFor(i) {
|
||||
return 'radio-' + this.id + '-' + i;
|
||||
|
@ -35,12 +38,9 @@ export class UIRadioButton extends UIElement {
|
|||
let body = "";
|
||||
let i = 0;
|
||||
for (const el of this._elements.data) {
|
||||
const uielement = el.element;
|
||||
const value = el.value;
|
||||
|
||||
const htmlElement =
|
||||
'<input type="radio" id="' + this.IdFor(i) + '" name="radiogroup-' + this.id + '" value="' + value + '">' +
|
||||
'<label for="' + this.IdFor(i) + '">' + uielement.Render() + '</label>' +
|
||||
'<input type="radio" id="' + this.IdFor(i) + '" name="radiogroup-' + this.id + '">' +
|
||||
'<label for="' + this.IdFor(i) + '">' + el.Render() + '</label>' +
|
||||
'<br>';
|
||||
body += htmlElement;
|
||||
|
||||
|
@ -51,7 +51,6 @@ export class UIRadioButton extends UIElement {
|
|||
}
|
||||
|
||||
InnerUpdate(htmlElement: HTMLElement) {
|
||||
super.InnerUpdate(htmlElement);
|
||||
const self = this;
|
||||
|
||||
function checkButtons() {
|
||||
|
@ -59,8 +58,7 @@ export class UIRadioButton extends UIElement {
|
|||
const el = document.getElementById(self.IdFor(i));
|
||||
// @ts-ignore
|
||||
if (el.checked) {
|
||||
var v = {index: i, value: self._elements.data[i].value}
|
||||
self.SelectedElementIndex.setData(v);
|
||||
self.SelectedElementIndex.setData(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -74,29 +72,32 @@ export class UIRadioButton extends UIElement {
|
|||
);
|
||||
|
||||
if (this.SelectedElementIndex.data == null) {
|
||||
const el = document.getElementById(this.IdFor(0));
|
||||
el.checked = true;
|
||||
checkButtons();
|
||||
if (this._selectFirstAsDefault) {
|
||||
const el = document.getElementById(this.IdFor(0));
|
||||
// @ts-ignore
|
||||
el.checked = true;
|
||||
checkButtons();
|
||||
}
|
||||
} else {
|
||||
|
||||
// We check that what is selected matches the previous rendering
|
||||
var checked = -1;
|
||||
var expected = -1
|
||||
for (let i = 0; i < self._elements.data.length; i++) {
|
||||
const el = document.getElementById(self.IdFor(i));
|
||||
// @ts-ignore
|
||||
if (el.checked) {
|
||||
checked = i;
|
||||
var expected = this.SelectedElementIndex.data;
|
||||
if (expected) {
|
||||
|
||||
for (let i = 0; i < self._elements.data.length; i++) {
|
||||
const el = document.getElementById(self.IdFor(i));
|
||||
// @ts-ignore
|
||||
if (el.checked) {
|
||||
checked = i;
|
||||
}
|
||||
}
|
||||
if (el.value === this.SelectedElementIndex.data.value) {
|
||||
expected = i;
|
||||
if (expected != checked) {
|
||||
const el = document.getElementById(this.IdFor(expected));
|
||||
// @ts-ignore
|
||||
el.checked = true;
|
||||
}
|
||||
}
|
||||
if (expected != checked) {
|
||||
const el = document.getElementById(this.IdFor(expected));
|
||||
// @ts-ignore
|
||||
el.checked = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
72
UI/Base/UIRadioButtonWithOther.ts
Normal file
72
UI/Base/UIRadioButtonWithOther.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import {UIInputElement} from "./UIInputElement";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {UIRadioButton} from "./UIRadioButton";
|
||||
import {UIElement} from "../UIElement";
|
||||
import {TextField} from "./TextField";
|
||||
import {FixedUiElement} from "./FixedUiElement";
|
||||
|
||||
|
||||
export class UIRadioButtonWithOther<T> extends UIInputElement<T> {
|
||||
private readonly _radioSelector: UIRadioButton<T>;
|
||||
private readonly _freeformText: TextField<T>;
|
||||
private readonly _value: UIEventSource<T> = new UIEventSource<T>(undefined)
|
||||
|
||||
constructor(choices: UIElement[],
|
||||
otherChoiceTemplate: string,
|
||||
placeholder: string,
|
||||
choiceToValue: ((i: number) => T),
|
||||
stringToValue: ((string: string) => T)) {
|
||||
super(undefined);
|
||||
const self = this;
|
||||
|
||||
this._freeformText = new TextField(
|
||||
new UIEventSource<string>(placeholder),
|
||||
stringToValue);
|
||||
|
||||
|
||||
const otherChoiceElement = new FixedUiElement(
|
||||
otherChoiceTemplate.replace("$$$", this._freeformText.Render()));
|
||||
choices.push(otherChoiceElement);
|
||||
|
||||
this._radioSelector = new UIRadioButton(new UIEventSource(choices),
|
||||
(i) => {
|
||||
if (i === undefined || i === null) {
|
||||
return undefined;
|
||||
}
|
||||
if (i + 1 >= choices.length) {
|
||||
return this._freeformText.GetValue().data
|
||||
}
|
||||
return choiceToValue(i);
|
||||
},
|
||||
false);
|
||||
|
||||
this._radioSelector.GetValue().addCallback(
|
||||
(i) => {
|
||||
self._value.setData(i);
|
||||
});
|
||||
this._freeformText.GetValue().addCallback((str) => {
|
||||
self._value.setData(str);
|
||||
}
|
||||
);
|
||||
this._freeformText.onClick(() => {
|
||||
self._radioSelector.SelectedElementIndex.setData(choices.length - 1);
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<T> {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
protected InnerRender(): string {
|
||||
return this._radioSelector.Render();
|
||||
}
|
||||
|
||||
InnerUpdate(htmlElement: HTMLElement) {
|
||||
super.InnerUpdate(htmlElement);
|
||||
this._radioSelector.Update();
|
||||
this._freeformText.Update();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue