Philip Pryce avatar Philip Pryce committed c37c9dc

Moved some utility functions from ui.js into new util.js, added themes

Comments (0)

Files changed (6)

 		<meta http-equiv="X-UA-Compatible" content="chrome=1" />
 		<title>Loading...</title>
 		<link type="text/css" rel="stylesheet" href="css/yui.css" />
-		<link type="text/css" rel="stylesheet" href="css/default.css" id="theme" />
+		<link type="text/css" rel="stylesheet" href="css/default.css" id="default-theme" />
 	</head>
 	<body>
 		<div class="split">
 				<dl></dl>
 			</div>
 		</div>
+		
 		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
 		<script src="javascript/swfobject.js" type="text/javascript"></script>
 		<script src="javascript/jquery.expander.js" type="text/javascript"></script>
 		<script src="javascript/config.js" type="text/javascript"></script>
+		<script src="javascript/util.js" type="text/javascript"></script>
 		<script src="javascript/ui.js" type="text/javascript"></script>
 		<script src="javascript/joules.js" type="text/javascript"></script>
 		<script src="javascript/jolt.js" type="text/javascript"></script>

javascript/config.js

 	
 	title: "Jolt Chat Client",	// Displayed in the title of the browser before the current views name
 	
-	// Current Theme
-	theme: '',
+	// Name of the theme you wish to use, it should be a .css file in the ./css directory
+	theme: null,
 	
 	// Jolt Extensions
 	extensions: [				// An array of file names in the extensions folder

javascript/jolt.js

 		});
 	});
 	
+	// block packet
 	Jolt.packet.register('block', function ()
 	{
 		if (this.parameters.e == 'ok')

javascript/joules.js

 	 **/
 	function onpacket (rpkt)
 	{
-		var ppkt = parse_packet($.trim(rpkt));
+		var ppkt = parse_packet(Util.trim(rpkt));
 		Events.emit('Joules:onpacket', ppkt);
 		Events.emit('Joules:packet:' + ppkt.command, ppkt);
 		if (ppkt.subcommand) { Events.emit('Joules:packet:' + ppkt.command + ":" + ppkt.subcommand, ppkt); }
 	    	if (!!value) { packet.push(key + "=" + value); }
 	    }
 	    if (!!content) { packet.push("\n" + content); }
-	    return $.trim(packet.join("\n"));
+	    return Util.trim(packet.join("\n"));
 	}
 
 	
  * @version:		1.0b
  **/
 
-// String Sha1 Prototytpe
-// Source: http://www.webtoolkit.info/javascript-sha1.html
-// With minor adjustments to make it JSLint compliant.
-String.prototype.sha1 = function ()
-{
-	function rotate_left (n,s)
-	{
-		var t4 = ( n<<s ) | (n>>>(32-s));
-		return t4;
-	}
-	function lsb_hex(val)
-	{
-		var str = "", i, vh, vl;
-		for( i=0; i<=6; i+=2 )
-		{
-			vh = (val>>>(i*4+4))&0x0f;
-			vl = (val>>>(i*4))&0x0f;
-			str += vh.toString(16) + vl.toString(16);
-		}
-		return str;
-	}
-	function cvt_hex(val)
-	{
-		var str = "", i, v;
-		for( i=7; i>=0; i-- )
-		{
-			v = (val>>>(i*4))&0x0f;
-			str += v.toString(16);
-		}
-		return str;
-	}
-	function Utf8Encode(string)
-	{
-		string = string.replace(/\r\n/g,"\n");
-		var utftext = "";
-		for (var n = 0; n < string.length; n++)
-		{
-			var c = string.charCodeAt(n);
-			if (c < 128)
-			{
-				utftext += String.fromCharCode(c);
-			}
-			else if((c > 127) && (c < 2048))
-			{
-				utftext += String.fromCharCode((c >> 6) | 192);
-				utftext += String.fromCharCode((c & 63) | 128);
-			}
-			else
-			{
-				utftext += String.fromCharCode((c >> 12) | 224);
-				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
-				utftext += String.fromCharCode((c & 63) | 128);
-			}
-		}
-		return utftext;
-	}
-	var blockstart;
-	var i, j;
-	var W = new Array(80);
-	var H0 = 0x67452301;
-	var H1 = 0xEFCDAB89;
-	var H2 = 0x98BADCFE;
-	var H3 = 0x10325476;
-	var H4 = 0xC3D2E1F0;
-	var A, B, C, D, E;
-	var temp;
-	var msg = Utf8Encode(this);
-	var msg_len = msg.length;
-	var word_array = [];
-	for( i=0; i<msg_len-3; i+=4 )
-	{
-		j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
-		msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
-		word_array.push( j );
-	}
-	switch( msg_len % 4 )
-	{
-		case 0:
-			i = 0x080000000;
-		break;
-		case 1:
-			i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
-		break;
-	
-		case 2:
-			i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
-		break;
-	
-		case 3:
-			i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8	| 0x80;
-		break;
-	}
-	word_array.push( i );
-	while( (word_array.length % 16) != 14 ) { word_array.push( 0 ); }
-	word_array.push( msg_len>>>29 );
-	word_array.push( (msg_len<<3)&0x0ffffffff );
-	for ( blockstart=0; blockstart<word_array.length; blockstart+=16 )
-	{
-		for( i=0; i<16; i++ ) { W[i] = word_array[blockstart+i]; }
-		for( i=16; i<=79; i++ ) { W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); }
-		A = H0;
-		B = H1;
-		C = H2;
-		D = H3;
-		E = H4;
-		for( i= 0; i<=19; i++ )
-		{
-			temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
-			E = D;
-			D = C;
-			C = rotate_left(B,30);
-			B = A;
-			A = temp;
-		}
-		for( i=20; i<=39; i++ )
-		{
-			temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
-			E = D;
-			D = C;
-			C = rotate_left(B,30);
-			B = A;
-			A = temp;
-		}
-		for( i=40; i<=59; i++ )
-		{
-			temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
-			E = D;
-			D = C;
-			C = rotate_left(B,30);
-			B = A;
-			A = temp;
-		}
-		for( i=60; i<=79; i++ )
-		{
-			temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
-			E = D;
-			D = C;
-			C = rotate_left(B,30);
-			B = A;
-			A = temp;
-		}
-		H0 = (H0 + A) & 0x0ffffffff;
-		H1 = (H1 + B) & 0x0ffffffff;
-		H2 = (H2 + C) & 0x0ffffffff;
-		H3 = (H3 + D) & 0x0ffffffff;
-		H4 = (H4 + E) & 0x0ffffffff;
-	}
-	return (cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4)).toLowerCase();
-};
-
-/**
- * Custom Event emitter
- **/
-var Events = (function ()
-{
-	function call (func, data) { func.call(data || null); }
-	
-	function each (arr, func)
-	{
-		if (Array.prototype.forEach) { return arr.forEach(func); }
-		for (var i = 0, len = arr.length; i < len; i++)
-		{
-			if (i in arr) { func.call(null, arr[i], i, arr); }
-		}
-	}
-	
-	var callbacks = { };
-	function emit (name, data)
-	{
-		if (!callbacks[name] || callbacks[name].length === 0) { return Events; }
-		var len = callbacks[name].length;
-		if (len === 0) { call(callbacks[name][0], data); }
-		else { each(callbacks[name], function (i) { call(i, data); }); }
-		return Events;
-	}
-	
-	function listen (name, func)
-	{
-		if (!callbacks[name]) { callbacks[name] = [func]; return Events; }
-		callbacks[name].push(func);
-		return Events;
-	}
-	
-	function clear (name) { callbacks[name] = []; return Events; }
-	
-	return { "emit": emit, "listen": listen, "clear": clear };
-})();
-
-/**
- * Key constants, for prettier code!
- **/
-var Keys = { ENTER: 13 , TAB: 9, UP: 38, DOWN: 40, SQ_BKT_LEFT: 27, SQ_BKT_RIGHT: 29 };
-
 /**
  * The HUGE UI object!
  **/
 		UI.bind_events();
 		// A necessary evil, unfortunately
 		if (navigator.userAgent.indexOf('MSIE') > -1) { $('body').addClass('ie'); }
+		
+		if (!!config.theme && config.theme !== '')
+		{
+			$('<link href="css/' + config.theme + '.css" type="text/css" rel="stylesheet" id="custom-theme" />').appendTo('head');
+		}
 	},
 	
 	/**
 		});
 		$('span.channel-name').live('click', function ()
 		{
-			var channel = $.trim(this.innerHTML) || false;
+			var channel = Util.trim(this.innerHTML) || false;
 			if (channel === false) { return; }
 			
 			Packet(['join', 'ns=' + channel.substr(1)]);
 		process: function ()
 		{
 			var user = $('input[name="login[user]"]', '.login-inset').val();
-			var pass = $('input[name="login[pass]"]', '.login-inset').val().sha1();
+			var pass = Util.sha1($('input[name="login[pass]"]', '.login-inset').val());
 			Events.emit('UI:login', { user: user, pass: pass });
 			return UI.login;
 		},

javascript/util.js

+/* @preserve
+ * @name:			util.js
+ * @author:			Philip Pryce <philip.pryce@me.com>
+ * @description:	Some utilities that come in handy
+ * @version:		1.0b
+ **/
+
+
+/**
+ * Common Utility Functions used around the website
+ **/
+var Util = (function ()
+{
+	// String Sha1 Prototytpe
+	// Source: http://www.webtoolkit.info/javascript-sha1.html
+	// With minor adjustments to make it JSLint compliant.
+	function sha1 (str)
+	{
+		function rotate_left (n,s)
+		{
+			var t4 = ( n<<s ) | (n>>>(32-s));
+			return t4;
+		}
+		function lsb_hex(val)
+		{
+			var str = "", i, vh, vl;
+			for( i=0; i<=6; i+=2 )
+			{
+				vh = (val>>>(i*4+4))&0x0f;
+				vl = (val>>>(i*4))&0x0f;
+				str += vh.toString(16) + vl.toString(16);
+			}
+			return str;
+		}
+		function cvt_hex(val)
+		{
+			var str = "", i, v;
+			for( i=7; i>=0; i-- )
+			{
+				v = (val>>>(i*4))&0x0f;
+				str += v.toString(16);
+			}
+			return str;
+		}
+		function Utf8Encode(string)
+		{
+			string = string.replace(/\r\n/g,"\n");
+			var utftext = "";
+			for (var n = 0; n < string.length; n++)
+			{
+				var c = string.charCodeAt(n);
+				if (c < 128)
+				{
+					utftext += String.fromCharCode(c);
+				}
+				else if((c > 127) && (c < 2048))
+				{
+					utftext += String.fromCharCode((c >> 6) | 192);
+					utftext += String.fromCharCode((c & 63) | 128);
+				}
+				else
+				{
+					utftext += String.fromCharCode((c >> 12) | 224);
+					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
+					utftext += String.fromCharCode((c & 63) | 128);
+				}
+			}
+			return utftext;
+		}
+		var blockstart;
+		var i, j;
+		var W = new Array(80);
+		var H0 = 0x67452301;
+		var H1 = 0xEFCDAB89;
+		var H2 = 0x98BADCFE;
+		var H3 = 0x10325476;
+		var H4 = 0xC3D2E1F0;
+		var A, B, C, D, E;
+		var temp;
+		var msg = Utf8Encode(str);
+		var msg_len = msg.length;
+		var word_array = [];
+		for( i=0; i<msg_len-3; i+=4 )
+		{
+			j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
+			msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
+			word_array.push( j );
+		}
+		switch( msg_len % 4 )
+		{
+			case 0:
+				i = 0x080000000;
+			break;
+			case 1:
+				i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
+			break;
+		
+			case 2:
+				i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
+			break;
+		
+			case 3:
+				i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8	| 0x80;
+			break;
+		}
+		word_array.push( i );
+		while( (word_array.length % 16) != 14 ) { word_array.push( 0 ); }
+		word_array.push( msg_len>>>29 );
+		word_array.push( (msg_len<<3)&0x0ffffffff );
+		for ( blockstart=0; blockstart<word_array.length; blockstart+=16 )
+		{
+			for( i=0; i<16; i++ ) { W[i] = word_array[blockstart+i]; }
+			for( i=16; i<=79; i++ ) { W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); }
+			A = H0;
+			B = H1;
+			C = H2;
+			D = H3;
+			E = H4;
+			for( i= 0; i<=19; i++ )
+			{
+				temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
+				E = D;
+				D = C;
+				C = rotate_left(B,30);
+				B = A;
+				A = temp;
+			}
+			for( i=20; i<=39; i++ )
+			{
+				temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
+				E = D;
+				D = C;
+				C = rotate_left(B,30);
+				B = A;
+				A = temp;
+			}
+			for( i=40; i<=59; i++ )
+			{
+				temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
+				E = D;
+				D = C;
+				C = rotate_left(B,30);
+				B = A;
+				A = temp;
+			}
+			for( i=60; i<=79; i++ )
+			{
+				temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
+				E = D;
+				D = C;
+				C = rotate_left(B,30);
+				B = A;
+				A = temp;
+			}
+			H0 = (H0 + A) & 0x0ffffffff;
+			H1 = (H1 + B) & 0x0ffffffff;
+			H2 = (H2 + C) & 0x0ffffffff;
+			H3 = (H3 + D) & 0x0ffffffff;
+			H4 = (H4 + E) & 0x0ffffffff;
+		}
+		return (cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4)).toLowerCase();
+	}
+	
+	/**
+	 * Trims white space from around strings
+	 **/
+	function trim (str)
+	{
+		return str.replace(/^\s+|\s+$/g,"");
+	}
+	
+	return {
+		"sha1": sha1,
+		"trim": trim
+	};
+	
+})();
+
+
+/**
+ * Custom Event emitter
+ **/
+var Events = (function ()
+{
+	function call (func, data) { func.call(data || null); }
+	
+	function each (arr, func)
+	{
+		if (Array.prototype.forEach) { return arr.forEach(func); }
+		for (var i = 0, len = arr.length; i < len; i++)
+		{
+			if (i in arr) { func.call(null, arr[i], i, arr); }
+		}
+	}
+	
+	var callbacks = { };
+	function emit (name, data)
+	{
+		if (!callbacks[name] || callbacks[name].length === 0) { return Events; }
+		var len = callbacks[name].length;
+		if (len === 0) { call(callbacks[name][0], data); }
+		else { each(callbacks[name], function (i) { call(i, data); }); }
+		return Events;
+	}
+	
+	function listen (name, func)
+	{
+		if (!callbacks[name]) { callbacks[name] = [func]; return Events; }
+		callbacks[name].push(func);
+		return Events;
+	}
+	
+	function clear (name) { callbacks[name] = []; return Events; }
+	
+	return { "emit": emit, "listen": listen, "clear": clear };
+})();
+
+/**
+ * Key constants
+ **/
+var Keys = { ENTER: 13 , TAB: 9, UP: 38, DOWN: 40, SQ_BKT_LEFT: 27, SQ_BKT_RIGHT: 29 };
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.