cryptalk/public/js/cryptalk_modules/client.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-09-26 08:46:35 -04:00
/*
Accepts:
mediator.on('command:help', ...);
mediator.on('command:nick', ...);
mediator.on('command:key', ...);
mediator.on('command:clear', ...);
2014-09-26 12:21:27 -04:00
mediator.on('command:title', ...);
2014-09-26 08:46:35 -04:00
Emits:
mediator.emit('nick:changed',...);
mediator.emit('key:changed',...);
mediator.emit('console:clear',...);
mediator.emit('console:info',...);
mediator.emit('console:error',...);
*/
define(
{
compiles: ['$'],
requires: ['castrato','settings','templates']
}, function ($, requires, data) {
var
// Private properties
nick,
key,
// Require shortcuts
mediator = requires.castrato,
settings = requires.settings,
templates = requires.templates,
setKey = function(payload) {
/*if (!host) {
return post('error', templates.messages.key_no_host);
}*/
// Make sure the key meets the length requirements
if (payload.length > settings.key.maxLen) {
return mediator.emit('console:error',templates.messages.key_to_long);
} else if (payload.length < settings.key.minLen) {
return mediator.emit('console:error',templates.messages.key_to_short);
}
// Set key
key = payload;
// Keep other modules informed
mediator.emit('key:changed',key);
// Inform that the key has been set
return mediator.emit('console:info', templates.messages.key_ok );
},
2014-09-26 12:21:27 -04:00
help = function () { mediator.emit('console:motd', templates.help); },
2014-09-26 08:46:35 -04:00
clear = function () { mediator.emit('console:clear'); },
nick = function (payload) {
// Make sure the nick meets the length requirements
if (payload.length > settings.nick.maxLen) {
return mediator('console:error', $.template(templates.messages.nick_to_long, { nick_maxLen: settings.nick.maxLen } ));
} else if (payload.length < settings.nick.minLen) {
return mediator('console:error', $.template(templates.messages.nick_to_short, {nick_minLen: settings.nick.minLen } ));
}
// Set nick
nick = payload;
// Keep other modules informed
mediator.emit('nick:changed',nick);
// Inform that the nick has been set
mediator.emit('console:info', $.template(templates.messages.nick_set, { nick: $.escapeHtml(nick)}));
2014-09-26 12:21:27 -04:00
},
title = function(payload) {
mediator.emit('window:title',payload);
mediator.emit('console:info', $.template(templates.messages.title_set, { title: $.escapeHtml(payload)}));
2014-09-26 08:46:35 -04:00
};
mediator.on('command:help', help);
mediator.on('command:clear', clear);
mediator.on('command:nick', nick);
mediator.on('command:key', setKey);
2014-09-26 12:21:27 -04:00
mediator.on('command:title', title);
2014-09-26 08:46:35 -04:00
});