Skip to content

Node.js based forum software built for the modern web

fixed a bug in templates, introduced categories, added default setup script onload, moved home.tpl code over to category, created hierachies

psychobunnyfa64e84dcc14
diff --git a/app.js b/app.js
index a6391208cd..7fbd6ae288 100644
--- a/app.js
+++ b/app.js
@@ -2,6 +2,7 @@ var modules = {
user: require('./src/user.js'),
topics: require('./src/topics.js'),
posts: require('./src/posts.js'),
+ categories: require('./src/categories.js'),
templates: require('./src/templates.js'),
webserver: require('./src/webserver.js'),
websockets: require('./src/websockets.js'),
@@ -23,4 +24,36 @@ global.modules = modules;
+
+ //setup scripts to be moved outside of the app in future.
+ function setup_categories() {
+ console.log('Checking categories...');
+ modules.categories.get(function(data) {
+ if (data.categories.length === 0) {
+ console.log('Setting up default categories...');
+
+ modules.categories.create({
+ 'name': 'General',
+ 'description': 'A place to talk about whateeeever you want'
+ });
+
+
+ modules.categories.create({
+ 'name': 'NodeBB Development',
+ 'description': 'Bugs? Dont worry, we dont read this thread, so post them here.'
+ });
+
+
+ modules.categories.create({
+ 'name': 'Design Create Play',
+ 'description': 'In future an example of how a hidden category should look like.'
+ });
+
+ } else console.log('Good.');
+ });
+ }
+
+
+ setup_categories();
+
}(global.configuration));
\ No newline at end of file
diff --git a/public/src/app.js b/public/src/app.js
index 90d8c8fbc2..89be5c0a2c 100644
--- a/public/src/app.js
+++ b/public/src/app.js
@@ -98,12 +98,12 @@ var socket,
post_window = post_window || document.getElementById('post_window');
jQuery(post_window).slideToggle(250);
- if (post_mode == null || post_mode == 'topic') {
+ if (post_mode == 'topic') {
post_title.style.display = "block";
reply_title.style.display = "none";
post_title.focus();
submit_post_btn.onclick = function() {
- app.post_topic();
+ app.post_topic(id);
}
} else {
if (post_mode == 'reply') {
@@ -146,7 +146,7 @@ var socket,
jQuery(post_window).slideToggle(250);
};
- app.post_topic = function() {
+ app.post_topic = function(category_id) {
var title = document.getElementById('post_title').value,
content = document.getElementById('post_content').value;
@@ -163,11 +163,11 @@ var socket,
socket.emit('api:topics.post', {
'title' : title,
- 'content' : content
+ 'content' : content,
+ 'category_id' : category_id
});
- jQuery('#post_title').val('');
- jQuery('#post_content').val('');
+ jQuery('#post_title, #post_content').val('');
jQuery(post_window).slideToggle(250);
};
diff --git a/public/src/templates.js b/public/src/templates.js
index ee900c66c5..56ceae5c48 100644
--- a/public/src/templates.js
+++ b/public/src/templates.js
@@ -49,7 +49,7 @@ var templates = {};
function init() {
loadTemplates([
- 'header', 'footer', 'register', 'home', 'topic','account',
+ 'header', 'footer', 'register', 'home', 'topic','account', 'category',
'login', 'reset', 'reset_code', 'account',
'confirm',
'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext'
@@ -90,9 +90,8 @@ var templates = {};
var template = this.html, regex, block;
return (function parse(data, namespace, template) {
- if (data.length == 0) {
- regex = makeRegex('[^]*');
- template = template.replace(regex, '');
+ if (!data || data.length == 0) {
+ template = '';
}
for (var d in data) {
@@ -147,6 +146,7 @@ function load_template(callback) {
url = (url === '' || url === '/') ? 'home' : url;
jQuery.get(API_URL + url, function(data) {
+ console.log(data)
document.getElementById('content').innerHTML = templates[url.split('/')[0]].parse(JSON.parse(data));
if (callback) callback();
});
diff --git a/public/templates/home.tpl b/public/templates/home.tpl
index 07b76c15d5..1b9bb49ffa 100644
--- a/public/templates/home.tpl
+++ b/public/templates/home.tpl
@@ -1,29 +1,9 @@
-<button id="new_post" class="btn btn-primary btn-large">New Post</button>
+<h3>Categories</h3>
<ul class="topic-container">
-<!-- BEGIN topics -->
-<a href="topic/{topics.slug}"><li class="topic-row">
- <h4>{topics.title}</h4>
- <p>Posted {topics.relativeTime} ago by <span class="username">{topics.username}</span>. {topics.post_count} posts.</p>
+<!-- BEGIN categories -->
+<a href="category/{categories.slug}"><li class="topic-row">
+ <h4>{categories.name}</h4>
+ <p>{categories.description}</p>
</li></a>
-<!-- END topics -->
-</ul>
-<script type="text/javascript">
-var new_post = document.getElementById('new_post');
-new_post.onclick = function() {
- app.open_post_window();
-}
-
-jQuery('document').ready(function() {
-
-
-
-});
-
-
-/*app.alert({
- title: 'Welcome back',
- message: 'Some welcome message to test alerts!',
- type: 'info',
- timeout: 2000
-});*/
-</script>
\ No newline at end of file
+<!-- END categories -->
+</ul>
\ No newline at end of file
diff --git a/src/posts.js b/src/posts.js
index 060a4edf97..4c729361b1 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -1,8 +1,7 @@
var RDB = require('./redis.js'),
utils = require('./utils.js'),
marked = require('marked'),
- user = require('./user.js'),
- async = require('async');
+ user = require('./user.js');
(function(Posts) {
diff --git a/src/topics.js b/src/topics.js
index 85b04974c3..2ae3a5fa65 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -11,12 +11,14 @@ var RDB = require('./redis.js'),
- Topics.get = function(callback, start, end) {
+ Topics.get = function(callback, category_id, start, end) {
if (start == null) start = 0;
if (end == null) end = start + 10;
+ //build a proper wrapper for this and move it into above function later
+ var range_var = (category_id) ? 'categories:' + category_id + ':tid' : 'topics:tid';
- RDB.lrange('topics:tid', start, end, function(tids) {
+ RDB.lrange(range_var, start, end, function(tids) {
var title = [],
uid = [],
timestamp = [],
@@ -64,17 +66,17 @@ var RDB = require('./redis.js'),
});
}
- callback({'topics': topics});
+ callback({'category_id': category_id, 'topics': topics});
});
}
);
- } else callback([]);
+ } else callback({'category_id': category_id, 'topics': []});
});
}
- Topics.post = function(socket, uid, title, content, category) {
+ Topics.post = function(socket, uid, title, content, category_id) {
if (uid === 0) {
socket.emit('event:alert', {
@@ -102,8 +104,8 @@ var RDB = require('./redis.js'),
- if (category) {
- RDB.lpush('topics:' + category + ':tid', tid);
+ if (category_id) {
+ RDB.lpush('categories:' + category_id + ':tid', tid);
}
var slug = tid + '/' + utils.slugify(title);
diff --git a/src/webserver.js b/src/webserver.js
index 50faa63fe0..228bb58d98 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -147,6 +147,10 @@ passport.deserializeUser(function(uid, done) {
res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + 'topic/' + req.params.topic_id + '");});</script>' + templates['footer']);
});
+ app.get('/category/:category_id/:slug?', function(req, res) {
+ res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + 'category/' + req.params.category_id + '");});</script>' + templates['footer']);
+ });
+
app.get('/confirm/:code', function(req, res) {
res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + 'confirm/' + req.params.code + '");});</script>' + templates['footer']);
});
@@ -155,7 +159,7 @@ passport.deserializeUser(function(uid, done) {
function api_method(req, res) {
switch(req.params.method) {
case 'home' :
- global.modules.topics.get(function(data) {
+ global.modules.categories.get(function(data) {
res.send(JSON.stringify(data));
});
break;
@@ -185,6 +189,12 @@ passport.deserializeUser(function(uid, done) {
res.send(JSON.stringify(data));
}, req.params.id, (req.user) ? req.user.uid : 0);
break;
+ case 'category' :
+ global.modules.topics.get(function(data) {
+ console.log(data);
+ res.send(JSON.stringify(data));
+ }, req.params.id);
+ break;
case 'account' :
get_account_fn(req, res, function(userData) {
res.send(JSON.stringify(userData));
diff --git a/src/websockets.js b/src/websockets.js
index 50373a7347..d9e17c80bf 100644
--- a/src/websockets.js
+++ b/src/websockets.js
@@ -161,7 +161,7 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
});
socket.on('api:topics.post', function(data) {
- modules.topics.post(socket, uid, data.title, data.content);
+ modules.topics.post(socket, uid, data.title, data.content, data.category_id);
});
socket.on('api:posts.reply', function(data) {