From 00d95c4be1498266438bd6631bd78a6ed98cf4af Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Mon, 20 Jul 2020 21:39:07 +0200 Subject: [PATCH] Finishing refactoring --- Customizations/TagRendering.ts | 2 +- UI/ImageUploadFlow.ts | 4 +- UI/Input/DropDown.ts | 94 ++++++++++++++++++++++------------ UI/Input/InputElement.ts | 2 - UI/Input/RadioButton.ts | 3 +- UI/Input/TextField.ts | 13 +++-- UI/UIElement.ts | 3 +- test.ts | 22 ++++---- 8 files changed, 86 insertions(+), 57 deletions(-) diff --git a/Customizations/TagRendering.ts b/Customizations/TagRendering.ts index 231c820..5f45a26 100644 --- a/Customizations/TagRendering.ts +++ b/Customizations/TagRendering.ts @@ -263,7 +263,7 @@ class TagRendering extends UIElement implements TagDependantUIElement { this._editButton = new FixedUiElement("edit") .onClick(() => { self._editMode.setData(true); - self._questionElement.ShowValue(self.CurrentValue()); + self._questionElement.GetValue().setData(self.CurrentValue()); }); } diff --git a/UI/ImageUploadFlow.ts b/UI/ImageUploadFlow.ts index e332bc2..5a55f53 100644 --- a/UI/ImageUploadFlow.ts +++ b/UI/ImageUploadFlow.ts @@ -41,7 +41,7 @@ export class ImageUploadFlow extends UIElement { preferedLicense ); this._licensePicker = licensePicker; - this._selectedLicence = licensePicker.selectedElement; + this._selectedLicence = licensePicker.GetValue(); const licenseExplanations = { @@ -62,7 +62,7 @@ export class ImageUploadFlow extends UIElement { } - protected InnerRender(): string { + InnerRender(): string { if (!this._userdetails.data.loggedIn) { return "
Gelieve je aan te melden om een foto toe te voegen of vragen te beantwoorden
"; diff --git a/UI/Input/DropDown.ts b/UI/Input/DropDown.ts index 78e2896..fb15f02 100644 --- a/UI/Input/DropDown.ts +++ b/UI/Input/DropDown.ts @@ -1,35 +1,61 @@ import {UIEventSource} from "../UIEventSource"; import {UIElement} from "../UIElement"; +import {InputElement} from "./InputElement"; +import instantiate = WebAssembly.instantiate; +import {FixedUiElement} from "../Base/FixedUiElement"; -export class DropDown extends UIElement { +export class DropDown extends InputElement { - selectedElement: UIEventSource - private _label: string; - private _values: { value: string; shown: string }[]; + private readonly _label: string; + private readonly _values: { value: T; shown: UIElement }[]; - constructor(label: string, values: { value: string, shown: string }[], - selectedElement: UIEventSource = undefined) { + private readonly _value; + + constructor(label: string, + values: { value: T, shown: string | UIElement }[], + value: UIEventSource = undefined) { super(undefined); + this._value = value ?? new UIEventSource(undefined); this._label = label; - this._values = values; - this.selectedElement = selectedElement ?? new UIEventSource(values[0].value); - if(selectedElement.data === undefined){ - this.selectedElement.setData(values[0].value) + this._values = values.map(v => { + return { + value: v.value, + shown: v.shown instanceof UIElement ? v.shown : new FixedUiElement(v.shown) + } + } + ); + this.ListenTo(this._value) + + } + + GetValue(): UIEventSource { + return this._value; + } + + ShowValue(t: T): boolean { + if (!this.IsValid(t)) { + return false; } - const self = this; - this.selectedElement.addCallback(() => { - self.InnerUpdate(); - }); + this._value.setData(t); + } + + IsValid(t: T): boolean { + for (const value of this._values) { + if (value.value === t) { + return true; + } + } + return false } - protected InnerRender(): string { + InnerRender(): string { let options = ""; - for (const value of this._values) { - options += "" - } + for (let i = 0; i < this._values.length; i++) { + options += "" + } return "
" + "" + "