Community Central
Community Central
 
No edit summary
Line 1: Line 1:
 
$(function () {
/* get all personal code files in chat */
 
  +
if(wgPageName == 'Special:Chat') {
/* Important: you must click the chat button in the rail for this to work. */
 
  +
$('body').prepend('<style type="text/css">.chat-mod, .mod, .mod::after {color:#da0;} .chat-mod.away {color:#eda;} .ChatHeader .username, .me, .me::after {color:#00c;} .UserStatsMenu {color:#000;}</style>');
/* Very important: Chat is still a beta feature. Chat hacks may break as the feature is changed and updated, as it is a work in progress. */
 
  +
$(setTimeout('ChatCheck()', 200));
 
  +
//Add a 'go afk' button
 
  +
$('#Write').append('<a class="wikia-button" href="javascript:ToggleAway()" style="margin-left:3px;">AFK</a>');
function ChatCheck() {
 
  +
//Unbind all of the listeners that set your status to back
if($('.chat-join button').length != 0) {
 
  +
$(window).unbind('mousemove').unbind('focus').unbind('keypress');
$('.chat-join button').replaceWith('<a class="wikia-button" onclick="OpenChatWindow()"><img src="http://images.wikia.com/common/__cb36140/extensions/wikia/Chat/images/chat_icon.png" style="margin-right:3px;">Join the Chat</a>');
 
  +
//Set status to back when you press enter
} else {
 
  +
$('input[name=message]').bind('keypress', function () {if(event.keyCode == 13) {NodeChatHelper.resetActivityTimer();}});
setTimeout('ChatCheck()', 200);
 
  +
//Add a 'clear chat' button
}
 
  +
$('#Write').append('<a class="wikia-button" href="javascript:$(\'.Chat ul\').html(\'\')" style="margin-left:6px;">Clear</a>');
}
 
  +
 
  +
window.mods = [];
function OpenChatWindow() {
 
  +
window.list = document.getElementById('Users').getElementsByTagName('ul')[0].getElementsByTagName('li');
window.chatwindow = window.open('/index.php?title=Special:Chat&useskin=wikia');
 
  +
for(i = 0; i < list.length; i++) {
window.chatwindow.onload = function () {
 
  +
if(list[i].className.split(' ')[1] == 'chat-mod') {
//addOnloadHook, importScript, and importStylesheet
 
  +
mods.push(list[i].id.substring(5, list[i].id.length));
window.chatwindow.$('body').prepend('<script>\nfunction importScript(b){var a=wgScript+"?title="+encodeURIComponent(b.replace(/ /g,"_")).replace(/%2F/ig,"/").replace(/%3A/ig,":")+"&action=raw&ctype=text/javascript";return importScriptURI(a)}\nfunction importScriptURI(a){var b=document.createElement("script");b.setAttribute("src",a);b.setAttribute("type","text/javascript");document.getElementsByTagName("head")[0].appendChild(b);return b}\nfunction importScriptPage(b,d){var a="/index.php?title="+encodeURIComponent(b.replace(/ /g,"_")).replace("%2F","/").replace("%3A",":")+"&action=raw&ctype=text/javascript";if(typeof d=="string"){if(d.indexOf("://")==-1){a="http://"+d+".wikia.com"+a}else{a=d+a}}return importScriptURI(a)}\nfunction importStylesheet(a){return importStylesheetURI(wgScript+"?action=raw&ctype=text/css&title="+encodeURIComponent(a.replace(/ /g,"_")))}\nfunction importStylesheetURI(b,d){var a=document.createElement("link");a.type="text/css";a.rel="stylesheet";a.href=b;if(d){a.media=d}document.getElementsByTagName("head")[0].appendChild(a);return a}\nfunction importStylesheetPage(b,d){var a="/index.php?title="+encodeURIComponent(b.replace(/ /g,"_")).replace("%2F","/").replace("%3A",":")+"&action=raw&ctype=text/css";if(typeof d=="string"){if(d.indexOf("://")==-1){a="http://"+d+".wikia.com"+a}else{a=d+a}}return importStylesheetURI(a)}\n//This isn\'t the same as the regular addOnloadHook, because the regular one runs from a script tag in the body that I don\'t feel like appending. It\'s easier to just make it $(function), which is essentially equivalent\nfunction addOnloadHook(func) {$(func);}\n</script>');
 
  +
}
//global.js
 
  +
}
window.chatwindow.importScriptURI('http://community.wikia.com/index.php?title=User:' + wgUserName + '/global.js&action=raw&ctype=text/javascript');
 
  +
//wikia.js
 
  +
//Add/remove mods when people join/leave
window.chatwindow.importScriptURI(wgServer + '/index.php?title=User:' + wgUserName + '/wikia.js&action=raw&ctype=text/javascript');
 
  +
NodeChatController.view.model.users._callbacks.add[0] = function(user) {
//global.css
 
  +
if(user.attributes.isModerator == true) {
window.chatwindow.importStylesheetURI('http://community.wikia.com/index.php?title=User:' + wgUserName + '/global.css&action=raw&ctype=text/css');
 
  +
var add = true;
//wikia.css
 
  +
for(i in mods) {if(mods[i] == user.attributes.name) {add = false;}}
window.chatwindow.importStylesheetURI(wgServer + '/index.php?title=User:' + wgUserName + '/wikia.css&action=raw&ctype=text/css');
 
  +
if(add == true) {mods.push(user.attributes.name)}
 
  +
}
//These are my chat hacks. If you don't want them, you can delete this line.
 
  +
var view = new UserView({model: user});
window.chatwindow.importScriptURI('http://community.wikia.com/index.php?title=User:Monchoman45/ChatHacks.js&action=raw&ctype=text/javascript');
 
  +
$('#Users > ul').append(view.render().el);
}
 
  +
}
}
 
  +
NodeChatController.view.model.users._callbacks.remove[0] = function(user) {
  +
NodeChatHelper.log("Trying to remove " + user.get('name') + " from the list.");
  +
NodeChatHelper.log("Matches found: " + $('#' + NodeChatHelper.liIdByUsername( user.get('name') )).length);
  +
for(i in mods) {if(mods[i] == user.attributes.name) {mods[i] = undefined;}}
  +
$('#' + NodeChatHelper.liIdByUsername( user.get('name') )).remove();
  +
}
  +
//Color mod posts when they're added
  +
NodeChatController.view.model.chats._callbacks.add[0] = function (chat) {
  +
// Determine if chat view is presently scrolled to the bottom
  +
var isAtBottom = false;
  +
if (($("#Chat").scrollTop() + 1) >= ($("#Chat ul").outerHeight() - $("#Chat").height())) {
  +
isAtBottom = true;
  +
}
  +
  +
// Add message to chat
  +
var view = new ChatView({model: chat});
  +
for(i in mods) {if(mods[i] == view.model.attributes.name) {view.model.attributes.name = '<span class="mod">' + view.model.attributes.name + '</span>'}}
  +
if(wgUserName == view.model.attributes.name) {view.model.attributes.name = '<span class="me">' + view.model.attributes.name + '</span>'}
  +
$('#Chat ul').append(view.render().el);
  +
  +
// Scroll chat to bottom
  +
if (chat.attributes.name == wgUserName || isAtBottom) {
  +
NodeChatHelper.scrollToBottom();
  +
}
  +
}
  +
  +
//Add a custom inline alert that tells you when your status changes
  +
NodeChatController.view.setAway = function (){
  +
var msg = '';
  +
//var msg = $(e.target).parent().find('.user').html();
  +
//if(!msg){msg = '';}
  +
NodeChatHelper.log("Attempting to go away with message: " + msg);
  +
var setStatusCommand = new models.SetStatusCommand({
  +
statusState: STATUS_STATE_AWAY,
  +
statusMessage: msg
  +
});
  +
InlineAlert('You are now away.');
  +
this.socket.send(setStatusCommand.xport());
  +
}
  +
NodeChatController.view.setBack = function (){
  +
NodeChatHelper.log("Attempting to come BACK from away.");
  +
var setStatusCommand = new models.SetStatusCommand({
  +
statusState: STATUS_STATE_PRESENT,
  +
statusMessage: ''
  +
});
  +
InlineAlert('You are no longer away.');
  +
this.socket.send(setStatusCommand.xport());
  +
}
  +
  +
//Add timestamp to messages - doesn't work yet, have to find this object ref
  +
$('#Write').unbind('submit'); //unbind the default, because it uses 6 nested function calls that make it impossible to know what the object reference is
  +
NodeChatController.view.sendMessage = function(){
  +
var inputField = $('input[name=message]');
  +
var nameField = $('input[name=user_name]');
  +
if (inputField.val()) {
  +
//Preparse message - will return true or false, based on whether the message should still be sent or not
  +
var run = Preparse(inputField.val());
  +
if(run == true) {
  +
var date = new Date();
  +
if(date.getUTCHours() >= 10) {var hours = date.getUTCHours();}
  +
else {var hours = '0' + date.getUTCHours();}
  +
if(date.getUTCMinutes() >= 10) {var minutes = date.getUTCMinutes();}
  +
else {var minutes = '0' + date.getUTCMinutes();}
  +
chatEntry = new models.ChatEntry({name: nameField.val(), text: '[' + hours + ':' + minutes + '] ' + inputField.val()});
  +
this.socket.send(chatEntry.xport());
  +
}
  +
inputField.val('');
  +
}
  +
NodeChatHelper.focusTextInput();
  +
}
  +
$('#Write').bind('submit', function(){NodeChatController.view.sendMessage.call(NodeChatController.view);}); //rebind our new function, even though it'll throw an XHR POST error every time we send
  +
}
  +
});
  +
  +
function InlineAlert(text) {
  +
$('.Chat ul').append('<li class="inline-alert">' + text + '</li>');
  +
NodeChatHelper.scrollToBottom();
  +
}
  +
  +
function Preparse(input) {
  +
var text = input.split(' ');
  +
switch(text[0]) {
  +
case '/away':
  +
case '/afk':
  +
ToggleAway();
  +
return false;
  +
break;
  +
case '/clear':
  +
$('.Chat ul').html('');
  +
InlineAlert('Window cleared.');
  +
return false;
  +
break;
  +
case '/kickban':
  +
//NodeChatController.view.kickBan(build(text, 1));
  +
return false;
  +
break;
  +
case '/self':
  +
if(text[1] != undefined) {
  +
str = build(text, 1);
  +
newstr = '';
  +
for(var i = 0; i < str.length; i++) {
  +
switch(str.charAt(i)) {
  +
case '[':
  +
if(str.charAt(i + 1) == '[') {
  +
var k = str.indexOf(']', i);
  +
if(str.charAt(k + 1) == ']') {
  +
var substr = str.substring(i + 2, k);
  +
if(substr.indexOf('|') != -1) {
  +
var link = substr.substring(0, substr.indexOf('|'));
  +
var linktext = substr.substring(substr.indexOf('|') + 1, substr.length);
  +
}
  +
else {
  +
var link = substr;
  +
var linktext = link;
  +
}
  +
var linkspl = link.split(' ');
  +
link = '';
  +
for(var j in linkspl) {link += linkspl[j] + '_';}
  +
link = link.substring(0, link.length - 1);
  +
newstr += '<a href="/wiki/' + link + '">' + linktext + '</a>';
  +
i = k + 1;
  +
}
  +
}
  +
else if(str.substring(i + 1, i + 8) == 'http://') {
  +
var k = str.indexOf(']', i);
  +
var splstr = str.substring(i + 1, k).split(' ');
  +
if(splstr == str.substring(i + 1, k)) {
  +
var text = '[1]';
  +
}
  +
else {
  +
var text = build(splstr, 1);
  +
}
  +
newstr += '<a href="' + splstr[0] + '">' + text + '</a>';
  +
i = k;
  +
}
  +
break;
  +
default:
  +
newstr += str.charAt(i);
  +
break;
  +
}
  +
}
  +
$('.Chat ul').append('<li><img class="avatar" src="' + $('#ChatHeader .User').attr('src') + '"><span class="username"><span class="me">' + wgUserName + '</span></span><span class="message" style="color:#999;"> (self) ' + newstr + '</span></li>');
  +
}
  +
else {
  +
$('.Chat ul').append('<li><img class="avatar" src="' + $('#ChatHeader .User').attr('src') + '"><span class="username"><span class="me">' + wgUserName + '</span></span><span class="message" style="color:#999;"> (self)</span></li>');
  +
}
  +
NodeChatHelper.scrollToBottom();
  +
return false;
  +
break;
  +
case '/me':
  +
if(text[1] != undefined) {$('input[name="message"]').val('* ' + wgUserName + ' ' + build(text, 1));}
  +
else {$('input[name="message"]').val('* ' + wgUserName);}
  +
return true;
  +
break;
  +
default:
  +
return true;
  +
break;
  +
}
  +
}
  +
  +
//Parser helping function - takes an array of text created with .split(' '),
  +
//and an index number for where to start, then rebuilds the string.
  +
//Can also accept an index value for where to stop.
  +
function build(text, index, stop) {
  +
var newtext = '';
  +
if(stop == undefined || stop == 0) {stop = text.length}
  +
else if(stop < 0) {stop += text.length}
  +
if(index == stop || index == text.length - 1) {return text[index];}
  +
for(var i = index; i < stop; i++) {
  +
newtext += text[i] + ' ';
  +
}
  +
return newtext;
  +
}
  +
 
function ToggleAway() {
  +
if($('#ChatHeader .User').hasClass('away') == true) {
  +
NodeChatController.view.setBack();
  +
}
  +
else {
  +
NodeChatController.view.setAway();
  +
}
  +
}

Revision as of 23:35, 24 April 2011

$(function () {
	if(wgPageName == 'Special:Chat') {
		$('body').prepend('<style type="text/css">.chat-mod, .mod, .mod::after {color:#da0;} .chat-mod.away {color:#eda;} .ChatHeader .username, .me, .me::after {color:#00c;} .UserStatsMenu {color:#000;}</style>');
 
		//Add a 'go afk' button
		$('#Write').append('<a class="wikia-button" href="javascript:ToggleAway()" style="margin-left:3px;">AFK</a>');
		//Unbind all of the listeners that set your status to back
		$(window).unbind('mousemove').unbind('focus').unbind('keypress');
		//Set status to back when you press enter
		$('input[name=message]').bind('keypress', function () {if(event.keyCode == 13) {NodeChatHelper.resetActivityTimer();}});
		//Add a 'clear chat' button
		$('#Write').append('<a class="wikia-button" href="javascript:$(\'.Chat ul\').html(\'\')" style="margin-left:6px;">Clear</a>');
 
		window.mods = [];
		window.list = document.getElementById('Users').getElementsByTagName('ul')[0].getElementsByTagName('li');
		for(i = 0; i < list.length; i++) {
			if(list[i].className.split(' ')[1] == 'chat-mod') {
				mods.push(list[i].id.substring(5, list[i].id.length));
			}
		}
 
		//Add/remove mods when people join/leave
		NodeChatController.view.model.users._callbacks.add[0] = function(user) {
			if(user.attributes.isModerator == true) {
				var add = true;
				for(i in mods) {if(mods[i] == user.attributes.name) {add = false;}}
				if(add == true) {mods.push(user.attributes.name)}
			}
			var view = new UserView({model: user});
			$('#Users > ul').append(view.render().el);
		}
		NodeChatController.view.model.users._callbacks.remove[0] = function(user) {
			NodeChatHelper.log("Trying to remove " + user.get('name') + " from the list.");
			NodeChatHelper.log("Matches found: " + $('#' + NodeChatHelper.liIdByUsername( user.get('name') )).length);
			for(i in mods) {if(mods[i] == user.attributes.name) {mods[i] = undefined;}}
			$('#' + NodeChatHelper.liIdByUsername( user.get('name') )).remove();
		}
		//Color mod posts when they're added
		NodeChatController.view.model.chats._callbacks.add[0] = function (chat) {
			// Determine if chat view is presently scrolled to the bottom
			var isAtBottom = false;             
			if (($("#Chat").scrollTop() + 1) >= ($("#Chat ul").outerHeight() - $("#Chat").height())) {
				isAtBottom = true;
			}
 
			// Add message to chat
			var view = new ChatView({model: chat});
			for(i in mods) {if(mods[i] == view.model.attributes.name) {view.model.attributes.name = '<span class="mod">' + view.model.attributes.name + '</span>'}}
			if(wgUserName == view.model.attributes.name) {view.model.attributes.name = '<span class="me">' + view.model.attributes.name + '</span>'}
			$('#Chat ul').append(view.render().el);
 
			// Scroll chat to bottom
			if (chat.attributes.name == wgUserName || isAtBottom) {
				NodeChatHelper.scrollToBottom();
			}
		}
 
		//Add a custom inline alert that tells you when your status changes
		NodeChatController.view.setAway = function (){
			var msg = '';
			//var msg = $(e.target).parent().find('.user').html();
			//if(!msg){msg = '';}
			NodeChatHelper.log("Attempting to go away with message: " + msg);
			var setStatusCommand = new models.SetStatusCommand({
				statusState: STATUS_STATE_AWAY,
				statusMessage: msg
			});
			InlineAlert('You are now away.');
			this.socket.send(setStatusCommand.xport());
		}
		NodeChatController.view.setBack = function (){
			NodeChatHelper.log("Attempting to come BACK from away.");
			var setStatusCommand = new models.SetStatusCommand({
				statusState: STATUS_STATE_PRESENT,
				statusMessage: ''
			});
			InlineAlert('You are no longer away.');
			this.socket.send(setStatusCommand.xport());
		}
 
		//Add timestamp to messages - doesn't work yet, have to find this object ref
		$('#Write').unbind('submit'); //unbind the default, because it uses 6 nested function calls that make it impossible to know what the object reference is
		NodeChatController.view.sendMessage = function(){
			var inputField = $('input[name=message]');
			var nameField = $('input[name=user_name]');
			if (inputField.val()) {
				//Preparse message - will return true or false, based on whether the message should still be sent or not
				var run = Preparse(inputField.val());
				if(run == true) {
					var date = new Date();
					if(date.getUTCHours() >= 10) {var hours = date.getUTCHours();}
					else {var hours = '0' + date.getUTCHours();}
				 	if(date.getUTCMinutes() >= 10) {var minutes = date.getUTCMinutes();}
					else {var minutes = '0' + date.getUTCMinutes();}
					chatEntry = new models.ChatEntry({name: nameField.val(), text: '[' + hours + ':' + minutes + '] ' + inputField.val()});
					this.socket.send(chatEntry.xport());
				}
				inputField.val('');
			}
			NodeChatHelper.focusTextInput();
		}
		$('#Write').bind('submit', function(){NodeChatController.view.sendMessage.call(NodeChatController.view);}); //rebind our new function, even though it'll throw an XHR POST error every time we send
	}
});
 
function InlineAlert(text) {
	$('.Chat ul').append('<li class="inline-alert">' + text + '</li>');
	NodeChatHelper.scrollToBottom();
}
 
function Preparse(input) {
	var text = input.split(' ');
	switch(text[0]) {
		case '/away':
		case '/afk':
			ToggleAway();
			return false;
			break;
		case '/clear':
			$('.Chat ul').html('');
			InlineAlert('Window cleared.');
			return false;
			break;
		case '/kickban':
			//NodeChatController.view.kickBan(build(text, 1));
			return false;
			break;
		case '/self':
			if(text[1] != undefined) {
				str = build(text, 1);
				newstr = '';
				for(var i = 0; i < str.length; i++) {
					switch(str.charAt(i)) {
						case '[':
							if(str.charAt(i + 1) == '[') {
								var k = str.indexOf(']', i);
								if(str.charAt(k + 1) == ']') {
									var substr = str.substring(i + 2, k);
									if(substr.indexOf('|') != -1) {
										var link = substr.substring(0, substr.indexOf('|'));
										var linktext = substr.substring(substr.indexOf('|') + 1, substr.length);
									}
									else {
										var link = substr;
										var linktext = link;
									}
									var linkspl = link.split(' ');
									link = '';
									for(var j in linkspl) {link += linkspl[j] + '_';}
									link = link.substring(0, link.length - 1);
									newstr += '<a href="/wiki/' + link + '">' + linktext + '</a>';
									i = k + 1;
								}
							}
							else if(str.substring(i + 1, i + 8) == 'http://') {
								var k = str.indexOf(']', i);
								var splstr = str.substring(i + 1, k).split(' ');
								if(splstr == str.substring(i + 1, k)) {
									var text = '[1]';
								}
								else {
									var text = build(splstr, 1);
								}
								newstr += '<a href="' + splstr[0] + '">' + text + '</a>';
								i = k;
							}
							break;
						default:
							newstr += str.charAt(i);
							break;
					}
				}
				$('.Chat ul').append('<li><img class="avatar" src="' + $('#ChatHeader .User').attr('src') + '"><span class="username"><span class="me">' + wgUserName + '</span></span><span class="message" style="color:#999;"> (self) ' + newstr + '</span></li>');
			}
			else {
				$('.Chat ul').append('<li><img class="avatar" src="' + $('#ChatHeader .User').attr('src') + '"><span class="username"><span class="me">' + wgUserName + '</span></span><span class="message" style="color:#999;"> (self)</span></li>');
			}
			NodeChatHelper.scrollToBottom();
			return false;
			break;
		case '/me':
			if(text[1] != undefined) {$('input[name="message"]').val('* ' + wgUserName + ' ' + build(text, 1));}
			else {$('input[name="message"]').val('* ' + wgUserName);}
			return true;
			break;
		default:
			return true;
			break;
	}
}
 
//Parser helping function - takes an array of text created with .split(' '),
//and an index number for where to start, then rebuilds the string.
//Can also accept an index value for where to stop.
function build(text, index, stop) {
	var newtext = '';
	if(stop == undefined || stop == 0) {stop = text.length}
	else if(stop < 0) {stop += text.length}
	if(index == stop || index == text.length - 1) {return text[index];}
	for(var i = index; i < stop; i++) {
		newtext += text[i] + ' ';
	}
	return newtext;
}
 
function ToggleAway() {
	if($('#ChatHeader .User').hasClass('away') == true) {
		NodeChatController.view.setBack();
	}
	else {
		NodeChatController.view.setAway();
	}
}