forked from MapComplete/MapComplete
Intermediary
This commit is contained in:
parent
5c9fb293e9
commit
dcf5d24002
10 changed files with 140 additions and 608 deletions
25
UI/Base/FixedInputElement.ts
Normal file
25
UI/Base/FixedInputElement.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import {UIInputElement} from "./UIInputElement";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import {UIElement} from "../UIElement";
|
||||
import {FixedUiElement} from "./FixedUiElement";
|
||||
|
||||
|
||||
export class FixedInputElement<T> extends UIInputElement<T> {
|
||||
private rendering: UIElement;
|
||||
private value: UIEventSource<T>;
|
||||
|
||||
constructor(rendering: UIElement | string, value: T) {
|
||||
super(undefined);
|
||||
this.value = new UIEventSource<T>(value);
|
||||
this.rendering = typeof (rendering) === 'string' ? new FixedUiElement(rendering) : rendering;
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<T> {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
protected InnerRender(): string {
|
||||
return this.rendering.Render();
|
||||
}
|
||||
|
||||
}
|
|
@ -5,33 +5,59 @@ import {UIInputElement} from "./UIInputElement";
|
|||
|
||||
export class TextField<T> extends UIInputElement<T> {
|
||||
|
||||
public value: UIEventSource<string> = new UIEventSource<string>("");
|
||||
private value: UIEventSource<string>;
|
||||
private mappedValue: UIEventSource<T>;
|
||||
/**
|
||||
* Pings and has the value data
|
||||
*/
|
||||
public enterPressed = new UIEventSource<string>(undefined);
|
||||
private _placeholder: UIEventSource<string>;
|
||||
private _mapping: (string) => T;
|
||||
private _pretext: string;
|
||||
private _fromString: (string: string) => T;
|
||||
|
||||
constructor(placeholder: UIEventSource<string>,
|
||||
mapping: ((string) => T)) {
|
||||
super(placeholder);
|
||||
this._placeholder = placeholder;
|
||||
this._mapping = mapping;
|
||||
constructor(options: {
|
||||
placeholder?: UIEventSource<string>,
|
||||
toString: (t: T) => string,
|
||||
fromString: (string: string) => T,
|
||||
pretext?: string,
|
||||
value?: UIEventSource<T>
|
||||
}) {
|
||||
super(options?.placeholder);
|
||||
this.value = new UIEventSource<string>("");
|
||||
this.mappedValue = options?.value ?? new UIEventSource<T>(undefined);
|
||||
|
||||
|
||||
this.value.addCallback((str) => this.mappedValue.setData(options.fromString(str)));
|
||||
this.mappedValue.addCallback((t) => this.value.setData(options.toString(t)));
|
||||
|
||||
|
||||
this._placeholder = options?.placeholder ?? new UIEventSource<string>("");
|
||||
this._pretext = options?.pretext ?? "";
|
||||
|
||||
const self = this;
|
||||
this.mappedValue.addCallback((t) => {
|
||||
if (t === undefined && t === null) {
|
||||
return;
|
||||
}
|
||||
const field = document.getElementById('text-' + this.id);
|
||||
if (field === undefined && field === null) {
|
||||
return;
|
||||
}
|
||||
field.value = options.toString(t);
|
||||
})
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<T> {
|
||||
return this.value.map(this._mapping);
|
||||
return this.mappedValue;
|
||||
}
|
||||
|
||||
protected InnerRender(): string {
|
||||
return "<form onSubmit='return false' class='form-text-field'>" +
|
||||
return this._pretext + "<form onSubmit='return false' class='form-text-field'>" +
|
||||
"<input type='text' placeholder='" + (this._placeholder.data ?? "") + "' id='text-" + this.id + "'>" +
|
||||
"</form>";
|
||||
}
|
||||
|
||||
InnerUpdate(htmlElement: HTMLElement) {
|
||||
super.InnerUpdate(htmlElement);
|
||||
const field = document.getElementById('text-' + this.id);
|
||||
const self = this;
|
||||
field.oninput = () => {
|
||||
|
|
|
@ -5,4 +5,6 @@ export abstract class UIInputElement<T> extends UIElement{
|
|||
|
||||
abstract GetValue() : UIEventSource<T>;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -7,25 +7,45 @@ export class UIRadioButton<T> extends UIInputElement<T> {
|
|||
public readonly SelectedElementIndex: UIEventSource<number>
|
||||
= new UIEventSource<number>(null);
|
||||
|
||||
private readonly _elements: UIEventSource<UIElement[]>
|
||||
private value: UIEventSource<T>;
|
||||
private readonly _elements: UIInputElement<T>[]
|
||||
private _selectFirstAsDefault: boolean;
|
||||
private _valueMapping: (i: number) => T;
|
||||
|
||||
constructor(elements: UIEventSource<UIElement[]>,
|
||||
valueMapping: ((i: number) => T),
|
||||
|
||||
constructor(elements: UIInputElement<T>[],
|
||||
selectFirstAsDefault = true) {
|
||||
super(elements);
|
||||
super(undefined);
|
||||
this._elements = elements;
|
||||
this._selectFirstAsDefault = selectFirstAsDefault;
|
||||
const self = this;
|
||||
this._valueMapping = valueMapping;
|
||||
this.SelectedElementIndex.addCallback(() => {
|
||||
self.InnerUpdate(undefined);
|
||||
})
|
||||
|
||||
|
||||
this.value =
|
||||
UIEventSource.flatten(this.SelectedElementIndex.map(
|
||||
(selectedIndex) => {
|
||||
if (selectedIndex !== undefined && selectedIndex !== null) {
|
||||
return elements[selectedIndex].GetValue()
|
||||
}
|
||||
}
|
||||
), elements.map(e => e.GetValue()))
|
||||
;
|
||||
|
||||
|
||||
for (let i = 0; i < elements.length; i ++){
|
||||
elements[i].onClick(( ) => {
|
||||
self.SelectedElementIndex.setData(i);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<T> {
|
||||
return this.SelectedElementIndex.map(this._valueMapping);
|
||||
return this.value;
|
||||
}
|
||||
|
||||
|
||||
|
@ -37,7 +57,7 @@ export class UIRadioButton<T> extends UIInputElement<T> {
|
|||
|
||||
let body = "";
|
||||
let i = 0;
|
||||
for (const el of this._elements.data) {
|
||||
for (const el of this._elements) {
|
||||
const htmlElement =
|
||||
'<input type="radio" id="' + this.IdFor(i) + '" name="radiogroup-' + this.id + '">' +
|
||||
'<label for="' + this.IdFor(i) + '">' + el.Render() + '</label>' +
|
||||
|
@ -54,7 +74,7 @@ export class UIRadioButton<T> extends UIInputElement<T> {
|
|||
const self = this;
|
||||
|
||||
function checkButtons() {
|
||||
for (let i = 0; i < self._elements.data.length; i++) {
|
||||
for (let i = 0; i < self._elements.length; i++) {
|
||||
const el = document.getElementById(self.IdFor(i));
|
||||
// @ts-ignore
|
||||
if (el.checked) {
|
||||
|
@ -87,7 +107,7 @@ export class UIRadioButton<T> extends UIInputElement<T> {
|
|||
var expected = this.SelectedElementIndex.data;
|
||||
if (expected) {
|
||||
|
||||
for (let i = 0; i < self._elements.data.length; i++) {
|
||||
for (let i = 0; i < self._elements.length; i++) {
|
||||
const el = document.getElementById(self.IdFor(i));
|
||||
// @ts-ignore
|
||||
if (el.checked) {
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
|
@ -27,15 +27,32 @@ export class UIEventSource<T>{
|
|||
}
|
||||
}
|
||||
|
||||
public map<J>(f: ((T) => J),
|
||||
extraSources : UIEventSource<any>[] = []): UIEventSource<J> {
|
||||
const self = this;
|
||||
public static flatten<X>(source: UIEventSource<UIEventSource<X>>, possibleSources: UIEventSource<any>[]): UIEventSource<X> {
|
||||
const sink = new UIEventSource<X>(source.data?.data);
|
||||
|
||||
source.addCallback((latestData) => {
|
||||
sink.setData(latestData?.data);
|
||||
});
|
||||
|
||||
for (const possibleSource of possibleSources) {
|
||||
possibleSource.addCallback(() => {
|
||||
sink.setData(source.data?.data);
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
return sink;
|
||||
}
|
||||
|
||||
public map<J>(f: ((T) => J),
|
||||
extraSources: UIEventSource<any>[] = []): UIEventSource<J> {
|
||||
const self = this;
|
||||
|
||||
const update = function () {
|
||||
newSource.setData(f(self.data));
|
||||
newSource.ping();
|
||||
}
|
||||
|
||||
|
||||
this.addCallback(update);
|
||||
for (const extraSource of extraSources) {
|
||||
extraSource.addCallback(update);
|
||||
|
|
|
@ -21,6 +21,7 @@ export class UserBadge extends UIElement {
|
|||
pendingChanges: UIElement,
|
||||
basemap: Basemap) {
|
||||
super(userDetails);
|
||||
|
||||
this._userDetails = userDetails;
|
||||
this._pendingChanges = pendingChanges;
|
||||
this._basemap = basemap;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue