| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  | import {UIElement} from "../UI/UIElement"; | 
					
						
							|  |  |  | import {State} from "../State"; | 
					
						
							|  |  |  | import Translations from "../UI/i18n/Translations"; | 
					
						
							| 
									
										
										
										
											2020-08-17 17:23:15 +02:00
										 |  |  | import {UIEventSource} from "./UIEventSource"; | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  | import {AllKnownLayouts} from "../Customizations/AllKnownLayouts"; | 
					
						
							|  |  |  | import Combine from "../UI/Base/Combine"; | 
					
						
							|  |  |  | import {CheckBox} from "../UI/Input/CheckBox"; | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  | import {PersonalLayout} from "./PersonalLayout"; | 
					
						
							| 
									
										
										
										
											2020-08-26 20:11:43 +02:00
										 |  |  | import {Layout} from "../Customizations/Layout"; | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  | import {SubtleButton} from "../UI/Base/SubtleButton"; | 
					
						
							|  |  |  | import {FixedUiElement} from "../UI/Base/FixedUiElement"; | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  | export class PersonalLayersPanel extends UIElement { | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |     private checkboxes: UIElement[] = []; | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |     constructor() { | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  |         super(State.state.favouriteLayers); | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |         this.ListenTo(State.state.osmConnection.userDetails); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 20:11:43 +02:00
										 |  |  |         this.UpdateView([]); | 
					
						
							|  |  |  |         const self = this; | 
					
						
							|  |  |  |         State.state.installedThemes.addCallback(extraThemes => { | 
					
						
							|  |  |  |             self.UpdateView(extraThemes.map(layout => layout.layout)); | 
					
						
							|  |  |  |             self.Update(); | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 20:11:43 +02:00
										 |  |  |     private UpdateView(extraThemes: Layout[]) { | 
					
						
							|  |  |  |         this.checkboxes = []; | 
					
						
							|  |  |  |         const favs = State.state.favouriteLayers.data ?? []; | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |         const controls = new Map<string, UIEventSource<boolean>>(); | 
					
						
							| 
									
										
										
										
											2020-08-26 20:11:43 +02:00
										 |  |  |         const allLayouts = AllKnownLayouts.layoutsList.concat(extraThemes); | 
					
						
							|  |  |  |         for (const layout of allLayouts) { | 
					
						
							| 
									
										
										
										
											2020-08-30 01:13:18 +02:00
										 |  |  |             if (layout.id === PersonalLayout.NAME) { | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-08-26 20:11:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |             const header = | 
					
						
							|  |  |  |                 new Combine([ | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                     `<img style="max-width: 3em;max-height: 3em; float: left; padding: 0.1em; margin-right: 0.3em;" src='${layout.icon}'>`, | 
					
						
							|  |  |  |                     "<b>", | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                     layout.title, | 
					
						
							|  |  |  |                     "</b><br/>", | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                     layout.description ?? "" | 
					
						
							|  |  |  |                 ]).SetStyle("background: #eee; display: block; padding: 0.5em; border-radius:0.5em; overflow:auto;") | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |             this.checkboxes.push(header); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const layer of layout.layers) { | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                 if (typeof layer === "string") { | 
					
						
							| 
									
										
										
										
											2020-09-10 19:33:06 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                 let icon = layer.icon ?? "./assets/checkmark.svg"; | 
					
						
							| 
									
										
										
										
											2020-09-18 22:23:49 +02:00
										 |  |  |                 let iconUnset = layer.icon ?? ""; | 
					
						
							|  |  |  |                 if (layer.icon !== undefined && typeof (layer.icon) !== "string") { | 
					
						
							|  |  |  |                     icon = layer.icon.GetContent({"id": "node/-123456"}).txt ?? "./assets/checkmark.svg"; | 
					
						
							|  |  |  |                     iconUnset = icon; | 
					
						
							| 
									
										
										
										
											2020-08-26 20:11:43 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 20:11:43 +02:00
										 |  |  |                 let name = layer.name ?? layer.id; | 
					
						
							|  |  |  |                 if (name === undefined) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                 const content = new Combine([ | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                     "<b>",  | 
					
						
							|  |  |  |                     name,  | 
					
						
							|  |  |  |                     "</b> ", | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |                     layer.description !== undefined ? new Combine(["<br/>", layer.description]) : "", | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                 ]) | 
					
						
							| 
									
										
										
										
											2020-09-18 22:23:49 +02:00
										 |  |  |                  | 
					
						
							|  |  |  |                 const iconImage = `<img src="${icon}">`; | 
					
						
							|  |  |  |                 const iconUnsetImage = `<img src="${iconUnset}">` | 
					
						
							|  |  |  |                  | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                 const cb = new CheckBox( | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                     new SubtleButton( | 
					
						
							| 
									
										
										
										
											2020-09-18 22:23:49 +02:00
										 |  |  |                         new FixedUiElement(iconImage).SetStyle(""),  | 
					
						
							|  |  |  |                         content), | 
					
						
							|  |  |  |                     new SubtleButton( | 
					
						
							|  |  |  |                         new FixedUiElement(iconUnsetImage).SetStyle("opacity:0.1;"), | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                         new Combine(["<del>", | 
					
						
							|  |  |  |                             content, | 
					
						
							|  |  |  |                             "</del>" | 
					
						
							|  |  |  |                         ])), | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                     controls[layer.id] ?? (favs.indexOf(layer.id) >= 0) | 
					
						
							|  |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2020-08-28 03:16:21 +02:00
										 |  |  |                 cb.SetClass("custom-layer-checkbox"); | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                 controls[layer.id] = cb.isEnabled; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 cb.isEnabled.addCallback((isEnabled) => { | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  |                     const favs = State.state.favouriteLayers; | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                     if (isEnabled) { | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |                         if(favs.data.indexOf(layer.id)>= 0){ | 
					
						
							|  |  |  |                             return; // Already added
 | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  |                         favs.data.push(layer.id); | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                     } else { | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  |                         favs.data.splice(favs.data.indexOf(layer.id), 1); | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2020-08-26 15:36:04 +02:00
										 |  |  |                     favs.ping(); | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |                 }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 this.checkboxes.push(cb); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         State.state.favouriteLayers.addCallback((layers) => { | 
					
						
							|  |  |  |             for (const layerId of layers) { | 
					
						
							|  |  |  |                 controls[layerId]?.setData(true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     InnerRender(): string { | 
					
						
							|  |  |  |         const t = Translations.t.favourite; | 
					
						
							|  |  |  |         const userDetails = State.state.osmConnection.userDetails.data; | 
					
						
							|  |  |  |         if(!userDetails.loggedIn){ | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  |             return t.loginNeeded.Render(); | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-31 16:17:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return new Combine([ | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |             t.panelIntro, | 
					
						
							|  |  |  |             ...this.checkboxes | 
					
						
							| 
									
										
										
										
											2020-09-15 02:29:31 +02:00
										 |  |  |         ]).Render(); | 
					
						
							| 
									
										
										
										
											2020-07-31 04:58:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |