Skip to content

Commit

Permalink
fix for rename. #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Marinacci committed Jun 23, 2014
1 parent 09db69d commit 3ead6e4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions electron.js
Expand Up @@ -151,6 +151,33 @@ app.post('/new',function(req,res) {
}
});

app.post('/rename',function(req,res) {
console.log(req.body.name);
if(!req.body.oldname) {
res.send(JSON.stringify({status:'missing sketch old name'}));
res.end();
return;
}
if(!req.body.newname) {
res.send(JSON.stringify({status:'missing sketch new name'}));
res.end();
return;
}

try {
var oldname = req.body.oldname;
var newname = req.body.newname;
sketches.renameSketch(oldname,newname, function(name) {
res.send(JSON.stringify({status:'okay', name:name}));
res.end();
})
} catch(err) {
console.log(err);
err.printStackTrace();
res.end(JSON.stringify({status:'error',output:err.toString()}));
}
});

app.post('/sketches/delete', function(req,res){
console.log(req.body.name);
if(!req.body.name) {
Expand Down
48 changes: 48 additions & 0 deletions public/index.html
Expand Up @@ -46,6 +46,33 @@ <h4 class="modal-title">New Sketch</h4>
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<!-- rename sketch dialog -->
<div id='rename-sketch-dialog' class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Rename Sketch</h4>
</div>
<div class="modal-body">
<form>
<div class='form-group'>
<label>Sketch Name</label>
<input type='hidden' class='form-control old-name' value='NewSketch'>
<input type='text' class='form-control new-name' value='NewSketch'>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" id='rename-sketch-button'>Create Sketch</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<nav class="navbar navbar-default navbar-fixed-top " role="navigation">
<div class="container-fluid">

Expand Down Expand Up @@ -419,6 +446,11 @@ <h3>Sketches</h3>
e.stopPropagation();
var name = $(this).attr('data-name');
console.log('renaming',name);

$("#rename-sketch-dialog .old-name").val(name);
$("#rename-sketch-dialog .new-name").val(name);

$("#rename-sketch-dialog").modal('show');
});
$("#sketch-picker .delete-button").click(function(e) {
e.preventDefault();
Expand Down Expand Up @@ -485,6 +517,22 @@ <h3>Sketches</h3>

});

$("#rename-sketch-button").click(function(e) {
e.preventDefault();
$("#rename-sketch-dialog").modal('hide');
var oldname = $("#rename-sketch-dialog .old-name").val();
var newname = $("#rename-sketch-dialog .new-name").val();
console.log('renaming name = ',oldname, 'to',newname);
POST('/rename', {oldname:oldname, newname:newname}, function(res) {
console.log("response from rename is ",res);
if(res.status == 'error') {
appendConsoleResponse(res.output);
} else {
fetchSketches(populateSketches);
}
})
});

$("#edit-sketches-button").click(function(e) {
e.preventDefault();
$("#sketch-picker .edit-button").toggleClass('hidden');
Expand Down
15 changes: 15 additions & 0 deletions sketches.js
Expand Up @@ -26,6 +26,21 @@ exports.deleteSketch = function(name, cb) {
if(cb) cb(name);
}

exports.renameSketch = function(oldname, newname, cb) {
fs.rename(
plat.getUserSketchesDir()+'/'+oldname,
plat.getUserSketchesDir()+'/'+newname, function(err) {
console.log("rename error = ",err);

fs.rename(
plat.getUserSketchesDir()+'/'+newname+'/'+oldname+'.ino',
plat.getUserSketchesDir()+'/'+newname+'/'+newname+'.ino', function(err) {
console.log("rename error = ",err);
cb(newname);
});
});
}

exports.listSketches = function(cb) {
var list = fs.readdirSync(plat.getUserSketchesDir());
list = list.filter(function(file) {
Expand Down
5 changes: 5 additions & 0 deletions test_homedir.js
@@ -0,0 +1,5 @@
var platform = require('./platform');

var plat = platform.getDefaultPlatform();

console.log("hoemdir = ",plat.getUserHome());

0 comments on commit 3ead6e4

Please sign in to comment.