| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig" | 
					
						
							|  |  |  | import { OsmConnection } from "../Osm/OsmConnection" | 
					
						
							|  |  |  | import { MangroveIdentity } from "../Web/MangroveReviews" | 
					
						
							|  |  |  | import { Store, UIEventSource } from "../UIEventSource" | 
					
						
							|  |  |  | import { QueryParameters } from "../Web/QueryParameters" | 
					
						
							|  |  |  | import { LocalStorageSource } from "../Web/LocalStorageSource" | 
					
						
							|  |  |  | import { Utils } from "../../Utils" | 
					
						
							|  |  |  | import Locale from "../../UI/i18n/Locale" | 
					
						
							|  |  |  | import ElementsState from "./ElementsState" | 
					
						
							|  |  |  | import SelectedElementTagsUpdater from "../Actors/SelectedElementTagsUpdater" | 
					
						
							|  |  |  | import { Changes } from "../Osm/Changes" | 
					
						
							|  |  |  | import ChangeToElementsActor from "../Actors/ChangeToElementsActor" | 
					
						
							|  |  |  | import PendingChangesUploader from "../Actors/PendingChangesUploader" | 
					
						
							| 
									
										
										
										
											2022-04-01 12:51:55 +02:00
										 |  |  | import * as translators from "../../assets/translators.json" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import Maproulette from "../Maproulette" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * The part of the state which keeps track of user-related stuff, e.g. the OSM-connection, | 
					
						
							|  |  |  |  * which layers they enabled, ... | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export default class UserRelatedState extends ElementsState { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      The user credentials | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public osmConnection: OsmConnection | 
					
						
							| 
									
										
										
										
											2022-02-16 01:46:55 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      THe change handler | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public changes: Changes | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * The key for mangrove | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public mangroveIdentity: MangroveIdentity | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Maproulette connection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     public maprouletteConnection: Maproulette | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public readonly isTranslator: Store<boolean> | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-22 20:18:17 +02:00
										 |  |  |     public readonly installedUserThemes: Store<string[]> | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |     constructor(layoutToUse: LayoutConfig, options?: { attemptLogin: true | boolean }) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super(layoutToUse) | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.osmConnection = new OsmConnection({ | 
					
						
							| 
									
										
										
										
											2022-01-21 01:57:16 +01:00
										 |  |  |             dryRun: this.featureSwitchIsTesting, | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |             fakeUser: this.featureSwitchFakeUser.data, | 
					
						
							|  |  |  |             oauth_token: QueryParameters.GetQueryParameter( | 
					
						
							|  |  |  |                 "oauth_token", | 
					
						
							|  |  |  |                 undefined, | 
					
						
							|  |  |  |                 "Used to complete the login" | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             osmConfiguration: <"osm" | "osm-test">this.featureSwitchApiURL.data, | 
					
						
							|  |  |  |             attemptLogin: options?.attemptLogin, | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const translationMode = this.osmConnection.GetPreference("translation-mode").sync( | 
					
						
							|  |  |  |             (str) => (str === undefined ? undefined : str === "true"), | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             (b) => (b === undefined ? undefined : b + "") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-06 12:41:24 +02:00
										 |  |  |         translationMode.syncWith(Locale.showLinkToWeblate) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.isTranslator = this.osmConnection.userDetails.map((ud) => { | 
					
						
							|  |  |  |             if (!ud.loggedIn) { | 
					
						
							|  |  |  |                 return false | 
					
						
							| 
									
										
										
										
											2022-04-01 12:51:55 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const name = ud.name.toLowerCase().replace(/\s+/g, "") | 
					
						
							|  |  |  |             return translators.contributors.some( | 
					
						
							|  |  |  |                 (c) => c.contributor.toLowerCase().replace(/\s+/g, "") === name | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-04-01 12:51:55 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         this.isTranslator.addCallbackAndRunD((ud) => { | 
					
						
							|  |  |  |             if (ud) { | 
					
						
							| 
									
										
										
										
											2022-04-01 12:51:55 +02:00
										 |  |  |                 Locale.showLinkToWeblate.setData(true) | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-02-16 01:46:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.changes = new Changes(this, layoutToUse?.isLeftRightSensitive() ?? false) | 
					
						
							| 
									
										
										
										
											2022-02-16 01:46:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         new ChangeToElementsActor(this.changes, this.allElements) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         new PendingChangesUploader(this.changes, this.selectedElement) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         this.mangroveIdentity = new MangroveIdentity( | 
					
						
							|  |  |  |             this.osmConnection.GetLongPreference("identity", "mangrove") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.maprouletteConnection = new Maproulette() | 
					
						
							| 
									
										
										
										
											2022-07-13 08:03:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         if (layoutToUse?.hideFromOverview) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             this.osmConnection.isLoggedIn.addCallbackAndRunD((loggedIn) => { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 if (loggedIn) { | 
					
						
							| 
									
										
										
										
											2021-10-15 19:58:02 +02:00
										 |  |  |                     this.osmConnection | 
					
						
							|  |  |  |                         .GetPreference("hidden-theme-" + layoutToUse?.id + "-enabled") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         .setData("true") | 
					
						
							|  |  |  |                     return true | 
					
						
							| 
									
										
										
										
											2021-10-15 19:58:02 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:36:26 +01:00
										 |  |  |         if (this.layoutToUse !== undefined && !this.layoutToUse.official) { | 
					
						
							|  |  |  |             console.log("Marking unofficial theme as visited") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             this.osmConnection.GetLongPreference("unofficial-theme-" + this.layoutToUse.id).setData( | 
					
						
							|  |  |  |                 JSON.stringify({ | 
					
						
							| 
									
										
										
										
											2022-02-04 14:36:26 +01:00
										 |  |  |                     id: this.layoutToUse.id, | 
					
						
							|  |  |  |                     icon: this.layoutToUse.icon, | 
					
						
							|  |  |  |                     title: this.layoutToUse.title.translations, | 
					
						
							| 
									
										
										
										
											2022-02-14 15:41:14 +01:00
										 |  |  |                     shortDescription: this.layoutToUse.shortDescription.translations, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     definition: this.layoutToUse["definition"], | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-01-25 00:46:57 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-21 20:57:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.InitializeLanguage() | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |         new SelectedElementTagsUpdater(this) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         this.installedUserThemes = this.InitInstalledUserThemes() | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private InitializeLanguage() { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const layoutToUse = this.layoutToUse | 
					
						
							|  |  |  |         Locale.language.syncWith(this.osmConnection.GetPreference("language")) | 
					
						
							|  |  |  |         Locale.language.addCallback((currentLanguage) => { | 
					
						
							|  |  |  |             if (layoutToUse === undefined) { | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (Locale.showLinkToWeblate.data) { | 
					
						
							|  |  |  |                 return true // Disable auto switching as we are in translators mode
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (this.layoutToUse.language.indexOf(currentLanguage) < 0) { | 
					
						
							|  |  |  |                 console.log( | 
					
						
							|  |  |  |                     "Resetting language to", | 
					
						
							|  |  |  |                     layoutToUse.language[0], | 
					
						
							|  |  |  |                     "as", | 
					
						
							|  |  |  |                     currentLanguage, | 
					
						
							|  |  |  |                     " is unsupported" | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 // The current language is not supported -> switch to a supported one
 | 
					
						
							|  |  |  |                 Locale.language.setData(layoutToUse.language[0]) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         Locale.language.ping() | 
					
						
							| 
									
										
										
										
											2021-10-15 05:20:02 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private InitInstalledUserThemes(): Store<string[]> { | 
					
						
							|  |  |  |         const prefix = "mapcomplete-unofficial-theme-" | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |         const postfix = "-combined-length" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return this.osmConnection.preferencesHandler.preferences.map((prefs) => | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |             Object.keys(prefs) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 .filter((k) => k.startsWith(prefix) && k.endsWith(postfix)) | 
					
						
							|  |  |  |                 .map((k) => k.substring(prefix.length, k.length - postfix.length)) | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public GetUnofficialTheme(id: string): | 
					
						
							|  |  |  |         | { | 
					
						
							|  |  |  |               id: string | 
					
						
							|  |  |  |               icon: string | 
					
						
							|  |  |  |               title: any | 
					
						
							|  |  |  |               shortDescription: any | 
					
						
							|  |  |  |               definition?: any | 
					
						
							|  |  |  |               isOfficial: boolean | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         | undefined { | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |         console.log("GETTING UNOFFICIAL THEME") | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const pref = this.osmConnection.GetLongPreference("unofficial-theme-" + id) | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |         const str = pref.data | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |         if (str === undefined || str === "undefined" || str === "") { | 
					
						
							|  |  |  |             pref.setData(null) | 
					
						
							|  |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             const value: { | 
					
						
							|  |  |  |                 id: string | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 icon: string | 
					
						
							|  |  |  |                 title: any | 
					
						
							|  |  |  |                 shortDescription: any | 
					
						
							|  |  |  |                 definition?: any | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |                 isOfficial: boolean | 
					
						
							|  |  |  |             } = JSON.parse(str) | 
					
						
							|  |  |  |             value.isOfficial = false | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return value | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |         } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             console.warn( | 
					
						
							|  |  |  |                 "Removing theme " + | 
					
						
							|  |  |  |                     id + | 
					
						
							|  |  |  |                     " as it could not be parsed from the preferences; the content is:", | 
					
						
							|  |  |  |                 str | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-06-21 18:22:09 +02:00
										 |  |  |             pref.setData(null) | 
					
						
							|  |  |  |             return undefined | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |