From 05c910c9d391dd1b43bc7d6997f833aca046b383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Jan=C5=BEi=C4=87?= Date: Sat, 27 Dec 2014 21:08:18 +0100 Subject: [PATCH 1/3] include plugins from shout config directory in html --- client/index.html | 4 ++++ src/server.js | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/client/index.html b/client/index.html index f1bd2f2..76264be 100644 --- a/client/index.html +++ b/client/index.html @@ -287,5 +287,9 @@ + <% plugins.forEach(function(file) { %> + + <% }) %> + diff --git a/src/server.js b/src/server.js index 39a6710..3c8d57c 100644 --- a/src/server.js +++ b/src/server.js @@ -13,12 +13,28 @@ var manager = new ClientManager(); module.exports = function(options) { config = Helper.getConfig(); - config = _.extend(config, options); + config = _.extend(config, options, { + plugins: [] + }); var app = express() .use(index) .use(express.static("client")); + fs.readdir(Helper.HOME + '/plugins', function(err, files) { + if (err) { + if (err.code !== 'ENOENT') { + console.log(err); + } + return; + } + if (files.length) { + config.plugins = files; + app.use('/plugins', express.static(Helper.HOME + '/plugins')); + } + + }); + app.enable("trust proxy"); var server = null; From 2c6aab4e9f1a90a3b2ee4111afb03e6bad7db21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Jan=C5=BEi=C4=87?= Date: Sun, 28 Dec 2014 00:48:28 +0100 Subject: [PATCH 2/3] added plugin hooks to client code --- client/index.html | 10 +++++++--- client/js/shout.js | 25 ++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/client/index.html b/client/index.html index 76264be..6e6a50b 100644 --- a/client/index.html +++ b/client/index.html @@ -283,13 +283,17 @@ - - - + + <% plugins.forEach(function(file) { %> <% }) %> + + + diff --git a/client/js/shout.js b/client/js/shout.js index 5b02e23..338365e 100644 --- a/client/js/shout.js +++ b/client/js/shout.js @@ -54,7 +54,9 @@ $(function() { }); function render(name, data) { - return Handlebars.templates[name](data); + data = pluginHandle('onprerender', [name, data])[1]; + var html = Handlebars.templates[name](data); + return pluginHandle('onpostrender', [name, html])[1]; } Handlebars.registerHelper( @@ -190,6 +192,8 @@ $(function() { var chan = chat.find(target); var from = data.msg.from; + data.msg = pluginHandle('onmessage', data.msg); + chan.find(".messages") .append(render("msg", {messages: [data.msg]})) .trigger("msg", [ @@ -744,6 +748,25 @@ $(function() { }); }, 1000 * 10); + $.each(shoutPlugins, function(i, plugin) { + if ($.isFunction(plugin)) { plugin(socket); } + }); + + function pluginHandle(action, data) { + var val = data; + + $.each(shoutPlugins, function(i, plugin) { + if ($.isFunction(plugin[action])) { + var newVal = plugin[action].apply(plugin, (val && !val.length) ? [val] : val); + if (newVal !== null && newVal !== undefined) { + val = newVal; + } + } + }); + + return val; + } + function clear() { chat.find(".active .messages").empty(); chat.find(".active .show-more").addClass("show"); From a469f58cd1390bfb037fbba84819c0a22cad8022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Jan=C5=BEi=C4=87?= Date: Sun, 28 Dec 2014 12:29:50 +0100 Subject: [PATCH 3/3] added oninput hook before form submit --- client/js/shout.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client/js/shout.js b/client/js/shout.js index 338365e..652c0fd 100644 --- a/client/js/shout.js +++ b/client/js/shout.js @@ -417,6 +417,9 @@ $(function() { clear(); return; } + text = pluginHandle('oninput', text); + if (!text) return; + socket.emit("input", { target: chat.data("id"), text: text