2022-12-16 13:45:07 +01:00
|
|
|
import ScrollableFullScreen from "../Base/ScrollableFullScreen"
|
|
|
|
import Translations from "../i18n/Translations"
|
|
|
|
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
|
|
|
|
import Combine from "../Base/Combine"
|
|
|
|
import { SubtleButton } from "../Base/SubtleButton"
|
|
|
|
import Svg from "../../Svg"
|
|
|
|
import { VariableUiElement } from "../Base/VariableUIElement"
|
|
|
|
import Img from "../Base/Img"
|
|
|
|
import { FixedUiElement } from "../Base/FixedUiElement"
|
|
|
|
import Link from "../Base/Link"
|
2023-01-11 04:27:42 +01:00
|
|
|
import { Store, UIEventSource } from "../../Logic/UIEventSource"
|
2022-12-16 13:45:07 +01:00
|
|
|
import Loc from "../../Models/Loc"
|
|
|
|
import BaseUIElement from "../BaseUIElement"
|
2022-12-13 03:46:53 +01:00
|
|
|
import Showdown from "showdown"
|
2022-12-16 13:45:07 +01:00
|
|
|
import LanguagePicker from "../LanguagePicker"
|
|
|
|
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
|
|
|
|
import Constants from "../../Models/Constants"
|
2023-01-11 03:53:58 +01:00
|
|
|
import EditableTagRendering from "../Popup/EditableTagRendering"
|
|
|
|
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig"
|
|
|
|
import { SaveButton } from "../Popup/SaveButton"
|
|
|
|
import { TagUtils } from "../../Logic/Tags/TagUtils"
|
2023-02-08 01:14:21 +01:00
|
|
|
import usersettings from "../../assets/generated/layers/usersettings.json"
|
2023-01-11 04:27:42 +01:00
|
|
|
import { LoginToggle } from "../Popup/LoginButton"
|
2023-01-12 01:17:07 +01:00
|
|
|
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
|
2023-02-08 01:14:21 +01:00
|
|
|
import translators from "../../assets/translators.json"
|
|
|
|
import codeContributors from "../../assets/contributors.json"
|
2023-01-14 04:19:10 +01:00
|
|
|
|
2022-12-16 01:08:07 +01:00
|
|
|
export class ImportViewerLinks extends VariableUiElement {
|
|
|
|
constructor(osmConnection: OsmConnection) {
|
|
|
|
super(
|
|
|
|
osmConnection.userDetails.map((ud) => {
|
|
|
|
if (ud.csCount < Constants.userJourney.importHelperUnlock) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return new Combine([
|
|
|
|
new SubtleButton(undefined, Translations.t.importHelper.title, {
|
|
|
|
url: "import_helper.html",
|
|
|
|
}),
|
|
|
|
new SubtleButton(Svg.note_svg(), Translations.t.importInspector.title, {
|
|
|
|
url: "import_viewer.html",
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 03:46:53 +01:00
|
|
|
|
2023-01-12 01:17:07 +01:00
|
|
|
class SingleUserSettingsPanel extends EditableTagRendering {
|
2023-01-13 02:48:48 +01:00
|
|
|
constructor(
|
|
|
|
config: TagRenderingConfig,
|
|
|
|
osmConnection: OsmConnection,
|
|
|
|
amendedPrefs: UIEventSource<any>,
|
|
|
|
userInfoFocusedQuestion?: UIEventSource<string>
|
|
|
|
) {
|
2023-01-12 01:17:07 +01:00
|
|
|
const editMode = new UIEventSource(false)
|
2023-01-13 02:48:48 +01:00
|
|
|
// Isolate the preferences. THey'll be updated explicitely later on anyway
|
2023-01-12 01:17:07 +01:00
|
|
|
super(
|
2023-01-13 02:48:48 +01:00
|
|
|
amendedPrefs,
|
2023-01-12 01:17:07 +01:00
|
|
|
config,
|
|
|
|
[],
|
|
|
|
{ osmConnection },
|
|
|
|
{
|
2023-01-13 02:48:48 +01:00
|
|
|
answerElementClasses: "p-2",
|
2023-01-12 01:17:07 +01:00
|
|
|
editMode,
|
|
|
|
createSaveButton: (store) =>
|
2023-01-13 02:48:48 +01:00
|
|
|
new SaveButton(amendedPrefs, osmConnection).onClick(() => {
|
2023-01-12 01:17:07 +01:00
|
|
|
const selection = TagUtils.FlattenMultiAnswer(
|
2023-01-13 02:48:48 +01:00
|
|
|
TagUtils.FlattenAnd(store.data, amendedPrefs.data)
|
|
|
|
).asChange(amendedPrefs.data)
|
2023-01-12 01:17:07 +01:00
|
|
|
for (const kv of selection) {
|
|
|
|
osmConnection.GetPreference(kv.k, "", "").setData(kv.v)
|
|
|
|
}
|
|
|
|
|
|
|
|
editMode.setData(false)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
)
|
2023-01-13 02:48:48 +01:00
|
|
|
const self = this
|
|
|
|
this.SetClass("rounded-xl")
|
|
|
|
userInfoFocusedQuestion.addCallbackAndRun((selected) => {
|
|
|
|
if (config.id !== selected) {
|
|
|
|
self.RemoveClass("glowing-shadow")
|
|
|
|
} else {
|
|
|
|
self.SetClass("glowing-shadow")
|
|
|
|
}
|
|
|
|
})
|
2023-01-12 01:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 03:53:58 +01:00
|
|
|
class UserInformationMainPanel extends VariableUiElement {
|
2023-01-13 14:31:28 +01:00
|
|
|
private readonly settings: UIEventSource<Record<string, BaseUIElement>>
|
|
|
|
private readonly userInfoFocusedQuestion?: UIEventSource<string>
|
|
|
|
|
2022-12-16 13:45:07 +01:00
|
|
|
constructor(
|
|
|
|
osmConnection: OsmConnection,
|
|
|
|
locationControl: UIEventSource<Loc>,
|
2022-12-21 01:39:16 +01:00
|
|
|
layout: LayoutConfig,
|
2023-01-13 02:48:48 +01:00
|
|
|
isOpened: UIEventSource<boolean>,
|
|
|
|
userInfoFocusedQuestion?: UIEventSource<string>
|
2022-12-16 13:45:07 +01:00
|
|
|
) {
|
|
|
|
const t = Translations.t.userinfo
|
2022-12-13 03:46:53 +01:00
|
|
|
const imgSize = "h-6 w-6"
|
2022-12-16 13:45:07 +01:00
|
|
|
const ud = osmConnection.userDetails
|
2023-01-13 14:31:28 +01:00
|
|
|
const settings = new UIEventSource<Record<string, BaseUIElement>>({})
|
2023-01-14 04:19:10 +01:00
|
|
|
const usersettingsConfig = new LayerConfig(usersettings, "userinformationpanel")
|
2023-01-13 02:48:48 +01:00
|
|
|
|
|
|
|
const amendedPrefs = new UIEventSource<any>({})
|
|
|
|
osmConnection.preferencesHandler.preferences.addCallback((newPrefs) => {
|
|
|
|
for (const k in newPrefs) {
|
|
|
|
amendedPrefs.data[k] = newPrefs[k]
|
|
|
|
}
|
|
|
|
amendedPrefs.ping()
|
|
|
|
})
|
|
|
|
osmConnection.userDetails.addCallback((userDetails) => {
|
|
|
|
for (const k in userDetails) {
|
|
|
|
amendedPrefs.data["_" + k] = "" + userDetails[k]
|
|
|
|
}
|
2023-01-14 04:19:10 +01:00
|
|
|
|
|
|
|
for (const [name, code, _] of usersettingsConfig.calculatedTags) {
|
|
|
|
try {
|
|
|
|
let result = new Function("feat", "return " + code + ";")({
|
|
|
|
properties: amendedPrefs.data,
|
|
|
|
})
|
|
|
|
if (result !== undefined && result !== "" && result !== null) {
|
|
|
|
if (typeof result !== "string") {
|
|
|
|
result = JSON.stringify(result)
|
|
|
|
}
|
|
|
|
amendedPrefs.data[name] = result
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(
|
|
|
|
"Calculating a tag for userprofile-settings failed for variable",
|
|
|
|
name,
|
|
|
|
e
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 02:48:48 +01:00
|
|
|
const simplifiedName = userDetails.name.toLowerCase().replace(/\s+/g, "")
|
2023-01-14 04:19:10 +01:00
|
|
|
const isTranslator = translators.contributors.find(
|
2023-01-13 02:48:48 +01:00
|
|
|
(c: { contributor: string; commits: number }) => {
|
|
|
|
const replaced = c.contributor.toLowerCase().replace(/\s+/g, "")
|
|
|
|
return replaced === simplifiedName
|
|
|
|
}
|
|
|
|
)
|
2023-01-29 17:46:44 +01:00
|
|
|
if (isTranslator) {
|
2023-01-15 22:30:33 +01:00
|
|
|
amendedPrefs.data["_translation_contributions"] = "" + isTranslator.commits
|
|
|
|
}
|
2023-01-14 04:19:10 +01:00
|
|
|
const isCodeContributor = codeContributors.contributors.find(
|
|
|
|
(c: { contributor: string; commits: number }) => {
|
|
|
|
const replaced = c.contributor.toLowerCase().replace(/\s+/g, "")
|
|
|
|
return replaced === simplifiedName
|
|
|
|
}
|
|
|
|
)
|
2023-01-29 17:46:44 +01:00
|
|
|
if (isCodeContributor) {
|
2023-01-15 22:30:33 +01:00
|
|
|
amendedPrefs.data["_code_contributions"] = "" + isCodeContributor.commits
|
|
|
|
}
|
2023-01-13 02:48:48 +01:00
|
|
|
amendedPrefs.ping()
|
|
|
|
})
|
|
|
|
|
2023-01-11 03:53:58 +01:00
|
|
|
super(
|
|
|
|
ud.map((ud) => {
|
2023-01-11 04:27:42 +01:00
|
|
|
let img: BaseUIElement = Svg.person_ui().SetClass("block")
|
2023-01-11 03:53:58 +01:00
|
|
|
if (ud.img !== undefined) {
|
|
|
|
img = new Img(ud.img)
|
|
|
|
}
|
|
|
|
img.SetClass("rounded-full h-12 w-12 m-4")
|
2022-12-13 03:46:53 +01:00
|
|
|
|
2023-01-11 03:53:58 +01:00
|
|
|
let description: BaseUIElement = undefined
|
2023-01-11 04:27:42 +01:00
|
|
|
const editLink = osmConnection.Backend() + "/profile/edit"
|
2023-01-11 03:53:58 +01:00
|
|
|
if (ud.description) {
|
|
|
|
const editButton = new Link(
|
|
|
|
Svg.pencil_svg().SetClass("h-4 w-4"),
|
2023-01-11 04:27:42 +01:00
|
|
|
editLink,
|
2023-01-11 03:53:58 +01:00
|
|
|
true
|
|
|
|
).SetClass(
|
|
|
|
"absolute block bg-subtle rounded-full p-2 bottom-2 right-2 w-min self-end"
|
|
|
|
)
|
2022-12-13 03:46:53 +01:00
|
|
|
|
2023-01-11 04:27:42 +01:00
|
|
|
const htmlString = new Showdown.Converter()
|
|
|
|
.makeHtml(ud.description)
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/</g, "<")
|
2023-01-11 03:53:58 +01:00
|
|
|
description = new Combine([
|
2023-01-11 04:27:42 +01:00
|
|
|
new FixedUiElement(htmlString).SetClass("link-underline"),
|
2023-01-11 03:53:58 +01:00
|
|
|
editButton,
|
|
|
|
]).SetClass("relative w-full m-2")
|
|
|
|
} else {
|
|
|
|
description = new Combine([
|
|
|
|
t.noDescription,
|
|
|
|
new SubtleButton(Svg.pencil_svg(), t.noDescriptionCallToAction, {
|
2022-12-16 13:45:07 +01:00
|
|
|
imgSize,
|
2023-01-11 04:27:42 +01:00
|
|
|
url: editLink,
|
|
|
|
newTab: true,
|
2023-01-11 03:53:58 +01:00
|
|
|
}),
|
|
|
|
]).SetClass("w-full m-2")
|
|
|
|
}
|
2022-12-13 03:46:53 +01:00
|
|
|
|
2023-01-11 03:53:58 +01:00
|
|
|
let panToHome: BaseUIElement
|
|
|
|
if (ud.home) {
|
|
|
|
panToHome = new SubtleButton(Svg.home_svg(), t.moveToHome, {
|
|
|
|
imgSize,
|
|
|
|
}).onClick(() => {
|
|
|
|
const home = ud?.home
|
|
|
|
if (home === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
locationControl.setData({ ...home, zoom: 16 })
|
|
|
|
isOpened.setData(false)
|
|
|
|
})
|
|
|
|
}
|
2022-12-13 03:46:53 +01:00
|
|
|
|
2023-01-13 14:31:28 +01:00
|
|
|
const settingElements = []
|
|
|
|
for (const c of usersettingsConfig.tagRenderings) {
|
|
|
|
const settingsPanel = new SingleUserSettingsPanel(
|
2023-01-13 02:48:48 +01:00
|
|
|
c,
|
|
|
|
osmConnection,
|
|
|
|
amendedPrefs,
|
|
|
|
userInfoFocusedQuestion
|
|
|
|
).SetClass("block my-4")
|
2023-01-13 14:31:28 +01:00
|
|
|
settings.data[c.id] = settingsPanel
|
|
|
|
settingElements.push(settingsPanel)
|
|
|
|
}
|
|
|
|
settings.ping()
|
2023-01-11 03:53:58 +01:00
|
|
|
|
|
|
|
return new Combine([
|
|
|
|
new Combine([img, description]).SetClass("flex border border-black rounded-md"),
|
|
|
|
new LanguagePicker(
|
|
|
|
layout.language,
|
|
|
|
Translations.t.general.pickLanguage.Clone()
|
|
|
|
),
|
2023-01-13 14:31:28 +01:00
|
|
|
...settingElements,
|
2023-01-11 03:53:58 +01:00
|
|
|
new SubtleButton(
|
|
|
|
Svg.envelope_svg(),
|
|
|
|
new Combine([
|
|
|
|
t.gotoInbox,
|
|
|
|
ud.unreadMessages == 0
|
|
|
|
? undefined
|
|
|
|
: t.newMessages.SetClass("alert block"),
|
|
|
|
]),
|
|
|
|
{ imgSize, url: `${ud.backend}/messages/inbox`, newTab: true }
|
|
|
|
),
|
|
|
|
new SubtleButton(Svg.gear_svg(), t.gotoSettings, {
|
|
|
|
imgSize,
|
|
|
|
url: `${ud.backend}/user/${encodeURIComponent(ud.name)}/account`,
|
|
|
|
newTab: true,
|
|
|
|
}),
|
|
|
|
panToHome,
|
|
|
|
new ImportViewerLinks(osmConnection),
|
|
|
|
new SubtleButton(Svg.logout_svg(), Translations.t.general.logout, {
|
|
|
|
imgSize,
|
|
|
|
}).onClick(() => {
|
|
|
|
osmConnection.LogOut()
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
)
|
|
|
|
this.SetClass("flex flex-col")
|
2023-01-13 14:31:28 +01:00
|
|
|
this.settings = settings
|
|
|
|
this.userInfoFocusedQuestion = userInfoFocusedQuestion
|
|
|
|
const self = this
|
|
|
|
userInfoFocusedQuestion.addCallbackD((_) => {
|
|
|
|
self.focusOnSelectedQuestion()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
public focusOnSelectedQuestion() {
|
|
|
|
const focusedId = this.userInfoFocusedQuestion.data
|
|
|
|
console.log("Focusing on", focusedId, this.settings.data[focusedId])
|
|
|
|
if (focusedId === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.settings.data[focusedId]?.ScrollIntoView()
|
2022-12-13 03:46:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class UserInformationPanel extends ScrollableFullScreen {
|
2023-01-13 14:31:28 +01:00
|
|
|
private readonly userPanel: UserInformationMainPanel
|
2023-01-14 04:19:10 +01:00
|
|
|
|
2023-01-03 02:24:03 +01:00
|
|
|
constructor(
|
|
|
|
state: {
|
2023-01-11 04:27:42 +01:00
|
|
|
readonly layoutToUse: LayoutConfig
|
|
|
|
readonly osmConnection: OsmConnection
|
|
|
|
readonly locationControl: UIEventSource<Loc>
|
|
|
|
readonly featureSwitchUserbadge: Store<boolean>
|
2023-01-03 02:24:03 +01:00
|
|
|
},
|
|
|
|
options?: {
|
|
|
|
isOpened?: UIEventSource<boolean>
|
2023-01-13 02:48:48 +01:00
|
|
|
userInfoFocusedQuestion?: UIEventSource<string>
|
2023-01-03 02:24:03 +01:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
const isOpened = options?.isOpened ?? new UIEventSource<boolean>(false)
|
2023-01-13 14:31:28 +01:00
|
|
|
const userPanel = new UserInformationMainPanel(
|
|
|
|
state.osmConnection,
|
|
|
|
state.locationControl,
|
|
|
|
state.layoutToUse,
|
|
|
|
isOpened,
|
|
|
|
options?.userInfoFocusedQuestion
|
|
|
|
)
|
2022-12-13 03:46:53 +01:00
|
|
|
super(
|
|
|
|
() => {
|
2022-12-16 13:45:07 +01:00
|
|
|
return new VariableUiElement(
|
2023-01-11 04:27:42 +01:00
|
|
|
state.osmConnection.userDetails.map((ud) => {
|
|
|
|
if (ud.loggedIn === false) {
|
|
|
|
return Translations.t.userinfo.titleNotLoggedIn
|
|
|
|
}
|
|
|
|
return Translations.t.userinfo.welcome.Subs(ud)
|
|
|
|
})
|
2022-12-16 13:45:07 +01:00
|
|
|
)
|
2022-12-13 03:46:53 +01:00
|
|
|
},
|
2023-01-13 14:31:28 +01:00
|
|
|
() => new LoginToggle(userPanel, Translations.t.general.getStartedLogin, state),
|
2022-12-21 01:39:16 +01:00
|
|
|
"userinfo",
|
|
|
|
isOpened
|
2022-12-16 13:45:07 +01:00
|
|
|
)
|
2023-01-13 14:31:28 +01:00
|
|
|
this.userPanel = userPanel
|
|
|
|
}
|
|
|
|
|
|
|
|
Activate() {
|
|
|
|
super.Activate()
|
2023-01-14 04:19:10 +01:00
|
|
|
this.userPanel?.focusOnSelectedQuestion()
|
2022-12-13 03:46:53 +01:00
|
|
|
}
|
|
|
|
}
|