From 276220eaf497e2cef03202803a1b748e49166d08 Mon Sep 17 00:00:00 2001 From: unkelpehr Date: Sat, 27 Sep 2014 14:40:08 +0200 Subject: [PATCH] Bughunt --- public/js/cryptalk_modules/console.js | 187 +++++++++++++------------- 1 file changed, 97 insertions(+), 90 deletions(-) diff --git a/public/js/cryptalk_modules/console.js b/public/js/cryptalk_modules/console.js index 5b7bb61..127a850 100644 --- a/public/js/cryptalk_modules/console.js +++ b/public/js/cryptalk_modules/console.js @@ -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 (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); }, + motd: function () { + commands.post('motd', settings.motd); + }, - clearInput = function () { - fandango.subordinate(function () { - components.input[0].value = ''; - }); - }, + info: function (message) { + commands.post('info', message); + }, - clear = function () { - fandango.subordinate(function () { - components.chat[0].innerHTML = ''; - }); - }, + error: function (message) { + commands.post('error', message); + }, - lockInput = function () { - components.input[0].setAttribute('disabled', 'disabled'); - components.inputWrapper[0].className = 'loading'; - }, + server: function (message) { + commands.post('server', message); + }, - unlockInput = function () { - components.input[0].removeAttribute('disabled'); - components.inputWrapper[0].className = ''; - components.input.focus(); - }, + message: function (data) { + commands.post('message', data.message, data.nick); + }, - _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(); - }); + clearInput: function () { + fandango.subordinate(function () { + components.input[0].value = ''; + }); + }, + + 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); }); }); \ No newline at end of file