cryptalk/public/js/lib/console.js

234 lines
6.2 KiB
JavaScript
Raw Normal View History

2014-09-26 08:46:35 -04:00
/*
Accepts:
mediator.on('console:clear', clear);
mediator.on('console:torch', ttl)
2014-09-26 08:46:35 -04:00
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);
2014-09-26 08:46:35 -04:00
mediator.on('console:param', param);
Emits:
mediator.emit('notification:send',...);
mediator.emit('audio:play',...);
ToDo
*/
2017-02-07 15:19:23 -05:00
define(['$', 'castrato', 'settings', 'templates', 'sounds', 'room', 'notifications', 'audio'], function ($, mediator, settings, templates, sounds) {
var // Collection of DOM components
2014-09-26 08:46:35 -04:00
components = {
chat: $('#chat'),
input: $('#input'),
inputWrapper: $('#input_wrapper')
},
// Collection of parameters
parameters = {},
// Adds a new message to the DOM
2014-09-27 08:40:08 -04:00
commands = {
post: function (type, text, nick) {
var tpl = templates.post[type],
uniqueId = 'msg_' + new Date().getTime() + '_' + Math.round(Math.random()*1000000),
2014-09-27 08:40:08 -04:00
post,
2017-02-07 15:19:23 -05:00
data = Object.assign({}, settings, {
2014-09-27 08:40:08 -04:00
nick: nick,
timestamp: new Date().toLocaleTimeString(),
id: uniqueId
2014-09-27 08:40:08 -04:00
});
data.text = $.template(text, data);
post = $.template(tpl, data);
// Request a notification
commands.showNotification(type, nick, text);
// Expire message
setTimeout(function() {
var parent = components.chat.first(),
child = $('#'+uniqueId).first();
parent.removeChild(child);
}, settings.ttl);
2014-09-27 08:40:08 -04:00
// Append the post to the chat DOM element
components.chat.append(post);
},
torch: function (ttl) {
2017-02-22 17:09:50 -05:00
ttl = parseInt(ttl);
if( ttl > 0 && ttl < 3600) {
mediator.emit('console:info', $.template(templates.messages.torch_is_now, { ttl: ttl }) );
settings.ttl = ttl*1000;
} else {
mediator.emit('console:error', $.template(templates.messages.torch_not_set) );
}
},
2014-09-27 08:40:08 -04:00
param: function (p) {
2017-02-07 15:19:23 -05:00
parameters = Object.assign({}, parameters, p);
2014-09-27 08:40:08 -04:00
},
showNotification: function (type, nick, text) {
2017-02-22 17:09:50 -05:00
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'));
2014-09-27 08:40:08 -04:00
// Emit notification
mediator.emit('notification:send', {
2014-09-26 08:46:35 -04:00
title: title.substring(0, 20),
body: text.substring(0, 80),
icon: icon
});
2014-09-27 08:40:08 -04:00
// Emit sound
if (type === 'message') {
mediator.emit('audio:play', sounds.message);
}
},
2014-09-26 08:46:35 -04:00
2014-10-20 13:14:44 -04:00
motd: function (message) {
commands.post('motd', message);
2014-09-27 08:40:08 -04:00
},
2014-09-26 08:46:35 -04:00
2014-09-27 08:40:08 -04:00
info: function (message) {
commands.post('info', message);
},
2014-09-26 08:46:35 -04:00
2014-09-27 08:40:08 -04:00
error: function (message) {
commands.post('error', message);
},
2014-09-26 12:21:27 -04:00
2014-09-27 08:40:08 -04:00
server: function (message) {
commands.post('server', message);
},
2014-09-26 08:46:35 -04:00
2014-09-27 08:40:08 -04:00
message: function (data) {
2017-02-23 18:03:24 -05:00
commands.post('message', data.message, '[' + data.uuid.substring(0,8) + '] ' + data.nick);
2014-09-27 08:40:08 -04:00
},
clearInput: function () {
2017-02-07 15:19:23 -05:00
components.input[0].value = '';
2014-09-27 08:40:08 -04:00
},
2014-09-26 08:46:35 -04:00
2014-09-27 08:40:08 -04:00
clear: function () {
2017-02-07 15:19:23 -05:00
components.chat[0].innerHTML = '';
2014-09-27 08:40:08 -04:00
},
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();
});
}
},
2014-09-26 08:46:35 -04:00
// Handler for the document`s keyDown-event.
onKeyDown = function (e) {
var buffer,
parts,
payload,
2017-02-22 17:09:50 -05:00
command;
2014-09-26 08:46:35 -04:00
// The Document object is bound to this element.
// If the active element is not the input, focus on it and exit the function.
// Ignore this when ctrl and/or alt is pressed!
if (!e.ctrlKey && !e.altKey && components.input[0] !== $.activeElement()) {
return components.input.focus();
}
// Return immediatly if the buffer is empty or if the hit key was not <enter>
if (e.keyCode !== 13 || !(buffer = components.input[0].value)) {
return;
}
// Handle command
if ((buffer[0] || buffer.slice(0, 1)) === '/') {
parts = $.ssplit(buffer.slice(1), ' ');
command = parts[0];
payload = parts[1];
// Shout this command to all modules
mediator.emit(
2014-10-20 13:14:44 -04:00
'command:' + command,
2014-09-26 08:46:35 -04:00
payload,
function(retvals, recipients) {
if(!recipients) {
2014-09-27 08:40:08 -04:00
return commands.post('error', $.template(templates.messages.unrecognized_command, { commandName: command }));
2014-09-26 08:46:35 -04:00
} else {
2014-09-27 08:40:08 -04:00
commands.clearInput();
2014-09-26 08:46:35 -04:00
}
}
);
} else /* Handle ordinary message */ {
if(!parameters.room || !parameters.key ) {
// Make sure that the user has joined a room and the key is set
2014-09-27 08:40:08 -04:00
return (!parameters.room) ? commands.post('error', templates.messages.msg_no_room) : commands.post('error', templates.messages.msg_no_key);
2014-09-26 08:46:35 -04:00
}
2017-02-23 18:03:24 -05:00
var plainData = {
msg: buffer,
nick: parameters.nick ? parameters.nick : false
},
encData = $.AES.encrypt(JSON.stringify(plainData), parameters.room + parameters.key).toString(),
hashRoom = $.SHA1(parameters.room);
2014-09-26 08:46:35 -04:00
mediator.emit(
'socket:emit',
{
data: 'message:send',
payload: {
2017-02-23 18:03:24 -05:00
room: hashRoom,
data: encData
2014-09-26 08:46:35 -04:00
}
}
);
// And clear the the buffer
2014-09-27 08:40:08 -04:00
commands.clearInput();
2014-09-26 08:46:35 -04:00
}
};
// Bind the necessary DOM events
$(document).on('keydown', onKeyDown);
2014-09-26 08:46:35 -04:00
// Put focus on the message input
components.input.focus();
// Connect events
2014-09-27 08:40:08 -04:00
for (var commandName in commands) {
2014-09-27 08:56:57 -04:00
if (commandName !== '_require' && commandName !== 'post') {
2014-09-27 08:40:08 -04:00
mediator.on('console:' + commandName, commands[commandName]);
}
}
mediator.on('console:require', commands._require);
mediator.on('console:post', function (data) {
2014-09-27 08:40:08 -04:00
commands.post(data.type, data.data, data.nick);
});
2014-09-26 08:46:35 -04:00
});