1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Save editor settings in /settings/user

This commit is contained in:
Nick O'Leary 2017-12-05 10:20:13 +00:00
parent fff0b15ae5
commit 1b632894d3
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 66 additions and 8 deletions

View File

@ -18,6 +18,9 @@
RED.settings = (function () { RED.settings = (function () {
var loadedSettings = {}; var loadedSettings = {};
var userSettings = {};
var settingsDirty = false;
var pendingSave;
var hasLocalStorage = function () { var hasLocalStorage = function () {
try { try {
@ -31,7 +34,12 @@ RED.settings = (function () {
if (!hasLocalStorage()) { if (!hasLocalStorage()) {
return; return;
} }
localStorage.setItem(key, JSON.stringify(value)); if (key === "auth-tokens") {
localStorage.setItem(key, JSON.stringify(value));
} else {
userSettings[key] = value;
saveUserSettings();
}
}; };
/** /**
@ -44,14 +52,23 @@ RED.settings = (function () {
if (!hasLocalStorage()) { if (!hasLocalStorage()) {
return undefined; return undefined;
} }
return JSON.parse(localStorage.getItem(key)); if (key === "auth-tokens") {
return JSON.parse(localStorage.getItem(key));
} else {
return userSettings[key];
}
}; };
var remove = function (key) { var remove = function (key) {
if (!hasLocalStorage()) { if (!hasLocalStorage()) {
return; return;
} }
localStorage.removeItem(key); if (key === "auth_tokens") {
localStorage.removeItem(key);
} else {
delete userSettings[key];
saveUserSettings();
}
}; };
var setProperties = function(data) { var setProperties = function(data) {
@ -68,6 +85,10 @@ RED.settings = (function () {
loadedSettings = data; loadedSettings = data;
}; };
var setUserSettings = function(data) {
userSettings = data;
}
var init = function (done) { var init = function (done) {
var accessTokenMatch = /[?&]access_token=(.*?)(?:$|&)/.exec(window.location.search); var accessTokenMatch = /[?&]access_token=(.*?)(?:$|&)/.exec(window.location.search);
if (accessTokenMatch) { if (accessTokenMatch) {
@ -106,7 +127,7 @@ RED.settings = (function () {
RED.settings.remove("auth-tokens"); RED.settings.remove("auth-tokens");
} }
console.log("Node-RED: " + data.version); console.log("Node-RED: " + data.version);
done(); loadUserSettings(done);
}, },
error: function(jqXHR,textStatus,errorThrown) { error: function(jqXHR,textStatus,errorThrown) {
if (jqXHR.status === 401) { if (jqXHR.status === 401) {
@ -115,12 +136,50 @@ RED.settings = (function () {
} }
RED.user.login(function() { load(done); }); RED.user.login(function() { load(done); });
} else { } else {
console.log("Unexpected error:",jqXHR.status,textStatus); console.log("Unexpected error loading settings:",jqXHR.status,textStatus);
} }
} }
}); });
}; };
function loadUserSettings(done) {
$.ajax({
headers: {
"Accept": "application/json"
},
dataType: "json",
cache: false,
url: 'settings/user',
success: function (data) {
setUserSettings(data);
done();
},
error: function(jqXHR,textStatus,errorThrown) {
console.log("Unexpected error loading user settings:",jqXHR.status,textStatus);
}
});
}
function saveUserSettings() {
if (pendingSave) {
clearTimeout(pendingSave);
}
pendingSave = setTimeout(function() {
pendingSave = null;
$.ajax({
method: 'POST',
contentType: 'application/json',
url: 'settings/user',
data: JSON.stringify(userSettings),
success: function (data) {
},
error: function(jqXHR,textStatus,errorThrown) {
console.log("Unexpected error saving user settings:",jqXHR.status,textStatus);
}
});
},300);
}
function theme(property,defaultValue) { function theme(property,defaultValue) {
if (!RED.settings.editorTheme) { if (!RED.settings.editorTheme) {
return defaultValue; return defaultValue;
@ -148,5 +207,4 @@ RED.settings = (function () {
remove: remove, remove: remove,
theme: theme theme: theme
} }
}) })();
();

View File

@ -197,7 +197,7 @@ RED.userSettings = (function() {
allSettings[opt.setting] = opt; allSettings[opt.setting] = opt;
if (opt.onchange) { if (opt.onchange) {
var value = RED.settings.get(opt.setting); var value = RED.settings.get(opt.setting);
if (value === null && opt.hasOwnProperty('default')) { if ((value === null || value === undefined) && opt.hasOwnProperty('default')) {
value = opt.default; value = opt.default;
RED.settings.set(opt.setting,value); RED.settings.set(opt.setting,value);
} }