Browse Source

Updated sidebars to work with the templating engine.

pull/41/head
jrtechs 5 years ago
parent
commit
a09fd94f19
6 changed files with 67 additions and 139 deletions
  1. +0
    -38
      sidebar/categoriesSideBar.js
  2. +0
    -35
      sidebar/popularPosts.js
  3. +0
    -10
      sidebar/projectSidebar.html
  4. +0
    -38
      sidebar/recentPosts.js
  5. +53
    -12
      sidebar/sidebar.js
  6. +14
    -6
      sites/blog.js

+ 0
- 38
sidebar/categoriesSideBar.js View File

@ -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 = "<br><br><div class=\"container\">";
content += "<div class=\"list-group\">";
content += " <a href=\"#\" class=\"list-group-item list-group-item-action flex-column align-items-start active\">\n" +
" <h5 class=\"mb-1\">Categories</h5>\n" +
" </a>";
sql.getCategories().then(function(categories)
{
categories.forEach(function(cat)
{
content += "<a class=\"list-group-item\" href='/category/" + cat.url + "'>" + cat.name + "<br></a>";
});
content += "</div></div><br>";
resolve(content);
}).catch(function(error)
{
reject(error);
});
});
}
};

+ 0
- 35
sidebar/popularPosts.js View File

@ -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("<div class=\"w3-card w3-margin\">");
res.write("<div class=\"w3-container w3-padding\">" +
"<h4>Popular Posts</h4></div>");
res.write("<div class=\"w3-sidebar w3-bar-block\">");
sql.getPopularPosts().then(function(posts)
{
posts.forEach(function(cat)
{
console.log(cat);
res.write("<a class=\"w3-bar-item w3-button\" href='"
+ url + "'>" + p.name + "<br></a>");
});
res.write("</div></div>");
resolve();
});
});
}
};

+ 0
- 10
sidebar/projectSidebar.html View File

@ -1,10 +0,0 @@
<div class="container">
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<h5 class="mb-1">Project Sites</h5>
</a>
<a class="list-group-item" href='https://jrtechs.net/steam/'>Steam Graph Analysis<br></a>
<a class="list-group-item" href='https://jrtechs.me/'>Portfolio<br></a>
<a class="list-group-item" href='https://clubpanda.jrtechs.net/'>Club Panda<br></a>
</div>
</div><br>

+ 0
- 38
sidebar/recentPosts.js View File

@ -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 = "<div class=\"container\">";
content +="<div class=\"list-group\">";
content +=" <a href=\"#\" class=\"list-group-item list-group-item-action flex-column align-items-start active\">\n" +
" <h5 class=\"mb-1\">Recent Posts</h5>\n" +
" </a>";
sql.getRecentPosts().then(function(posts)
{
posts.forEach(function(p)
{
var url = '/' + p.category + '/' + p.url;
content += "<a class=\"list-group-item\" href='"
+ url + "'>" + p.name + "<br></a>";
});
content +="</div></div>";
resolve(content);
}).catch(function(error)
{
reject(error);
})
});
}
};

+ 53
- 12
sidebar/sidebar.js View File

@ -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("<div class=\"col-md-4\">" + content.join('') + "</div>");
templateContext.sideBar = content[0];
resolve();
}).catch(function(error)
{
reject(error);

+ 14
- 6
sites/blog.js View File

@ -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)
{

Loading…
Cancel
Save