All pastes #801492 Raw Edit

Stuff

public text v1 · immutable
#801492 ·published 2007-11-29 09:42 UTC
rendered paste body
function updateConversations( parentId, conversationsList ) {
	var parent = document.getElementById( parentId );
	for (var i = 0; i < conversationsList.length; i++) {
		var conversation = conversationsList[i];
		var element = document.getElementById( conversation.id );
		if (element==null) {
			appendConversation( parent, conversation );
		}
	   else {
			updateConversationDisplay( element, conversation );
	   }
	}
	var displayedList = parent.childNodes;
	for (var i=0; i < displayedList.length; i++) {
		if (displayedList[i].nodeType != 1) {
			continue;
		}
		if ( !contains( conversationsList, displayedList[i].id )) {
			parent.removeChild( displayedList[i]);
		}
	}
}
	
function contains( conversationsList, id ) {
	for (var i = 0; i < conversationsList.length; i++) {
		if (id==conversationsList[i].id) {
			return true;
		}
	}
	return false;	
}
	
function updateConversationDisplay( link, conversation ) {
	link.setAttribute( 'class', conversation.state );
	if (link.firstChild) {
		link.firstChild.nodeValue = conversation.name;
	}
	else {
		link.appendChild(document.createTextNode(conversation.name));	
	}
}
	
function openChat( e ) {
	if (!e) var e = window.event;
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;	
	
	var conversationUrl = targ.getAttribute('conversationUrl');
	var conversationId = targ.getAttribute( 'conversationId' ); 
	window.open( conversationUrl, conversationId);
}

function appendConversation( parent, conversation ) {
	var link = document.createElement('a');
	link.setAttribute( "id",conversation.id );
	link.setAttribute( "href","#");
	link.setAttribute( "conversationUrl", conversation.url );
	link.setAttribute( "conversationId", conversation.id );
	if (link.addEventListener){
		link.addEventListener('click', openChat, false); 
	}
	else {
		link.attachEvent('onclick', openChat );
	}
	updateConversationDisplay( link, conversation );					
	parent.appendChild( link );	
}