| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | import {UIElement} from "./UIElement"; | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  | import L from "leaflet"; | 
					
						
							| 
									
										
										
										
											2020-07-01 17:38:48 +02:00
										 |  |  | import {FixedUiElement} from "./Base/FixedUiElement"; | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  | import {VariableUiElement} from "./Base/VariableUIElement"; | 
					
						
							| 
									
										
										
										
											2020-07-21 00:07:04 +02:00
										 |  |  | import Translations from "./i18n/Translations"; | 
					
						
							| 
									
										
										
										
											2020-07-30 00:59:08 +02:00
										 |  |  | import {UserDetails} from "../Logic/Osm/OsmConnection"; | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  | import {State} from "../State"; | 
					
						
							| 
									
										
										
										
											2020-08-17 17:23:15 +02:00
										 |  |  | import {UIEventSource} from "../Logic/UIEventSource"; | 
					
						
							| 
									
										
										
										
											2020-08-31 02:59:47 +02:00
										 |  |  | import {InitUiElements} from "../InitUiElements"; | 
					
						
							| 
									
										
										
										
											2020-09-05 15:27:35 +02:00
										 |  |  | import Combine from "./Base/Combine"; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Handles and updates the user badge | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class UserBadge extends UIElement { | 
					
						
							|  |  |  |     private _userDetails: UIEventSource<UserDetails>; | 
					
						
							| 
									
										
										
										
											2020-07-01 17:38:48 +02:00
										 |  |  |     private _logout: UIElement; | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |     private _homeButton: UIElement; | 
					
						
							| 
									
										
										
										
											2020-07-21 00:07:04 +02:00
										 |  |  |     private _languagePicker: UIElement; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 18:38:14 +02:00
										 |  |  |     private _loginButton : UIElement; | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |     constructor() { | 
					
						
							|  |  |  |         super(State.state.osmConnection.userDetails); | 
					
						
							|  |  |  |         this._userDetails = State.state.osmConnection.userDetails; | 
					
						
							| 
									
										
										
										
											2020-09-05 23:44:33 +02:00
										 |  |  |         this._languagePicker = InitUiElements.CreateLanguagePicker() ?? new FixedUiElement(""); | 
					
						
							| 
									
										
										
										
											2020-08-27 18:44:16 +02:00
										 |  |  |         this._loginButton = Translations.t.general.loginWithOpenStreetMap | 
					
						
							|  |  |  |             .Clone() | 
					
						
							|  |  |  |             .SetClass("userbadge-login") | 
					
						
							|  |  |  |             .onClick(() => State.state.osmConnection.AttemptLogin()); | 
					
						
							| 
									
										
										
										
											2020-07-01 17:51:55 +02:00
										 |  |  |         this._logout = new FixedUiElement("<img src='assets/logout.svg' class='small-userbadge-icon' alt='logout'>") | 
					
						
							|  |  |  |             .onClick(() => { | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |                 State.state.osmConnection.LogOut(); | 
					
						
							| 
									
										
										
										
											2020-07-01 17:51:55 +02:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |         this._userDetails.addCallback(function () { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |             const profilePic = document.getElementById("profile-pic"); | 
					
						
							| 
									
										
										
										
											2020-07-01 17:51:55 +02:00
										 |  |  |             if (profilePic) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 profilePic.onload = function () { | 
					
						
							|  |  |  |                     profilePic.style.opacity = "1" | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |         this._homeButton = new VariableUiElement( | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |             this._userDetails.map((userinfo) => { | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |                 if (userinfo.home) { | 
					
						
							| 
									
										
										
										
											2020-09-03 19:05:18 +02:00
										 |  |  |                     return "<img src='./assets/home.svg' alt='home' class='small-userbadge-icon'> "; | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 return ""; | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         ).onClick(() => { | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |             const home = State.state.osmConnection.userDetails.data?.home; | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |             if (home === undefined) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |             State.state.bm.map.flyTo([home.lat, home.lon], 18); | 
					
						
							| 
									
										
										
										
											2020-07-20 09:57:19 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 18:24:00 +02:00
										 |  |  |     InnerRender(): string { | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         const user = this._userDetails.data; | 
					
						
							|  |  |  |         if (!user.loggedIn) { | 
					
						
							| 
									
										
										
										
											2020-07-31 18:38:14 +02:00
										 |  |  |             return this._loginButton.Render(); | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |          | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         let messageSpan = "<span id='messages'>" + | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |             "     <a href='https://www.openstreetmap.org/messages/inbox' target='_blank'><img class='small-userbadge-icon' src='./assets/envelope.svg' alt='msgs'>" + | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |             user.totalMessages + | 
					
						
							|  |  |  |             "</a></span>"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (user.unreadMessages > 0) { | 
					
						
							|  |  |  |             messageSpan = "<span id='messages' class='alert'>" + | 
					
						
							| 
									
										
										
										
											2020-07-23 16:28:19 +02:00
										 |  |  |                 "<a href='https://www.openstreetmap.org/messages/inbox' target='_blank'><img class='small-userbadge-icon' src='./assets/envelope.svg' alt='msgs'/>" + | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |                 user.unreadMessages.toString() + | 
					
						
							|  |  |  |                 "</a></span>"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let dryrun = ""; | 
					
						
							|  |  |  |         if (user.dryRun) { | 
					
						
							|  |  |  |             dryrun = " <span class='alert'>TESTING</span>"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         if (user.home !== undefined) { | 
					
						
							|  |  |  |             const icon = L.icon({ | 
					
						
							|  |  |  |                 iconUrl: 'assets/home.svg', | 
					
						
							|  |  |  |                 iconSize: [20, 20], | 
					
						
							|  |  |  |                 iconAnchor: [10, 10] | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2020-07-31 01:45:54 +02:00
										 |  |  |             L.marker([user.home.lat, user.home.lon], {icon: icon}).addTo(State.state.bm.map); | 
					
						
							| 
									
										
										
										
											2020-06-29 03:12:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 03:40:19 +02:00
										 |  |  |         const settings = | 
					
						
							|  |  |  |             "<a href='https://www.openstreetmap.org/user/" + encodeURIComponent(user.name) + "/account' target='_blank'>" + | 
					
						
							|  |  |  |             "<img class='small-userbadge-icon' src='./assets/gear.svg' alt='settings'>" + | 
					
						
							|  |  |  |             "</a> "; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 16:00:49 +02:00
										 |  |  |         const userIcon = "<a href='https://www.openstreetmap.org/user/" + encodeURIComponent(user.name) + "' target='_blank'><img id='profile-pic' src='" + user.img + "' alt='profile-pic'/></a>"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const userName = "<p id='username'>" + | 
					
						
							| 
									
										
										
										
											2020-06-27 03:06:51 +02:00
										 |  |  |             "<a href='https://www.openstreetmap.org/user/" + user.name + "' target='_blank'>" + user.name + "</a>" + | 
					
						
							| 
									
										
										
										
											2020-07-23 16:00:49 +02:00
										 |  |  |             dryrun + "</p>"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const csCount = "<span id='csCount'> " + | 
					
						
							|  |  |  |             "   <a href='https://www.openstreetmap.org/user/" + user.name + "/history' target='_blank'><img class='small-userbadge-icon' src='./assets/star.svg' alt='star'/> " + user.csCount + | 
					
						
							|  |  |  |             "</a></span> "; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-05 15:27:35 +02:00
										 |  |  |         const userStats = new Combine(["<div id='userstats'>", | 
					
						
							|  |  |  |             this._homeButton, | 
					
						
							|  |  |  |             settings, | 
					
						
							|  |  |  |             messageSpan, | 
					
						
							|  |  |  |             csCount, | 
					
						
							|  |  |  |             this._logout, | 
					
						
							|  |  |  |             this._languagePicker, | 
					
						
							|  |  |  |             "</div>"]).Render(); | 
					
						
							| 
									
										
										
										
											2020-07-23 16:00:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return userIcon + "<div id='usertext'>" + userName + userStats + "</div>"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 00:35:19 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |