File size: 3,989 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { UserArg } from "@/UI/Forms/FormFull/FormFullInterfaces";
import { localStorageGetItem, localStorageSetItem } from "@/Core/LocalStorage";

/**
 * Get the default settings.
 *
 * @returns {any}  The default settings.
 */
export async function defaultSettings(): Promise<any> {
    // Leave one processor free
    const maxProcsAvailable = navigator.hardwareConcurrency || 4;
    const procsToRecommend =
        maxProcsAvailable - 1 > 0 ? maxProcsAvailable - 1 : 1;

    // For allowCookies, try to use existing value if it is available.
    // Otherwise, use false as default.
    const allowCookies = await localStorageGetItem("allowCookies", false);

    return {
        maxProcs: procsToRecommend,
        initialCompoundsVisible: 50,
        molViewer: "3dmol",
        allowCookies: allowCookies,
        allowExternalWebAccess: false,
        autoSaveFrequencyMinutes: 5
    };
}

/**
 * Saves settings to local storage. Note that not just accessing local storage
 * directly because this also converts from UserArg[] to an object, considers
 * defaults, etc.
 *
 * @param  {UserArg[]} settings  The settings to save.
 */
export async function saveSettings(settings: UserArg[]) {
    // Convert list of userargs into an object.
    const settingsObj: any = {};
    for (const setting of settings) {
        settingsObj[setting.id] = setting.val;
    }
    delete settingsObj["allowCookiesAlert"];

    // Now get the default values (in cases you need them, and for the keys).
    const defaults = await defaultSettings();

    // Sanity check: if a setting is not in the defaults, throw an error.
    for (const key in settingsObj) {
        if (defaults[key] === undefined) {
            throw new Error(
                `Setting ${key} has no default value specified in defaultSettings().`
            );
        }
    }

    for (const key in defaults) {
        // Add in defaults
        const val =
            settingsObj[key] === undefined ? defaults[key] : settingsObj[key];
        await localStorageSetItem(key, val);  // , 0.000173611);
    }

    // await localStorageSetItem("settings", settings);
}

/**
 * Gets settings from local storage. Not just accessing local storage directly
 * because this accounts for defaults (if not set).
 *
 * @returns {UserArg[]}  The settings.
 */
export async function getSettings(): Promise<{ [key: string]: any }> {
    const settings = await defaultSettings();
    for (const key in settings) {
        // Set to what's in local storage, if available.
        settings[key] = await localStorageGetItem(key, settings[key]);
    }

    return settings;
}

/**
 * Gets the value of a specific setting, using default value if not present. Not
 * just accessing local storage directly because this accounts for defaults (if
 * not set).
 *
 * @param  {string} id  The id of the setting.
 * @returns {any}  The value of the setting.
 */
export async function getSetting(id: string): Promise<any> {
    const settings = await getSettings();
    return settings[id];
}

/**
 * Given settings, apply them (meaning, update app per settings).
 *
 * @param  {any} settings  The settings to apply.
 */
export function applySettings(settings: { [key: string]: any }) {
    // NOTE: Previously this was necessary to change the molecular viewer
    // without having to reload. We're now just using one viewer, but good to
    // leave this function here in case you ever want to implement something
    // similar (not just changing setting values, but actually changing
    // something in the UI).
    // Convert the settings to a map for easy lookup.
    // const settingsMap = new Map<string, UserArg>();
    // for (const setting of settings) {
    //     settingsMap.set(setting.id, setting);
    // }
    // const defaults = await defaultSettings();
    // const molViewer = settingsMap.get("molViewer")?.val ?? defaults.molViewer;
    // visualizationApi.viewerObj?.unLoadViewer();
    // setStoreVar("molViewer", molViewer);
}