2012-02-06 22:00:24 +00:00
|
|
|
/*
|
2012-02-02 22:47:23 +00:00
|
|
|
* Copyright (c) 2012, Andy Janata
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
|
|
* provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
|
|
* and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
|
|
* conditions and the following disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
|
|
|
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-12-23 02:48:20 +00:00
|
|
|
/**
|
2012-03-15 17:14:32 +00:00
|
|
|
* Main application for Pretend You're Xyzzy. This only has to handle the initial nickname
|
2012-02-06 22:00:24 +00:00
|
|
|
* registration, the chat box, the logout button, and resizing the window. It should probably be
|
|
|
|
* split up into multiple files.
|
2011-12-23 02:48:20 +00:00
|
|
|
*
|
2012-02-06 22:00:24 +00:00
|
|
|
* @author Andy Janata (ajanata@socialgamer.net)
|
2011-12-23 02:48:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
2011-12-25 03:37:45 +00:00
|
|
|
// see if we already exist on the server so we can resume
|
2012-01-13 04:05:39 +00:00
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.FIRST_LOAD).run();
|
2011-12-25 03:37:45 +00:00
|
|
|
|
2012-10-12 05:18:40 +01:00
|
|
|
if ($.cookie("nickname")) {
|
|
|
|
$("#nickname").val($.cookie("nickname"));
|
|
|
|
}
|
2011-12-23 02:48:20 +00:00
|
|
|
$("#nicknameconfirm").click(nicknameconfirm_click);
|
|
|
|
$("#nickbox").keyup(nickbox_keyup);
|
|
|
|
$("#nickbox").focus();
|
2011-12-25 03:37:45 +00:00
|
|
|
|
|
|
|
$("#chat").keyup(chat_keyup);
|
|
|
|
$("#chat_submit").click(chatsubmit_click);
|
2012-01-06 23:53:04 +00:00
|
|
|
|
|
|
|
// TODO: have some sort of mechanism to alert the server that we have unloaded the page, but
|
|
|
|
// have not expressed an interest in being cleared out yet.
|
|
|
|
// $(window).bind("beforeunload", window_beforeunload);
|
|
|
|
$("#logout").click(logout_click);
|
2012-10-16 04:23:37 +01:00
|
|
|
|
2012-10-12 05:18:40 +01:00
|
|
|
load_preferences();
|
2012-01-25 08:03:41 +00:00
|
|
|
|
2012-10-16 04:23:37 +01:00
|
|
|
$("#tabs").tabs();
|
|
|
|
$("#button-global").click();
|
|
|
|
|
2012-01-25 08:03:41 +00:00
|
|
|
if ($.browser.mozilla) {
|
|
|
|
// Firefox sucks.
|
|
|
|
$("body").css("font-size", "12px");
|
|
|
|
}
|
2012-01-30 08:35:27 +00:00
|
|
|
|
|
|
|
$(window).resize(app_resize);
|
|
|
|
app_resize();
|
2011-12-23 02:48:20 +00:00
|
|
|
});
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handle a key up event in the nick box. If the key was enter, try to register with the server.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event}
|
|
|
|
* e
|
|
|
|
*/
|
2011-12-23 02:48:20 +00:00
|
|
|
function nickbox_keyup(e) {
|
|
|
|
if (e.which == 13) {
|
|
|
|
$("#nicknameconfirm").click();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handle a click event on the set nickname box. Try to register with the server.
|
|
|
|
*/
|
|
|
|
function nicknameconfirm_click() {
|
2011-12-25 03:37:45 +00:00
|
|
|
var nickname = $.trim($("#nickname").val());
|
2012-10-12 05:18:40 +01:00
|
|
|
$.cookie("nickname", nickname, {
|
|
|
|
expires : 365
|
|
|
|
});
|
2012-01-13 04:05:39 +00:00
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.REGISTER).withNickname(nickname).run();
|
2011-12-25 03:37:45 +00:00
|
|
|
}
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handle a key up event in the chat box. If the key was enter, send the message to the server.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event}
|
|
|
|
* e
|
|
|
|
*/
|
2011-12-25 03:37:45 +00:00
|
|
|
function chat_keyup(e) {
|
|
|
|
if (e.which == 13) {
|
|
|
|
$("#chat_submit").click();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handle a click even on the chat button. Send the message to the server.
|
|
|
|
*/
|
|
|
|
function chatsubmit_click() {
|
2011-12-25 03:37:45 +00:00
|
|
|
var text = $.trim($("#chat").val());
|
2012-03-13 02:49:36 +00:00
|
|
|
if (text == "") {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-21 06:41:06 +01:00
|
|
|
var cmd = '';
|
|
|
|
if ('/' == text.substring(0, 1)) {
|
|
|
|
cmd = text.substring(1, text.indexOf(' ') >= 0 ? text.indexOf(' ') : undefined);
|
|
|
|
if (text.indexOf(' ') >= 0) {
|
|
|
|
text = text.substring(text.indexOf(' ') + 1);
|
|
|
|
} else {
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (cmd) {
|
2012-10-12 05:18:40 +01:00
|
|
|
// TODO support an /ignore command
|
2012-08-21 06:41:06 +01:00
|
|
|
case '':
|
|
|
|
// TODO when I get multiple channels working, this needs to know active and pass it
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.CHAT).withMessage(text).run();
|
|
|
|
cah.log.status("<" + cah.nickname + "> " + text);
|
|
|
|
break;
|
|
|
|
case 'kick':
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.KICK).withNickname(text.split(' ')[0]).run();
|
|
|
|
break;
|
|
|
|
case 'ban':
|
|
|
|
// this could also be an IP address
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.BAN).withNickname(text.split(' ')[0]).run();
|
|
|
|
break;
|
|
|
|
case 'names':
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.NAMES).run();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2011-12-25 03:37:45 +00:00
|
|
|
$("#chat").val("");
|
|
|
|
$("#chat").focus();
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
2012-01-06 23:53:04 +00:00
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handle a click event on the log out button. Tell the server to log us out.
|
|
|
|
*/
|
|
|
|
function logout_click() {
|
2012-03-15 20:50:24 +00:00
|
|
|
if (confirm("Are you sure you wish to log out?")) {
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.LOG_OUT).run();
|
|
|
|
}
|
2012-01-06 23:53:04 +00:00
|
|
|
}
|
2012-01-30 08:35:27 +00:00
|
|
|
|
2012-10-12 05:18:40 +01:00
|
|
|
/**
|
|
|
|
* Handle a click event on the preferences button. Shows the preferences modal dialog.
|
|
|
|
*/
|
|
|
|
function preferences_click() {
|
|
|
|
$("#preferences_dialog").dialog({
|
|
|
|
modal : true,
|
|
|
|
buttons : {
|
|
|
|
Ok : function() {
|
|
|
|
save_preferences();
|
|
|
|
$(this).dialog("close");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function load_preferences() {
|
|
|
|
if ($.cookie("hide_connect_quit")) {
|
|
|
|
$("#hide_connect_quit").attr('checked', 'checked');
|
|
|
|
} else {
|
|
|
|
$("#hide_connect_quit").removeAttr('checked');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($.cookie("ignore_list")) {
|
|
|
|
$("#ignore_list").val($.cookie("ignore_list"));
|
|
|
|
} else {
|
|
|
|
$("#ignore_list").val("");
|
|
|
|
}
|
|
|
|
|
|
|
|
apply_preferences();
|
|
|
|
}
|
|
|
|
|
|
|
|
function save_preferences() {
|
|
|
|
if ($("#hide_connect_quit").attr("checked")) {
|
|
|
|
$.cookie("hide_connect_quit", true, {
|
|
|
|
expires : 365
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$.removeCookie("hide_connect_quit");
|
|
|
|
}
|
|
|
|
|
|
|
|
$.cookie("ignore_list", $("#ignore_list").val(), {
|
|
|
|
expires : 365
|
|
|
|
});
|
|
|
|
|
|
|
|
apply_preferences();
|
|
|
|
}
|
|
|
|
|
|
|
|
function apply_preferences() {
|
|
|
|
cah.hideConnectQuit = !!$("#hide_connect_quit").attr("checked");
|
|
|
|
|
|
|
|
cah.ignoreList = {};
|
|
|
|
$($('#ignore_list').val().split('\n')).each(function() {
|
|
|
|
cah.ignoreList[this] = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Handle a window resize event. Resize the chat and info areas to fit vertically and horizontally.
|
|
|
|
* This was tested extensively in Chrome. It may not be pixel-perfect in other browsers.
|
|
|
|
*/
|
2012-01-30 08:35:27 +00:00
|
|
|
function app_resize() {
|
2012-10-16 04:23:37 +01:00
|
|
|
var chatWidth = $("#canvas").width() - 257;
|
|
|
|
$("#tabs").width(chatWidth + 'px');
|
|
|
|
$("#log").width((chatWidth + 2) + 'px');
|
|
|
|
$("#chat").width((chatWidth - 42) + 'px');
|
|
|
|
var bottomHeight = $(window).height() - $("#main").height() - $("#menubar").height() - 29;
|
2012-01-30 08:35:27 +00:00
|
|
|
$("#bottom").height(bottomHeight);
|
|
|
|
$("#info_area").height(bottomHeight);
|
2012-10-16 04:23:37 +01:00
|
|
|
$("#tabs").height(bottomHeight);
|
|
|
|
$("#log").height(bottomHeight - $("#chat").height() - 40);
|
2012-01-30 08:35:27 +00:00
|
|
|
// this is ugly and terrible.
|
|
|
|
if ($(window).height() < 650) {
|
|
|
|
$("body").css("overflow-y", "auto");
|
|
|
|
} else {
|
|
|
|
$("body").css("overflow-y", "hidden");
|
|
|
|
}
|
|
|
|
}
|