This commit is contained in:
unkelpehr 2014-09-27 14:40:08 +02:00
parent 65d8a1c122
commit 276220eaf4
1 changed files with 97 additions and 90 deletions

View File

@ -16,15 +16,12 @@
mediator.emit('audio:play',...); mediator.emit('audio:play',...);
ToDo ToDo
*/ */
define( define({
{
compiles: ['$'], compiles: ['$'],
requires: ['castrato', 'fandango', 'settings', 'templates', 'sounds', 'room', 'notifications', 'audio'] requires: ['castrato', 'fandango', 'settings', 'templates', 'sounds', 'room', 'notifications', 'audio']
}, function ($, requires, data) { }, function ($, requires, data) {
var var // Require shortcuts
// Require shortcuts
fandango = requires.fandango, fandango = requires.fandango,
mediator = requires.castrato, mediator = requires.castrato,
settings = requires.settings, settings = requires.settings,
@ -42,8 +39,8 @@ define(
parameters = {}, parameters = {},
// Adds a new message to the DOM // Adds a new message to the DOM
post = function (type, text, nick) { commands = {
post: function (type, text, nick) {
var tpl = templates.post[type], var tpl = templates.post[type],
post, post,
data = fandango.merge({}, settings, { data = fandango.merge({}, settings, {
@ -55,76 +52,89 @@ define(
post = $.template(tpl, data); post = $.template(tpl, data);
// Request a notification // Request a notification
showNotification(type, nick, text); commands.showNotification(type, nick, text);
// Append the post to the chat DOM element // Append the post to the chat DOM element
components.chat.append(post); components.chat.append(post);
}, },
param = function (p) { param: function (p) {
parameters = fandango.merge({}, parameters, p); parameters = fandango.merge({}, parameters, p);
}, },
showNotification = function (type, nick, text) { showNotification: function (type, nick, text) {
var title = type !== 'message' ? 'Cryptalk' : nick,
var title = (type!='message') ? 'Cryptalk' : nick, icon = type === 'message'? 'gfx/icon_128x128.png' : (type == 'error' ? 'gfx/icon_128x128_error.png' : 'gfx/icon_128x128_info.png');
icon = (type == 'message') ? 'gfx/icon_128x128.png' : (type == 'error') ? 'gfx/icon_128x128_error.png' : 'gfx/icon_128x128_info.png';
// Emit notification // Emit notification
mediator.emit('notification:send', mediator.emit('notification:send', {
{
title: title.substring(0, 20), title: title.substring(0, 20),
body: text.substring(0, 80), body: text.substring(0, 80),
icon: icon icon: icon
}); });
// Emit sound // Emit sound
if ( type == 'message' ) mediator.emit('audio:play', sounds.message); if (type === 'message') {
mediator.emit('audio:play', sounds.message);
}
}, },
motd = function (payload) { post('motd', settings.motd); }, motd: function () {
info = function (payload, done) { post('info', payload); }, commands.post('motd', settings.motd);
error = function (payload, done) { post('error', payload); }, },
message = function (payload, done) { post('message', payload.message , payload.nick ); },
server = function (payload, done) { post('server', payload); },
clearInput = function () { info: function (message) {
commands.post('info', message);
},
error: function (message) {
commands.post('error', message);
},
server: function (message) {
commands.post('server', message);
},
message: function (data) {
commands.post('message', data.message, data.nick);
},
clearInput: function () {
fandango.subordinate(function () { fandango.subordinate(function () {
components.input[0].value = ''; components.input[0].value = '';
}); });
}, },
clear = function () { clear: function () {
fandango.subordinate(function () { fandango.subordinate(function () {
components.chat[0].innerHTML = ''; components.chat[0].innerHTML = '';
}); });
}, },
lockInput = function () { lockInput: function () {
components.input[0].setAttribute('disabled', 'disabled'); components.input[0].setAttribute('disabled', 'disabled');
components.inputWrapper[0].className = 'loading'; components.inputWrapper[0].className = 'loading';
}, },
unlockInput = function () { unlockInput: function () {
components.input[0].removeAttribute('disabled'); components.input[0].removeAttribute('disabled');
components.inputWrapper[0].className = ''; components.inputWrapper[0].className = '';
components.input.focus(); components.input.focus();
}, },
_require = function (filepath, done) { _require: function (filepath, done) {
lockInput(); commands.lockInput();
post('info', 'Requiring ' + filepath + '...'); commands.post('info', 'Requiring ' + filepath + '...');
require([filepath], function () { require([filepath], function () {
post('info', 'Successfully required ' + filepath + '.'); commands.post('info', 'Successfully required ' + filepath + '.');
unlockInput(); commands.unlockInput();
done(); done();
}, function (e) { }, function (e) {
post('error', 'An error occurred while trying to load "' + filepath + '":\n' + e); commands.post('error', 'An error occurred while trying to load "' + filepath + '":\n' + e);
unlockInput(); commands.unlockInput();
done(); done();
}); });
}
}, },
// Handler for the document`s keyDown-event. // Handler for the document`s keyDown-event.
@ -159,9 +169,9 @@ define(
payload, payload,
function(retvals, recipients) { function(retvals, recipients) {
if(!recipients) { if(!recipients) {
return post('error', $.template(templates.messages.unrecognized_command, { commandName: command })); return commands.post('error', $.template(templates.messages.unrecognized_command, { commandName: command }));
} else { } else {
clearInput(); commands.clearInput();
} }
} }
); );
@ -170,7 +180,7 @@ define(
if(!parameters.room || !parameters.key ) { if(!parameters.room || !parameters.key ) {
// Make sure that the user has joined a room and the key is set // Make sure that the user has joined a room and the key is set
return (!parameters.room) ? post('error', templates.messages.msg_no_room) : post('error', templates.messages.msg_no_key); return (!parameters.room) ? commands.post('error', templates.messages.msg_no_room) : commands.post('error', templates.messages.msg_no_key);
} }
// Before sending the message. // Before sending the message.
@ -188,8 +198,7 @@ define(
); );
// And clear the the buffer // And clear the the buffer
clearInput(); commands.clearInput();
} }
}; };
@ -200,17 +209,15 @@ define(
components.input.focus(); components.input.focus();
// Connect events // Connect events
mediator.on('console:clear', clear); for (var commandName in commands) {
mediator.on('console:motd', motd); if (commandName === '_require' && commandName !== 'post') {
mediator.on('console:info', info); mediator.on('console:' + commandName, commands[commandName]);
mediator.on('console:error', error); }
mediator.on('console:server', server); }
mediator.on('console:message', message);
mediator.on('console:lockinput', lockInput); mediator.on('console:require', commands._require);
mediator.on('console:unlockinput', unlockInput);
mediator.on('console:param', param);
mediator.on('console:require', _require);
mediator.on('console:post', function (data) { mediator.on('console:post', function (data) {
post(data.type, data.data, data.nick); commands.post(data.type, data.data, data.nick);
}); });
}); });