mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
small refactor of routing, cleanup, templates are now parsed entirely on client side for /, /register, /login for now
This commit is contained in:
@@ -22,7 +22,7 @@ var ajaxify = {};
|
|||||||
|
|
||||||
ajaxify.go = function(url, callback) {
|
ajaxify.go = function(url, callback) {
|
||||||
var url = url.replace(/\/$/, "");
|
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]) {
|
if (templates[tpl_url]) {
|
||||||
window.history.pushState({}, url, "/" + url);
|
window.history.pushState({}, url, "/" + url);
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
var templates = {};
|
var templates = {};
|
||||||
|
|
||||||
(function() {
|
(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) {
|
function loadTemplates(templatesToLoad) {
|
||||||
var timestamp = new Date().getTime();
|
var timestamp = new Date().getTime();
|
||||||
|
var loaded = templatesToLoad.length;
|
||||||
|
|
||||||
for (var t in templatesToLoad) {
|
for (var t in templatesToLoad) {
|
||||||
(function(file) {
|
(function(file) {
|
||||||
@@ -18,6 +26,12 @@ var templates = {};
|
|||||||
template.prototype.html = String(html);
|
template.prototype.html = String(html);
|
||||||
|
|
||||||
templates[file] = new template;
|
templates[file] = new template;
|
||||||
|
|
||||||
|
loaded--;
|
||||||
|
if (loaded == 0) templates.ready();
|
||||||
|
}).fail(function() {
|
||||||
|
loaded--;
|
||||||
|
if (loaded == 0) templates.ready();
|
||||||
});
|
});
|
||||||
}(templatesToLoad[t]));
|
}(templatesToLoad[t]));
|
||||||
}
|
}
|
||||||
@@ -116,7 +130,7 @@ function load_template(callback) {
|
|||||||
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : '');
|
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : '');
|
||||||
|
|
||||||
var url = location.href.replace(rootUrl +'/', '');
|
var url = location.href.replace(rootUrl +'/', '');
|
||||||
url = (url === '') ? 'home' : url;
|
url = (url === '' || url === '/') ? 'home' : url;
|
||||||
|
|
||||||
jQuery.get(API_URL + url, function(data) {
|
jQuery.get(API_URL + url, function(data) {
|
||||||
document.getElementById('content').innerHTML = templates[url.split('/')[0]].parse(JSON.parse(data));
|
document.getElementById('content').innerHTML = templates[url.split('/')[0]].parse(JSON.parse(data));
|
||||||
|
|||||||
@@ -108,6 +108,35 @@
|
|||||||
background-color: #eee;
|
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 {
|
#user_label img {
|
||||||
border: 1px solid #999;
|
border: 1px solid #999;
|
||||||
margin-right: 8px;
|
margin-right: 8px;
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ul class="topic-container">
|
<ul class="post-container">
|
||||||
<!-- BEGIN posts -->
|
<!-- BEGIN posts -->
|
||||||
<li class="topic-row">
|
<li class="post-row">
|
||||||
<p>{posts.content}</p>
|
<p>{posts.content}</p>
|
||||||
<p>Posted {posts.relativeTime} by user {posts.uid}.</p>
|
<p>Posted {posts.relativeTime} by user {posts.uid}.</p>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -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)
|
// Useful if you want to use app.put and app.delete (instead of app.post all the time)
|
||||||
// app.use(express.methodOverride());
|
// 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) {
|
function generate_topic_body(req, res) {
|
||||||
global.modules.topics.generate_topic_body(function(topic_body) {
|
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('/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) {
|
app.get('/logout', function(req, res) {
|
||||||
console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')');
|
console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')');
|
||||||
global.modules.user.logout(req.sessionID, function(logout) {
|
global.modules.user.logout(req.sessionID, function(logout) {
|
||||||
@@ -124,10 +128,6 @@ var express = require('express'),
|
|||||||
res.send(templates['header'] + templates['reset'] + templates['footer']);
|
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) {
|
app.get('/403', function(req, res) {
|
||||||
res.send(templates['header'] + templates['403'] + templates['footer']);
|
res.send(templates['header'] + templates['403'] + templates['footer']);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user