Added function to switch to a different pad without having to reload the whole page.

pull/2180/head
Mike DeRosa 2014-06-14 14:24:54 -04:00
parent 71c7deecd9
commit 4ccd7131d3
4 changed files with 109 additions and 41 deletions

View File

@ -217,6 +217,8 @@ exports.handleMessage = function(client, message)
} else {
messageLogger.warn("Dropped message, unknown COLLABROOM Data Type " + message.data.type);
}
} else if(message.type == "CLEAR_SESSION_INFO") {
handleClearSessionInfo(client, message);
} else {
messageLogger.warn("Dropped message, unknown Message Type " + message.type);
}
@ -872,6 +874,32 @@ function _correctMarkersInPad(atext, apool) {
return builder.toString();
}
function handleClearSessionInfo(client, message)
{
var infoMsg = {
type: "COLLABROOM",
data: {
type: "CLEAR_CHAT_MESSAGES"
}
};
// send the messages back to the client to clear the chat messages
client.json.send(infoMsg);
// clear the session and leave the room
var currentSession = sessioninfos[client.id];
var padId = currentSession.padId;
var roomClients = socketio.sockets.clients(padId);
for(var i = 0; i < roomClients.length; i++) {
var sinfo = sessioninfos[roomClients[i].id];
if(sinfo && sinfo == currentSession) {
// fix user's counter, works on page refresh or if user closes browser window and then rejoins
sessioninfos[roomClients[i].id] = {};
roomClients[i].leave(padId);
}
}
}
/**
* Handles a CLIENT_READY. A CLIENT_READY is the first message from the client to the server. The Client sends his token
* and the pad it wants to enter. The Server answers with the inital values (clientVars) of the pad

View File

@ -79,6 +79,10 @@ var chat = (function()
this._pad.collabClient.sendMessage({"type": "CHAT_MESSAGE", "text": text});
$("#chatinput").val("");
},
clearChatMessages: function()
{
$('#chattext p').remove();
},
addMessage: function(msg, increment, isHistoryAdd)
{
//correct the time

View File

@ -388,6 +388,10 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
{
chat.addMessage(msg, true, false);
}
else if (msg.type == "CLEAR_CHAT_MESSAGES")
{
chat.clearChatMessages();
}
else if (msg.type == "CHAT_MESSAGES")
{
for(var i = msg.messages.length - 1; i >= 0; i--)

View File

@ -51,6 +51,8 @@ var gritter = require('./gritter').gritter;
var hooks = require('./pluginfw/hooks');
var receivedClientVars = false;
function createCookie(name, value, days, path){ /* Warning Internet Explorer doesn't use this it uses the one from pad_utils.js */
if (days)
{
@ -160,29 +162,28 @@ function savePassword()
return false;
}
function handshake()
function sendClearSessionInfo()
{
var loc = document.location;
//get the correct port
var port = loc.port == "" ? (loc.protocol == "https:" ? 443 : 80) : loc.port;
//create the url
var url = loc.protocol + "//" + loc.hostname + ":" + port + "/";
//find out in which subfolder we are
var resource = exports.baseURL.substring(1) + "socket.io";
//connect
socket = pad.socket = io.connect(url, {
resource: resource,
'max reconnection attempts': 3,
'sync disconnect on unload' : false
});
var msg = {
"component": "pad",
"type": "CLEAR_SESSION_INFO",
"protocolVersion": 2
};
function sendClientReady(isReconnect)
{
socket.json.send(msg);
}
function sendClientReady(isReconnect)
{
var padId = document.location.pathname.substring(document.location.pathname.lastIndexOf("/") + 1);
padId = decodeURIComponent(padId); // unescape neccesary due to Safari and Opera interpretation of spaces
if(!isReconnect)
document.title = padId.replace(/_+/g, ' ') + " | " + document.title;
{
var titleArray = document.title.split('|');
var title = titleArray[titleArray.length - 1];
document.title = padId.replace(/_+/g, ' ') + " | " + title;
}
var token = readCookie("token");
if (token == null)
@ -212,7 +213,23 @@ function handshake()
}
socket.json.send(msg);
};
}
function handshake()
{
var loc = document.location;
//get the correct port
var port = loc.port == "" ? (loc.protocol == "https:" ? 443 : 80) : loc.port;
//create the url
var url = loc.protocol + "//" + loc.hostname + ":" + port + "/";
//find out in which subfolder we are
var resource = exports.baseURL.substring(1) + "socket.io";
//connect
socket = pad.socket = io.connect(url, {
resource: resource,
'max reconnection attempts': 3,
'sync disconnect on unload' : false
});
var disconnectTimeout;
@ -228,7 +245,7 @@ function handshake()
}
pad.collabClient.setChannelState("CONNECTED");
sendClientReady(true);
pad.sendClientReady(true);
});
socket.on('disconnect', function (reason) {
@ -246,7 +263,6 @@ function handshake()
}
});
var receivedClientVars = false;
var initalized = false;
socket.on('message', function(obj)
@ -286,7 +302,7 @@ function handshake()
}
//if we haven't recieved the clientVars yet, then this message should it be
else if (!receivedClientVars)
else if (!receivedClientVars && obj.type == "CLIENT_VARS")
{
//log the message
if (window.console) console.log(obj);
@ -426,6 +442,22 @@ var pad = {
{
return pad.myUserInfo.name;
},
sendClientReady: function(isReconnect)
{
sendClientReady(isReconnect);
},
switchToPad: function(padId)
{
var options = document.location.href.split('?')[1];
if(options != null)
window.history.pushState("", "", "/p/" + padId + '?' + options);
else
window.history.pushState("", "", "/p/" + padId);
sendClearSessionInfo();
receivedClientVars = false;
sendClientReady(false);
},
sendClientMessage: function(msg)
{
pad.collabClient.sendClientMessage(msg);