Node.js based forum software built for the modern web
client side templates in, fixed home page to parse templates on ajaxify, added footer to the page transition animation for smoothness
psychobunny51734981804b
diff --git a/public/src/ajaxify.js b/public/src/ajaxify.jsindex e3a5192071..0e881f0da2 100644--- a/public/src/ajaxify.js+++ b/public/src/ajaxify.js@@ -18,17 +18,20 @@ var ajaxify = {};current_state = url;window.history.pushState({}, url, "/" + url);-- jQuery('#content').fadeOut(100, function() {- content.innerHTML = templates[tpl_url];- exec_body_scripts(content);-- ajaxify.enable();- if (callback) {- callback();- }++ jQuery('#content, #footer').fadeOut(150, function() {+ //content.innerHTML = templates[tpl_url];+ load_template(function() {+ exec_body_scripts(content);++ ajaxify.enable();+ if (callback) {+ callback();+ }++ jQuery('#content, #footer').fadeIn(200);+ });- jQuery('#content').fadeIn(200);});}@@ -111,4 +114,5 @@ var ajaxify = {};evalScript(scripts[i]);}};+}(jQuery));\ No newline at end of filediff --git a/public/src/app.js b/public/src/app.jsindex 7d64a87537..435e667cb1 100644--- a/public/src/app.js+++ b/public/src/app.js@@ -135,4 +135,7 @@ var socket,if (data.status === 'ok') alert('Logged out.');});})+++}());diff --git a/public/src/templates.js b/public/src/templates.jsindex cbe94d1643..c43c30f1ac 100644--- a/public/src/templates.js+++ b/public/src/templates.js@@ -1,19 +1,122 @@var templates = {};-function loadTemplates(templatesToLoad) {- var timestamp = new Date().getTime();-- for (var t in templatesToLoad) {- (function(template) {- $.get('/templates/' + template + '.tpl?v=' + timestamp, function(html) {- templates[template] = html;- });- }(templatesToLoad[t]));+(function() {++ function loadTemplates(templatesToLoad) {+ var timestamp = new Date().getTime();++ for (var t in templatesToLoad) {+ (function(file) {+ $.get('/templates/' + file + '.tpl?v=' + timestamp, function(html) {+ var template = function() {+ this.toString = function() {+ return this.html;+ };+ }++ template.prototype.parse = parse;+ template.prototype.html = String(html);++ templates[file] = new template;+ });+ }(templatesToLoad[t]));+ }+ }+++ function init() {+ loadTemplates([+ 'header', 'footer', 'register', 'home',+ 'login', 'reset', 'reset_code', 'account_settings',+ 'logout',+ 'emails/reset', 'emails/reset_plaintext'+ ]);+ }+++ //modified from https://github.com/psychobunny/dcp.templates+ var parse = function(data) {+ function replace(key, value, template) {+ var searchRegex = new RegExp('{' + key + '}', 'g');+ return template.replace(searchRegex, value);+ }++ function makeRegex(block) {+ return new RegExp("<!-- BEGIN " + block + " -->[^]*<!-- END " + block + " -->", 'g');+ }++ function getBlock(regex, block, template) {+ data = template.match(regex);+ if (data == null) return;++ data = data[0]+ .replace("<!-- BEGIN " + block + " -->", "")+ .replace("<!-- END " + block + " -->", "");++ return data;+ }++ function setBlock(regex, block, template) {+ return template.replace(regex, block);+ }++ var template = this.html, regex, block;++ return (function parse(data, namespace, template) {++ for (var d in data) {+ if (data.hasOwnProperty(d)) {+ if (data[d] instanceof String || data[d] === null) {+ continue;+ } else if (data[d].constructor == Array) {+ namespace += d;++ regex = makeRegex(d),+ block = getBlock(regex, namespace, template)+ if (block == null) continue;++ var numblocks = data[d].length - 1, i = 0, result = "";++ do {+ result += parse(data[d][i], namespace + '.', block);+ } while (i++ < numblocks);++ template = setBlock(regex, result, template);++ } else if (data[d] instanceof Object) {+ namespace += d + '.';++ regex = makeRegex(d),+ block = getBlock(regex, namespace, template)+ if (block == null) continue;++ block = parse(data[d], namespace, block);+ template = setBlock(regex, block, template);+ } else {+ template = replace(namespace + d, data[d], template);+ }+ }+ }++ return template;++ })(data, "", template);}-}-function templates_init() {- loadTemplates(['register', 'home', 'login', 'reset']);-}-templates_init();\ No newline at end of file+ init();++}());++function load_template(callback) {+ var location = document.location || window.location,+ rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : '');++ var url = location.href.replace(rootUrl +'/', '');+ if (url == '') url = 'home';+ jQuery.get('api/' + url, function(data) {++ document.getElementById('content').innerHTML = templates[url].parse(JSON.parse(data));+ if (callback) callback();+ });+}\ No newline at end of filediff --git a/public/templates/footer.tpl b/public/templates/footer.tplindex 9cea2835f6..472f5db1e0 100644--- a/public/templates/footer.tpl+++ b/public/templates/footer.tpl@@ -2,7 +2,7 @@</div><!--END container --><!-- START Forum Info -->- <div class="container" style="padding-top: 50px;">+ <div id="footer" class="container" style="padding-top: 50px;"><div class="alert alert-info"><span id="number_of_users"></span><br /><span id="latest_user"></span>diff --git a/public/templates/header.tpl b/public/templates/header.tplindex ecac91904d..bc132a5004 100644--- a/public/templates/header.tpl+++ b/public/templates/header.tpl@@ -110,7 +110,7 @@<div class="navbar-inner"><div class="container"><div class="nav-collapse collapse">- <a class="brand" href="#">NodeBB</a>+ <a class="brand" href="/">NodeBB</a><ul class="nav"><li class="active"><a href="/">Home</a></li><li><a href="/register">Register</a></li>diff --git a/src/topics.js b/src/topics.jsindex 446d11d91a..222c55c459 100644--- a/src/topics.js+++ b/src/topics.js@@ -26,8 +26,7 @@ var RDB = require('./redis.js'),Topics.get(function(data) {- // console.log({'topics': data});- forum_body = forum_body.parse({'topics': data});+ forum_body = forum_body.parse(data);callback(forum_body);}, start, end);};@@ -76,7 +75,7 @@ var RDB = require('./redis.js'),});}- callback(topics);+ callback({'topics': topics});});} else callback([]);diff --git a/src/webserver.js b/src/webserver.jsindex 678ad74b19..82381a5121 100644--- a/src/webserver.js+++ b/src/webserver.js@@ -57,9 +57,19 @@ var express = require('express'),app.get('/', function(req, res) {global.modules.topics.generate_forum_body(function(forum_body) {res.send(templates['header'] + forum_body + templates['footer']);- })-- //res.send(templates['header'] + templates['home'] + templates['footer']);+ });+ });++ app.get('/api/:method', function(req, res) {+ switch(req.params.method) {+ case 'home' :+ global.modules.topics.get(function(data) {+ res.send(JSON.stringify(data));+ });+ break;+ default :+ res.send('{}');+ }});app.get('/login', function(req, res) {