| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Handles and updates the user badge | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-01-04 04:06:21 +01:00
										 |  |  | import {VariableUiElement} from "../Base/VariableUIElement"; | 
					
						
							|  |  |  | 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"; | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | import Toggle from "../Input/Toggle"; | 
					
						
							|  |  |  | import Img from "../Base/Img"; | 
					
						
							| 
									
										
										
										
											2021-01-04 04:06:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | export default class UserBadge extends Toggle { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2020-11-05 12:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const userDetails = State.state.osmConnection.userDetails; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const loginButton = Translations.t.general.loginWithOpenStreetMap | 
					
						
							| 
									
										
										
										
											2020-08-27 18:44:16 +02:00
										 |  |  |             .Clone() | 
					
						
							| 
									
										
										
										
											2021-01-20 21:48:47 +01:00
										 |  |  |             .SetClass("userbadge-login pt-3 w-full") | 
					
						
							| 
									
										
										
										
											2020-08-27 18:44:16 +02:00
										 |  |  |             .onClick(() => State.state.osmConnection.AttemptLogin()); | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const logout = | 
					
						
							| 
									
										
										
										
											2020-11-14 02:54:33 +01:00
										 |  |  |             Svg.logout_svg() | 
					
						
							| 
									
										
										
										
											2020-11-05 12:28:02 +01:00
										 |  |  |                 .onClick(() => { | 
					
						
							|  |  |  |                     State.state.osmConnection.LogOut(); | 
					
						
							|  |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 17:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  |         const userBadge = userDetails.map(user => { | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 const homeButton = new VariableUiElement( | 
					
						
							|  |  |  |                     userDetails.map((userinfo) => { | 
					
						
							|  |  |  |                         if (userinfo.home) { | 
					
						
							|  |  |  |                             return Svg.home_ui(); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         return " "; | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                 ).onClick(() => { | 
					
						
							|  |  |  |                     const home = State.state.osmConnection.userDetails.data?.home; | 
					
						
							|  |  |  |                     if (home === undefined) { | 
					
						
							|  |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     State.state.leafletMap.data.setView([home.lat, home.lon], 16); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const linkStyle = "flex items-baseline" | 
					
						
							|  |  |  |                 const languagePicker = (LanguagePicker.CreateLanguagePicker(State.state.layoutToUse.data.language) ?? new FixedUiElement("")) | 
					
						
							|  |  |  |                     .SetStyle("width:min-content;"); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  |                 let messageSpan = | 
					
						
							|  |  |  |                     new Link( | 
					
						
							|  |  |  |                         new Combine([Svg.envelope, "" + user.totalMessages]).SetClass(linkStyle), | 
					
						
							|  |  |  |                         'https://www.openstreetmap.org/messages/inbox', | 
					
						
							|  |  |  |                         true | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const csCount = | 
					
						
							|  |  |  |                     new Link( | 
					
						
							|  |  |  |                         new Combine([Svg.star, "" + user.csCount]).SetClass(linkStyle), | 
					
						
							|  |  |  |                         `https://www.openstreetmap.org/user/${user.name}/history`, | 
					
						
							|  |  |  |                         true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (user.unreadMessages > 0) { | 
					
						
							|  |  |  |                     messageSpan = new Link( | 
					
						
							|  |  |  |                         new Combine([Svg.envelope, "" + user.unreadMessages]), | 
					
						
							|  |  |  |                         'https://www.openstreetmap.org/messages/inbox', | 
					
						
							|  |  |  |                         true | 
					
						
							|  |  |  |                     ).SetClass("alert") | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 let dryrun = new FixedUiElement(""); | 
					
						
							|  |  |  |                 if (user.dryRun) { | 
					
						
							|  |  |  |                     dryrun = new FixedUiElement("TESTING").SetClass("alert"); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const settings = | 
					
						
							|  |  |  |                     new Link(Svg.gear_svg(), | 
					
						
							|  |  |  |                         `https://www.openstreetmap.org/user/${encodeURIComponent(user.name)}/account`, | 
					
						
							|  |  |  |                         true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const userIcon = new Link( | 
					
						
							|  |  |  |                     new Img(user.img) | 
					
						
							|  |  |  |                         .SetClass("rounded-full opacity-0 m-0 p-0 duration-500 w-16 h16 float-left") | 
					
						
							|  |  |  |                     , | 
					
						
							|  |  |  |                     `https://www.openstreetmap.org/user/${encodeURIComponent(user.name)}`, | 
					
						
							|  |  |  |                     true | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const userName = new Link( | 
					
						
							|  |  |  |                     new FixedUiElement(user.name), | 
					
						
							|  |  |  |                     `https://www.openstreetmap.org/user/${user.name}`, | 
					
						
							|  |  |  |                     true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const userStats = new Combine([ | 
					
						
							|  |  |  |                     homeButton, | 
					
						
							|  |  |  |                     settings, | 
					
						
							|  |  |  |                     messageSpan, | 
					
						
							|  |  |  |                     csCount, | 
					
						
							|  |  |  |                     languagePicker, | 
					
						
							|  |  |  |                     logout | 
					
						
							|  |  |  |                 ]) | 
					
						
							|  |  |  |                     .SetClass("userstats") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const usertext = new Combine([ | 
					
						
							|  |  |  |                     userName, | 
					
						
							|  |  |  |                     dryrun, | 
					
						
							|  |  |  |                     userStats | 
					
						
							|  |  |  |                 ]).SetClass("usertext") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return new Combine([ | 
					
						
							|  |  |  |                     userIcon, | 
					
						
							|  |  |  |                     usertext, | 
					
						
							|  |  |  |                 ]).SetClass("h-16") | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-12 02:58:32 +02:00
										 |  |  |         super( | 
					
						
							|  |  |  |             new VariableUiElement(userBadge), | 
					
						
							|  |  |  |             loginButton, | 
					
						
							|  |  |  |             State.state.osmConnection.isLoggedIn | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-07-23 16:00:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |