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,89 +39,102 @@ 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],
post,
data = fandango.merge({}, settings, {
nick: nick,
timestamp: new Date().toLocaleTimeString()
});
var tpl = templates.post[type], data.text = $.template(text, data);
post, post = $.template(tpl, data);
data = fandango.merge({}, settings, {
nick: nick,
timestamp: new Date().toLocaleTimeString()
});
data.text = $.template(text, data); // Request a notification
post = $.template(tpl, data); commands.showNotification(type, nick, text);
// Request a notification // Append the post to the chat DOM element
showNotification(type, nick, text); components.chat.append(post);
},
// Append the post to the chat DOM element param: function (p) {
components.chat.append(post); parameters = fandango.merge({}, parameters, p);
},
}, showNotification: function (type, nick, text) {
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');
param = function (p) { // Emit notification
parameters = fandango.merge({}, parameters, p ); mediator.emit('notification:send', {
},
showNotification = function (type, nick, text) {
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';
// Emit notification
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 () {
commands.post('motd', settings.motd);
},
motd = function (payload) { post('motd', settings.motd); }, info: function (message) {
info = function (payload, done) { post('info', payload); }, commands.post('info', message);
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 () { error: function (message) {
fandango.subordinate(function () { commands.post('error', message);
components.input[0].value = ''; },
});
},
clear = function () { server: function (message) {
fandango.subordinate(function () { commands.post('server', message);
components.chat[0].innerHTML = ''; },
});
},
lockInput = function () { message: function (data) {
components.input[0].setAttribute('disabled', 'disabled'); commands.post('message', data.message, data.nick);
components.inputWrapper[0].className = 'loading'; },
},
unlockInput = function () { clearInput: function () {
components.input[0].removeAttribute('disabled'); fandango.subordinate(function () {
components.inputWrapper[0].className = ''; components.input[0].value = '';
components.input.focus(); });
}, },
_require = function (filepath, done) { clear: function () {
lockInput(); fandango.subordinate(function () {
post('info', 'Requiring ' + filepath + '...'); components.chat[0].innerHTML = '';
require([filepath], function () { });
post('info', 'Successfully required ' + filepath + '.'); },
unlockInput();
done(); lockInput: function () {
}, function (e) { components.input[0].setAttribute('disabled', 'disabled');
post('error', 'An error occurred while trying to load "' + filepath + '":\n' + e); components.inputWrapper[0].className = 'loading';
unlockInput(); },
done();
}); unlockInput: function () {
components.input[0].removeAttribute('disabled');
components.inputWrapper[0].className = '';
components.input.focus();
},
_require: function (filepath, done) {
commands.lockInput();
commands.post('info', 'Requiring ' + filepath + '...');
require([filepath], function () {
commands.post('info', 'Successfully required ' + filepath + '.');
commands.unlockInput();
done();
}, function (e) {
commands.post('error', 'An error occurred while trying to load "' + filepath + '":\n' + e);
commands.unlockInput();
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);
}); });
}); });