Baris Soner Usakli 12 years ago
commit 8a0266562c

@ -26,30 +26,28 @@ var ajaxify = {};
ajaxify.go = function(url, callback) {
// leave room and join global
if (current_room != 'global') {
socket.emit('event:enter_room', 'global');
current_room = 'global';
}
app.enter_room('global');
var url = url.replace(/\/$/, "");
var tpl_url = (url === '' || url === '/') ? 'home' : url.split('/')[0];
if (templates[tpl_url]) {
window.history.pushState({}, url, "/" + url);
jQuery('#content, #footer').fadeOut(100);
load_template(function() {
exec_body_scripts(content);
ajaxify.enable();
if (callback) {
callback();
}
jQuery('#content, #footer').fadeIn(200);
jQuery('#content, #footer').fadeOut(100, function() {
load_template(function() {
exec_body_scripts(content);
ajaxify.enable();
if (callback) {
callback();
}
jQuery('#content, #footer').fadeIn(250);
});
});
return true;
}
@ -62,7 +60,6 @@ var ajaxify = {};
ajaxify.onclick = function(ev) {
if (this.href == window.location.href + "#") return;
console.log(this.href);
var url = this.href.replace(rootUrl +'/', '');
if (ajaxify.go(url)) {

@ -1,7 +1,6 @@
var socket,
config,
app = {},
current_room,
API_URL = null;
// todo: cleanup,etc
@ -172,11 +171,21 @@ var socket,
jQuery(post_window).slideToggle(250);
};
app.current_room = null;
app.enter_room = function(room) {
if (app.current_room === room) return;
socket.emit('event:enter_room', {
'enter': room,
'leave': app.current_room
});
app.current_room = room;
};
jQuery('document').ready(function() {
if (current_room != 'global') {
socket.emit('event:enter_room', 'global');
current_room = 'global';
}
app.enter_room('global');
// On menu click, change "active" state

@ -4,7 +4,7 @@
<li class="active">{topic_name}</li>
</ul>
<small>psychobunny, null, and 2 guests are browsing this thread</small><br />
<div id="thread_active_users"></div><br />
</div>
<ul id="post-container" class="post-container container">
@ -38,15 +38,18 @@
<script type="text/javascript">
jQuery('document').ready(function() {
// join room for this thread - DRY failure, see ajaxify and app.js
socket.emit('event:enter_room', 'topic_' + '{topic_id}');
current_room = 'topic_' + '{topic_id}';
var room = 'topic_' + '{topic_id}';
app.enter_room(room);
set_up_posts();
});
ajaxify.register_events(['event:rep_up', 'event:rep_down', 'event:new_post']);
ajaxify.register_events(['event:rep_up', 'event:rep_down', 'event:new_post', 'api:get_users_in_room']);
socket.on('api:get_users_in_room', function(users) {
document.getElementById('thread_active_users').innerHTML = (users.uids.join(', ')) + ' are browsing this thread';
});
socket.on('event:rep_up', function(data) {
adjust_rep(1, data.pid, data.uid);
@ -56,6 +59,7 @@ socket.on('event:rep_down', function(data) {
adjust_rep(-1, data.pid, data.uid);
});
socket.on('event:new_post', function(data) {
var html = templates.prepare(templates['topic'].blocks['posts']).parse(data),
uniqueid = new Date().getTime();
@ -64,6 +68,8 @@ socket.on('event:new_post', function(data) {
set_up_posts(uniqueid);
});
function adjust_rep(value, pid, uid) {
var post_rep = jQuery('.post_rep_' + pid),
user_rep = jQuery('.user_rep_' + uid);
@ -109,11 +115,11 @@ function set_up_posts(div) {
if (this.children[1].className == 'icon-star-empty') {
this.children[1].className = 'icon-star';
socket.emit('api:posts.favourite', {pid: pid, room_id: current_room});
socket.emit('api:posts.favourite', {pid: pid, room_id: app.current_room});
}
else {
this.children[1].className = 'icon-star-empty';
socket.emit('api:posts.unfavourite', {pid: pid, room_id: current_room});
socket.emit('api:posts.unfavourite', {pid: pid, room_id: app.current_room});
}
});
}

@ -5,7 +5,9 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
(function(io) {
var modules = null,
users = {};
users = {},
rooms = {};
global.io = io;
module.exports.init = function() {
modules = global.modules;
@ -67,11 +69,30 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}),
});
socket.on('event:enter_room', function(data) {
if (data.leave !== null) socket.leave (data.leave);
socket.join(data.enter);
rooms[data.enter] = rooms[data.enter] || {};
if (uid) {
rooms[data.enter][uid] = true;
if (rooms[data.leave]) {
delete rooms[data.leave][uid];
}
} else {
rooms[data.enter].anonymous = rooms[data.enter].anonymous ? rooms[data.enter].anonymous + 1 : 1;
rooms[data.leave].anonymous = rooms[data.leave].anonymous ? rooms[data.enter].anonymous - 1 : 0;
}
socket.emit('api:get_users_in_room', {
uids: Object.keys(rooms[data.enter] || {}),
anonymous: rooms[data.enter] ? rooms[data.enter].anonymous : 0
});
});
// BEGIN: API calls (todo: organize)
// julian: :^)
socket.on('event:enter_room', function(room) {
socket.join(room);
});
socket.on('api:updateHeader', function(data) {
if(uid) {

Loading…
Cancel
Save