diff --git a/app.js b/app.js index 0071993..164923b 100755 --- a/app.js +++ b/app.js @@ -8,6 +8,12 @@ var routes = require("./routes").routes; // set the view engine to ejs app.set('view engine', 'ejs'); +var bodyParser = require('body-parser') +app.use( bodyParser.json() ); // to support JSON-encoded bodies +app.use(bodyParser.urlencoded({ // to support URL-encoded bodies + extended: true +})); + // include all folders app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public/css')); @@ -21,7 +27,6 @@ app.get(routes.root, function(req, res) { //crontab.create_new("/usr/bin/find", "0 2 12 * *"); //crontab.create_new("/sbin/ping -c 1 192.168.0.1 > /dev/null", "* * * * *"); crontab.crontabs( function(docs){ - console.log(docs); res.render('index', { routes : routes, crontabs : JSON.stringify(docs) @@ -29,8 +34,16 @@ app.get(routes.root, function(req, res) { }); }) -app.get(routes.save, function(req, res) { - res.render('index'); +app.post(routes.save, function(req, res) { + // new job + if(req.body._id == -1){ + crontab.create_new(req.body.name, req.body.command, req.body.schedule); + } + // edit job + else{ + crontab.update(req.body); + } + res.end(); }) app.listen(app.get('port'), function() { diff --git a/crontab.js b/crontab.js index 985403e..5e0a694 100644 --- a/crontab.js +++ b/crontab.js @@ -11,7 +11,7 @@ crontab = function(name, command, schedule, stopped){ data.name = name; data.command = command; data.schedule = schedule; - data.stopped = stopped; + if(stopped != null) data.stopped = stopped; data.timestamp = (new Date()).toString(); return data; } @@ -21,12 +21,14 @@ exports.create_new = function(name, command, schedule){ db.insert(tab); } +exports.update = function(data){ + db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null)); +} exports.crontabs = function(callback){ db.find({}, function(err, docs){ callback(docs); }); } - exports.set_crontab = function(){ exports.crontabs( function(tabs){ var crontab_string = ""; diff --git a/package.json b/package.json index 68751d7..9c59d0d 100755 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "dependencies": { "express": "latest", "ejs": "latest", - "nedb": "latest" + "nedb": "latest", + "body-parser": "latest" }, "engines": { "node": "latest" diff --git a/public/js/script.js b/public/js/script.js index 49e8411..d620786 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -52,7 +52,6 @@ function editJob(_id){ // if macro not used if(job.schedule.indexOf("@") != 0){ var components = job.schedule.split(" "); - console.log(components); $("#job-minute").val(components[0]); $("#job-hour").val(components[1]); $("#job-day").val(components[2]); @@ -63,10 +62,34 @@ function editJob(_id){ job_command = job.command; job_string(); } + + $("#job-save").click(function(){ + // TODO good old boring validations + $.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){ + location.reload(); + }) + }); } function newJob(){ + schedule = "" + job_command = "" + $("#job-minute").val("*"); + $("#job-hour").val("*"); + $("#job-day").val("*"); + $("#job-month").val("*"); + $("#job-week").val("*"); + $("#job").modal("show"); + $("#job-name").val(""); + $("#job-command").val(""); + job_string(); + $("#job-save").click(function(){ + // TODO good old boring validations + $.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){ + location.reload(); + }) + }); } @@ -82,3 +105,4 @@ function set_schedule(){ schedule = $("#job-minute").val() + " " +$("#job-hour").val() + " " +$("#job-day").val() + " " +$("#job-month").val() + " " +$("#job-week").val(); job_string(); } + diff --git a/routes.js b/routes.js index fcf12cb..05b19b0 100644 --- a/routes.js +++ b/routes.js @@ -1,5 +1,6 @@ exports.routes = { "root" : "/", "downloads" : "/downloads", - "save" : "/save" + "save" : "/save", + "crontab" : "/crontab" } diff --git a/views/popup.ejs b/views/popup.ejs index 7d2e06a..de4e4a1 100644 --- a/views/popup.ejs +++ b/views/popup.ejs @@ -58,7 +58,7 @@