added plugin hooks to client code

This commit is contained in:
Josip Janžić 2014-12-28 00:48:28 +01:00
parent 05c910c9d3
commit 2c6aab4e9f
2 changed files with 31 additions and 4 deletions

View File

@ -283,13 +283,17 @@
</div>
</div>
<script src="js/libs.min.js"></script>
<script src="js/shout.templates.js"></script>
<script src="js/shout.js"></script>
<script src="js/libs.min.js"></script>
<script>
var shoutPlugins = [];
</script>
<% plugins.forEach(function(file) { %>
<script src="plugins/<%= file %>"></script>
<% }) %>
<script src="js/shout.templates.js"></script>
<script src="js/shout.js"></script>
</body>
</html>

View File

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