-
Notifications
You must be signed in to change notification settings - Fork 2
/
deepSkySettingsManager.js
171 lines (147 loc) · 5.52 KB
/
deepSkySettingsManager.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
deepSkySettingsManager.js: Settings and parameters.
================================================================================
Helper for retrieving and storing settings and parameters with UI databinding
capabilities.
================================================================================
*
*
* Copyright Jeremy Likness, 2021
*
* License: https://github.com/DeepSkyWorkflows/DeepSkyWorkflowScripts/LICENSE
*
* Source: https://github.com/DeepSkyWorkflows/DeepSkyWorkflowScripts
*
* mailTo:deepskyworkflows@gmail.com
*
*/
#script-id DeepSkySettingsManager
#include "deepSkyInit.js"
#define DEBUG_SETTINGS false
(function (ds) {
ds.debug.register('settings', DEBUG_SETTINGS);
ds.debug.settings.debugLn('Settings debugging is on.');
if (ds.createSettingsManager) {
return;
}
// save a setting
let saveSetting = function (key, type, value) {
ds.debug.settings.debugLn('Save setting:', key, '=', value == null ? "null" : value.toString());
Settings.write(key, type, value);
};
// read a setting or return null
let readSetting = function (key, type) {
const val = Settings.read(key, type);
if (Settings.lastReadOK) {
ds.debug.settings.debugLn('Read setting:', key, '=', val);
return val;
}
ds.debug.settings.debugLn('Setting', key, 'not found');
return null;
}
let noop = function () { };
// pass in an array
// each element should be:
// name, data type, default value, label, tooltip, range
ds.createSettingsManager = function (config) {
let mappings = [];
mappings[DataType_Boolean] = Parameters.getBoolean;
mappings[DataType_Int16] = Parameters.getInteger;
mappings[DataType_Int32] = Parameters.getInteger;
mappings[DataType_Int64] = Parameters.getInteger;
mappings[DataType_Double] = Parameters.getReal;
mappings[DataType_Float] = Parameters.getReal;
mappings[DataType_String] = Parameters.getString;
let defaults = {};
let prefs = {};
let funcs = {};
for (let idx = 0; idx < config.length; idx++) {
let configValue = config[idx];
ds.debug.settings.debugLn('Configuring setting:', JSON.stringify(configValue));
let setting = configValue.setting;
let dataType = configValue.dataType;
let defaultValue = configValue.defaultValue;
let label = configValue.label;
let tooltip = configValue.tooltip;
let range = configValue.range;
let precision = configValue.precision || 0;
let persist = !(configValue.hasOwnProperty("persist") && configValue.persist === false);
defaults[setting] = defaultValue;
prefs[setting] = defaultValue;
funcs[setting] = {
dataType: dataType,
label: label,
tooltip: tooltip,
range: range,
precision: precision,
reset: function () { },
saveSetting: persist ? function () {
saveSetting(setting, dataType, prefs[setting]);
} : noop,
readSetting: function () {
let result = readSetting(setting, dataType);
if (result !== null) {
prefs[setting] = result;
}
},
saveParameter: persist ? function () {
ds.debug.settings.debugLn('Saving parameter', setting, '=', prefs[setting]);
Parameters.set(setting, prefs[setting]);
} : noop,
readParameter: function () {
if (Parameters.has(setting)) {
prefs[setting] = mappings[dataType](setting);
ds.debug.settings.debugLn('Read parameter', setting, '=', prefs[setting]);
}
else {
ds.debug.settings.debugLn('No parameter saved for', setting);
}
}
}
}
let newConfig = {
defaults: defaults,
prefs: prefs,
funcs: funcs,
closeOnExit: false
};
newConfig.init = function () {
for (let setting in funcs) {
if (funcs.hasOwnProperty(setting)) {
funcs[setting].readSetting();
}
}
};
newConfig.loadParameters = function () {
for (let setting in funcs) {
if (funcs.hasOwnProperty(setting)) {
funcs[setting].readParameter();
}
}
};
newConfig.saveParameters = function () {
for (let setting in prefs) {
if (prefs.hasOwnProperty(setting)) {
funcs[setting].saveParameter();
}
}
};
newConfig.saveSettings = function () {
for (let setting in prefs) {
if (prefs.hasOwnProperty(setting)) {
funcs[setting].saveSetting();
}
}
};
newConfig.reset = function () {
for (let setting in defaults) {
if (defaults.hasOwnProperty(setting)) {
prefs[setting] = defaults[setting];
funcs[setting].reset();
}
}
newConfig.saveSettings();
};
return newConfig;
};
})(deepSky);