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

Merge branch 'projects' into sshkey-management

This commit is contained in:
Hideki Nakamura 2017-12-07 23:29:58 +09:00
commit 6a5cf7a1fa
5 changed files with 103 additions and 1 deletions

View File

@ -217,6 +217,7 @@
menuOptions.push({id:"menu-item-projects-menu",label:"NLS: Projects",options:[ menuOptions.push({id:"menu-item-projects-menu",label:"NLS: Projects",options:[
{id:"menu-item-projects-new",label:"New...",disabled:false,onselect:"core:new-project"}, {id:"menu-item-projects-new",label:"New...",disabled:false,onselect:"core:new-project"},
{id:"menu-item-projects-open",label:"Open...",disabled:false,onselect:"core:open-project"}, {id:"menu-item-projects-open",label:"Open...",disabled:false,onselect:"core:open-project"},
{id:"menu-item-projects-delete",label:"Delete...",disabled:false,onselect:"core:delete-project"},
]}); ]});

View File

@ -505,6 +505,9 @@ RED.projects = (function() {
canSelectActive: false, canSelectActive: false,
dblclick: function() { dblclick: function() {
$("#projects-dialog-open").click(); $("#projects-dialog-open").click();
},
select: function() {
$("#projects-dialog-open").prop('disabled',false).removeClass('disabled ui-button-disabled ui-state-disabled');
} }
}) })
}, },
@ -536,6 +539,49 @@ RED.projects = (function() {
} }
} }
] ]
},
'delete': {
content: function() {
return createProjectList({
canSelectActive: false,
dblclick: function() {
$("#projects-dialog-delete").click();
},
select: function() {
$("#projects-dialog-delete").prop('disabled',false).removeClass('disabled ui-button-disabled ui-state-disabled');
}
})
},
buttons: [
{
// id: "clipboard-dialog-cancel",
text: RED._("common.label.cancel"),
click: function() {
$( this ).dialog( "close" );
}
},
{
id: "projects-dialog-delete",
text: "Delete project", // TODO: nls
class: "primary disabled",
disabled: true,
click: function() {
deleteProject(selectedProject.name,function(err,data) {
if (err) {
if (err.error === 'credentials_load_failed') {
dialog.dialog( "close" );
} else {
console.log("unexpected_error",err)
}
} else {
dialog.dialog( "close" );
}
})
}
}
]
} }
} }
} }
@ -565,6 +611,23 @@ RED.projects = (function() {
}) })
} }
function deleteProject(name,done) {
sendRequest({
url: "projects/"+name,
type: "DELETE",
responses: {
200: function(data) {
done(null,data);
},
400: {
'unexpected_error': function(error) {
done(error,null);
}
}
}
});
}
function show(s,options) { function show(s,options) {
if (!dialog) { if (!dialog) {
RED.projects.init(); RED.projects.init();
@ -607,7 +670,6 @@ RED.projects = (function() {
row.click(function(evt) { row.click(function(evt) {
$('.projects-dialog-project-list-entry').removeClass('selected'); $('.projects-dialog-project-list-entry').removeClass('selected');
header.addClass('selected'); header.addClass('selected');
$("#projects-dialog-open").prop('disabled',false).removeClass('disabled ui-button-disabled ui-state-disabled');
selectedProject = entry; selectedProject = entry;
if (options.select) { if (options.select) {
options.select(entry); options.select(entry);
@ -947,6 +1009,7 @@ RED.projects = (function() {
RED.actions.add("core:new-project",RED.projects.newProject); RED.actions.add("core:new-project",RED.projects.newProject);
RED.actions.add("core:open-project",RED.projects.selectProject); RED.actions.add("core:open-project",RED.projects.selectProject);
RED.actions.add("core:delete-project",RED.projects.deleteProject);
var projectsAPI = { var projectsAPI = {
sendRequest:sendRequest, sendRequest:sendRequest,
createBranchList:createBranchList, createBranchList:createBranchList,
@ -990,6 +1053,9 @@ RED.projects = (function() {
selectProject: function() { selectProject: function() {
show('open') show('open')
}, },
deleteProject: function() {
show('delete')
},
showCredentialsPrompt: function() { //TODO: rename this function showCredentialsPrompt: function() { //TODO: rename this function
RED.projects.settings.show('settings'); RED.projects.settings.show('settings');
}, },

View File

@ -117,6 +117,13 @@ module.exports = {
// Delete project - tbd // Delete project - tbd
app.delete("/:id", needsPermission("projects.write"), function(req,res) { app.delete("/:id", needsPermission("projects.write"), function(req,res) {
runtime.storage.projects.deleteProject(req.user, req.params.id).then(function() {
res.status(204).end();
})
.catch(function(err) {
console.log(err.stack);
res.status(400).json({error:"unexpected_error", message:err.toString()})
});
}); });

View File

@ -583,6 +583,11 @@ function createProjectDirectory(project) {
return fs.ensureDir(projectPath); return fs.ensureDir(projectPath);
} }
function deleteProjectDirectory(project) {
var projectPath = fspath.join(projectsDir,project);
return fs.remove(projectPath);
}
function createDefaultProject(user, project) { function createDefaultProject(user, project) {
var projectPath = fspath.join(projectsDir,project.name); var projectPath = fspath.join(projectsDir,project.name);
// Create a basic skeleton of a project // Create a basic skeleton of a project
@ -710,6 +715,23 @@ function createProject(user, metadata) {
}) })
} }
function deleteProject(user, name) {
return checkProjectExists(name).then(function() {
if (currentProject && currentProject.name === name) {
var e = new Error("NLS: Can't delete the active project");
e.code = "cannot_delete_active_project";
throw e;
}
else {
return deleteProjectDirectory(name).then(function() {
var projects = settings.get('projects');
delete projects.projects[name];
return settings.set('projects', projects);
});
}
});
}
var currentProject; var currentProject;
function getProject(name) { function getProject(name) {
@ -749,6 +771,7 @@ module.exports = {
init: init, init: init,
get: getProject, get: getProject,
create: createProject, create: createProject,
delete: deleteProject,
list: listProjects list: listProjects
} }

View File

@ -141,6 +141,10 @@ function getProject(user, name) {
}); });
} }
function deleteProject(user, name) {
return Projects.delete(user, name);
}
function checkActiveProject(project) { function checkActiveProject(project) {
if (!activeProject || activeProject.name !== project) { if (!activeProject || activeProject.name !== project) {
//TODO: throw better err //TODO: throw better err
@ -398,6 +402,7 @@ module.exports = {
getActiveProject: getActiveProject, getActiveProject: getActiveProject,
setActiveProject: setActiveProject, setActiveProject: setActiveProject,
getProject: getProject, getProject: getProject,
deleteProject: deleteProject,
createProject: createProject, createProject: createProject,
updateProject: updateProject, updateProject: updateProject,
getFiles: getFiles, getFiles: getFiles,