mapcomplete/UI/Base/SubtleButton.ts

60 lines
2 KiB
TypeScript
Raw Normal View History

import {UIElement} from "../UIElement";
import Translations from "../i18n/Translations";
import Combine from "./Combine";
2020-09-15 00:29:31 +00:00
import {FixedUiElement} from "./FixedUiElement";
export class SubtleButton extends UIElement{
2020-09-15 00:29:31 +00:00
private readonly image: UIElement;
2020-09-02 09:37:34 +00:00
private readonly message: UIElement;
private readonly linkTo: { url: string, newTab?: boolean } = undefined;
2020-09-15 00:29:31 +00:00
constructor(imageUrl: string | UIElement, message: string | UIElement, linkTo: { url: string, newTab?: boolean } = undefined) {
super(undefined);
2020-07-29 17:02:36 +00:00
this.linkTo = linkTo;
this.message = Translations.W(message);
2020-09-15 00:29:31 +00:00
if(this.message !== null){
this.message.dumbMode = false;
2020-09-15 00:29:31 +00:00
}
let img;
2020-09-15 00:29:31 +00:00
if ((imageUrl ?? "") === "") {
img = new FixedUiElement("");
2020-09-15 00:29:31 +00:00
} else if (typeof (imageUrl) === "string") {
img = new FixedUiElement(`<img style="width: 100%;" src="${imageUrl}" alt="">`);
2020-09-15 00:29:31 +00:00
} else {
img = imageUrl;
2020-09-15 00:29:31 +00:00
}
2021-01-25 02:12:09 +00:00
img.SetClass("block flex items-center justify-center h-11 w-11 flex-shrink0")
this.image = new Combine([img])
2021-01-25 02:12:09 +00:00
.SetClass("flex-shrink-0");
}
InnerRender(): string {
2020-09-02 09:37:34 +00:00
if(this.message !== null && this.message.IsEmpty()){
2020-09-15 00:29:31 +00:00
// Message == null: special case to force empty text
return "";
}
2020-07-29 17:02:36 +00:00
if(this.linkTo != undefined){
return new Combine([
`<a class='block flex group p-3 my-2 bg-blue-100 rounded-lg hover:shadow-xl hover:bg-blue-200' href="${this.linkTo.url}" ${this.linkTo.newTab ? 'target="_blank"' : ""}>`,
2020-09-15 00:29:31 +00:00
this.image,
`<div class='ml-4'>`,
2020-09-15 00:29:31 +00:00
this.message,
`</div>`,
`</a>`
2020-07-29 17:02:36 +00:00
]).Render();
}
return new Combine([
2020-09-15 00:29:31 +00:00
this.image,
this.message,
2021-01-25 02:12:09 +00:00
]).SetClass("block flex p-3 my-2 bg-blue-100 rounded-lg hover:shadow-xl hover:bg-blue-200")
.Render();
}
}