Merge pull request #1358 from mluto/clientvars-hook

Make the creation of clientVars hookable
This commit is contained in:
John McLear 2013-01-14 14:26:30 -08:00
commit 5eb09f981b
2 changed files with 101 additions and 69 deletions

View file

@ -175,6 +175,27 @@ function handleMessage ( hook, context, callback ) {
}; };
``` ```
## clientVars
Called from: src/node/handler/PadMessageHandler.js
Things in context:
1. clientVars - the basic `clientVars` built by the core
2. pad - the pad this session is about
This hook will be called once a client connects and the `clientVars` are being sent. Plugins can use this hook to give the client a initial configuriation, like the tracking-id of an external analytics-tool that is used on the client-side. You can also overwrite values from the original `clientVars`.
Example:
```
exports.clientVars = function(hook, context, callback)
{
// tell the client which year we are in
return callback({ "currentYear": new Date().getFullYear() });
};
```
This can be accessed on the client-side using `clientVars.currentYear`.
## getLineHTMLForExport ## getLineHTMLForExport
Called from: src/node/utils/ExportHtml.js Called from: src/node/utils/ExportHtml.js

View file

@ -991,6 +991,15 @@ function handleClientReady(client, message)
//Saves in pad2sessions that this session belongs to this pad //Saves in pad2sessions that this session belongs to this pad
pad2sessions[padIds.padId].push(client.id); pad2sessions[padIds.padId].push(client.id);
//If this is a reconnect, we don't have to send the client the ClientVars again
if(message.reconnect == true)
{
//Save the revision in sessioninfos, we take the revision from the info the client send to us
sessioninfos[client.id].rev = message.client_rev;
}
//This is a normal first connect
else
{
//prepare all values for the wire //prepare all values for the wire
var atext = Changeset.cloneAText(pad.atext); var atext = Changeset.cloneAText(pad.atext);
var attribsForWire = Changeset.prepareForWire(atext.attribs, pad.pool); var attribsForWire = Changeset.prepareForWire(atext.attribs, pad.pool);
@ -1012,7 +1021,6 @@ function handleClientReady(client, message)
"collab_client_vars": { "collab_client_vars": {
"initialAttributedText": atext, "initialAttributedText": atext,
"clientIp": "127.0.0.1", "clientIp": "127.0.0.1",
//"clientAgent": "Anonymous Agent",
"padId": message.padId, "padId": message.padId,
"historicalAuthorData": historicalAuthorData, "historicalAuthorData": historicalAuthorData,
"apool": apool, "apool": apool,
@ -1055,19 +1063,22 @@ function handleClientReady(client, message)
clientVars.userName = authorName; clientVars.userName = authorName;
} }
//If this is a reconnect, we don't have to send the client the ClientVars again //call the clientVars-hook so plugins can modify them before they get sent to the client
if(message.reconnect == true) hooks.aCallAll("clientVars", { clientVars: clientVars, pad: pad }, function ( err, messages ) {
{ if(ERR(err, callback)) return;
//Save the revision in sessioninfos, we take the revision from the info the client send to us
sessioninfos[client.id].rev = message.client_rev; _.each(messages, function(newVars) {
//combine our old object with the new attributes from the hook
for(var attr in newVars) {
clientVars[attr] = newVars[attr];
} }
//This is a normal first connect });
else
{
//Send the clientVars to the Client //Send the clientVars to the Client
client.json.send({type: "CLIENT_VARS", data: clientVars}); client.json.send({type: "CLIENT_VARS", data: clientVars});
//Save the current revision in sessioninfos, should be the same as in clientVars //Save the current revision in sessioninfos, should be the same as in clientVars
sessioninfos[client.id].rev = pad.getHeadRevisionNumber(); sessioninfos[client.id].rev = pad.getHeadRevisionNumber();
});
} }
sessioninfos[client.id].author = author; sessioninfos[client.id].author = author;