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] 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");