Fred T-H avatar Fred T-H committed 69a6263

polishing up (functionally speaking) the js client. removing dependencies to any js lib but jQuery

Comments (0)

Files changed (2)

 	},
 	callbacks: {
 		success: function(){
-			alert('huge success');
 		},
 		error: function(){
-			alert('cake is a lie?');
 		},
 		listen: function(Val){
-			ScriptCommunicator.callback_called = true;
+			// add boxes
 			$.map(Val,function(Msg){
 				var Box = $('#chut_'+Msg.to);
-				if(Box.length > 0) {
-					Box.append("<dl><dd>"+Msg.action+": "+Msg.to+"</dd><dt>"+Msg.message+"</dt></dl>");
-				} else {
+				if(Box.length === 0) {
 					Chut.ui.add_chutbox('#chut',Msg.to);
-					Chut.callbacks.listen(Val);
 				}
 			});
+			// in two passes because DOM updating messed things up
+			var user_id = escape($('#chut_nick').val());
+			$.map(Val,function(Msg){
+				var Box = $('#chut_'+Msg.to);
+				if(Box.length > 0){
+					if(Msg.action == 'sent'){
+						Box.append('<dl class="sent"><dt>'+user_id+':</dt><dd>'+Msg.message+'</dd></dl>');
+					} else {
+						Box.append('<dl class="received><dt>'+Msg.to+':</dt><dd>'+Msg.message+'</dd></dl>');
+					}
+					console.log(Msg);
+					Box.scrollTop(Box.height());
+				}
+			});
+			Chut.listen($('#chut_nick').val());
 		},
 		message: function(e){
-			ScriptCommunicator.callback_called = true;
-			alert('called message: ' + e);
 		},
-		history: function(e){
-			ScriptCommunicator.callback_called = true;
-			alert('history is: ' + e);
+		history: function(Val){
+			// add boxes
+			$.map(Val,function(Msg){
+				var Box = $('#chut_'+Msg.to);
+				if(Box.length === 0) {
+					Chut.ui.add_chutbox('#chut',Msg.to);
+				}
+			});
+			// in two passes because DOM updating messed things up
+			var user_id = escape($('#chut_nick').val());
+			$.map(Val,function(Msg){
+				var Box = $('#chut_'+Msg.to);
+				if(Box.length > 0){
+					if(Msg.action == 'sent'){
+						Box.append('<dl class="hist sent"><dt>'+user_id+':</dt><dd>'+Msg.message+'</dd></dl>');
+					} else {
+						Box.append('<dl class="hist received"><dt>'+Msg.to+':</dt><dd>'+Msg.message+'</dd></dl>');
+					}
+					Box.scrollTop(Box.height());
+				}
+			});
 		}
 	},
 	// The server only has to return 'callback(JSON);'
 	_JSONP: function (url, callback) {
-		Chut._call(url+'&callback='+callback);
-	},
-	_call: function (url){
-		ScriptCommunicator.sourceJavaScript(url, Chut.callbacks.success, Chut.callbacks.error);
+		jQuery.ajax({
+			dataType: 'jsonp',
+			jsonp: 'callback',
+			jsonpCallback: callback,
+			url: url
+		});
 	},
 	// define some kind of JSON parser, maybe Crockford's?
 	// Yes, it is crockford's!
 					'<textarea id="chut_'+user_id+'_msg"></textarea>'+
 					'<label for="chut_'+user_id+'_to">To:</label>'+
 					'<input type="text" id="chut_'+user_id+'_to" value='+user_id+' readonly="readonly" />'+
-					'<input type="submit" id="chut_'+user_id+'_send" />'+
+					'<input type="submit" id="chut_'+user_id+'_send" value="Message"/>'+
 				'</div>';
 			$(el).append(txt);
 			// bind events!
 			$('#chut_'+user_id+'_send').click(function(){
 				var from = $('#chut_nick').val();
-				var msg = $('#chut_'+user_id+'_msg').val();
+				var msg_box = $('#chut_'+user_id+'_msg');
+				var msg = msg_box.val();
 				Chut.message(from, user_id, msg);
+				msg_box.val('');
 			});
 		}
 	}
 
 
 $(document).ready(function(){
-	// Assumes that ScriptCommunicator is loaded
-	if(typeof(ScriptCommunicator) === 'undefined'){
-		alert("ScriptCommunicator is missing");
-		return false;
-	}
 	// events about logging on?
 	/* Reserved IDs
-	 * #chut:		   general chut container
-	 * #chut_nick:	  user_id
+	 * #chut:           general chut container
+	 * #chut_nick:      user_id
 	 * #chut_<id>_to:   user_id of message receiver
 	 * #chut_<id>_msg:  message to send to a user
-	 * #chut_<id>:	  chat box of a discussion with a given user
+	 * #chut_<id>:      chat box of a discussion with a given user
 	 */
 	$('#connect').click(function(){
 		var nickbox = $('#chut_nick');
 <head>
 	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 	<link rel="icon" type="image/vnd.microsoft.icon" href="favicon.ico" />
-	<title>Chut - Demo comet implementation</title>
+	<title>Chut - Demo implementation</title>
 	<style type="text/css">
 		label {
 			display: block;
 			height: 10em;
 			overflow-y: scroll;
 		}
+		.chut dl {
+			margin: 0.3em;
+		}
+		.chut dl.hist {
+			color: #666;
+			font-style: italic;
+		}
+		.chut dl.sent dt {
+			font-weight: bold;
+		}
+		.chut dl dt {
+			margin: 0;
+		}
+		.chut dl dd {
+			margin: 0 0 0 1.5em;
+		}
 	</style>
 	<!-- javascript ici -->
 	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
-	<script type="text/javascript" src="script_communicator.js"></script>
-	<script type="text/javascript" src="json_parse.js"></script>
 	<script type="text/javascript" src="client.js"></script>
 </head>
 <body>
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.