mapcomplete/UI/BigComponents/UserBadge.ts

144 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-06-23 22:35:19 +00:00
/**
* Handles and updates the user badge
*/
import {UIEventSource} from "../../Logic/UIEventSource";
import {UIElement} from "../UIElement";
import {VariableUiElement} from "../Base/VariableUIElement";
import {UserDetails} from "../../Logic/Osm/OsmConnection";
import Svg from "../../Svg";
import State from "../../State";
import Combine from "../Base/Combine";
import {FixedUiElement} from "../Base/FixedUiElement";
import LanguagePicker from "../LanguagePicker";
import Translations from "../i18n/Translations";
import Link from "../Base/Link";
export default class UserBadge extends UIElement {
2020-06-23 22:35:19 +00:00
private _userDetails: UIEventSource<UserDetails>;
private _logout: UIElement;
2020-07-20 07:57:19 +00:00
private _homeButton: UIElement;
2020-07-20 22:07:04 +00:00
private _languagePicker: UIElement;
2020-06-23 22:35:19 +00:00
private _loginButton: UIElement;
2020-06-23 22:35:19 +00:00
constructor() {
super(State.state.osmConnection.userDetails);
this._userDetails = State.state.osmConnection.userDetails;
2020-11-11 15:23:49 +00:00
this._languagePicker = (LanguagePicker.CreateLanguagePicker(State.state.layoutToUse.data.language) ?? new FixedUiElement(""))
2020-09-21 21:35:15 +00:00
.SetStyle("display:inline-block;width:min-content;");
2020-11-05 11:28:02 +00:00
this._loginButton = Translations.t.general.loginWithOpenStreetMap
.Clone()
.SetClass("userbadge-login")
.onClick(() => State.state.osmConnection.AttemptLogin());
2020-11-05 11:28:02 +00:00
this._logout =
Svg.logout_svg()
2020-11-05 11:28:02 +00:00
.onClick(() => {
State.state.osmConnection.LogOut();
});
2020-06-23 22:35:19 +00:00
this._userDetails.addCallback(function () {
2020-06-23 22:35:19 +00:00
const profilePic = document.getElementById("profile-pic");
2020-07-01 15:51:55 +00:00
if (profilePic) {
profilePic.onload = function () {
profilePic.style.opacity = "1"
};
}
2020-06-23 22:35:19 +00:00
});
2020-07-20 07:57:19 +00:00
this._homeButton = new VariableUiElement(
this._userDetails.map((userinfo) => {
2020-07-20 07:57:19 +00:00
if (userinfo.home) {
return Svg.home;
2020-07-20 07:57:19 +00:00
}
return "";
})
).onClick(() => {
const home = State.state.osmConnection.userDetails.data?.home;
2020-07-20 07:57:19 +00:00
if (home === undefined) {
return;
}
2021-01-02 20:03:40 +00:00
State.state.leafletMap.data.setView([home.lat, home.lon], 16);
2020-07-20 07:57:19 +00:00
});
2020-06-23 22:35:19 +00:00
}
2020-07-20 16:24:00 +00:00
InnerRender(): string {
2020-06-23 22:35:19 +00:00
const user = this._userDetails.data;
if (!user.loggedIn) {
2020-07-31 16:38:14 +00:00
return this._loginButton.Render();
2020-06-23 22:35:19 +00:00
}
2020-11-05 11:28:02 +00:00
let messageSpan: UIElement =
new Link(
new Combine([Svg.envelope, "" + user.totalMessages]),
2020-11-05 11:28:02 +00:00
'https://www.openstreetmap.org/messages/inbox',
true
)
if (user.unreadMessages > 0) {
2020-11-05 11:28:02 +00:00
messageSpan = new Link(
new Combine([Svg.envelope, "" + user.unreadMessages]),
2020-11-05 11:28:02 +00:00
'https://www.openstreetmap.org/messages/inbox',
true
).SetClass("alert")
}
2020-11-05 11:28:02 +00:00
let dryrun: UIElement = new FixedUiElement("");
if (user.dryRun) {
2020-11-05 11:28:02 +00:00
dryrun = new FixedUiElement("TESTING").SetClass("alert");
}
2020-06-23 22:35:19 +00:00
2020-06-29 01:40:19 +00:00
const settings =
new Link(Svg.gear_svg(),
`https://www.openstreetmap.org/user/${encodeURIComponent(user.name)}/account`,
2020-11-05 11:28:02 +00:00
true)
2020-06-29 01:40:19 +00:00
2020-07-23 14:00:49 +00:00
2020-11-05 11:28:02 +00:00
const userIcon = new Link(
new FixedUiElement(`<img id='profile-pic' src='${user.img}' alt='profile-pic'/>`),
`https://www.openstreetmap.org/user/${encodeURIComponent(user.name)}`,
true
);
2020-07-23 14:00:49 +00:00
2020-11-05 11:28:02 +00:00
const userName = new Link(
new FixedUiElement(user.name),
2020-11-18 11:50:06 +00:00
`https://www.openstreetmap.org/user/${user.name}`,
2020-11-05 11:28:02 +00:00
true);
2020-07-23 14:00:49 +00:00
2020-11-05 11:28:02 +00:00
const csCount =
new Link(
new Combine([Svg.star, "" + user.csCount]),
2020-11-05 11:28:02 +00:00
`https://www.openstreetmap.org/user/${user.name}/history`,
true);
const userStats = new Combine([
this._homeButton,
settings,
messageSpan,
csCount,
this._logout,
2020-11-05 11:28:02 +00:00
this._languagePicker])
.SetClass("userstats")
const usertext = new Combine([
userName,
dryrun,
userStats
]).SetClass("usertext")
return new Combine([
userIcon,
usertext,
]).Render()
2020-07-23 14:00:49 +00:00
2020-06-23 22:35:19 +00:00
}
}