forked from MapComplete/MapComplete
		
	Fix: remember previous, externally loaded themes
This commit is contained in:
		
							parent
							
								
									06f9f2d94c
								
							
						
					
					
						commit
						b61c0a1180
					
				
					 3 changed files with 71 additions and 40 deletions
				
			
		| 
						 | 
				
			
			@ -178,12 +178,40 @@ export class OsmPreferences {
 | 
			
		|||
            content: v
 | 
			
		||||
        }, function (error) {
 | 
			
		||||
            if (error) {
 | 
			
		||||
                console.log(`Could not set preference "${k}"'`, error);
 | 
			
		||||
                console.warn(`Could not set preference "${k}"'`, error);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            console.log(`Preference ${k} written!`);
 | 
			
		||||
            console.debug(`Preference ${k} written!`);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public ClearPreferences(){
 | 
			
		||||
        let isRunning = false;
 | 
			
		||||
        const self = this;
 | 
			
		||||
        this.preferences.addCallbackAndRun(prefs => {
 | 
			
		||||
            if(Object.keys(prefs).length == 0){
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            if (isRunning) {
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
            isRunning = true
 | 
			
		||||
            const prefixes = ["mapcomplete-installed-theme","mapcomplete-installed-themes-","mapcomplete-current-open-changeset","mapcomplete-personal-theme-layer"]
 | 
			
		||||
            for (const key in prefs) {
 | 
			
		||||
                for (const prefix of prefixes) {
 | 
			
		||||
                    // console.log(key)
 | 
			
		||||
                    if (key.startsWith(prefix)) {
 | 
			
		||||
                        console.log("Clearing ", key)
 | 
			
		||||
                        self.GetPreference(key, "").setData("")
 | 
			
		||||
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            isRunning = false;
 | 
			
		||||
            return true;
 | 
			
		||||
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -32,10 +32,12 @@ export default class UserRelatedState extends ElementsState {
 | 
			
		|||
    /**
 | 
			
		||||
     * WHich other themes the user previously visited
 | 
			
		||||
     */
 | 
			
		||||
    public installedThemes: UIEventSource<{ id: string, // The id doubles as the URL
 | 
			
		||||
    public installedThemes: UIEventSource<{
 | 
			
		||||
        id: string, // The id doubles as the URL
 | 
			
		||||
        icon: string,
 | 
			
		||||
        title: any,
 | 
			
		||||
        shortDescription: any}[]>;
 | 
			
		||||
        shortDescription: any
 | 
			
		||||
    }[]>;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    constructor(layoutToUse: LayoutConfig, options?: { attemptLogin: true | boolean }) {
 | 
			
		||||
| 
						 | 
				
			
			@ -77,40 +79,47 @@ export default class UserRelatedState extends ElementsState {
 | 
			
		|||
                    return []
 | 
			
		||||
                }
 | 
			
		||||
                try {
 | 
			
		||||
                    return JSON.parse(str)
 | 
			
		||||
                    return JSON.parse(str);
 | 
			
		||||
                } catch (e) {
 | 
			
		||||
                    console.warn("Could not parse preference with installed themes due to ", e, "\nThe offending string is", str)
 | 
			
		||||
                    return []
 | 
			
		||||
                }
 | 
			
		||||
            }, [],(installed => JSON.stringify(installed))
 | 
			
		||||
            }, [], (installed) => JSON.stringify(installed)
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        const self = this;
 | 
			
		||||
        this.osmConnection.isLoggedIn.addCallbackAndRunD(loggedIn => {
 | 
			
		||||
            if(!loggedIn){
 | 
			
		||||
 | 
			
		||||
        if (this.layoutToUse?.id?.startsWith("http")) {
 | 
			
		||||
            this.installedThemes.addCallbackAndRun(currentThemes => {
 | 
			
		||||
                if (currentThemes === undefined) {
 | 
			
		||||
                    // We wait till we are logged in
 | 
			
		||||
                    return
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
            if(this.layoutToUse?.id?.startsWith("http")){
 | 
			
		||||
                if(!this.installedThemes.data.some(installed => installed.id === this.layoutToUse.id)){
 | 
			
		||||
 | 
			
		||||
                    this.installedThemes.data.push({
 | 
			
		||||
                        id: this.layoutToUse.id,
 | 
			
		||||
                        icon: this.layoutToUse.icon,
 | 
			
		||||
                        title: this.layoutToUse.title.translations,
 | 
			
		||||
                        shortDescription: this.layoutToUse.shortDescription.translations
 | 
			
		||||
                    })
 | 
			
		||||
                }
 | 
			
		||||
                this.installedThemes.ping()
 | 
			
		||||
                console.log("Registered "+this.layoutToUse.id+" as installed themes")
 | 
			
		||||
                if(self.osmConnection.isLoggedIn.data == false){
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (currentThemes.some(installed => installed.id === this.layoutToUse.id)) {
 | 
			
		||||
                    // Already added to the 'installed theme' list
 | 
			
		||||
                    return; 
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            return true;
 | 
			
		||||
                console.log("Current installed themes are", this.installedThemes.data)
 | 
			
		||||
                currentThemes.push({
 | 
			
		||||
                    id: self.layoutToUse.id,
 | 
			
		||||
                    icon: self.layoutToUse.icon,
 | 
			
		||||
                    title: self.layoutToUse.title.translations,
 | 
			
		||||
                    shortDescription: self.layoutToUse.shortDescription.translations
 | 
			
		||||
                })
 | 
			
		||||
                self.installedThemes.ping()
 | 
			
		||||
                console.log("Registered " + self.layoutToUse.id + " as installed themes")
 | 
			
		||||
            
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        // Important: the favourite layers are initialized _after_ the installed themes, as these might contain an installedTheme
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										6
									
								
								test.ts
									
										
									
									
									
								
							
							
						
						
									
										6
									
								
								test.ts
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,6 +0,0 @@
 | 
			
		|||
import ValidatedTextField from "./UI/Input/ValidatedTextField";
 | 
			
		||||
import {VariableUiElement} from "./UI/Base/VariableUIElement";
 | 
			
		||||
 | 
			
		||||
const tf = ValidatedTextField.InputForType("url")
 | 
			
		||||
tf.AttachTo("maindiv")
 | 
			
		||||
new VariableUiElement(tf.GetValue()).AttachTo("extradiv")
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue