-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathconfig.js
202 lines (175 loc) · 6.28 KB
/
config.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
Copyright 2014 Gordon Williams (gw@pur3.co.uk)
This Source Code is subject to the terms of the Mozilla Public
License, v2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
------------------------------------------------------------------
Central place to store and retrieve Options
To use this, on your plugin's `init` function, do something like the
following:
Espruino.Core.Config.add("MAX_FOOBARS", {
section : "Communications", // Heading this will come under in the config screen
name : "Foobars", // Nice name
description : "How many foobars?", // More detail about this
type : "int"/"boolean"/"string"/{ value1:niceName, value2:niceName },
defaultValue : 20,
onChange : function(newValue) { ... }
});
* onChange will be called whenever the value changes from the default
(including when it is loaded)
Then use:
Espruino.Config.MAX_FOOBARS in your code
------------------------------------------------------------------
**/
"use strict";
(function() {
/** See addSection and getSections */
var builtinSections = {};
function _get(callback) {
if (typeof chrome !== 'undefined' && chrome.storage) {
chrome.storage.sync.get( "CONFIGS", function (data) {
var value = data["CONFIGS"];
logger.debug("GET chrome.storage.sync = "+JSON.stringify(value));
callback(value);
});
} else if (typeof window !== 'undefined' && window.localStorage) {
var data = {};
var value = window.localStorage.getItem("CONFIG");
logger.debug("GET window.localStorage = "+JSON.stringify(value));
try {
data = JSON.parse(value);
} catch (e) {
logger.error("Invalid config data");
}
callback(data);
} else if (typeof document != "undefined") {
var data = {};
var cookie = document.cookie;
if (cookie!==undefined && cookie.indexOf("CONFIG=")>=0) {
cookie = cookie.substring(cookie.indexOf("CONFIG=")+7);
cookie = cookie.substring(0,cookie.indexOf(";"));
try {
var json = atob(cookie);
data = JSON.parse(json);
} catch (e) {
logger.error("Got ", e, " while reading info");
}
}
callback(data);
} else {
callback({});
}
}
function _set(data) {
if (typeof chrome !== 'undefined' && chrome.storage) {
logger.debug("SET chrome.storage.sync = "+JSON.stringify(data,null,2));
chrome.storage.sync.set({ CONFIGS : data });
} else if (typeof window !== 'undefined' && window.localStorage) {
logger.debug("SET window.localStorage = "+JSON.stringify(data,null,2));
window.localStorage.setItem("CONFIG",JSON.stringify(data));
} else if (typeof document != "undefined") {
document.cookie = "CONFIG="+btoa(JSON.stringify(data));
}
}
function loadConfiguration(callback) {
_get(function (value) {
for (var key in value) {
if (key=="set") continue;
Espruino.Config[key] = value[key];
if (Espruino.Core.Config.data[key] !== undefined &&
Espruino.Core.Config.data[key].onChange !== undefined)
Espruino.Core.Config.data[key].onChange(value[key]);
}
if (callback!==undefined)
callback();
});
}
function init() {
addSection("General", { sortOrder:100, description: "General Web IDE Settings" });
addSection("Communications", { sortOrder:200, description: "Settings for communicating with the Espruino Board" });
addSection("Board", { sortOrder:300, description: "Settings for the Espruino Board itself" });
}
function add(name, options) {
Espruino.Core.Config.data[name] = options;
if (Espruino.Config[name] === undefined)
Espruino.Config[name] = options.defaultValue;
}
/** Add a section (or information on the page).
* options = {
* sortOrder : int, // a number used for sorting
* description : "",
* getHTML : function(callback(html)) // optional
* };
*/
function addSection(name, options) {
options.name = name;
builtinSections[name] = options;
}
/** Get an object containing the information on a section used in configs */
function getSection(name) {
if (builtinSections[name]!==undefined)
return builtinSections[name];
// not found - but we warned about this in getSections
return {
name : name
};
}
/** Get an object containing information on all 'sections' used in all the configs */
function getSections() {
var sections = [];
// add sections we know about
for (var name in builtinSections)
sections.push(builtinSections[name]);
// add other sections
for (var i in Espruino.Core.Config.data) {
var c = Espruino.Core.Config.data[i];
var found = false;
for (var s in sections)
if (sections[s].name == c.section)
found = true;
if (!found) {
logger.warn("Section named "+c.section+" was not added with Config.addSection");
sections[c.section] = {
name : c.section,
sortOrder : 0
};
}
}
// Now sort by sortOrder
sections.sort(function (a,b) { return a.sortOrder - b.sortOrder; });
return sections;
}
Espruino.Config = {};
Espruino.Config.set = function (key, value) {
if (Espruino.Config[key] != value) {
Espruino.Config[key] = value;
// Do the callback
if (Espruino.Core.Config.data[key] !== undefined &&
Espruino.Core.Config.data[key].onChange !== undefined)
Espruino.Core.Config.data[key].onChange(value);
// Save to synchronized storage...
var data = {};
for (var key in Espruino.Config)
if (key != "set")
data[key] = Espruino.Config[key];
_set(data);
}
};
function clearAll() { // clear all settings
_set({});
for (var name in Espruino.Core.Config.data) {
var options = Espruino.Core.Config.data[name];
Espruino.Config[name] = options.defaultValue;
}
}
Espruino.Core.Config = {
loadConfiguration : loadConfiguration, // special - called before init
init : init,
add : add,
data : {},
addSection : addSection,
getSection : getSection,
getSections : getSections,
clearAll : clearAll, // clear all settings
};
})();