From a09fd94f1952d7f06386e62b2d73ea25924464aa Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 13 Jan 2019 16:07:01 -0500 Subject: [PATCH] Updated sidebars to work with the templating engine. --- sidebar/categoriesSideBar.js | 38 --------------------- sidebar/popularPosts.js | 35 ------------------- sidebar/projectSidebar.html | 10 ------ sidebar/recentPosts.js | 38 --------------------- sidebar/sidebar.js | 65 +++++++++++++++++++++++++++++------- sites/blog.js | 20 +++++++---- 6 files changed, 67 insertions(+), 139 deletions(-) delete mode 100644 sidebar/categoriesSideBar.js delete mode 100644 sidebar/popularPosts.js delete mode 100644 sidebar/projectSidebar.html delete mode 100644 sidebar/recentPosts.js diff --git a/sidebar/categoriesSideBar.js b/sidebar/categoriesSideBar.js deleted file mode 100644 index 0429960..0000000 --- a/sidebar/categoriesSideBar.js +++ /dev/null @@ -1,38 +0,0 @@ -const sql = require('../utils/sql'); - -module.exports= - { - /** - * Responsible for querying the database and displaying all - * categories that the blog has in the sidebar - * - * @param res - * @return {*|Promise} - */ - main: function() - { - return new Promise(function(resolve, reject) - { - var content = "

"; - - content += "
"; - - content += " \n" + - "
Categories
\n" + - "
"; - - sql.getCategories().then(function(categories) - { - categories.forEach(function(cat) - { - content += "" + cat.name + "
"; - }); - content += "

"; - resolve(content); - }).catch(function(error) - { - reject(error); - }); - }); - } - }; diff --git a/sidebar/popularPosts.js b/sidebar/popularPosts.js deleted file mode 100644 index be465a1..0000000 --- a/sidebar/popularPosts.js +++ /dev/null @@ -1,35 +0,0 @@ -const sql = require('../utils/sql'); - -module.exports= - { - /**Renders the popular posts sidebar. - * - * @param res - * @returns {*|Promise} - */ - main: function(res) - { - return new Promise(function(resolve, reject) - { - res.write("
"); - - res.write("
" + - "

Popular Posts

"); - - res.write("
"); - - sql.getPopularPosts().then(function(posts) - { - posts.forEach(function(cat) - { - console.log(cat); - - res.write("" + p.name + "
"); - }); - res.write("
"); - resolve(); - }); - }); - } - }; \ No newline at end of file diff --git a/sidebar/projectSidebar.html b/sidebar/projectSidebar.html deleted file mode 100644 index 1eefaa4..0000000 --- a/sidebar/projectSidebar.html +++ /dev/null @@ -1,10 +0,0 @@ -
- -

\ No newline at end of file diff --git a/sidebar/recentPosts.js b/sidebar/recentPosts.js deleted file mode 100644 index 7c8f8d3..0000000 --- a/sidebar/recentPosts.js +++ /dev/null @@ -1,38 +0,0 @@ -const Promise = require('promise'); -const sql = require('../utils/sql'); - -module.exports= - { - /** Renders the the recent post sidebar. - * - * @returns {*|Promise} - */ - main: function() - { - return new Promise(function(resolve, reject) - { - var content = "
"; - - content +="
"; - - content +=" \n" + - "
Recent Posts
\n" + - "
"; - - sql.getRecentPosts().then(function(posts) - { - posts.forEach(function(p) - { - var url = '/' + p.category + '/' + p.url; - content += "" + p.name + "
"; - }); - content +="
"; - resolve(content); - }).catch(function(error) - { - reject(error); - }) - }); - } - }; \ No newline at end of file diff --git a/sidebar/sidebar.js b/sidebar/sidebar.js index 07ddfc7..ac82c1a 100644 --- a/sidebar/sidebar.js +++ b/sidebar/sidebar.js @@ -1,22 +1,63 @@ -const utils = require('../utils/utils.js'); +const sql = require('../utils/sql'); + +const TEMPLATE_FILE = "blog/sideBar.html"; + +const includes = require('../includes/includes.js'); + + +const getInformationForRecentPosts = function(templateContext) +{ + return new Promise(function(resolve, reject) + { + sql.getRecentPosts().then(function(posts) + { + posts.forEach(function(p) + { + p.url = '/' + p.category + '/' + p.url; + }); + templateContext.recentPosts = posts; + resolve(); + }).catch(function(error) + { + reject(error); + }) + }); +}; + +const getInformationForCategories = function(templateContext) +{ + return new Promise(function(resolve, reject) + { + sql.getCategories().then(function(categories) + { + categories.forEach(function(cat) + { + cat.url = "/category/" + cat.url; + }); + + templateContext.categories = categories; + resolve(); + }).catch(function(error) + { + reject(error); + }); + }); +}; + module.exports= { - /** Method which renders the entire sidebar through calling - * appropriate widget js files. - * - * @param res - * @returns {*|Promise} - */ - main: function() + main: function(templateContext) { return new Promise(function(resolve, reject) { - Promise.all([utils.include("sidebar/projectSidebar.html"), - require("../sidebar/recentPosts.js").main(), - require("../sidebar/categoriesSideBar.js").main()]).then(function(content) + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + getInformationForRecentPosts(templateContext), + getInformationForCategories(templateContext)]) + .then(function(content) { - resolve("
" + content.join('') + "
"); + templateContext.sideBar = content[0]; + resolve(); }).catch(function(error) { reject(error); diff --git a/sites/blog.js b/sites/blog.js index a979091..319139a 100644 --- a/sites/blog.js +++ b/sites/blog.js @@ -4,6 +4,10 @@ const includes = require('../includes/includes.js'); //used to append static content to result const contentLoader = require('../includes/staticContentServer.js'); +const whiskers = require('whiskers'); + +const TEMPLATE_FILE="blog/blogMain.html"; + //caching program to make the application run faster const cache = require('memory-cache'); @@ -49,7 +53,8 @@ module.exports= const html = cache.get(filename + "?page=" + page); result.writeHead(200, {'Content-Type': 'text/html'}); - if (html == null) { + if (html == null) + { var file = ""; if (filename === '' || filename === '/') @@ -69,16 +74,19 @@ module.exports= // cache is not tricked into storing same blog post a ton of times } } - - Promise.all([includes.printHeader(), - require(file).main(filename, request), - includes.printFooter()]).then(function (content) + var templateContext = Object(); + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + includes.printHeader(templateContext), + includes.printFooter(templateContext), + require("../sidebar/sidebar.js").main(templateContext)]) + .then(function (content) { - result.write(content.join('')); + result.write(whiskers.render(content[0], templateContext)); result.end(); cache.put(filename + "?page=" + page, content.join('')); }).catch(function (err) { + console.log(err); cache.del(filename + "?page=" + page); utils.print404().then(function(content) {