forked from MapComplete/MapComplete
More refactoring, stuff kindoff works
This commit is contained in:
parent
62f471df1e
commit
3943100e54
52 changed files with 635 additions and 1010 deletions
|
@ -4,10 +4,9 @@ import {UIEventSource} from "../../Logic/UIEventSource";
|
|||
import BaseUIElement from "../BaseUIElement";
|
||||
|
||||
export class TextField extends InputElement<string> {
|
||||
private readonly value: UIEventSource<string>;
|
||||
public readonly enterPressed = new UIEventSource<string>(undefined);
|
||||
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
|
||||
|
||||
private readonly value: UIEventSource<string>;
|
||||
private _element: HTMLElement;
|
||||
private readonly _isValid: (s: string, country?: () => string) => boolean;
|
||||
|
||||
|
@ -19,6 +18,7 @@ export class TextField extends InputElement<string> {
|
|||
inputMode?: string,
|
||||
label?: BaseUIElement,
|
||||
textAreaRows?: number,
|
||||
inputStyle?: string,
|
||||
isValid?: ((s: string, country?: () => string) => boolean)
|
||||
}) {
|
||||
super();
|
||||
|
@ -26,39 +26,40 @@ export class TextField extends InputElement<string> {
|
|||
options = options ?? {};
|
||||
this.value = options?.value ?? new UIEventSource<string>(undefined);
|
||||
this._isValid = options.isValid ?? (_ => true);
|
||||
|
||||
|
||||
this.onClick(() => {
|
||||
self.IsSelected.setData(true)
|
||||
});
|
||||
|
||||
|
||||
const placeholder = Translations.W(options.placeholder ?? "").ConstructElement().innerText.replace("'", "'");
|
||||
|
||||
const placeholder = Translations.W(options. placeholder ?? "").ConstructElement().innerText.replace("'", "'");
|
||||
|
||||
this.SetClass("form-text-field")
|
||||
let inputEl : HTMLElement
|
||||
if(options.htmlType === "area"){
|
||||
let inputEl: HTMLElement
|
||||
if (options.htmlType === "area") {
|
||||
const el = document.createElement("textarea")
|
||||
el.placeholder = placeholder
|
||||
el.rows = options.textAreaRows
|
||||
el.cols = 50
|
||||
el.style.cssText = "max-width: 100%; width: 100%; box-sizing: border-box"
|
||||
inputEl = el;
|
||||
}else{
|
||||
} else {
|
||||
const el = document.createElement("input")
|
||||
el.type = options.htmlType
|
||||
el.inputMode = options.inputMode
|
||||
el.type = options.htmlType ?? "text"
|
||||
el.inputMode = options.inputMode
|
||||
el.placeholder = placeholder
|
||||
el.style.cssText = options.inputStyle
|
||||
inputEl = el
|
||||
}
|
||||
|
||||
const form = document.createElement("form")
|
||||
form.appendChild(inputEl)
|
||||
form.onsubmit = () => false;
|
||||
|
||||
if(options.label){
|
||||
form.appendChild(options.label.ConstructElement())
|
||||
}
|
||||
|
||||
|
||||
if (options.label) {
|
||||
form.appendChild(options.label.ConstructElement())
|
||||
}
|
||||
|
||||
this._element = form;
|
||||
|
||||
const field = inputEl;
|
||||
|
@ -70,9 +71,9 @@ export class TextField extends InputElement<string> {
|
|||
}
|
||||
// @ts-ignore
|
||||
field.value = value;
|
||||
if(self.IsValid(value)){
|
||||
if (self.IsValid(value)) {
|
||||
self.RemoveClass("invalid")
|
||||
}else{
|
||||
} else {
|
||||
self.SetClass("invalid")
|
||||
}
|
||||
|
||||
|
@ -82,7 +83,7 @@ export class TextField extends InputElement<string> {
|
|||
|
||||
// How much characters are on the right, not including spaces?
|
||||
// @ts-ignore
|
||||
const endDistance = field.value.substring(field.selectionEnd).replace(/ /g,'').length;
|
||||
const endDistance = field.value.substring(field.selectionEnd).replace(/ /g, '').length;
|
||||
// @ts-ignore
|
||||
let val: string = field.value;
|
||||
if (!self.IsValid(val)) {
|
||||
|
@ -97,18 +98,18 @@ export class TextField extends InputElement<string> {
|
|||
// @ts-ignore
|
||||
val = field.value;
|
||||
let newCursorPos = val.length - endDistance;
|
||||
while(newCursorPos >= 0 &&
|
||||
while (newCursorPos >= 0 &&
|
||||
// We count the number of _actual_ characters (non-space characters) on the right of the new value
|
||||
// This count should become bigger then the end distance
|
||||
val.substr(newCursorPos).replace(/ /g, '').length < endDistance
|
||||
){
|
||||
newCursorPos --;
|
||||
) {
|
||||
newCursorPos--;
|
||||
}
|
||||
// @ts-ignore
|
||||
TextField.SetCursorPosition(newCursorPos);
|
||||
};
|
||||
|
||||
|
||||
|
||||
field.addEventListener("focusin", () => self.IsSelected.setData(true));
|
||||
field.addEventListener("focusout", () => self.IsSelected.setData(false));
|
||||
|
||||
|
@ -118,22 +119,13 @@ export class TextField extends InputElement<string> {
|
|||
// @ts-ignore
|
||||
self.enterPressed.setData(field.value);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
GetValue(): UIEventSource<string> {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
protected InnerConstructElement(): HTMLElement {
|
||||
return this._element;
|
||||
}
|
||||
|
||||
private static SetCursorPosition(textfield: HTMLElement, i: number) {
|
||||
if(textfield === undefined || textfield === null){
|
||||
if (textfield === undefined || textfield === null) {
|
||||
return;
|
||||
}
|
||||
if (i === -1) {
|
||||
|
@ -146,6 +138,10 @@ export class TextField extends InputElement<string> {
|
|||
|
||||
}
|
||||
|
||||
GetValue(): UIEventSource<string> {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
IsValid(t: string): boolean {
|
||||
if (t === undefined || t === null) {
|
||||
return false
|
||||
|
@ -153,4 +149,8 @@ export class TextField extends InputElement<string> {
|
|||
return this._isValid(t, undefined);
|
||||
}
|
||||
|
||||
protected InnerConstructElement(): HTMLElement {
|
||||
return this._element;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue