126 lines
2.3 KiB
TypeScript
126 lines
2.3 KiB
TypeScript
namespace formgen.input
|
|
{
|
|
|
|
/**
|
|
*/
|
|
export class class_input_checkbox implements interface_input<boolean>
|
|
{
|
|
|
|
/**
|
|
*/
|
|
private additional_classes : Array<string>;
|
|
|
|
|
|
/**
|
|
*/
|
|
private label : (null | string);
|
|
|
|
|
|
/**
|
|
*/
|
|
private hint : (null | string);
|
|
|
|
|
|
/**
|
|
*/
|
|
private element_input : (null | Element);
|
|
|
|
|
|
/**
|
|
*/
|
|
public constructor(
|
|
{
|
|
"additional_classes": additional_classes = [],
|
|
"label": label = null,
|
|
"hint": hint = null,
|
|
}
|
|
:
|
|
{
|
|
additional_classes ?: Array<string>,
|
|
label ?: (null | string);
|
|
hint ?: (null | string);
|
|
}
|
|
=
|
|
{
|
|
}
|
|
)
|
|
{
|
|
this.additional_classes = ["formgen-input-checkbox"].concat(additional_classes);
|
|
this.label = label;
|
|
this.hint = hint;
|
|
this.element_input = null;
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public async setup(
|
|
target : Element
|
|
) : Promise<void>
|
|
{
|
|
const id : string = formgen.helpers.string.generate();
|
|
|
|
const element_container : Element = document.createElement("div");
|
|
element_container.classList.add("formgen-input");
|
|
for (const class_ of this.additional_classes)
|
|
{
|
|
element_container.classList.add(class_);
|
|
}
|
|
// label
|
|
{
|
|
const element_label : Element = document.createElement("label");
|
|
element_label.setAttribute("for", id);
|
|
element_label.textContent = (this.label ?? "");
|
|
// hint
|
|
{
|
|
if (this.hint === null)
|
|
{
|
|
// do nothing
|
|
}
|
|
else
|
|
{
|
|
await (new formgen.info.class_info(this.hint)).setup(element_label);
|
|
}
|
|
}
|
|
element_container.appendChild(element_label);
|
|
}
|
|
// input
|
|
{
|
|
const element_input : Element = document.createElement("input");
|
|
element_input.setAttribute("type", "checkbox");
|
|
element_input.setAttribute("id", id);
|
|
element_container.appendChild(element_input);
|
|
this.element_input = element_input;
|
|
}
|
|
target.appendChild(element_container);
|
|
// return Promise.resolve<void>(undefined);
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public read(
|
|
) : Promise<boolean>
|
|
{
|
|
const value : boolean = (this.element_input as HTMLInputElement).checked;
|
|
return Promise.resolve<boolean>(value);
|
|
}
|
|
|
|
|
|
/**
|
|
* [implementation]
|
|
*/
|
|
public write(
|
|
value : boolean
|
|
) : Promise<void>
|
|
{
|
|
(this.element_input as HTMLInputElement).checked = value;
|
|
return Promise.resolve<void>(undefined);
|
|
}
|
|
|
|
}
|
|
|
|
}
|