| 
									
										
										
										
											2021-01-03 00:19:42 +01:00
										 |  |  | import {UIEventSource} from "../UIEventSource"; | 
					
						
							|  |  |  | import {OsmConnection} from "../Osm/OsmConnection"; | 
					
						
							| 
									
										
										
										
											2021-04-11 23:43:23 +02:00
										 |  |  | import {Utils} from "../../Utils"; | 
					
						
							|  |  |  | import LZString from "lz-string"; | 
					
						
							| 
									
										
										
										
											2021-08-07 23:11:34 +02:00
										 |  |  | import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"; | 
					
						
							| 
									
										
										
										
											2020-12-18 12:10:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class InstalledThemes { | 
					
						
							| 
									
										
										
										
											2021-01-03 00:19:42 +01:00
										 |  |  |     public installedThemes: UIEventSource<{ layout: LayoutConfig; definition: string }[]>; | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-03 00:19:42 +01:00
										 |  |  |     constructor(osmConnection: OsmConnection) { | 
					
						
							|  |  |  |         this.installedThemes = osmConnection.preferencesHandler.preferences.map<{ layout: LayoutConfig, definition: string }[]>(allPreferences => { | 
					
						
							| 
									
										
										
										
											2020-12-18 12:10:43 +01:00
										 |  |  |             const installedThemes: { layout: LayoutConfig, definition: string }[] = []; | 
					
						
							|  |  |  |             if (allPreferences === undefined) { | 
					
						
							|  |  |  |                 console.log("All prefs is undefined"); | 
					
						
							|  |  |  |                 return installedThemes; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const invalidThemes = [] | 
					
						
							| 
									
										
										
										
											2021-02-20 01:45:51 +01:00
										 |  |  |             for (const allPreferencesKey in allPreferences) { | 
					
						
							| 
									
										
										
										
											2020-12-18 12:10:43 +01:00
										 |  |  |                 const themename = allPreferencesKey.match(/^mapcomplete-installed-theme-(.*)-combined-length$/); | 
					
						
							|  |  |  |                 if (themename && themename[1] !== "") { | 
					
						
							|  |  |  |                     const customLayout = osmConnection.GetLongPreference("installed-theme-" + themename[1]); | 
					
						
							|  |  |  |                     if (customLayout.data === undefined) { | 
					
						
							|  |  |  |                         console.log("No data defined for ", themename[1]); | 
					
						
							|  |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     try { | 
					
						
							| 
									
										
										
										
											2021-04-11 23:43:23 +02:00
										 |  |  |                         let layoutJson; | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                         try { | 
					
						
							| 
									
										
										
										
											2021-04-11 23:43:23 +02:00
										 |  |  |                             layoutJson = JSON.parse(atob(customLayout.data)) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                         } catch (e) { | 
					
						
							|  |  |  |                             layoutJson = JSON.parse(Utils.UnMinify(LZString.decompressFromBase64(customLayout.data))) | 
					
						
							| 
									
										
										
										
											2021-04-11 23:43:23 +02:00
										 |  |  |                         } | 
					
						
							|  |  |  |                         const layout = new LayoutConfig(layoutJson, false); | 
					
						
							| 
									
										
										
										
											2020-12-18 12:10:43 +01:00
										 |  |  |                         installedThemes.push({ | 
					
						
							|  |  |  |                             layout: layout, | 
					
						
							|  |  |  |                             definition: customLayout.data | 
					
						
							|  |  |  |                         }); | 
					
						
							|  |  |  |                     } catch (e) { | 
					
						
							|  |  |  |                         console.warn("Could not parse custom layout from preferences - deleting: ", allPreferencesKey, e, customLayout.data); | 
					
						
							|  |  |  |                         invalidThemes.push(themename[1]) | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             InstalledThemes.DeleteInvalid(osmConnection, invalidThemes); | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 12:10:43 +01:00
										 |  |  |             return installedThemes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-02 23:28:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private static DeleteInvalid(osmConnection: OsmConnection, invalidThemes: any[]) { | 
					
						
							|  |  |  |         for (const invalid of invalidThemes) { | 
					
						
							|  |  |  |             console.error("Attempting to remove ", invalid) | 
					
						
							|  |  |  |             osmConnection.GetLongPreference( | 
					
						
							|  |  |  |                 "installed-theme-" + invalid | 
					
						
							|  |  |  |             ).setData(null); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 12:10:43 +01:00
										 |  |  | } |