diff --git a/doc/api/hooks_server-side.md b/doc/api/hooks_server-side.md index 010bc8a3a..c60d810e8 100644 --- a/doc/api/hooks_server-side.md +++ b/doc/api/hooks_server-side.md @@ -65,6 +65,42 @@ This hook gets called upon the rendering of an ejs template block. For any speci Have a look at `src/templates/pad.html` and `src/templates/timeslider.html` to see which blocks are available. +## padCreate +Called from: src/node/db/Pad.js + +Things in context: + +1. pad - the pad instance + +This hook gets called when a new pad was created. + +## padLoad +Called from: src/node/db/Pad.js + +Things in context: + +1. pad - the pad instance + +This hook gets called when an pad was loaded. If a new pad was created and loaded this event will be emitted too. + +## padUpdate +Called from: src/node/db/Pad.js + +Things in context: + +1. pad - the pad instance + +This hook gets called when an existing pad was updated. + +## padRemove +Called from: src/node/db/Pad.js + +Things in context: + +1. padID + +This hook gets called when an existing pad was removed/deleted. + ## socketio Called from: src/node/hooks/express/socketio.js diff --git a/src/node/db/AuthorManager.js b/src/node/db/AuthorManager.js index 2a6625c85..28b2dd91e 100644 --- a/src/node/db/AuthorManager.js +++ b/src/node/db/AuthorManager.js @@ -141,22 +141,6 @@ exports.getAuthor = function (author, callback) db.get("globalAuthor:" + author, callback); } -/** - * Returns the Author Name of the author - * @param {String} author The id of the author - * @param {Function} callback callback(err, name) - */ - -exports.getAuthorName = function (authorID, callback) -{ - db.getSub("globalAuthor:" + author, ["name"], callback); - console.log(authorID); - db.getSub("globalAuthor:" + authorID, ["name"], function(err, authorName){ - if(ERR(err, callback)) return; - callback(null, {authorName: authorName}); - }); -} - /** diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index ad2d59f38..dba791fd2 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -16,6 +16,7 @@ var padMessageHandler = require("../handler/PadMessageHandler"); var readOnlyManager = require("./ReadOnlyManager"); var crypto = require("crypto"); var randomString = require("../utils/randomstring"); +var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks'); //serialization/deserialization attributes var attributeBlackList = ["id"]; @@ -86,6 +87,12 @@ Pad.prototype.appendRevision = function appendRevision(aChangeset, author) { // set the author to pad if(author) authorManager.addPad(author, this.id); + + if (this.head == 0) { + hooks.callAll("padCreate", {'pad':this}); + } else { + hooks.callAll("padUpdate", {'pad':this}); + } }; //save all attributes to the database @@ -368,6 +375,7 @@ Pad.prototype.init = function init(text, callback) { _this.appendRevision(firstChangeset, ''); } + hooks.callAll("padLoad", {'pad':_this}); callback(null); }); }; @@ -467,6 +475,7 @@ Pad.prototype.remove = function remove(callback) { { db.remove("pad:"+padID); padManager.unloadPad(padID); + hooks.callAll("padRemove", {'padID':padID}); callback(); } ], function(err) diff --git a/src/static/js/collab_client.js b/src/static/js/collab_client.js index d149b2565..b3e17c256 100644 --- a/src/static/js/collab_client.js +++ b/src/static/js/collab_client.js @@ -358,6 +358,14 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) { var userInfo = msg.userInfo; var id = userInfo.userId; + + // Avoid a race condition when setting colors. If our color was set by a + // query param, ignore our own "new user" message's color value. + if (id === initialUserInfo.userId && initialUserInfo.globalUserColor) + { + msg.userInfo.colorId = initialUserInfo.globalUserColor; + } + if (userSet[id]) { diff --git a/src/static/js/colorutils.js b/src/static/js/colorutils.js index 5fbefb4df..74a2e4635 100644 --- a/src/static/js/colorutils.js +++ b/src/static/js/colorutils.js @@ -24,6 +24,13 @@ var colorutils = {}; +// Check that a given value is a css hex color value, e.g. +// "#ffffff" or "#fff" +colorutils.isCssHex = function(cssColor) +{ + return /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(cssColor); +} + // "#ffffff" or "#fff" or "ffffff" or "fff" to [1.0, 1.0, 1.0] colorutils.css2triple = function(cssColor) { diff --git a/src/static/js/pad.js b/src/static/js/pad.js index ca7e06273..ea63c884b 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -43,6 +43,7 @@ var padmodals = require('./pad_modals').padmodals; var padsavedrevs = require('./pad_savedrevs'); var paduserlist = require('./pad_userlist').paduserlist; var padutils = require('./pad_utils').padutils; +var colorutils = require('./colorutils').colorutils; var createCookie = require('./pad_utils').createCookie; var readCookie = require('./pad_utils').readCookie; @@ -114,6 +115,7 @@ function getParams() var showControls = params["showControls"]; var showChat = params["showChat"]; var userName = params["userName"]; + var userColor = params["userColor"]; var showLineNumbers = params["showLineNumbers"]; var useMonospaceFont = params["useMonospaceFont"]; var IsnoColors = params["noColors"]; @@ -162,6 +164,11 @@ function getParams() // If the username is set as a parameter we should set a global value that we can call once we have initiated the pad. settings.globalUserName = decodeURIComponent(userName); } + if(userColor) + // If the userColor is set as a parameter, set a global value to use once we have initiated the pad. + { + settings.globalUserColor = decodeURIComponent(userColor); + } if(rtl) { if(rtl == "true") @@ -363,6 +370,14 @@ function handshake() pad.myUserInfo.name = settings.globalUserName; $('#myusernameedit').attr({"value":settings.globalUserName}); // Updates the current users UI } + if (settings.globalUserColor !== false && colorutils.isCssHex(settings.globalUserColor)) + { + + // Add a 'globalUserColor' property to myUserInfo, so collabClient knows we have a query parameter. + pad.myUserInfo.globalUserColor = settings.globalUserColor; + pad.notifyChangeColor(settings.globalUserColor); // Updates pad.myUserInfo.colorId + paduserlist.setMyUserInfo(pad.myUserInfo); + } } //This handles every Message after the clientVars else @@ -1029,6 +1044,7 @@ var settings = { , noColors: false , useMonospaceFontGlobal: false , globalUserName: false +, globalUserColor: false , rtlIsTrue: false };