From 9119a9d10439b773b15c7897942a7af973b2d529 Mon Sep 17 00:00:00 2001
From: psychobunny <rodrigues.andrew@gmail.com>
Date: Wed, 1 May 2013 22:19:54 +0000
Subject: [PATCH] small refactor of routing, cleanup, templates are now parsed
 entirely on client side for /, /register, /login for now

---
 public/src/ajaxify.js       |  2 +-
 public/src/templates.js     | 16 +++++++++++++++-
 public/templates/header.tpl | 29 +++++++++++++++++++++++++++++
 public/templates/topic.tpl  |  4 ++--
 src/webserver.js            | 26 +++++++++++++-------------
 5 files changed, 60 insertions(+), 17 deletions(-)

diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js
index 9646720521..ac96a7d6be 100644
--- a/public/src/ajaxify.js
+++ b/public/src/ajaxify.js
@@ -22,7 +22,7 @@ var ajaxify = {};
 
 	ajaxify.go = function(url, callback) {
 		var url = url.replace(/\/$/, "");
-		var tpl_url = (url === '') ? 'home' : url.split('/')[0];
+		var tpl_url = (url === '' || url === '/') ? 'home' : url.split('/')[0];
 		
 		if (templates[tpl_url]) {
 			window.history.pushState({}, url, "/" + url);
diff --git a/public/src/templates.js b/public/src/templates.js
index e7ead3b743..f18b1e50b6 100644
--- a/public/src/templates.js
+++ b/public/src/templates.js
@@ -1,9 +1,17 @@
 var templates = {};
 
 (function() {
+	var ready_callback;
+
+	templates.ready = function(callback) {
+		//quick implementation because introducing a lib to handle several async callbacks
+		if (callback == null) ready_callback();
+		else ready_callback = callback;
+	}
 
 	function loadTemplates(templatesToLoad) {
 		var timestamp = new Date().getTime();
+		var loaded = templatesToLoad.length;
 
 		for (var t in templatesToLoad) {
 			(function(file) {
@@ -18,6 +26,12 @@ var templates = {};
 					template.prototype.html = String(html);
 					
 					templates[file] = new template;
+
+					loaded--;
+					if (loaded == 0) templates.ready();
+				}).fail(function() {
+					loaded--;
+					if (loaded == 0) templates.ready();
 				});
 			}(templatesToLoad[t]));
 		}
@@ -116,7 +130,7 @@ function load_template(callback) {
 		rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : '');
 
 	var url = location.href.replace(rootUrl +'/', '');
-	url = (url === '') ? 'home' : url;
+	url = (url === '' || url === '/') ? 'home' : url;
 
 	jQuery.get(API_URL + url, function(data) {
 		document.getElementById('content').innerHTML = templates[url.split('/')[0]].parse(JSON.parse(data));
diff --git a/public/templates/header.tpl b/public/templates/header.tpl
index 45604a7adf..a22c9e7e0a 100644
--- a/public/templates/header.tpl
+++ b/public/templates/header.tpl
@@ -108,6 +108,35 @@
 		background-color: #eee;
 	}
 
+
+
+	.post-container {
+		list-style-type: none;
+		padding: 0;
+		margin: 0;
+		border: 1px solid #eee;
+		
+	}
+	.post-container li.post-row:nth-child(odd) {
+		background-color:#fdfdfd;
+	}
+	.post-container li.post-row:nth-child(even) {
+		background-color:#fff;
+	}
+	.post-container li.post-row {
+		cursor: pointer;
+		border-bottom: 1px solid #eee;
+		padding: 10px;
+	}
+	.post-container li:last-child {
+		border-bottom: 0;
+	}
+	.post-container li.post-row:hover {
+		background-color: #eee;
+	}
+
+
+
 	#user_label img {
 		border: 1px solid #999;
 		margin-right: 8px;
diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl
index 6abc380cf8..4c8ebfa9cb 100644
--- a/public/templates/topic.tpl
+++ b/public/templates/topic.tpl
@@ -5,9 +5,9 @@
 	</ul>
 </div>
 
-<ul class="topic-container">
+<ul class="post-container">
 <!-- BEGIN posts -->
-<li class="topic-row">
+<li class="post-row">
 	<p>{posts.content}</p>
 	<p>Posted {posts.relativeTime} by user {posts.uid}.</p>
 </li>
diff --git a/src/webserver.js b/src/webserver.js
index 740d0aefdf..8a7a229c50 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -62,12 +62,20 @@ var express = require('express'),
 	// Useful if you want to use app.put and app.delete (instead of app.post all the time)
 	//	app.use(express.methodOverride());
 
-	app.get('/', function(req, res) {
-		global.modules.topics.generate_forum_body(function(forum_body) {
-			res.send(templates['header'] + forum_body + templates['footer']);	
-		});
-	});
 
+	// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
+	(function() {
+		var routes = ['', 'login', 'register'];
+
+		for (var i=0, ii=routes.length; i<ii; i++) {
+			(function(route) {
+				app.get('/' + route, function(req, res) {
+					res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + route + '");});</script>' + templates['footer']);
+				});
+			}(routes[i]));
+		}
+	}());
+	
 
 	function generate_topic_body(req, res) {
 		global.modules.topics.generate_topic_body(function(topic_body) {
@@ -100,10 +108,6 @@ var express = require('express'),
 	app.get('/api/:method/:id', api_method);
 	app.get('/api/:method/:id*', api_method);
 
-	app.get('/login', function(req, res) {
-		res.send(templates['header'] + templates['login'] + templates['footer']);
-	});
-
 	app.get('/logout', function(req, res) {
 		console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')');
 		global.modules.user.logout(req.sessionID, function(logout) {
@@ -124,10 +128,6 @@ var express = require('express'),
 		res.send(templates['header'] + templates['reset'] + templates['footer']);
 	});
 
-	app.get('/register', function(req, res) {
-		res.send(templates['header'] + templates['register'] + templates['footer']);
-	});
-
 	app.get('/403', function(req, res) {
 		res.send(templates['header'] + templates['403'] + templates['footer']);
 	});