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',...);
ToDo
*/
define(
{
compiles: ['$'],
requires: ['castrato','fandango','settings','templates','sounds','room','notifications','audio']
}, function ($, requires, data) {
define({
compiles: ['$'],
requires: ['castrato', 'fandango', 'settings', 'templates', 'sounds', 'room', 'notifications', 'audio']
}, function ($, requires, data) {
var
// Require shortcuts
var // Require shortcuts
fandango = requires.fandango,
mediator = requires.castrato,
settings = requires.settings,
@ -42,89 +39,102 @@ define(
parameters = {},
// 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],
post,
data = fandango.merge({}, settings, {
nick: nick,
timestamp: new Date().toLocaleTimeString()
});
data.text = $.template(text, data);
post = $.template(tpl, data);
data.text = $.template(text, data);
post = $.template(tpl, data);
// Request a notification
commands.showNotification(type, nick, text);
// Request a notification
showNotification(type, nick, text);
// Append the post to the chat DOM element
components.chat.append(post);
},
// Append the post to the chat DOM element
components.chat.append(post);
param: function (p) {
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) {
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';
// Emit notification
mediator.emit('notification:send',
{
// Emit notification
mediator.emit('notification:send', {
title: title.substring(0, 20),
body: text.substring(0, 80),
icon: icon
});
// Emit sound
if ( type == 'message' ) mediator.emit('audio:play', sounds.message);
// Emit sound
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 (payload, done) { post('info', payload); },
error = function (payload, done) { post('error', payload); },
message = function (payload, done) { post('message', payload.message , payload.nick ); },
server = function (payload, done) { post('server', payload); },
info: function (message) {
commands.post('info', message);
},
clearInput = function () {
fandango.subordinate(function () {
components.input[0].value = '';
});
},
error: function (message) {
commands.post('error', message);
},
clear = function () {
fandango.subordinate(function () {
components.chat[0].innerHTML = '';
});
},
server: function (message) {
commands.post('server', message);
},
lockInput = function () {
components.input[0].setAttribute('disabled', 'disabled');
components.inputWrapper[0].className = 'loading';
},
message: function (data) {
commands.post('message', data.message, data.nick);
},
unlockInput = function () {
components.input[0].removeAttribute('disabled');
components.inputWrapper[0].className = '';
components.input.focus();
},
clearInput: function () {
fandango.subordinate(function () {
components.input[0].value = '';
});
},
_require = function (filepath, done) {
lockInput();
post('info', 'Requiring ' + filepath + '...');
require([filepath], function () {
post('info', 'Successfully required ' + filepath + '.');
unlockInput();
done();
}, function (e) {
post('error', 'An error occurred while trying to load "' + filepath + '":\n' + e);
unlockInput();
done();
});
clear: function () {
fandango.subordinate(function () {
components.chat[0].innerHTML = '';
});
},
lockInput: function () {
components.input[0].setAttribute('disabled', 'disabled');
components.inputWrapper[0].className = 'loading';
},
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.
@ -159,9 +169,9 @@ define(
payload,
function(retvals, 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 {
clearInput();
commands.clearInput();
}
}
);
@ -170,7 +180,7 @@ define(
if(!parameters.room || !parameters.key ) {
// 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.
@ -188,8 +198,7 @@ define(
);
// And clear the the buffer
clearInput();
commands.clearInput();
}
};
@ -200,17 +209,15 @@ define(
components.input.focus();
// Connect events
mediator.on('console:clear', clear);
mediator.on('console:motd', motd);
mediator.on('console:info', info);
mediator.on('console:error', error);
mediator.on('console:server', server);
mediator.on('console:message', message);
mediator.on('console:lockinput', lockInput);
mediator.on('console:unlockinput', unlockInput);
mediator.on('console:param', param);
mediator.on('console:require', _require);
for (var commandName in commands) {
if (commandName === '_require' && commandName !== 'post') {
mediator.on('console:' + commandName, commands[commandName]);
}
}
mediator.on('console:require', commands._require);
mediator.on('console:post', function (data) {
post(data.type, data.data, data.nick);
commands.post(data.type, data.data, data.nick);
});
});