Intermediary refactoring

This commit is contained in:
Pieter Vander Vennet 2020-07-20 15:54:50 +02:00
parent dcf5d24002
commit 93db813cfc
15 changed files with 280 additions and 178 deletions

View file

@ -123,6 +123,7 @@ export class AddButton extends UIElement {
const self = this;
htmlElement.onclick = function (event) {
// @ts-ignore
if(event.consumed){
return;
}

View file

@ -5,7 +5,7 @@ export class FixedUiElement extends UIElement {
constructor(html: string) {
super(undefined);
this._html = html;
this._html = html ?? "";
}
protected InnerRender(): string {

View file

@ -1,10 +0,0 @@
import {UIElement} from "../UIElement";
import {UIEventSource} from "../UIEventSource";
export abstract class UIInputElement<T> extends UIElement{
abstract GetValue() : UIEventSource<T>;
}

View file

@ -115,4 +115,6 @@ export class FeatureInfoBox extends UIElement {
"</div>";
}
}

View file

@ -3,7 +3,7 @@ import {UIEventSource} from "./UIEventSource";
import $ from "jquery"
import {Imgur} from "../Logic/Imgur";
import {UserDetails} from "../Logic/OsmConnection";
import {DropDownUI} from "./Base/DropDownUI";
import {DropDown} from "./Input/DropDown";
import {VariableUiElement} from "./Base/VariableUIElement";
export class ImageUploadFlow extends UIElement {
@ -31,7 +31,7 @@ export class ImageUploadFlow extends UIElement {
this._uploadOptions = uploadOptions;
this.ListenTo(this._isUploading);
const licensePicker = new DropDownUI("Jouw foto wordt gepubliceerd ",
const licensePicker = new DropDown("Jouw foto wordt gepubliceerd ",
[
{value: "CC0", shown: "in het publiek domein"},

View file

@ -1,7 +1,7 @@
import {UIEventSource} from "../UIEventSource";
import {UIElement} from "../UIElement";
export class DropDownUI extends UIElement {
export class DropDown extends UIElement {
selectedElement: UIEventSource<string>
private _label: string;

View file

@ -1,10 +1,10 @@
import {UIInputElement} from "./UIInputElement";
import {InputElement} from "./InputElement";
import {UIEventSource} from "../UIEventSource";
import {UIElement} from "../UIElement";
import {FixedUiElement} from "./FixedUiElement";
import {FixedUiElement} from "../Base/FixedUiElement";
export class FixedInputElement<T> extends UIInputElement<T> {
export class FixedInputElement<T> extends InputElement<T> {
private rendering: UIElement;
private value: UIEventSource<T>;
@ -22,4 +22,8 @@ export class FixedInputElement<T> extends UIInputElement<T> {
return this.rendering.Render();
}
IsValid(t: T): boolean {
return t === this.value.data;
}
}

11
UI/Input/InputElement.ts Normal file
View file

@ -0,0 +1,11 @@
import {UIElement} from "../UIElement";
import {UIEventSource} from "../UIEventSource";
import {FixedUiElement} from "../Base/FixedUiElement";
export abstract class InputElement<T> extends UIElement{
abstract GetValue() : UIEventSource<T>;
abstract IsValid(t: T) : boolean;
}

View file

@ -0,0 +1,37 @@
import {InputElement} from "./InputElement";
import {UIEventSource} from "../UIEventSource";
import {UIElement} from "../UIElement";
import {FixedUiElement} from "../Base/FixedUiElement";
export class InputElementWrapper<T> extends InputElement<T>{
private pre: UIElement ;
private input: InputElement<T>;
private post: UIElement ;
constructor(
pre: UIElement | string,
input: InputElement<T>,
post: UIElement | string
) {
super(undefined);
this.pre = typeof(pre) === 'string' ? new FixedUiElement(pre) : pre
this.input = input;
this.post =typeof(post) === 'string' ? new FixedUiElement(post) : post
}
GetValue(): UIEventSource<T> {
return this.input.GetValue();
}
protected InnerRender(): string {
return this.pre.Render() + this.input.Render() + this.post.Render();
}
IsValid(t: T): boolean {
return this.input.IsValid(t);
}
}

View file

@ -1,31 +1,27 @@
import {UIElement} from "../UIElement";
import {UIEventSource} from "../UIEventSource";
import {UIInputElement} from "./UIInputElement";
import {InputElement} from "./InputElement";
export class UIRadioButton<T> extends UIInputElement<T> {
export class RadioButton<T> extends InputElement<T> {
public readonly SelectedElementIndex: UIEventSource<number>
private readonly _selectedElementIndex: UIEventSource<number>
= new UIEventSource<number>(null);
private value: UIEventSource<T>;
private readonly _elements: UIInputElement<T>[]
private readonly _elements: InputElement<T>[]
private _selectFirstAsDefault: boolean;
private _valueMapping: (i: number) => T;
constructor(elements: UIInputElement<T>[],
constructor(elements: InputElement<T>[],
selectFirstAsDefault = true) {
super(undefined);
this._elements = elements;
this._selectFirstAsDefault = selectFirstAsDefault;
const self = this;
this.SelectedElementIndex.addCallback(() => {
self.InnerUpdate(undefined);
})
this.value =
UIEventSource.flatten(this.SelectedElementIndex.map(
UIEventSource.flatten(this._selectedElementIndex.map(
(selectedIndex) => {
if (selectedIndex !== undefined && selectedIndex !== null) {
return elements[selectedIndex].GetValue()
@ -34,16 +30,29 @@ export class UIRadioButton<T> extends UIInputElement<T> {
), elements.map(e => e.GetValue()))
;
this.value.addCallback((t) => {
self.SetCorrectValue(t);
})
for (let i = 0; i < elements.length; i ++){
elements[i].onClick(( ) => {
self.SelectedElementIndex.setData(i);
for (let i = 0; i < elements.length; i++) {
// If an element is clicked, the radio button corresponding with it should be selected as well
elements[i].onClick(() => {
self._selectedElementIndex.setData(i);
});
}
}
IsValid(t: T): boolean {
for (const inputElement of this._elements) {
if (inputElement.IsValid(t)) {
return true;
}
}
return false;
}
GetValue(): UIEventSource<T> {
return this.value;
}
@ -70,6 +79,24 @@ export class UIRadioButton<T> extends UIInputElement<T> {
return "<form id='" + this.id + "-form'>" + body + "</form>";
}
private SetCorrectValue(t: T) {
if (t === undefined) {
return;
}
// We check that what is selected matches the previous rendering
for (let i = 0; i < this._elements.length; i++) {
const e = this._elements[i];
if (e.IsValid(t)) {
this._selectedElementIndex.setData(i);
e.GetValue().setData(t);
// @ts-ignore
document.getElementById(this.IdFor(i)).checked = true;
return;
}
}
}
InnerUpdate(htmlElement: HTMLElement) {
const self = this;
@ -78,7 +105,7 @@ export class UIRadioButton<T> extends UIInputElement<T> {
const el = document.getElementById(self.IdFor(i));
// @ts-ignore
if (el.checked) {
self.SelectedElementIndex.setData(i);
self._selectedElementIndex.setData(i);
}
}
}
@ -91,8 +118,9 @@ export class UIRadioButton<T> extends UIInputElement<T> {
}
);
if (this.SelectedElementIndex.data == null) {
if (this._selectFirstAsDefault) {
if (this._selectFirstAsDefault) {
this.SetCorrectValue(this.value.data);
if (this._selectedElementIndex.data === null || this._selectedElementIndex.data === undefined) {
const el = document.getElementById(this.IdFor(0));
if (el) {
// @ts-ignore
@ -100,30 +128,10 @@ export class UIRadioButton<T> extends UIInputElement<T> {
checkButtons();
}
}
} else {
// We check that what is selected matches the previous rendering
var checked = -1;
var expected = this.SelectedElementIndex.data;
if (expected) {
for (let i = 0; i < self._elements.length; i++) {
const el = document.getElementById(self.IdFor(i));
// @ts-ignore
if (el.checked) {
checked = i;
}
}
if (expected != checked) {
const el = document.getElementById(this.IdFor(expected));
// @ts-ignore
el.checked = true;
}
}
}
}
};
}

View file

@ -1,9 +1,10 @@
import {UIElement} from "../UIElement";
import {UIEventSource} from "../UIEventSource";
import {UIInputElement} from "./UIInputElement";
import {InputElement} from "./InputElement";
import {FixedUiElement} from "../Base/FixedUiElement";
export class TextField<T> extends UIInputElement<T> {
export class TextField<T> extends InputElement<T> {
private value: UIEventSource<string>;
private mappedValue: UIEventSource<T>;
@ -11,28 +12,32 @@ export class TextField<T> extends UIInputElement<T> {
* Pings and has the value data
*/
public enterPressed = new UIEventSource<string>(undefined);
private _placeholder: UIEventSource<string>;
private _pretext: string;
private _fromString: (string: string) => T;
private _placeholder: UIElement;
private _fromString?: (string: string) => T;
private _toString: (t: T) => string;
constructor(options: {
placeholder?: UIEventSource<string>,
toString: (t: T) => string,
fromString: (string: string) => T,
pretext?: string,
placeholder?: string | UIElement,
toString?: (t: T) => string,
fromString?: (string: string) => T,
value?: UIEventSource<T>
}) {
super(options?.placeholder);
super(undefined);
this.value = new UIEventSource<string>("");
this.mappedValue = options?.value ?? new UIEventSource<T>(undefined);
// @ts-ignore
this._fromString = options.fromString ?? ((str) => (str))
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 ?? "";
this._placeholder =
typeof(options.placeholder) === "string" ? new FixedUiElement(options.placeholder) :
(options.placeholder ?? new FixedUiElement(""));
this._toString = options.toString ?? ((t) => ("" + t));
const self = this;
this.mappedValue.addCallback((t) => {
@ -40,9 +45,10 @@ export class TextField<T> extends UIInputElement<T> {
return;
}
const field = document.getElementById('text-' + this.id);
if (field === undefined && field === null) {
if (field === undefined || field === null) {
return;
}
// @ts-ignore
field.value = options.toString(t);
})
}
@ -52,13 +58,16 @@ export class TextField<T> extends UIInputElement<T> {
}
protected InnerRender(): string {
return this._pretext + "<form onSubmit='return false' class='form-text-field'>" +
"<input type='text' placeholder='" + (this._placeholder.data ?? "") + "' id='text-" + this.id + "'>" +
return "<form onSubmit='return false' class='form-text-field'>" +
"<input type='text' placeholder='" + this._placeholder.InnerRender() + "' id='text-" + this.id + "'>" +
"</form>";
}
InnerUpdate(htmlElement: HTMLElement) {
const field = document.getElementById('text-' + this.id);
if(field === null){
return;
}
const self = this;
field.oninput = () => {
// @ts-ignore
@ -75,6 +84,14 @@ export class TextField<T> extends UIInputElement<T> {
}
IsValid(t: T): boolean {
if(t === undefined || t === null){
return false;
}
const result = this._toString(t);
return result !== undefined && result !== null;
}
Clear() {
const field = document.getElementById('text-' + this.id);
if (field !== undefined) {

View file

@ -1,5 +1,5 @@
import {UIElement} from "./UIElement";
import {TextField} from "./Base/TextField";
import {TextField} from "./Input/TextField";
import {UIEventSource} from "./UIEventSource";
import {FixedUiElement} from "./Base/FixedUiElement";
import {Geocoding} from "../Logic/Geocoding";
@ -9,7 +9,10 @@ import {Basemap} from "../Logic/Basemap";
export class SearchAndGo extends UIElement {
private _placeholder = new UIEventSource("Zoek naar een locatie...")
private _searchField = new TextField(this._placeholder);
private _searchField = new TextField<string>({
placeholder: this._placeholder
}
);
private _foundEntries = new UIEventSource([]);
private _map: Basemap;
@ -33,7 +36,7 @@ export class SearchAndGo extends UIElement {
// Triggered by 'enter' or onclick
private RunSearch() {
const searchString = this._searchField.value.data;
const searchString = this._searchField.GetValue().data;
this._searchField.Clear();
this._placeholder.setData("Bezig met zoeken...");
const self = this;

View file

@ -1,8 +1,7 @@
import {UIEventSource} from "./UIEventSource";
import instantiate = WebAssembly.instantiate;
export abstract class UIElement {
private static nextId: number = 0;
public readonly id: string;
@ -35,7 +34,7 @@ export abstract class UIElement {
this.Update();
return this;
}
Update(): void {
let element = document.getElementById(this.id);
if (element === null || element === undefined) {
@ -121,5 +120,6 @@ export abstract class UIElement {
public IsEmpty(): boolean {
return this.InnerRender() === "";
}
}
}