Skip to content

Commit

Permalink
Implemented layout management.
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrofranceschi committed Dec 12, 2010
1 parent d683dab commit aa4ace5
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
1 change: 0 additions & 1 deletion README.markdown
Expand Up @@ -18,7 +18,6 @@ TODO
----
* Refactor with Tests
* Add other databases
* Implement layout management (user view and admin view)
* Implement posts sharing for social networks (Twitter and Facebook)
* Implement plugins API
* Implement statistics
Expand Down
63 changes: 63 additions & 0 deletions blogode.js
@@ -1,5 +1,6 @@
var express = require("express")
var sys = require("sys");
var fs = require("fs");
var app = express.createServer();
var faye = require('faye');

Expand Down Expand Up @@ -155,6 +156,68 @@ app.get('/admin/posts/destroy/:id', adminLoginFilter, function(req, res) {
});
});

app.get('/admin/template', adminLoginFilter, function(req, res) {
// returns the template file editor

res.render('admin/template/index', {
layout: false
});
});

app.get('/admin/template/get_file_content', adminLoginFilter, function(req, res) {
// returns a template file content

var fileToRead = ""
if(req.param('file_type') == 'layout') {
fileToRead = "./views/layout.ejs";
} else if(req.param('file_type') == 'index') {
fileToRead = "./views/posts/index.ejs";
} else if(req.param('file_type') == "post_show") {
fileToRead = "./views/posts/show.ejs";
} else if(req.param('file_type') == "stylesheet") {
fileToRead = "./public/stylesheet.css";
} else {
return res.send("File not found.");
}

fs.readFile(fileToRead, function(err, content) {
if (err) throw err;
return res.send(content);
});

});

app.put('/admin/template/set_file_content', adminLoginFilter, function(req, res) {
// sets a template file some content

if(req.param('content') == '' || req.param('content') == undefined) {
return res.send("Content can't be blank!");
}

var fileToWrite = ""
if(req.param('file_type') == 'layout') {
fileToWrite = "./views/layout.ejs";
} else if(req.param('file_type') == 'index') {
fileToWrite = "./views/posts/index.ejs";
} else if(req.param('file_type') == "post_show") {
fileToWrite = "./views/posts/show.ejs";
} else if(req.param('file_type') == "stylesheet") {
fileToWrite = "./public/stylesheet.css";
} else {
return res.send("File not found.");
}

fs.writeFile(fileToWrite, req.param('content'), function (err) {
if (err) throw err;
return res.redirect('/admin/template')
});

// fs.readFile(fileToRead, function(err, content) {
// return res.send(content);
// });

});

app.get("/search", function(req, res){
// performs a search for a post

Expand Down
3 changes: 3 additions & 0 deletions public/stylesheet.css
@@ -0,0 +1,3 @@
body {
background-color: #FFFFFF;
}
3 changes: 2 additions & 1 deletion views/admin/panel.ejs
@@ -1,3 +1,4 @@
Admin panel.<br/><br/>

<a href="/admin/posts">Manage posts</a>
<a href="/admin/posts">Manage posts</a><br/><br/>
<a href="/admin/template">Manage template</a>
42 changes: 42 additions & 0 deletions views/admin/template/index.ejs
@@ -0,0 +1,42 @@
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<script>
function updateCurrentEditFile() {
$("input[name=\"file_type\"]").val($("select#template_select_list").val());
$.get('/admin/template/get_file_content?file_type=' + $("select#template_select_list").val(), function(data) {
$('textarea#elm1').text(data);
});
}
function updateFileContent() {
$.ajax({
type: "PUT",
url: "/admin/template/set_file_content",
data: { content: $("textarea#elm1").val(), file_type: $("input[name=\"file_type\"]").val() },
success: function() {
}
});
}
</script>

<body onLoad="updateCurrentEditFile();">

<h1>Template manager</h1>

File to edit:
<select id="template_select_list" onChange="updateCurrentEditFile();">
<option value="layout">Content template</option>
<option value="index">Index</option>
<option value="post_show">Post reading</option>
<option value="stylesheet">Stylesheet</option>
</select>

<br/><br/>

Template editor:
<br/><br/>

<input type="hidden" name="file_type"/>
<textarea id="elm1" name="content" rows="15" cols="80" style="550px; height:300px;">
</textarea><br/><br/>

<input type="submit" id="submit" value="Save" onClick="updateFileContent();"/>
1 change: 1 addition & 0 deletions views/layout.ejs
Expand Up @@ -3,6 +3,7 @@
<title>Blogode</title>
<script src="/faye.js"></script>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<link href="/stylesheet.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>HEADER</h1>
Expand Down

0 comments on commit aa4ace5

Please sign in to comment.