mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
streaming posts. also moved topics to be entirely client side parsed. made some changes. added block level caching to templates. added a prepare method to templates
This commit is contained in:
@@ -27,7 +27,6 @@ var ajaxify = {};
|
|||||||
current_room = 'global';
|
current_room = 'global';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var url = url.replace(/\/$/, "");
|
var url = url.replace(/\/$/, "");
|
||||||
var tpl_url = (url === '' || url === '/') ? 'home' : url.split('/')[0];
|
var tpl_url = (url === '' || url === '/') ? 'home' : url.split('/')[0];
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,15 @@ var templates = {};
|
|||||||
//quick implementation because introducing a lib to handle several async callbacks
|
//quick implementation because introducing a lib to handle several async callbacks
|
||||||
if (callback == null && ready_callback) ready_callback();
|
if (callback == null && ready_callback) ready_callback();
|
||||||
else ready_callback = callback;
|
else ready_callback = callback;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
templates.prepare = function(raw_tpl, data) {
|
||||||
|
var template = {};
|
||||||
|
template.html = raw_tpl;
|
||||||
|
template.parse = parse;
|
||||||
|
template.blocks = {};
|
||||||
|
return template;
|
||||||
|
};
|
||||||
|
|
||||||
function loadTemplates(templatesToLoad) {
|
function loadTemplates(templatesToLoad) {
|
||||||
var timestamp = new Date().getTime();
|
var timestamp = new Date().getTime();
|
||||||
@@ -24,6 +32,7 @@ var templates = {};
|
|||||||
|
|
||||||
template.prototype.parse = parse;
|
template.prototype.parse = parse;
|
||||||
template.prototype.html = String(html);
|
template.prototype.html = String(html);
|
||||||
|
template.prototype.blocks = {};
|
||||||
|
|
||||||
templates[file] = new template;
|
templates[file] = new template;
|
||||||
|
|
||||||
@@ -49,6 +58,8 @@ var templates = {};
|
|||||||
|
|
||||||
//modified from https://github.com/psychobunny/dcp.templates
|
//modified from https://github.com/psychobunny/dcp.templates
|
||||||
var parse = function(data) {
|
var parse = function(data) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
function replace(key, value, template) {
|
function replace(key, value, template) {
|
||||||
var searchRegex = new RegExp('{' + key + '}', 'g');
|
var searchRegex = new RegExp('{' + key + '}', 'g');
|
||||||
return template.replace(searchRegex, value);
|
return template.replace(searchRegex, value);
|
||||||
@@ -62,6 +73,8 @@ var templates = {};
|
|||||||
data = template.match(regex);
|
data = template.match(regex);
|
||||||
if (data == null) return;
|
if (data == null) return;
|
||||||
|
|
||||||
|
if (block !== undefined) self.blocks[block] = data[0];
|
||||||
|
|
||||||
data = data[0]
|
data = data[0]
|
||||||
.replace("<!-- BEGIN " + block + " -->", "")
|
.replace("<!-- BEGIN " + block + " -->", "")
|
||||||
.replace("<!-- END " + block + " -->", "");
|
.replace("<!-- END " + block + " -->", "");
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ul class="post-container container">
|
<ul id="post-container" class="post-container container">
|
||||||
<!-- BEGIN posts -->
|
<!-- BEGIN posts -->
|
||||||
<li class="row">
|
<li class="row">
|
||||||
<div class="span1 profile-image-block">
|
<div class="span1 profile-image-block">
|
||||||
@@ -63,6 +63,28 @@ socket.on('event:rep_down', function(data) {
|
|||||||
adjust_rep(-1, data.pid, data.uid);
|
adjust_rep(-1, data.pid, data.uid);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('event:new_post', function(data) {
|
||||||
|
var html = templates.prepare(templates['topic'].blocks['posts']).parse(data);
|
||||||
|
|
||||||
|
jQuery('<div></div>').appendTo("#post-container").hide().append(html).fadeIn('slow');
|
||||||
|
});
|
||||||
|
/*
|
||||||
|
jQuery('document').ready(function() {
|
||||||
|
setTimeout(function() {
|
||||||
|
//console.log(JSON.stringify(templates['topic'].blocks));
|
||||||
|
var html = templates.prepare(templates['topic'].blocks['posts']).parse({
|
||||||
|
'posts' : [
|
||||||
|
{
|
||||||
|
'username' : 'derp'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
jQuery('<div></div>').appendTo("#post-container").hide().append(html).fadeIn('slow');
|
||||||
|
|
||||||
|
}, 1500);
|
||||||
|
});
|
||||||
|
*/
|
||||||
function adjust_rep(value, pid, uid) {
|
function adjust_rep(value, pid, uid) {
|
||||||
var post_rep = document.getElementById('post_rep_' + pid),
|
var post_rep = document.getElementById('post_rep_' + pid),
|
||||||
user_rep = document.getElementById('user_rep_' + uid);
|
user_rep = document.getElementById('user_rep_' + uid);
|
||||||
|
|||||||
27
src/posts.js
27
src/posts.js
@@ -92,12 +92,38 @@ var RDB = require('./redis.js'),
|
|||||||
Posts.create(uid, tid, content, function(pid) {
|
Posts.create(uid, tid, content, function(pid) {
|
||||||
RDB.rpush('tid:' + tid + ':posts', pid);
|
RDB.rpush('tid:' + tid + ':posts', pid);
|
||||||
|
|
||||||
|
|
||||||
socket.emit('event:alert', {
|
socket.emit('event:alert', {
|
||||||
title: 'Reply Successful',
|
title: 'Reply Successful',
|
||||||
message: 'You have successfully replied. Click here to view your reply.',
|
message: 'You have successfully replied. Click here to view your reply.',
|
||||||
type: 'notify',
|
type: 'notify',
|
||||||
timeout: 2000
|
timeout: 2000
|
||||||
});
|
});
|
||||||
|
|
||||||
|
user.get_user_postdetails([uid], function(user_details) {
|
||||||
|
user.get_gravatars_by_uids([uid], 80, function(gravatars) {
|
||||||
|
var timestamp = new Date().getTime();
|
||||||
|
|
||||||
|
socket.broadcast.to('topic_' + tid).emit('event:new_post', {
|
||||||
|
'posts' : [
|
||||||
|
{
|
||||||
|
'pid' : pid,
|
||||||
|
'content' : marked(content || ''),
|
||||||
|
'uid' : uid,
|
||||||
|
'username' : user_details.username[0] || 'anonymous',
|
||||||
|
'user_rep' : user_details.rep[0] || 0,
|
||||||
|
'post_rep' : 0,
|
||||||
|
'gravatar' : gravatars[0],
|
||||||
|
'timestamp' : timestamp,
|
||||||
|
'relativeTime': utils.relativeTime(timestamp),
|
||||||
|
'fav_star_class' :'icon-star-empty'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -117,7 +143,6 @@ var RDB = require('./redis.js'),
|
|||||||
|
|
||||||
if (callback) callback(pid);
|
if (callback) callback(pid);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -118,6 +118,8 @@ passport.deserializeUser(function(uid, done) {
|
|||||||
res.send(templates['header'] + templates['403'] + templates['footer']);
|
res.send(templates['header'] + templates['403'] + templates['footer']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
|
// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
|
||||||
(function() {
|
(function() {
|
||||||
var routes = ['', 'login', 'register'];
|
var routes = ['', 'login', 'register'];
|
||||||
@@ -131,17 +133,14 @@ passport.deserializeUser(function(uid, done) {
|
|||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
// Complex Routes
|
||||||
function generate_topic_body(req, res) {
|
app.get('/topic/:topic_id/:slug?', function(req, res) {
|
||||||
global.modules.topics.generate_topic_body(function(topic_body) {
|
res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + 'topic/' + req.params.topic_id + '");});</script>' + templates['footer']);
|
||||||
res.send(templates['header'] + topic_body + templates['footer']);
|
});
|
||||||
}, req.params.topic_id);
|
|
||||||
}
|
|
||||||
app.get('/topic/:topic_id', generate_topic_body);
|
|
||||||
app.get('/topic/:topic_id*', generate_topic_body);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// These functions are called via ajax once the initial page is loaded to populate templates with data
|
||||||
function api_method(req, res) {
|
function api_method(req, res) {
|
||||||
switch(req.params.method) {
|
switch(req.params.method) {
|
||||||
case 'home' :
|
case 'home' :
|
||||||
@@ -173,7 +172,7 @@ passport.deserializeUser(function(uid, done) {
|
|||||||
case 'topic' :
|
case 'topic' :
|
||||||
global.modules.posts.get(function(data) {
|
global.modules.posts.get(function(data) {
|
||||||
res.send(JSON.stringify(data));
|
res.send(JSON.stringify(data));
|
||||||
}, req.params.id, req.user.uid);
|
}, req.params.id, req.user.uid || 0);
|
||||||
break;
|
break;
|
||||||
default :
|
default :
|
||||||
res.send('{}');
|
res.send('{}');
|
||||||
|
|||||||
Reference in New Issue
Block a user