pad.pub0.org/static/js/collab_client.js

897 lines
21 KiB
JavaScript
Raw Normal View History

2011-03-26 13:10:41 +00:00
/**
* Copyright 2009 Google Inc.
2011-07-07 17:59:34 +00:00
*
2011-03-26 13:10:41 +00:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-07-07 17:59:34 +00:00
*
2011-03-26 13:10:41 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 17:59:34 +00:00
*
2011-03-26 13:10:41 +00:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2011-07-07 17:59:34 +00:00
$(window).bind("load", function()
{
2011-03-26 13:10:41 +00:00
getCollabClient.windowLoaded = true;
});
/** Call this when the document is ready, and a new Ace2Editor() has been created and inited.
ACE's ready callback does not need to have fired yet.
"serverVars" are from calling doc.getCollabClientVars() on the server. */
2011-07-07 17:59:34 +00:00
function getCollabClient(ace2editor, serverVars, initialUserInfo, options)
{
2011-03-26 13:10:41 +00:00
var editor = ace2editor;
var rev = serverVars.rev;
var padId = serverVars.padId;
var globalPadId = serverVars.globalPadId;
var state = "IDLE";
var stateMessage;
var stateMessageSocketId;
var channelState = "CONNECTING";
var appLevelDisconnectReason = null;
var lastCommitTime = 0;
var initialStartConnectTime = 0;
var userId = initialUserInfo.userId;
var socketId;
//var socket;
var userSet = {}; // userId -> userInfo
userSet[userId] = initialUserInfo;
var reconnectTimes = [];
var caughtErrors = [];
var caughtErrorCatchers = [];
var caughtErrorTimes = [];
var debugMessages = [];
tellAceAboutHistoricalAuthors(serverVars.historicalAuthorData);
tellAceActiveAuthorInfo(initialUserInfo);
var callbacks = {
2011-07-07 17:59:34 +00:00
onUserJoin: function()
{},
onUserLeave: function()
{},
onUpdateUserInfo: function()
{},
onChannelStateChange: function()
{},
onClientMessage: function()
{},
onInternalAction: function()
{},
onConnectionTrouble: function()
{},
onServerMessage: function()
{}
2011-03-26 13:10:41 +00:00
};
2011-07-07 17:59:34 +00:00
$(window).bind("unload", function()
{
if (socket)
{
/*socket.onclosed = function() {};
2011-03-26 13:10:41 +00:00
socket.onhiccup = function() {};
socket.disconnect(true);*/
socket.disconnect();
}
});
2011-07-07 17:59:34 +00:00
if ($.browser.mozilla)
{
2011-03-26 13:10:41 +00:00
// Prevent "escape" from taking effect and canceling a comet connection;
// doesn't work if focus is on an iframe.
2011-07-07 17:59:34 +00:00
$(window).bind("keydown", function(evt)
{
if (evt.which == 27)
{
evt.preventDefault()
}
});
2011-03-26 13:10:41 +00:00
}
editor.setProperty("userAuthor", userId);
editor.setBaseAttributedText(serverVars.initialAttributedText, serverVars.apool);
editor.setUserChangeNotificationCallback(wrapRecordingErrors("handleUserChanges", handleUserChanges));
2011-07-07 17:59:34 +00:00
function abandonConnection(reason)
{
if (socket)
{
/*socket.onclosed = function() {};
2011-03-26 13:10:41 +00:00
socket.onhiccup = function() {};*/
2011-07-07 17:59:34 +00:00
socket.disconnect();
2011-03-26 13:10:41 +00:00
}
socket = null;
setChannelState("DISCONNECTED", reason);
}
2011-07-07 17:59:34 +00:00
function dmesg(str)
{
if (typeof window.ajlog == "string") window.ajlog += str + '\n';
2011-03-26 13:10:41 +00:00
debugMessages.push(str);
}
2011-07-07 17:59:34 +00:00
function handleUserChanges()
{
if ((!socket) || channelState == "CONNECTING")
{
if (channelState == "CONNECTING" && (((+new Date()) - initialStartConnectTime) > 20000))
{
2011-03-26 13:10:41 +00:00
abandonConnection("initsocketfail"); // give up
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
// check again in a bit
2011-07-07 17:59:34 +00:00
setTimeout(wrapRecordingErrors("setTimeout(handleUserChanges)", handleUserChanges), 1000);
2011-03-26 13:10:41 +00:00
}
return;
}
var t = (+new Date());
2011-07-07 17:59:34 +00:00
if (state != "IDLE")
{
if (state == "COMMITTING" && (t - lastCommitTime) > 20000)
{
2011-03-26 13:10:41 +00:00
// a commit is taking too long
appLevelDisconnectReason = "slowcommit";
socket.disconnect();
}
2011-07-07 17:59:34 +00:00
else if (state == "COMMITTING" && (t - lastCommitTime) > 5000)
{
2011-03-26 13:10:41 +00:00
callbacks.onConnectionTrouble("SLOW");
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
// run again in a few seconds, to detect a disconnect
2011-07-07 17:59:34 +00:00
setTimeout(wrapRecordingErrors("setTimeout(handleUserChanges)", handleUserChanges), 3000);
2011-03-26 13:10:41 +00:00
}
return;
}
var earliestCommit = lastCommitTime + 500;
2011-07-07 17:59:34 +00:00
if (t < earliestCommit)
{
setTimeout(wrapRecordingErrors("setTimeout(handleUserChanges)", handleUserChanges), earliestCommit - t);
2011-03-26 13:10:41 +00:00
return;
}
var sentMessage = false;
var userChangesData = editor.prepareUserChangeset();
2011-07-07 17:59:34 +00:00
if (userChangesData.changeset)
{
2011-03-26 13:10:41 +00:00
lastCommitTime = t;
state = "COMMITTING";
2011-07-07 17:59:34 +00:00
stateMessage = {
type: "USER_CHANGES",
baseRev: rev,
changeset: userChangesData.changeset,
apool: userChangesData.apool
};
2011-03-26 13:10:41 +00:00
stateMessageSocketId = socketId;
sendMessage(stateMessage);
sentMessage = true;
callbacks.onInternalAction("commitPerformed");
}
2011-07-07 17:59:34 +00:00
if (sentMessage)
{
2011-03-26 13:10:41 +00:00
// run again in a few seconds, to detect a disconnect
2011-07-07 17:59:34 +00:00
setTimeout(wrapRecordingErrors("setTimeout(handleUserChanges)", handleUserChanges), 3000);
2011-03-26 13:10:41 +00:00
}
}
2011-07-07 17:59:34 +00:00
function getStats()
{
2011-03-26 13:10:41 +00:00
var stats = {};
2011-07-07 17:59:34 +00:00
stats.screen = [$(window).width(), $(window).height(), window.screen.availWidth, window.screen.availHeight, window.screen.width, window.screen.height].join(',');
2011-03-26 13:10:41 +00:00
stats.ip = serverVars.clientIp;
stats.useragent = serverVars.clientAgent;
return stats;
}
2011-07-07 17:59:34 +00:00
function setUpSocket()
2011-03-26 13:10:41 +00:00
{
//oldSocketId = String(Math.floor(Math.random()*1e12));
//socketId = String(Math.floor(Math.random()*1e12));
2011-07-07 17:59:34 +00:00
/*socket = new io.Socket();
2011-03-26 13:10:41 +00:00
socket.connect();*/
2011-07-07 17:59:34 +00:00
2011-03-26 13:10:41 +00:00
//socket.on('connect', function(){
2011-07-07 17:59:34 +00:00
hiccupCount = 0;
setChannelState("CONNECTED");
/*var msg = { type:"CLIENT_READY", roomType:'padpage',
2011-03-26 13:10:41 +00:00
roomName:'padpage/'+globalPadId,
data: {
lastRev:rev,
userInfo:userSet[userId],
stats: getStats() } };
if (oldSocketId) {
msg.data.isReconnectOf = oldSocketId;
msg.data.isCommitPending = (state == "COMMITTING");
}
sendMessage(msg);*/
2011-07-07 17:59:34 +00:00
doDeferredActions();
initialStartConnectTime = +new Date();
// });
/*socket.on('message', function(obj){
2011-03-26 13:10:41 +00:00
if(window.console)
console.log(obj);
handleMessageFromServer(obj);
});*/
2011-07-07 17:59:34 +00:00
socket.on('disconnect', function(obj)
{
2011-03-26 13:10:41 +00:00
handleSocketClosed(true);
});
2011-07-07 17:59:34 +00:00
/*var success = false;
2011-03-26 13:10:41 +00:00
callCatchingErrors("setUpSocket", function() {
appLevelDisconnectReason = null;
var oldSocketId = socketId;
socketId = String(Math.floor(Math.random()*1e12));
socket = new WebSocket(socketId);
socket.onmessage = wrapRecordingErrors("socket.onmessage", handleMessageFromServer);
socket.onclosed = wrapRecordingErrors("socket.onclosed", handleSocketClosed);
socket.onopen = wrapRecordingErrors("socket.onopen", function() {
hiccupCount = 0;
setChannelState("CONNECTED");
var msg = { type:"CLIENT_READY", roomType:'padpage',
roomName:'padpage/'+globalPadId,
data: {
lastRev:rev,
userInfo:userSet[userId],
stats: getStats() } };
if (oldSocketId) {
msg.data.isReconnectOf = oldSocketId;
msg.data.isCommitPending = (state == "COMMITTING");
}
sendMessage(msg);
doDeferredActions();
});
socket.onhiccup = wrapRecordingErrors("socket.onhiccup", handleCometHiccup);
socket.onlogmessage = dmesg;
socket.connect();
success = true;
});
if (success) {
initialStartConnectTime = +new Date();
}
else {
abandonConnection("initsocketfail");
}*/
}
2011-07-07 17:59:34 +00:00
function setUpSocketWhenWindowLoaded()
{
if (getCollabClient.windowLoaded)
{
2011-03-26 13:10:41 +00:00
setUpSocket();
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
setTimeout(setUpSocketWhenWindowLoaded, 200);
}
}
setTimeout(setUpSocketWhenWindowLoaded, 0);
var hiccupCount = 0;
2011-07-07 17:59:34 +00:00
function handleCometHiccup(params)
{
dmesg("HICCUP (connected:" + ( !! params.connected) + ")");
2011-03-26 13:10:41 +00:00
var connectedNow = params.connected;
2011-07-07 17:59:34 +00:00
if (!connectedNow)
{
2011-03-26 13:10:41 +00:00
hiccupCount++;
// skip first "cut off from server" notification
2011-07-07 17:59:34 +00:00
if (hiccupCount > 1)
{
2011-03-26 13:10:41 +00:00
setChannelState("RECONNECTING");
}
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
hiccupCount = 0;
setChannelState("CONNECTED");
}
}
2011-07-07 17:59:34 +00:00
function sendMessage(msg)
{
socket.json.send(
{
type: "COLLABROOM",
component: "pad",
data: msg
});
2011-03-26 13:10:41 +00:00
}
2011-07-07 17:59:34 +00:00
function wrapRecordingErrors(catcher, func)
{
return function()
{
try
{
2011-03-26 13:10:41 +00:00
return func.apply(this, Array.prototype.slice.call(arguments));
}
2011-07-07 17:59:34 +00:00
catch (e)
{
2011-03-26 13:10:41 +00:00
caughtErrors.push(e);
caughtErrorCatchers.push(catcher);
caughtErrorTimes.push(+new Date());
//console.dir({catcher: catcher, e: e});
throw e;
}
};
}
2011-07-07 17:59:34 +00:00
function callCatchingErrors(catcher, func)
{
try
{
2011-03-26 13:10:41 +00:00
wrapRecordingErrors(catcher, func)();
}
2011-07-07 17:59:34 +00:00
catch (e)
{ /*absorb*/
}
2011-03-26 13:10:41 +00:00
}
2011-07-07 17:59:34 +00:00
function handleMessageFromServer(evt)
{
if (window.console) console.log(evt);
if (!socket) return;
if (!evt.data) return;
2011-03-26 13:10:41 +00:00
var wrapper = evt;
2011-07-07 17:59:34 +00:00
if (wrapper.type != "COLLABROOM") return;
2011-03-26 13:10:41 +00:00
var msg = wrapper.data;
2011-07-07 17:59:34 +00:00
if (msg.type == "NEW_CHANGES")
{
2011-03-26 13:10:41 +00:00
var newRev = msg.newRev;
var changeset = msg.changeset;
var author = (msg.author || '');
var apool = msg.apool;
2011-07-07 17:59:34 +00:00
if (newRev != (rev + 1))
{
dmesg("bad message revision on NEW_CHANGES: " + newRev + " not " + (rev + 1));
2011-03-26 13:10:41 +00:00
socket.disconnect();
return;
}
rev = newRev;
editor.applyChangesToBase(changeset, author, apool);
}
2011-07-07 17:59:34 +00:00
else if (msg.type == "ACCEPT_COMMIT")
{
2011-03-26 13:10:41 +00:00
var newRev = msg.newRev;
2011-07-07 17:59:34 +00:00
if (newRev != (rev + 1))
{
dmesg("bad message revision on ACCEPT_COMMIT: " + newRev + " not " + (rev + 1));
2011-03-26 13:10:41 +00:00
socket.disconnect();
return;
}
rev = newRev;
editor.applyPreparedChangesetToBase();
setStateIdle();
2011-07-07 17:59:34 +00:00
callCatchingErrors("onInternalAction", function()
{
2011-03-26 13:10:41 +00:00
callbacks.onInternalAction("commitAcceptedByServer");
});
2011-07-07 17:59:34 +00:00
callCatchingErrors("onConnectionTrouble", function()
{
2011-03-26 13:10:41 +00:00
callbacks.onConnectionTrouble("OK");
});
handleUserChanges();
}
2011-07-07 17:59:34 +00:00
else if (msg.type == "NO_COMMIT_PENDING")
{
if (state == "COMMITTING")
{
2011-03-26 13:10:41 +00:00
// server missed our commit message; abort that commit
setStateIdle();
handleUserChanges();
}
}
2011-07-07 17:59:34 +00:00
else if (msg.type == "USER_NEWINFO")
{
2011-03-26 13:10:41 +00:00
var userInfo = msg.userInfo;
var id = userInfo.userId;
2011-07-07 17:59:34 +00:00
if (userSet[id])
{
2011-03-26 13:10:41 +00:00
userSet[id] = userInfo;
callbacks.onUpdateUserInfo(userInfo);
dmesgUsers();
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
userSet[id] = userInfo;
callbacks.onUserJoin(userInfo);
dmesgUsers();
}
tellAceActiveAuthorInfo(userInfo);
}
2011-07-07 17:59:34 +00:00
else if (msg.type == "USER_LEAVE")
{
2011-03-26 13:10:41 +00:00
var userInfo = msg.userInfo;
var id = userInfo.userId;
2011-07-07 17:59:34 +00:00
if (userSet[id])
{
2011-03-26 13:10:41 +00:00
delete userSet[userInfo.userId];
fadeAceAuthorInfo(userInfo);
callbacks.onUserLeave(userInfo);
dmesgUsers();
}
}
2011-07-07 17:59:34 +00:00
else if (msg.type == "DISCONNECT_REASON")
{
2011-03-26 13:10:41 +00:00
appLevelDisconnectReason = msg.reason;
}
2011-07-07 17:59:34 +00:00
else if (msg.type == "CLIENT_MESSAGE")
{
2011-03-26 13:10:41 +00:00
callbacks.onClientMessage(msg.payload);
}
2011-07-14 15:15:38 +00:00
else if (msg.type == "CHAT_MESSAGE")
{
chat.addMessage(msg, true);
}
2011-07-07 17:59:34 +00:00
else if (msg.type == "SERVER_MESSAGE")
{
2011-03-26 13:10:41 +00:00
callbacks.onServerMessage(msg.payload);
}
}
2011-07-07 17:59:34 +00:00
function updateUserInfo(userInfo)
{
2011-03-26 13:10:41 +00:00
userInfo.userId = userId;
userSet[userId] = userInfo;
tellAceActiveAuthorInfo(userInfo);
2011-07-07 17:59:34 +00:00
if (!socket) return;
sendMessage(
{
type: "USERINFO_UPDATE",
userInfo: userInfo
});
2011-03-26 13:10:41 +00:00
}
2011-07-07 17:59:34 +00:00
function tellAceActiveAuthorInfo(userInfo)
{
2011-03-26 13:10:41 +00:00
tellAceAuthorInfo(userInfo.userId, userInfo.colorId);
}
2011-07-07 17:59:34 +00:00
function tellAceAuthorInfo(userId, colorId, inactive)
{
if (colorId || (typeof colorId) == "number")
{
2011-03-26 13:10:41 +00:00
colorId = Number(colorId);
2011-07-07 17:59:34 +00:00
if (options && options.colorPalette && options.colorPalette[colorId])
{
2011-03-26 13:10:41 +00:00
var cssColor = options.colorPalette[colorId];
2011-07-07 17:59:34 +00:00
if (inactive)
{
editor.setAuthorInfo(userId, {
bgcolor: cssColor,
fade: 0.5
});
2011-03-26 13:10:41 +00:00
}
2011-07-07 17:59:34 +00:00
else
{
editor.setAuthorInfo(userId, {
bgcolor: cssColor
});
2011-03-26 13:10:41 +00:00
}
}
}
}
2011-07-07 17:59:34 +00:00
function fadeAceAuthorInfo(userInfo)
{
2011-03-26 13:10:41 +00:00
tellAceAuthorInfo(userInfo.userId, userInfo.colorId, true);
}
2011-07-07 17:59:34 +00:00
function getConnectedUsers()
{
2011-03-26 13:10:41 +00:00
return valuesArray(userSet);
}
2011-07-07 17:59:34 +00:00
function tellAceAboutHistoricalAuthors(hadata)
{
for (var author in hadata)
{
2011-03-26 13:10:41 +00:00
var data = hadata[author];
2011-07-07 17:59:34 +00:00
if (!userSet[author])
{
2011-03-26 13:10:41 +00:00
tellAceAuthorInfo(author, data.colorId, true);
}
}
}
2011-07-07 17:59:34 +00:00
function dmesgUsers()
{
2011-03-26 13:10:41 +00:00
//pad.dmesg($.map(getConnectedUsers(), function(u) { return u.userId.slice(-2); }).join(','));
}
2011-07-07 17:59:34 +00:00
function handleSocketClosed(params)
{
2011-03-26 13:10:41 +00:00
socket = null;
2011-07-07 17:59:34 +00:00
$.each(keys(userSet), function()
{
2011-03-26 13:10:41 +00:00
var uid = String(this);
2011-07-07 17:59:34 +00:00
if (uid != userId)
{
2011-03-26 13:10:41 +00:00
var userInfo = userSet[uid];
delete userSet[uid];
callbacks.onUserLeave(userInfo);
dmesgUsers();
}
});
var reason = appLevelDisconnectReason || params.reason;
var shouldReconnect = params.reconnect;
2011-07-07 17:59:34 +00:00
if (shouldReconnect)
{
2011-03-26 13:10:41 +00:00
// determine if this is a tight reconnect loop due to weird connectivity problems
reconnectTimes.push(+new Date());
var TOO_MANY_RECONNECTS = 8;
var TOO_SHORT_A_TIME_MS = 10000;
2011-07-07 17:59:34 +00:00
if (reconnectTimes.length >= TOO_MANY_RECONNECTS && ((+new Date()) - reconnectTimes[reconnectTimes.length - TOO_MANY_RECONNECTS]) < TOO_SHORT_A_TIME_MS)
{
2011-03-26 13:10:41 +00:00
setChannelState("DISCONNECTED", "looping");
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
setChannelState("RECONNECTING", reason);
setUpSocket();
}
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
setChannelState("DISCONNECTED", reason);
}
}
2011-07-07 17:59:34 +00:00
function setChannelState(newChannelState, moreInfo)
{
if (newChannelState != channelState)
{
2011-03-26 13:10:41 +00:00
channelState = newChannelState;
callbacks.onChannelStateChange(channelState, moreInfo);
}
}
2011-07-07 17:59:34 +00:00
function keys(obj)
{
2011-03-26 13:10:41 +00:00
var array = [];
2011-07-07 17:59:34 +00:00
$.each(obj, function(k, v)
{
array.push(k);
});
2011-03-26 13:10:41 +00:00
return array;
}
2011-07-07 17:59:34 +00:00
function valuesArray(obj)
{
2011-03-26 13:10:41 +00:00
var array = [];
2011-07-07 17:59:34 +00:00
$.each(obj, function(k, v)
{
array.push(v);
});
2011-03-26 13:10:41 +00:00
return array;
}
// We need to present a working interface even before the socket
// is connected for the first time.
var deferredActions = [];
2011-07-07 17:59:34 +00:00
function defer(func, tag)
{
return function()
{
2011-03-26 13:10:41 +00:00
var that = this;
var args = arguments;
2011-07-07 17:59:34 +00:00
function action()
{
2011-03-26 13:10:41 +00:00
func.apply(that, args);
}
action.tag = tag;
2011-07-07 17:59:34 +00:00
if (channelState == "CONNECTING")
{
2011-03-26 13:10:41 +00:00
deferredActions.push(action);
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
action();
}
}
}
2011-07-07 17:59:34 +00:00
function doDeferredActions(tag)
{
2011-03-26 13:10:41 +00:00
var newArray = [];
2011-07-07 17:59:34 +00:00
for (var i = 0; i < deferredActions.length; i++)
{
2011-03-26 13:10:41 +00:00
var a = deferredActions[i];
2011-07-07 17:59:34 +00:00
if ((!tag) || (tag == a.tag))
{
2011-03-26 13:10:41 +00:00
a();
}
2011-07-07 17:59:34 +00:00
else
{
2011-03-26 13:10:41 +00:00
newArray.push(a);
}
}
deferredActions = newArray;
}
2011-07-07 17:59:34 +00:00
function sendClientMessage(msg)
{
sendMessage(
{
type: "CLIENT_MESSAGE",
payload: msg
});
2011-03-26 13:10:41 +00:00
}
2011-07-07 17:59:34 +00:00
function getCurrentRevisionNumber()
{
2011-03-26 13:10:41 +00:00
return rev;
}
2011-07-07 17:59:34 +00:00
function getDiagnosticInfo()
{
2011-03-26 13:10:41 +00:00
var maxCaughtErrors = 3;
var maxAceErrors = 3;
var maxDebugMessages = 50;
var longStringCutoff = 500;
2011-07-07 17:59:34 +00:00
function trunc(str)
{
2011-03-26 13:10:41 +00:00
return String(str).substring(0, longStringCutoff);
}
2011-07-07 17:59:34 +00:00
var info = {
errors: {
length: 0
}
};
function addError(e, catcher, time)
{
var error = {
catcher: catcher
};
2011-03-26 13:10:41 +00:00
if (time) error.time = time;
// a little over-cautious?
2011-07-07 17:59:34 +00:00
try
{
if (e.description) error.description = e.description;
}
catch (x)
{}
try
{
if (e.fileName) error.fileName = e.fileName;
}
catch (x)
{}
try
{
if (e.lineNumber) error.lineNumber = e.lineNumber;
}
catch (x)
{}
try
{
if (e.message) error.message = e.message;
}
catch (x)
{}
try
{
if (e.name) error.name = e.name;
}
catch (x)
{}
try
{
if (e.number) error.number = e.number;
}
catch (x)
{}
try
{
if (e.stack) error.stack = trunc(e.stack);
}
catch (x)
{}
2011-03-26 13:10:41 +00:00
info.errors[info.errors.length] = error;
info.errors.length++;
}
2011-07-07 17:59:34 +00:00
for (var i = 0;
((i < caughtErrors.length) && (i < maxCaughtErrors)); i++)
{
2011-03-26 13:10:41 +00:00
addError(caughtErrors[i], caughtErrorCatchers[i], caughtErrorTimes[i]);
}
2011-07-07 17:59:34 +00:00
if (editor)
{
2011-03-26 13:10:41 +00:00
var aceErrors = editor.getUnhandledErrors();
2011-07-07 17:59:34 +00:00
for (var i = 0;
((i < aceErrors.length) && (i < maxAceErrors)); i++)
{
2011-03-26 13:10:41 +00:00
var errorRecord = aceErrors[i];
addError(errorRecord.error, "ACE", errorRecord.time);
}
}
info.time = +new Date();
info.collabState = state;
info.channelState = channelState;
info.lastCommitTime = lastCommitTime;
info.numSocketReconnects = reconnectTimes.length;
info.userId = userId;
info.currentRev = rev;
2011-07-07 17:59:34 +00:00
info.participants = (function()
{
2011-03-26 13:10:41 +00:00
var pp = [];
2011-07-07 17:59:34 +00:00
for (var u in userSet)
{
2011-03-26 13:10:41 +00:00
pp.push(u);
}
return pp.join(',');
})();
2011-07-07 17:59:34 +00:00
if (debugMessages.length > maxDebugMessages)
{
debugMessages = debugMessages.slice(debugMessages.length - maxDebugMessages, debugMessages.length);
2011-03-26 13:10:41 +00:00
}
2011-07-07 17:59:34 +00:00
info.debugMessages = {
length: 0
};
for (var i = 0; i < debugMessages.length; i++)
{
2011-03-26 13:10:41 +00:00
info.debugMessages[i] = trunc(debugMessages[i]);
info.debugMessages.length++;
}
return info;
}
2011-07-07 17:59:34 +00:00
function getMissedChanges()
{
2011-03-26 13:10:41 +00:00
var obj = {};
obj.userInfo = userSet[userId];
obj.baseRev = rev;
2011-07-07 17:59:34 +00:00
if (state == "COMMITTING" && stateMessage)
{
2011-03-26 13:10:41 +00:00
obj.committedChangeset = stateMessage.changeset;
obj.committedChangesetAPool = stateMessage.apool;
obj.committedChangesetSocketId = stateMessageSocketId;
editor.applyPreparedChangesetToBase();
}
var userChangesData = editor.prepareUserChangeset();
2011-07-07 17:59:34 +00:00
if (userChangesData.changeset)
{
2011-03-26 13:10:41 +00:00
obj.furtherChangeset = userChangesData.changeset;
obj.furtherChangesetAPool = userChangesData.apool;
}
return obj;
}
2011-07-07 17:59:34 +00:00
function setStateIdle()
{
2011-03-26 13:10:41 +00:00
state = "IDLE";
callbacks.onInternalAction("newlyIdle");
schedulePerhapsCallIdleFuncs();
}
2011-07-07 17:59:34 +00:00
function callWhenNotCommitting(func)
{
2011-03-26 13:10:41 +00:00
idleFuncs.push(func);
schedulePerhapsCallIdleFuncs();
}
var idleFuncs = [];
2011-07-07 17:59:34 +00:00
function schedulePerhapsCallIdleFuncs()
{
setTimeout(function()
{
if (state == "IDLE")
{
while (idleFuncs.length > 0)
{
2011-03-26 13:10:41 +00:00
var f = idleFuncs.shift();
f();
}
}
}, 0);
}
var self;
return (self = {
2011-07-07 17:59:34 +00:00
setOnUserJoin: function(cb)
{
callbacks.onUserJoin = cb;
},
setOnUserLeave: function(cb)
{
callbacks.onUserLeave = cb;
},
setOnUpdateUserInfo: function(cb)
{
callbacks.onUpdateUserInfo = cb;
},
setOnChannelStateChange: function(cb)
{
callbacks.onChannelStateChange = cb;
},
setOnClientMessage: function(cb)
{
callbacks.onClientMessage = cb;
},
setOnInternalAction: function(cb)
{
callbacks.onInternalAction = cb;
},
setOnConnectionTrouble: function(cb)
{
callbacks.onConnectionTrouble = cb;
},
setOnServerMessage: function(cb)
{
callbacks.onServerMessage = cb;
},
2011-03-26 13:10:41 +00:00
updateUserInfo: defer(updateUserInfo),
handleMessageFromServer: handleMessageFromServer,
2011-03-26 13:10:41 +00:00
getConnectedUsers: getConnectedUsers,
sendClientMessage: sendClientMessage,
2011-07-14 15:15:38 +00:00
sendMessage: sendMessage,
2011-03-26 13:10:41 +00:00
getCurrentRevisionNumber: getCurrentRevisionNumber,
getDiagnosticInfo: getDiagnosticInfo,
getMissedChanges: getMissedChanges,
callWhenNotCommitting: callWhenNotCommitting,
addHistoricalAuthors: tellAceAboutHistoricalAuthors
});
}
2011-07-07 17:59:34 +00:00
function selectElementContents(elem)
{
if ($.browser.msie)
{
2011-03-26 13:10:41 +00:00
var range = document.body.createTextRange();
range.moveToElementText(elem);
range.select();
}
2011-07-07 17:59:34 +00:00
else
{
if (window.getSelection)
{
2011-03-26 13:10:41 +00:00
var browserSelection = window.getSelection();
2011-07-07 17:59:34 +00:00
if (browserSelection)
{
2011-03-26 13:10:41 +00:00
var range = document.createRange();
range.selectNodeContents(elem);
browserSelection.removeAllRanges();
browserSelection.addRange(range);
}
}
}
}