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

Restructure how editor/git settings are saved in userSettings

This commit is contained in:
Nick O'Leary 2017-12-05 23:50:32 +00:00
parent 3f6b1f6ccb
commit 1840d15397
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
7 changed files with 200 additions and 262 deletions

View File

@ -202,6 +202,7 @@ RED.settings = (function () {
return { return {
init: init, init: init,
load: load, load: load,
loadUserSettings: loadUserSettings,
set: set, set: set,
get: get, get: get,
remove: remove, remove: remove,

View File

@ -57,8 +57,24 @@ RED.keyboard = (function() {
173:189 173:189
} }
function migrateOldKeymap() {
if ('localStorage' in window && window['localStorage'] !== null) {
var oldKeyMap = localStorage.getItem("keymap");
if (oldKeyMap !== null) {
localStorage.removeItem("keymap");
var currentEditorSettings = RED.settings.get('editor') || {};
currentEditorSettings.keymap = JSON.parse(oldKeyMap);
RED.settings.set('editor',currentEditorSettings);
}
}
}
function init() { function init() {
var userKeymap = RED.settings.get('keymap') || {}; // Migrate from pre-0.18
migrateOldKeymap();
var currentEditorSettings = RED.settings.get('editor') || {};
var userKeymap = currentEditorSettings.keymap || {};
$.getJSON("red/keymap.json",function(data) { $.getJSON("red/keymap.json",function(data) {
for (var scope in data) { for (var scope in data) {
if (data.hasOwnProperty(scope)) { if (data.hasOwnProperty(scope)) {
@ -67,6 +83,7 @@ RED.keyboard = (function() {
if (keys.hasOwnProperty(key)) { if (keys.hasOwnProperty(key)) {
if (!userKeymap.hasOwnProperty(keys[key])) { if (!userKeymap.hasOwnProperty(keys[key])) {
addHandler(scope,key,keys[key],false); addHandler(scope,key,keys[key],false);
}
defaultKeyMap[keys[key]] = { defaultKeyMap[keys[key]] = {
scope:scope, scope:scope,
key:key, key:key,
@ -76,7 +93,6 @@ RED.keyboard = (function() {
} }
} }
} }
}
for (var action in userKeymap) { for (var action in userKeymap) {
if (userKeymap.hasOwnProperty(action)) { if (userKeymap.hasOwnProperty(action)) {
var obj = userKeymap[action]; var obj = userKeymap[action];
@ -369,8 +385,11 @@ RED.keyboard = (function() {
container.removeClass('keyboard-shortcut-entry-expanded'); container.removeClass('keyboard-shortcut-entry-expanded');
var shortcut = RED.keyboard.getShortcut(object.id); var shortcut = RED.keyboard.getShortcut(object.id);
var userKeymap = RED.settings.get('keymap') || {}; var userKeymap = RED.settings.get('keymap') || {};
delete userKeymap[object.id];
RED.settings.set('keymap',userKeymap); var currentEditorSettings = RED.settings.get('editor') || {};
var userKeymap = currentEditorSettings.keymap || {};
userKeymap[object.id] = null;
RED.settings.set('editor',currentEditorSettings);
var obj = { var obj = {
id:object.id, id:object.id,
@ -419,9 +438,11 @@ RED.keyboard = (function() {
object.scope = scope; object.scope = scope;
RED.keyboard.add(object.scope,object.key,object.id,true); RED.keyboard.add(object.scope,object.key,object.id,true);
} }
var userKeymap = RED.settings.get('keymap') || {};
var currentEditorSettings = RED.settings.get('editor') || {};
var userKeymap = currentEditorSettings.keymap || {};
userKeymap[object.id] = RED.keyboard.getShortcut(object.id); userKeymap[object.id] = RED.keyboard.getShortcut(object.id);
RED.settings.set('keymap',userKeymap); RED.settings.set('editor',currentEditorSettings);
} }
} }
} }

View File

@ -16,43 +16,30 @@
RED.projects.userSettings = (function() { RED.projects.userSettings = (function() {
var trayWidth = 700; var gitUsernameInput;
var settingsVisible = false; var gitEmailInput;
function createRemoteRepositorySection(pane) { function createRemoteRepositorySection(pane) {
var title = $('<h3></h3>').text("Version Control").appendTo(pane);
var editGitUserButton = $('<button class="editor-button editor-button-small" style="float: right;">edit</button>')
.appendTo(title)
.click(function(evt) {
editGitUserButton.hide();
formButtons.show();
gitUsernameLabel.hide(); var currentGitSettings = RED.settings.get('git') || {};
gitUsernameInput.show(); currentGitSettings.user = currentGitSettings.user || {};
gitEmailLabel.hide();
gitEmailInput.show();
});
var title = $('<h3></h3>').text("Committer Details").appendTo(pane);
var gitconfigContainer = $('<div class="user-settings-section"></div>').appendTo(pane); var gitconfigContainer = $('<div class="user-settings-section"></div>').appendTo(pane);
var subtitle = $('<h4></h4>').text("Committer Details").appendTo(gitconfigContainer); $('<div style="color:#aaa;"></div>').appendTo(gitconfigContainer).text("Leave blank to use system default");
$('<div style="display: inline-block; margin-left: 20px;"><small style="color:#aaa;"></small></div>').appendTo(subtitle).find('small').text("Leave blank to use system default");
var row = $('<div class="user-settings-row"></div>').appendTo(gitconfigContainer); var row = $('<div class="user-settings-row"></div>').appendTo(gitconfigContainer);
$('<label for=""></label>').text('Username').appendTo(row); $('<label for=""></label>').text('Username').appendTo(row);
var gitUsernameLabel = $('<div class="uneditable-input">').appendTo(row); gitUsernameInput = $('<input type="text">').appendTo(row);
var gitUsernameInput = $('<input type="text">').hide().appendTo(row); gitUsernameInput.val(currentGitSettings.user.name||"");
row = $('<div class="user-settings-row"></div>').appendTo(gitconfigContainer); row = $('<div class="user-settings-row"></div>').appendTo(gitconfigContainer);
$('<label for=""></label>').text('Email').appendTo(row); $('<label for=""></label>').text('Email').appendTo(row);
var gitEmailLabel = $('<div class="uneditable-input">').appendTo(row); gitEmailInput = $('<input type="text">').appendTo(row);
var gitEmailInput = $('<input type="text">').hide().appendTo(row); gitEmailInput.val(currentGitSettings.user.email||"");
// var formButtons = $('<span class="button-group" style="position: relative; float: right; margin-right:0;"></span>')
var formButtonArea = $('<div style="width: 100%; height: 35px;"></div>').appendTo(gitconfigContainer);
var formButtons = $('<span class="button-group" style="position: absolute; right: 0px; margin-right:0;"></span>')
.hide().appendTo(formButtonArea);
// var sshkeyTitle = $('<h4></h4>').text("SSH Keys").appendTo(gitconfigContainer); // var sshkeyTitle = $('<h4></h4>').text("SSH Keys").appendTo(gitconfigContainer);
// var generateSshKeyButton = $('<button class="editor-button editor-button-small" style="float: right;">generate new ssh key</button>') // var generateSshKeyButton = $('<button class="editor-button editor-button-small" style="float: right;">generate new ssh key</button>')
// .appendTo(sshkeyTitle) // .appendTo(sshkeyTitle)
@ -119,116 +106,6 @@ RED.projects.userSettings = (function() {
// }); // });
// var remoteListAddButton = row.find(".red-ui-editableList-addButton").hide(); // var remoteListAddButton = row.find(".red-ui-editableList-addButton").hide();
var hideGitUserEditForm = function() {
editGitUserButton.show();
formButtons.hide();
// $('.projects-dialog-remote-list-entry-delete').hide();
// remoteListAddButton.hide();
gitUsernameLabel.show();
gitUsernameInput.hide();
gitEmailLabel.show();
gitEmailInput.hide();
}
$('<button class="editor-button">Cancel</button>')
.appendTo(formButtons)
.click(function(evt) {
evt.preventDefault();
hideGitUserEditForm();
});
var saveButton = $('<button class="editor-button">Save</button>')
.appendTo(formButtons)
.click(function(evt) {
evt.preventDefault();
var spinner = utils.addSpinnerOverlay(gitconfigContainer);
var body = {
git: {
user: {
name: gitUsernameInput.val(),
email: gitEmailInput.val()
}
}
}
var done = function(err) {
spinner.remove();
if (err) {
console.log(err);
return;
}
hideGitUserEditForm();
}
utils.sendRequest({
url: "settings/user",
type: "POST",
responses: {
0: function(error) {
done(error);
},
200: function(data) {
gitUsernameLabel.text(body.git.user.name);
gitEmailLabel.text(body.git.user.email);
done();
},
400: {
'unexpected_error': function(error) {
console.log(error);
done(error);
}
},
}
},body);
});
var updateForm = function() {
// sshkeysList.editableList('empty');
// if (activeProject.git.hasOwnProperty('remotes')) {
// for (var name in activeProject.git.remotes) {
// if (activeProject.git.remotes.hasOwnProperty(name)) {
// remotesList.editableList('addItem',{name:name,urls:activeProject.git.remotes[name]});
// }
// }
// }
// var sshkeyFiles = ["/User/hideki/.node-red/sshkeys/node-red-ssh-test01", "/User/hideki/.node-red/sshkeys/node-red-ssh-test02"];
// if ( sshkeyFiles ) {
// sshkeyFiles.map(function(sshkeyFilePath) {
// sshkeysList.editableList('addItem', {path: sshkeyFilePath, data: 'XXXXXXX'});
// });
// }
}
utils.sendRequest({
url: "settings/user",
type: "GET",
responses: {
0: function(error) {
console.log(error);
},
200: function(result) {
console.log(result);
if ( result && result.git && result.git.user ) {
var username = result.git.user.name || "";
var email = result.git.user.email || "";
gitUsernameLabel.text(username);
gitUsernameInput.val(username);
gitEmailLabel.text(email);
gitEmailInput.val(email);
}
},
400: {
'unexpected_error': function(error) {
console.log(error);
}
}
}
})
updateForm();
} }
function createSettingsPane(activeProject) { function createSettingsPane(activeProject) {
@ -237,8 +114,6 @@ RED.projects.userSettings = (function() {
return pane; return pane;
} }
var popover;
var utils; var utils;
function init(_utils) { function init(_utils) {
utils = _utils; utils = _utils;
@ -247,10 +122,11 @@ RED.projects.userSettings = (function() {
title: "Git config", // TODO: nls title: "Git config", // TODO: nls
get: createSettingsPane, get: createSettingsPane,
close: function() { close: function() {
if (popover) { var currentGitSettings = RED.settings.get('git') || {};
popover.close(); currentGitSettings.user = currentGitSettings.user || {};
popover = null; currentGitSettings.user.name = gitUsernameInput.val();
} currentGitSettings.user.email = gitEmailInput.val();
RED.settings.set('git', currentGitSettings);
} }
}); });
@ -260,4 +136,3 @@ RED.projects.userSettings = (function() {
init: init, init: init,
}; };
})(); })();

View File

@ -127,10 +127,13 @@ RED.userSettings = (function() {
var pane = $('<div id="user-settings-tab-view" class="node-help"></div>'); var pane = $('<div id="user-settings-tab-view" class="node-help"></div>');
var currentEditorSettings = RED.settings.get('editor') || {};
currentEditorSettings.view = currentEditorSettings.view || {};
viewSettings.forEach(function(section) { viewSettings.forEach(function(section) {
$('<h3></h3>').text(RED._(section.title)).appendTo(pane); $('<h3></h3>').text(RED._(section.title)).appendTo(pane);
section.options.forEach(function(opt) { section.options.forEach(function(opt) {
var initialState = RED.settings.get(opt.setting); var initialState = currentEditorSettings.view[opt.setting];
var row = $('<div class="user-settings-row"></div>').appendTo(pane); var row = $('<div class="user-settings-row"></div>').appendTo(pane);
var input; var input;
if (opt.toggle) { if (opt.toggle) {
@ -147,7 +150,10 @@ RED.userSettings = (function() {
function setSelected(id, value) { function setSelected(id, value) {
var opt = allSettings[id]; var opt = allSettings[id];
RED.settings.set(opt.setting,value); var currentEditorSettings = RED.settings.get('editor') || {};
currentEditorSettings.view = currentEditorSettings.view || {};
currentEditorSettings.view[opt.setting] = value;
RED.settings.set('editor', currentEditorSettings);
var callback = opt.onchange; var callback = opt.onchange;
if (typeof callback === 'string') { if (typeof callback === 'string') {
callback = RED.actions.get(callback); callback = RED.actions.get(callback);
@ -158,8 +164,9 @@ RED.userSettings = (function() {
} }
function toggle(id) { function toggle(id) {
var opt = allSettings[id]; var opt = allSettings[id];
var state = RED.settings.get(opt.setting); var currentEditorSettings = RED.settings.get('editor') || {};
setSelected(id,!state); currentEditorSettings.view = currentEditorSettings.view || {};
setSelected(id,!currentEditorSettings.view[opt.setting]);
} }
@ -185,21 +192,26 @@ RED.userSettings = (function() {
} }
}) })
var currentEditorSettings = RED.settings.get('editor') || {};
currentEditorSettings.view = currentEditorSettings.view || {};
var editorSettingsChanged = false;
viewSettings.forEach(function(section) { viewSettings.forEach(function(section) {
section.options.forEach(function(opt) { section.options.forEach(function(opt) {
if (opt.oldSetting) { if (opt.oldSetting) {
var oldValue = RED.settings.get(opt.oldSetting); var oldValue = RED.settings.get(opt.oldSetting);
if (oldValue !== undefined && oldValue !== null) { if (oldValue !== undefined && oldValue !== null) {
RED.settings.set(opt.setting,oldValue); currentEditorSettings.view[opt.setting] = oldValue;
editorSettingsChanged = true;
RED.settings.remove(opt.oldSetting); RED.settings.remove(opt.oldSetting);
} }
} }
allSettings[opt.setting] = opt; allSettings[opt.setting] = opt;
if (opt.onchange) { if (opt.onchange) {
var value = RED.settings.get(opt.setting); var value = currentEditorSettings.view[opt.setting];
if ((value === null || value === undefined) && opt.hasOwnProperty('default')) { if ((value === null || value === undefined) && opt.hasOwnProperty('default')) {
value = opt.default; value = opt.default;
RED.settings.set(opt.setting,value); currentEditorSettings.view[opt.setting] = value;
editorSettingsChanged = true;
} }
var callback = opt.onchange; var callback = opt.onchange;
@ -212,6 +224,9 @@ RED.userSettings = (function() {
} }
}); });
}); });
if (editorSettingsChanged) {
RED.settings.set('editor',currentEditorSettings);
}
} }
return { return {

View File

@ -68,7 +68,9 @@ module.exports = {
} else { } else {
username = req.user.username; username = req.user.username;
} }
settings.setUserSettings(username, req.body).then(function() { var currentSettings = settings.getUserSettings(username)||{};
currentSettings = extend(currentSettings, req.body);
settings.setUserSettings(username, currentSettings).then(function() {
log.audit({event: "settings.update",username:username},req); log.audit({event: "settings.update",username:username},req);
res.status(204).end(); res.status(204).end();
}).otherwise(function(err) { }).otherwise(function(err) {
@ -77,3 +79,27 @@ module.exports = {
}); });
} }
} }
function extend(target, source) {
var keys = Object.keys(source);
var i = keys.length;
while(i--) {
var value = source[keys[i]]
var type = typeof value;
if (type === 'string' || type === 'number' || type === 'boolean' || Array.isArray(value)) {
target[keys[i]] = value;
} else if (value === null) {
if (target.hasOwnProperty(keys[i])) {
delete target[keys[i]];
}
} else {
// Object
if (target.hasOwnProperty(keys[i])) {
target[keys[i]] = extend(target[keys[i]],value);
} else {
target[keys[i]] = value;
}
}
}
return target;
}

View File

@ -165,17 +165,18 @@ var persistentSettings = {
}); });
}, },
getUserSettings: function(username) { getUserSettings: function(username) {
console.log(username); return clone(userSettings[username]);
return userSettings[username];
}, },
setUserSettings: function(username,settings) { setUserSettings: function(username,settings) {
var current = userSettings[username]; var current = userSettings[username];
userSettings[username] = settings; userSettings[username] = settings;
try { try {
assert.deepEqual(current,settings); assert.deepEqual(current,settings);
console.log("skip the save");
return when.resolve(); return when.resolve();
} catch(err) { } catch(err) {
globalSettings.users = userSettings; globalSettings.users = userSettings;
console.log("saving");
return storage.saveSettings(globalSettings); return storage.saveSettings(globalSettings);
} }
} }

View File

@ -53,7 +53,6 @@ Project.prototype.load = function () {
this.credentialSecret = projectSettings.credentialSecret; this.credentialSecret = projectSettings.credentialSecret;
this.git = projectSettings.git || { user:{} }; this.git = projectSettings.git || { user:{} };
console.log("LOADED",this.git);
// this.paths.flowFile = fspath.join(this.path,"flow.json"); // this.paths.flowFile = fspath.join(this.path,"flow.json");
// this.paths.credentialsFile = fspath.join(this.path,"flow_cred.json"); // this.paths.credentialsFile = fspath.join(this.path,"flow_cred.json");