Permalink
Browse files

Implemented templates changing.

  • Loading branch information...
pedrofranceschi committed Dec 12, 2010
1 parent aa4ace5 commit b44e75a1e13336bde8493a541d5c439d6cb4a7f4
View
@@ -26,6 +26,7 @@ bayeux = new faye.NodeAdapter({
var posts = require('./lib/posts');
var users = require('./lib/users');
var comments = require('./lib/comments');
+var config = require('./lib/config');
app.get("/", function(req, res){
// return posts list
@@ -159,9 +160,29 @@ 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
- });
+ config.getBlogConfigKeyValue('current_template', function(value) {
+ var templateInfos = new Array();
+ fs.readdir('./public/templates', function(err, files) {
+ for (var i=0; i < files.length; i++) {
+ var content = fs.readFileSync('./public/templates/' + files[i] + '/info.json')
+ var templateParser = JSON.parse(content);
+ templateParser['template_folder_name'] = files[i];
+
+ var isCurrentTheme = false;
+ if(value == files[i]) {
+ isCurrentTheme = true;
+ }
+
+ templateParser['current_theme'] = isCurrentTheme;
+ templateInfos.push(templateParser);
+ }
+
+ res.render('admin/template/index', {
+ layout: false,
+ locals: { 'template_infos': templateInfos }
+ });
+ });
+ })
});
app.get('/admin/template/get_file_content', adminLoginFilter, function(req, res) {
@@ -212,10 +233,50 @@ app.put('/admin/template/set_file_content', adminLoginFilter, function(req, res)
return res.redirect('/admin/template')
});
- // fs.readFile(fileToRead, function(err, content) {
- // return res.send(content);
- // });
+});
+
+app.post('/admin/template/apply_template', adminLoginFilter, function(req, res) {
+ // apply a template as the current template
+
+ if(req.param('name') == '' || req.param('name') == undefined) {
+ return res.send("Template name can't be blank!");
+ }
+
+ var templatePath = './public/templates/' + req.param('name');
+ fs.readFile(templatePath + '/info.json', function(err, content) {
+ if (err) {
+ if(err.message.indexOf("No such file or directory") >= 0) {
+ return res.send("Template not found");
+ } else {
+ return res.send("Unknown error")
+ }
+ }
+
+ fs.readFile(templatePath + '/layout.html', function(err, content) {
+ if (err) return res.send("Layout file not found");
+ fs.writeFile('./views/layout.ejs', content, function (err) {
+ fs.readFile(templatePath + '/posts/index.html', function(err, content) {
+ if (err) return res.send("Index not found");
+ fs.writeFile('./views/posts/index.ejs', content, function (err) {
+ fs.readFile(templatePath + '/posts/show.html', function(err, content) {
+ if (err) return res.send("Read post file not found");
+ fs.writeFile('./views/posts/show.ejs', content, function (err) {
+ fs.readFile(templatePath + '/stylesheet.css', function(err, content) {
+ if (err) return res.send("Stylesheet not found");
+ fs.writeFile('./public/stylesheet.css', content, function (err) {
+ config.setBlogConfigKeyValue('current_template', req.param('name'), function() {
+ return res.redirect('/admin/template');
+ })
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
});
app.get("/search", function(req, res){
View
@@ -0,0 +1 @@
+{"current_template":"default_theme"}
View
@@ -0,0 +1,29 @@
+var sys = require('sys');
+var fs = require('fs');
+
+exports.getBlogConfigKeyValue = function(key, callback) {
+ fs.readFile('./config/blog.json', function(err, content) {
+ if(err) {
+ throw err;
+ } else {
+ var configParser = JSON.parse(content);
+ callback(configParser[key]);
+ }
+ });
+}
+
+exports.setBlogConfigKeyValue = function(key, value, callback) {
+ fs.readFile('./config/blog.json', function(err, content) {
+ if(err) {
+ throw err;
+ } else {
+ var configParser = JSON.parse(content);
+ configParser[key] = value;
+
+ fs.writeFile('./config/blog.json', JSON.stringify(configParser), function (err) {
+ if (err) throw err;
+ callback();
+ });
+ }
+ });
+}
@@ -0,0 +1,6 @@
+{
+ "template_name": "Blogode's Default Template.",
+ "author_name": "Pedro Franceschi",
+ "author_email": "pedrohfranceschi@gmail.com",
+ "version": "0.1"
+}
@@ -0,0 +1,19 @@
+<html>
+ <head>
+ <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>
+ <form method="get" action="/search">
+ <input type="text" name="keywords" placeholder="Search term">
+ <input type="submit" value="search">
+ </form>
+ <br/>
+ <%- body %>
+ <br/><br/>
+ <h1>FOOTER</h1>
+ </body>
+</html>
@@ -0,0 +1,13 @@
+<link rel="alternate" type="application/rss+xml" title="My Blog Name's RSS" href="/feed" />
+
+<% if(posts != undefined) { %>
+ <% for (var i=0; i < posts.length; i++) { %>
+ <h2><a href="/<%= posts[i].id %>"><%= unescape(posts[i].title) %></a></h2>
+ <p>by <%= posts[i].user_name %></p>
+ <% if(unescape(posts[i].body).indexOf("###") >= 0) { %>
+ <p><%- unescape(posts[i].body).split("###")[0] %></p><br/>
+ <% } else { %>
+ <p><%- unescape(posts[i].body) %></p><br/>
+ <% } %>
+ <% } %>
+<% } %>
@@ -0,0 +1,55 @@
+<script>
+
+$(document).ready(function() {
+ $("#submit").click(function() {
+ $.ajax({
+ type: "POST",
+ url: "/<%= post.id %>/comments/save",
+ data: { author_name: $("input#author_name").val(), author_email: $("input#author_email").val(), comment: $("textarea#comment").val() },
+ success: function() {
+ }
+ });
+ return false;
+ });
+});
+
+var client = new Faye.Client('/faye', {
+ timeout: 120
+});
+
+client.subscribe('/<%= post.id %>/comments/bayeux', function(response) {
+ $("#comments_list").append("<div id=\"comment_" + response.id + "\"> " +
+ " <h4>" + unescape(response.author_name) + "</h4> " +
+ " <p>" + unescape(response.comment) + "</p> " +
+ "</div>");
+});
+
+</script>
+
+<h1><%= unescape(post.title) %></h1>
+<p>by <%= post.user_name %></p>
+<p><%- unescape(post.body) %></p>
+
+<br/>
+<h2>Comments</h2>
+
+<div id="comments_list">
+ <% for (var i=0; i < comments.length; i++) { %>
+ <div id="comment_<%= i %>">
+ <h4><%= unescape(comments[i].author_name) %></h4>
+ <p><%= unescape(comments[i].comment) %></p>
+ </div>
+ <% } %>
+</div>
+
+<br/>
+<h2>New comment</h2>
+
+<form>
+ <input type="text" id="author_name" placeholder="Your name" size="41"/><br/>
+ <input type="text" id="author_email" placeholder="Your e-mail" size="41"/><br/><br/>
+
+ <textarea id="comment" cols="30" rows="5" placeholder="Your comment"></textarea><br/><br/>
+
+ <input type="submit" id="submit"/>
+</form>
@@ -0,0 +1,3 @@
+body {
+ background-color: #FFFFFF;
+}
@@ -0,0 +1,6 @@
+{
+ "template_name": "My other theme",
+ "author_name": "Pedro Franceschi",
+ "author_email": "pedrohfranceschi@gmail.com",
+ "version": "0.1"
+}
@@ -0,0 +1,19 @@
+<html>
+ <head>
+ <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 (of my other theme)</h1>
+ <form method="get" action="/search">
+ <input type="text" name="keywords" placeholder="Search term">
+ <input type="submit" value="search">
+ </form>
+ <br/>
+ <%- body %>
+ <br/><br/>
+ <h1>FOOTER</h1>
+ </body>
+</html>
@@ -0,0 +1,13 @@
+<link rel="alternate" type="application/rss+xml" title="My Blog Name's RSS" href="/feed" />
+
+<% if(posts != undefined) { %>
+ <% for (var i=0; i < posts.length; i++) { %>
+ <h2><a href="/<%= posts[i].id %>"><%= unescape(posts[i].title) %></a></h2>
+ <p>by <%= posts[i].user_name %></p>
+ <% if(unescape(posts[i].body).indexOf("###") >= 0) { %>
+ <p><%- unescape(posts[i].body).split("###")[0] %></p><br/>
+ <% } else { %>
+ <p><%- unescape(posts[i].body) %></p><br/>
+ <% } %>
+ <% } %>
+<% } %>
@@ -0,0 +1,55 @@
+<script>
+
+$(document).ready(function() {
+ $("#submit").click(function() {
+ $.ajax({
+ type: "POST",
+ url: "/<%= post.id %>/comments/save",
+ data: { author_name: $("input#author_name").val(), author_email: $("input#author_email").val(), comment: $("textarea#comment").val() },
+ success: function() {
+ }
+ });
+ return false;
+ });
+});
+
+var client = new Faye.Client('/faye', {
+ timeout: 120
+});
+
+client.subscribe('/<%= post.id %>/comments/bayeux', function(response) {
+ $("#comments_list").append("<div id=\"comment_" + response.id + "\"> " +
+ " <h4>" + unescape(response.author_name) + "</h4> " +
+ " <p>" + unescape(response.comment) + "</p> " +
+ "</div>");
+});
+
+</script>
+
+<h1><%= unescape(post.title) %></h1>
+<p>by <%= post.user_name %></p>
+<p><%- unescape(post.body) %></p>
+
+<br/>
+<h2>Comments</h2>
+
+<div id="comments_list">
+ <% for (var i=0; i < comments.length; i++) { %>
+ <div id="comment_<%= i %>">
+ <h4><%= unescape(comments[i].author_name) %></h4>
+ <p><%= unescape(comments[i].comment) %></p>
+ </div>
+ <% } %>
+</div>
+
+<br/>
+<h2>New comment</h2>
+
+<form>
+ <input type="text" id="author_name" placeholder="Your name" size="41"/><br/>
+ <input type="text" id="author_email" placeholder="Your e-mail" size="41"/><br/><br/>
+
+ <textarea id="comment" cols="30" rows="5" placeholder="Your comment"></textarea><br/><br/>
+
+ <input type="submit" id="submit"/>
+</form>
@@ -0,0 +1,3 @@
+body {
+ background-color: #F1F1F1;
+}
@@ -1,8 +1,8 @@
<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) {
+ $("input[name=\"file_type\"]").val($("select#file_select_list").val());
+ $.get('/admin/template/get_file_content?file_type=' + $("select#file_select_list").val(), function(data) {
$('textarea#elm1').text(data);
});
}
@@ -22,8 +22,24 @@
<h1>Template manager</h1>
+<form method="post" action="/admin/template/apply_template">
+ Change theme:
+ <select id="template_select_list" name="name">
+ <% for (var i=0; i < template_infos.length; i++) { %>
+ <option value="<%= template_infos[i].template_folder_name %>"
+
+ <% if(template_infos[i].current_theme == true) { %>
+ selected
+ <% } %>
+
+ ><%= template_infos[i].template_name %></option>
+ <% } %>
+ </select>
+ <input type="submit" value="Apply">
+</form>
+
File to edit:
-<select id="template_select_list" onChange="updateCurrentEditFile();">
+<select id="file_select_list" onChange="updateCurrentEditFile();">
<option value="layout">Content template</option>
<option value="index">Index</option>
<option value="post_show">Post reading</option>

0 comments on commit b44e75a

Please sign in to comment.