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,12 +83,12 @@ 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]] = {
scope:scope,
key:key,
user:false
};
} }
defaultKeyMap[keys[key]] = {
scope:scope,
key:key,
user:false
};
} }
} }
} }
@ -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

@ -15,249 +15,124 @@
**/ **/
RED.projects.userSettings = (function() { RED.projects.userSettings = (function() {
var trayWidth = 700;
var settingsVisible = false;
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();
gitUsernameInput.show();
gitEmailLabel.hide();
gitEmailInput.show();
});
var gitconfigContainer = $('<div class="user-settings-section"></div>').appendTo(pane);
var subtitle = $('<h4></h4>').text("Committer Details").appendTo(gitconfigContainer);
$('<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);
$('<label for=""></label>').text('Username').appendTo(row);
var gitUsernameLabel = $('<div class="uneditable-input">').appendTo(row);
var gitUsernameInput = $('<input type="text">').hide().appendTo(row);
row = $('<div class="user-settings-row"></div>').appendTo(gitconfigContainer);
$('<label for=""></label>').text('Email').appendTo(row);
var gitEmailLabel = $('<div class="uneditable-input">').appendTo(row);
var gitEmailInput = $('<input type="text">').hide().appendTo(row);
// 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 gitUsernameInput;
// var sshkeyTitle = $('<h4></h4>').text("SSH Keys").appendTo(gitconfigContainer); var gitEmailInput;
// var generateSshKeyButton = $('<button class="editor-button editor-button-small" style="float: right;">generate new ssh key</button>')
// .appendTo(sshkeyTitle)
// .click(function(evt) {
// console.log('click generateSshKeyButton');
// });
// row = $('<div class="user-settings-row projects-dialog-remote-list"></div>').appendTo(gitconfigContainer); function createRemoteRepositorySection(pane) {
// var sshkeysList = $('<ol>').appendTo(row);
// sshkeysList.editableList({ var currentGitSettings = RED.settings.get('git') || {};
// addButton: false, currentGitSettings.user = currentGitSettings.user || {};
// height: 'auto',
// addItem: function(outer,index,entry) {
// var header = $('<div class="projects-dialog-remote-list-entry-header"></div>').appendTo(outer); var title = $('<h3></h3>').text("Committer Details").appendTo(pane);
// entry.header = $('<span>').text(entry.path||"Add new remote").appendTo(header);
// var body = $('<div>').appendTo(outer); var gitconfigContainer = $('<div class="user-settings-section"></div>').appendTo(pane);
// entry.body = body; $('<div style="color:#aaa;"></div>').appendTo(gitconfigContainer).text("Leave blank to use system default");
// if (entry.path) {
// entry.removeButton = $('<button class="editor-button editor-button-small projects-dialog-remote-list-entry-delete">remove</button>') var row = $('<div class="user-settings-row"></div>').appendTo(gitconfigContainer);
// // .hide() $('<label for=""></label>').text('Username').appendTo(row);
// .appendTo(header) gitUsernameInput = $('<input type="text">').appendTo(row);
// .click(function(e) { gitUsernameInput.val(currentGitSettings.user.name||"");
// entry.removed = true;
// body.fadeOut(100); row = $('<div class="user-settings-row"></div>').appendTo(gitconfigContainer);
// entry.header.css("text-decoration","line-through") $('<label for=""></label>').text('Email').appendTo(row);
// entry.header.css("font-style","italic") gitEmailInput = $('<input type="text">').appendTo(row);
// if (entry.copyToClipboard) { gitEmailInput.val(currentGitSettings.user.email||"");
// entry.copyToClipboard.hide(); // 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>')
// $(this).hide(); // .appendTo(sshkeyTitle)
// }); // .click(function(evt) {
// if (entry.data) { // console.log('click generateSshKeyButton');
// entry.copyToClipboard = $('<button class="editor-button editor-button-small projects-dialog-remote-list-entry-copy">copy</button>') // });
// // .hide()
// .appendTo(header) // row = $('<div class="user-settings-row projects-dialog-remote-list"></div>').appendTo(gitconfigContainer);
// .click(function(e) { // var sshkeysList = $('<ol>').appendTo(row);
// var textarea = document.createElement("textarea"); // sshkeysList.editableList({
// textarea.style.position = 'fixed'; // addButton: false,
// textarea.style.top = 0; // height: 'auto',
// textarea.style.left = 0; // addItem: function(outer,index,entry) {
// textarea.style.width = '2em';
// textarea.style.height = '2em'; // var header = $('<div class="projects-dialog-remote-list-entry-header"></div>').appendTo(outer);
// textarea.style.padding = 0; // entry.header = $('<span>').text(entry.path||"Add new remote").appendTo(header);
// textarea.style.border = 'none'; // var body = $('<div>').appendTo(outer);
// textarea.style.outline = 'none'; // entry.body = body;
// textarea.style.boxShadow = 'none'; // if (entry.path) {
// textarea.style.background = 'transparent'; // entry.removeButton = $('<button class="editor-button editor-button-small projects-dialog-remote-list-entry-delete">remove</button>')
// textarea.value = entry.data; // // .hide()
// document.body.appendChild(textarea); // .appendTo(header)
// textarea.select(); // .click(function(e) {
// try { // entry.removed = true;
// var ret = document.execCommand('copy'); // body.fadeOut(100);
// var msg = ret ? 'successful' : 'unsuccessful'; // entry.header.css("text-decoration","line-through")
// console.log('Copy text command was ' + msg); // entry.header.css("font-style","italic")
// } catch (err) { // if (entry.copyToClipboard) {
// console.log('Oops unable to copy'); // entry.copyToClipboard.hide();
// } // }
// document.body.removeChild(textarea); // $(this).hide();
// }); // });
// } // if (entry.data) {
// } // entry.copyToClipboard = $('<button class="editor-button editor-button-small projects-dialog-remote-list-entry-copy">copy</button>')
// } // // .hide()
// }); // .appendTo(header)
// .click(function(e) {
// var remoteListAddButton = row.find(".red-ui-editableList-addButton").hide(); // var textarea = document.createElement("textarea");
// textarea.style.position = 'fixed';
var hideGitUserEditForm = function() { // textarea.style.top = 0;
editGitUserButton.show(); // textarea.style.left = 0;
formButtons.hide(); // textarea.style.width = '2em';
// $('.projects-dialog-remote-list-entry-delete').hide(); // textarea.style.height = '2em';
// remoteListAddButton.hide(); // textarea.style.padding = 0;
// textarea.style.border = 'none';
gitUsernameLabel.show(); // textarea.style.outline = 'none';
gitUsernameInput.hide(); // textarea.style.boxShadow = 'none';
gitEmailLabel.show(); // textarea.style.background = 'transparent';
gitEmailInput.hide(); // textarea.value = entry.data;
// document.body.appendChild(textarea);
} // textarea.select();
// try {
$('<button class="editor-button">Cancel</button>') // var ret = document.execCommand('copy');
.appendTo(formButtons) // var msg = ret ? 'successful' : 'unsuccessful';
.click(function(evt) { // console.log('Copy text command was ' + msg);
evt.preventDefault(); // } catch (err) {
hideGitUserEditForm(); // console.log('Oops unable to copy');
}); // }
// document.body.removeChild(textarea);
var saveButton = $('<button class="editor-button">Save</button>') // });
.appendTo(formButtons) // }
.click(function(evt) { // }
evt.preventDefault(); // }
var spinner = utils.addSpinnerOverlay(gitconfigContainer); // });
var body = { // var remoteListAddButton = row.find(".red-ui-editableList-addButton").hide();
git: { }
user: {
name: gitUsernameInput.val(), function createSettingsPane(activeProject) {
email: gitEmailInput.val() var pane = $('<div id="user-settings-tab-gitconfig" class="project-settings-tab-pane node-help"></div>');
} createRemoteRepositorySection(pane);
} return pane;
} }
var done = function(err) { var utils;
spinner.remove(); function init(_utils) {
if (err) { utils = _utils;
console.log(err); RED.userSettings.add({
return; id:'gitconfig',
} title: "Git config", // TODO: nls
hideGitUserEditForm(); get: createSettingsPane,
} close: function() {
var currentGitSettings = RED.settings.get('git') || {};
utils.sendRequest({ currentGitSettings.user = currentGitSettings.user || {};
url: "settings/user", currentGitSettings.user.name = gitUsernameInput.val();
type: "POST", currentGitSettings.user.email = gitEmailInput.val();
responses: { RED.settings.set('git', currentGitSettings);
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(); return {
} init: init,
};
function createSettingsPane(activeProject) { })();
var pane = $('<div id="user-settings-tab-gitconfig" class="project-settings-tab-pane node-help"></div>');
createRemoteRepositorySection(pane);
return pane;
}
var popover;
var utils;
function init(_utils) {
utils = _utils;
RED.userSettings.add({
id:'gitconfig',
title: "Git config", // TODO: nls
get: createSettingsPane,
close: function() {
if (popover) {
popover.close();
popover = null;
}
}
});
}
return {
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");