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:
parent
fff0b15ae5
commit
1b632894d3
@ -18,6 +18,9 @@
|
||||
RED.settings = (function () {
|
||||
|
||||
var loadedSettings = {};
|
||||
var userSettings = {};
|
||||
var settingsDirty = false;
|
||||
var pendingSave;
|
||||
|
||||
var hasLocalStorage = function () {
|
||||
try {
|
||||
@ -31,7 +34,12 @@ RED.settings = (function () {
|
||||
if (!hasLocalStorage()) {
|
||||
return;
|
||||
}
|
||||
if (key === "auth-tokens") {
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
} else {
|
||||
userSettings[key] = value;
|
||||
saveUserSettings();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -44,14 +52,23 @@ RED.settings = (function () {
|
||||
if (!hasLocalStorage()) {
|
||||
return undefined;
|
||||
}
|
||||
if (key === "auth-tokens") {
|
||||
return JSON.parse(localStorage.getItem(key));
|
||||
} else {
|
||||
return userSettings[key];
|
||||
}
|
||||
};
|
||||
|
||||
var remove = function (key) {
|
||||
if (!hasLocalStorage()) {
|
||||
return;
|
||||
}
|
||||
if (key === "auth_tokens") {
|
||||
localStorage.removeItem(key);
|
||||
} else {
|
||||
delete userSettings[key];
|
||||
saveUserSettings();
|
||||
}
|
||||
};
|
||||
|
||||
var setProperties = function(data) {
|
||||
@ -68,6 +85,10 @@ RED.settings = (function () {
|
||||
loadedSettings = data;
|
||||
};
|
||||
|
||||
var setUserSettings = function(data) {
|
||||
userSettings = data;
|
||||
}
|
||||
|
||||
var init = function (done) {
|
||||
var accessTokenMatch = /[?&]access_token=(.*?)(?:$|&)/.exec(window.location.search);
|
||||
if (accessTokenMatch) {
|
||||
@ -106,7 +127,7 @@ RED.settings = (function () {
|
||||
RED.settings.remove("auth-tokens");
|
||||
}
|
||||
console.log("Node-RED: " + data.version);
|
||||
done();
|
||||
loadUserSettings(done);
|
||||
},
|
||||
error: function(jqXHR,textStatus,errorThrown) {
|
||||
if (jqXHR.status === 401) {
|
||||
@ -115,12 +136,50 @@ RED.settings = (function () {
|
||||
}
|
||||
RED.user.login(function() { load(done); });
|
||||
} 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) {
|
||||
if (!RED.settings.editorTheme) {
|
||||
return defaultValue;
|
||||
@ -148,5 +207,4 @@ RED.settings = (function () {
|
||||
remove: remove,
|
||||
theme: theme
|
||||
}
|
||||
})
|
||||
();
|
||||
})();
|
||||
|
@ -197,7 +197,7 @@ RED.userSettings = (function() {
|
||||
allSettings[opt.setting] = opt;
|
||||
if (opt.onchange) {
|
||||
var value = RED.settings.get(opt.setting);
|
||||
if (value === null && opt.hasOwnProperty('default')) {
|
||||
if ((value === null || value === undefined) && opt.hasOwnProperty('default')) {
|
||||
value = opt.default;
|
||||
RED.settings.set(opt.setting,value);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user