General cleanup

This commit is contained in:
unkelpehr 2014-09-24 17:20:20 +02:00
parent 8b84f8f9fd
commit 65de12499d
8 changed files with 233 additions and 85 deletions

View File

@ -25,6 +25,10 @@ fandango.defaults({
// Require main cryptalk module. // Require main cryptalk module.
require(['cryptalk'], function () {}, function (e) { require(['cryptalk'], function () {}, function (e) {
document.getElementById('chat').innerHTML = '<li><i class="fatal">Fatal: An error was thrown during initialization causing the application to stop.<br>Examine the logs for more details.</i></li>'; document.getElementById('chat').innerHTML = '<li><i class="fatal">Fatal: An error was thrown during initialization causing the application to stop.<br>Examine the logs for more details.</i></li>';
console.log && console.log(e);
if (console.log) {
console.log(e);
}
throw e; throw e;
}); });

View File

@ -30,7 +30,7 @@ define(['fandango', 'websocket', 'aes'], function (fandango, websocket, aes) {
} else { } else {
selector = selector.slice(1); selector = selector.slice(1);
if (match = document.getElementById(selector)) { if ((match = document.getElementById(selector))) {
matches.push(match); matches.push(match);
} }
} }
@ -69,7 +69,7 @@ define(['fandango', 'websocket', 'aes'], function (fandango, websocket, aes) {
utils.activeElement = function () { utils.activeElement = function () {
try { return document.activeElement; } catch (e) {} try { return document.activeElement; } catch (e) {}
} };
/** /**
* Removes all characters but 0 - 9 from given string. * Removes all characters but 0 - 9 from given string.

View File

@ -1,8 +1,8 @@
/* Usage: /* Usage:
channel.emit('audio:play',message); mediator.emit('audio:play',message);
channel.emit('audio:on'); mediator.emit('audio:on');
channel.emit('audio:off'); mediator.emit('audio:off');
*/ */
@ -12,8 +12,6 @@ define(['queue','mediator'], function (queue,mediator) {
var ac = false, var ac = false,
enabled = true, enabled = true,
channel = mediator(),
// Recursive function for playing tones, takes an array of [tone,start_ms,duration_ms] - entries // Recursive function for playing tones, takes an array of [tone,start_ms,duration_ms] - entries
// i is only used for recursion // i is only used for recursion
playTones = function (tones, i) { playTones = function (tones, i) {
@ -22,7 +20,9 @@ define(['queue','mediator'], function (queue,mediator) {
i = (i === undefined) ? 0 : i; i = (i === undefined) ? 0 : i;
// Stop if we've reached the end of iteration, and require ac // Stop if we've reached the end of iteration, and require ac
if( !(i < Object.keys(tones).length) || !ac || !enabled ) return; if (!ac || !enabled || !(i < Object.keys(tones).length)) {
return;
}
// Add tones to execution queue // Add tones to execution queue
var current_tones = tones[i], var current_tones = tones[i],
@ -58,10 +58,12 @@ define(['queue','mediator'], function (queue,mediator) {
enabled = false; enabled = false;
}; };
if( window.AudioContext || window.webkitAudioContext ) ac = new ( window.AudioContext || window.webkitAudioContext ); if (window.AudioContext || window.webkitAudioContext) {
ac = new (window.AudioContext || window.webkitAudioContext);
}
channel.on('audio:play',function(message) { playTones(message) }); mediator.on('audio:play', function (message) { playTones(message); });
channel.on('audio:on',function(message) { on(); }); mediator.on('audio:on', function (message) { on(); });
channel.on('audio:off',function(message) { off(); }); mediator.on('audio:off', function (message) { off(); });
}); });

View File

@ -1,9 +1,8 @@
// Main cryptalk module // Main cryptalk module
define({ define({
compiles: ['$'], compiles: ['$'],
requires: ['mediator','hosts', 'templates', 'audio', 'fandango','notifications','sounds','win'] requires: ['mediator','hosts', 'templates', 'audio', 'fandango','notifications', 'sounds', 'win']
}, function ($, requires, data) { }, function ($, requires, data) {
var socket, var socket,
key, key,
host, host,
@ -29,9 +28,9 @@ define({
// Shortcut // Shortcut
hosts = requires.hosts, hosts = requires.hosts,
fandango = requires.fandango, fandango = requires.fandango,
mediator = requires.mediator,
templates = requires.templates, templates = requires.templates,
sounds = requires.sounds, sounds = requires.sounds,
channel = requires.mediator(),
win = requires.win, win = requires.win,
lockInput = function () { lockInput = function () {
@ -51,7 +50,7 @@ define({
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
channel.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),
@ -59,7 +58,7 @@ define({
}); });
// Emit sound // Emit sound
if ( type == 'message' ) channel.emit('audio:play',sounds.message); if ( type == 'message' ) mediator.emit('audio:play',sounds.message);
}, },
@ -81,7 +80,7 @@ define({
clearInput(); clearInput();
} }
showNotification(type, nick, text) showNotification(type, nick, text);
// Append the post to the chat DOM element // Append the post to the chat DOM element
components.chat[clearChat ? 'html' : 'append'](post); components.chat[clearChat ? 'html' : 'append'](post);
@ -117,14 +116,14 @@ define({
post('info', strhosts); post('info', strhosts);
done(); done();
} }
} };
}; };
// //
force = (force && force.toLowerCase() === 'force'); force = (force && force.toLowerCase() === 'force');
// Loop through all the hosts // Loop through all the hosts
while (host = hosts.hosts[i]) { while ((host = hosts.hosts[i])) {
if (!force && host.settings !== undefined) { if (!force && host.settings !== undefined) {
if (host.settings) { if (host.settings) {
callback(host, i, 1)(); callback(host, i, 1)();
@ -150,7 +149,7 @@ define({
} }
if ($.isDigits(toHost)) { if ($.isDigits(toHost)) {
if (host = hosts.hosts[+toHost]) { if ((host = hosts.hosts[+toHost])) {
if (host.settings) { if (host.settings) {
settings = host.settings; settings = host.settings;
} else { } else {
@ -213,7 +212,7 @@ define({
.on('message:send', function (data) { .on('message:send', function (data) {
var decrypted = $.AES.decrypt(data.msg, room + key), var decrypted = $.AES.decrypt(data.msg, room + key),
sanitized = $.escapeHtml(decrypted), sanitized = $.escapeHtml(decrypted),
nick = (data.nick == undefined || !data.nick ) ? templates.default_nick : $.escapeHtml($.AES.decrypt(data.nick, room + key)); nick = !data.nick ? templates.default_nick : $.escapeHtml($.AES.decrypt(data.nick, room + key));
if (!decrypted) { if (!decrypted) {
post('error', templates.messages.unable_to_decrypt); post('error', templates.messages.unable_to_decrypt);
@ -274,7 +273,7 @@ define({
reconnect: function (foo, done) { reconnect: function (foo, done) {
if (host) { if (host) {
if (host.connected) { if (host.connected) {
commands.disconnect() commands.disconnect();
commands.connect(host, done); commands.connect(host, done);
} else { } else {
commands.connect(host, done); commands.connect(host, done);
@ -503,7 +502,7 @@ define({
socket.emit('message:send', { socket.emit('message:send', {
room: room, room: room,
msg: $.AES.encrypt(buffer, room + key).toString(), msg: $.AES.encrypt(buffer, room + key).toString(),
nick: (nick && nick != undefined) ? $.AES.encrypt(nick, room + key).toString() : false nick: nick ? $.AES.encrypt(nick, room + key).toString() : false
}); });
// And clear the the buffer // And clear the the buffer
@ -524,13 +523,13 @@ define({
post('motd', templates.motd, true); post('motd', templates.motd, true);
// Route mediator messages // Route mediator messages
channel.on('window:focused',function() { mediator.on('window:focused',function() {
channel.emit('audio:off'); mediator.emit('audio:off');
channel.emit('notification:off'); mediator.emit('notification:off');
}); });
channel.on('window:blurred',function() { mediator.on('window:blurred',function() {
if( !mute ) channel.emit('audio:on'); if( !mute ) mediator.emit('audio:on');
channel.emit('notification:on'); mediator.emit('notification:on');
}); });
unlockInput(); unlockInput();

View File

@ -1 +1,188 @@
define(function(){function f(d,a,b){var c=e[d],g=c.length,h=g,f=h,k=[],l=b?function(a){a&&k.push(a);!--h&&b(k,g)}:m;if(c)for(;d=c[--f];)d[1](a,l),2>d[1].length&&h--;else b&&b(k,g)}var l=0,e={},m=function(){};return function(){var d=l++;return{emit:function(a,b,c){f(a,b,c);return this},on:function(a,b){(e[a]||(e[a]=[])).push([d,b]);return this},off:function(a){var b=0,c=e[a];if(c)for(;a=c[b++];)a[0]===d&&c.splice(--b,1);return this}}}}); (function (self, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') { // Node
module.exports = factory();
} else {
// Attaches to the current context
self.castrato = factory;
}
}(this, (function () {
var
/**
* Contains the next unique node id.
*
* @property index
* @type {Integer}
* @private
*/
index = 0,
/**
* Contains all subscriptions
*
* @property subs
* @type {Object}
* @private
*/
subs = {},
/**
* An empty function.
* This method does not accept any arguments.
*
* @property noop
* @type {function}
* @private
*/
noop = function () {};
/**
* Creates a new entry in the `subs` object.
*
* @method on
* @private
* @param {Integer} fromId The unique subscriber ID.
* @param {String} event The event to subscribe to.
* @param {Function} func A function to execute when the event is triggered.
*/
function on (fromId, event, func) {
(subs[event] || (subs[event] = [])).push([fromId, func, func.length]);
}
/**
* Removes all event handlers originating from `fromId` and optionally filter by handler.
*
* @method off
* @private
* @param {Integer} fromId The unique subscriber ID.
* @param {String} event The event to unsubscribe from.
* @param {Function} [func=null] The original handler that was attached to this event. If not passed, all subscriptions will be removed.
*/
function off (fromId, event, func) {
var sub,
i = 0,
toSubs = subs[event];
if (toSubs) {
while ((sub = toSubs[i++])) {
if (sub[0] === fromId && (!func || func === sub[1])) {
toSubs.splice(--i, 1);
}
}
}
}
/**
* Loops through all subscriptions, calling all handlers attached to given `event`.
*
* @method emit
* @private
* @param {Integer} fromId The unique subscriber ID.
* @param {String} event The event to emit
* @param {Object} [data=undefined] Parameters to pass along to the event handler.
* @param {Function} [func=undefined] A function to execute when all event handlers has returned.
*/
function emit (persistent, event, data, func) {
var sub,
toSubs = subs[event] || [],
total = toSubs.length,
left = total,
loop = total,
answers = [],
done;
if (loop) {
done = !func ? noop : function (data) {
if (data) {
answers.push(data);
}
if (!--left) {
func(answers, total);
func = 0;
}
};
while ((sub = toSubs[--loop])) {
sub[1](data, (sub[2] > 1) ? done : left--);
}
}
// `func` get destructed when called.
// It has to be called at least once - even if no one was subscribing.
// Execute it if it still exists.
if (func) {
func(answers, total);
}
}
return function () {
var nodeId = index++;
return {
/**
* Execute all handlers attached to the given event.
*
* @method emit
* @param {String} event The event to emit
* @param {Object} [data=undefined] Parameters to pass along to the event handler.
* @param {Function} [func=undefined] A function to execute when all event handlers has returned.
* @return {Object} `this`
* @example
* $.emit('something');
* $.emit('something', { foo: 'bar' });
* $.emit('something', { foo: 'bar' }, function (data, subscribers) {
* console.log('Emit done, a total of ' + subscribers + ' subscribers returned: ', data);
* });
*/
emit: function (persistent, event, data, func) {
// emit('something', { data: true }, function () {});
if (persistent !== true || persistent !== false) {
func = data;
data = event;
event = persistent;
persistent = false;
}
emit(persistent, event, data, func);
return this;
},
/**
* Attach an event handler function for one event.
*
* @method on
* @param {String} event The event to subscribe to.
* @param {Function} func A function to execute when the event is triggered.
* @return {Object} `this`
* @example
* $.on('something', function (data) {
* console.log('Got something!', data);
* });
*/
on: function (event, func) {
on(nodeId, event, func);
return this;
},
/**
* Removes an event handler function for one event.
*
* @method off
* @param {String} event The event to unsubscribe from.
* @param {Function} [func=null] The original handler that was attached to this event. If not passed, all subscriptions will be removed.
* @return {Object} `this`
* @example
* $.off('something');
* $.off('something else', handler);
*/
off: function (event) {
off(nodeId, event);
return this;
}
};
};
}())));

View File

@ -2,20 +2,20 @@
Usage Usage
// Send an notification // Send an notification
channel.emit('notification:send',{ mediator.emit('notification:send',{
title: 'Woop', title: 'Woop',
body: 'Woop woop', body: 'Woop woop',
icon: 'gfx/icon.png' icon: 'gfx/icon.png'
}); });
// Turn notifications on // Turn notifications on
channel.emit('notification:on'); mediator.emit('notification:on');
// Turn notifications off // Turn notifications off
channel.emit('notification:off'); mediator.emit('notification:off');
*/ */
define(['mediator','win'],function (mediator,win){ define(['mediator','win'],function (mediator, win){
var enabled = true, var enabled = true,
@ -26,8 +26,6 @@ define(['mediator','win'],function (mediator,win){
blink_timer, blink_timer,
interval, interval,
channel = mediator(),
now = function () { now = function () {
return performance.now() || Date.now(); return performance.now() || Date.now();
}, },
@ -104,9 +102,9 @@ define(['mediator','win'],function (mediator,win){
native_supported = (window.Notification !== undefined); native_supported = (window.Notification !== undefined);
channel.on('notification:send',function(data) { notify(data.title,data.body,data.icon,true); }); mediator.on('notification:send',function(data) { notify(data.title,data.body,data.icon,true); });
channel.on('notification:on',function() { on(); }); mediator.on('notification:on',function() { on(); });
channel.on('notification:off',function() { off(); }); mediator.on('notification:off',function() { off(); });
enableNative(); enableNative();

View File

@ -12,14 +12,13 @@
define(['mediator'],function (mediator){ define(['mediator'],function (mediator){
var exports = {}, var exports = {},
channel = mediator(),
focusCallback = function() { focusCallback = function() {
channel.emit('window:focused'); mediator.emit('window:focused');
}, },
blurCallback = function() { blurCallback = function() {
channel.emit('window:blurred'); mediator.emit('window:blurred');
}; };
exports.setTitle = function(t) { document.title = t; }, exports.setTitle = function(t) { document.title = t; },

View File

@ -1,41 +0,0 @@
/*
Emits:
'window:focused'
'window:blurred'
Exports:
title = window.getTitle();
window.setTitle(title);
*/
define(['mediator'],function (mediator){
var exports = {},
channel = mediator(),
focusCallback = function() {
channel.emit('window:focused');
},
blurCallback = function() {
channel.emit('window:blurred');
};
exports.getTitle = function(t) { document.title = t; },
exports.setTitle = function() { return document.title; };
// Keep track of document focus/blur
if (window.addEventListener){
// Normal browsers
window.addEventListener("focus", focusCallback, true);
window.addEventListener("blur", blurCallback, true);
} else {
// IE
window.observe("focusin", focusCallback);
window.observe("focusout", blurCallback);
}
return exports;
});