Merge upstream pull request #320 from janza/client-plugins

Client plugins

closes #14
This commit is contained in:
Rylee Fowler 2015-08-04 21:16:57 -05:00
commit 04a2c9a659
3 changed files with 53 additions and 3 deletions

View File

@ -287,6 +287,14 @@
</div>
<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>

View File

@ -61,7 +61,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(
@ -197,6 +199,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", [
@ -416,6 +420,9 @@ $(function() {
clear();
return;
}
text = pluginHandle('oninput', text);
if (!text) return;
socket.emit("input", {
target: chat.data("id"),
text: text
@ -751,6 +758,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");

View File

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