1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-23 21:50:09 +00:00
Tims-Chat/file/js/TimWolla.WCF.Chat.js

198 lines
5.4 KiB
JavaScript
Raw Normal View History

2011-11-26 14:17:17 +00:00
/**
* TimWolla.WCF.Chat
*
* @author Tim Düsterhus
* @copyright 2010-2011 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package timwolla.wcf.chat
*/
if (typeof TimWolla == 'undefined') var TimWolla = {};
if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {};
2011-11-26 14:17:17 +00:00
2011-11-26 14:17:17 +00:00
(function ($, document) {
TimWolla.WCF.Chat = {
titleTemplate: null,
messageTemplate: null,
init: function(roomID, messageID) {
2011-12-02 22:16:55 +00:00
this.bindEvents();
2011-12-13 20:44:56 +00:00
// calculate the width for the chatRoomContent, 'cause the styles width isn't fixed.
2011-12-11 22:29:43 +00:00
$('#chatRoomContent').width($('#chatBox').width() - 400);
2011-12-13 20:44:56 +00:00
// add toRight1.svg to WCF.Icon-storage
2011-12-13 20:40:39 +00:00
WCF.Icon.add('wcf.icon.toRight1', $('#chatForm .inputImage').attr('src'));
$('#chatInput').focus();
2011-12-02 22:16:55 +00:00
},
2011-12-13 20:48:13 +00:00
/**
* Binds all the events needed for Tims Chat.
*/
bindEvents: function () {
2011-12-11 12:01:35 +00:00
$('.smiley').click($.proxy(function (event) {
this.insertText($(event.target).attr('alt'));
}, this));
2011-12-11 22:29:43 +00:00
2011-12-13 20:44:56 +00:00
// recalculate the width of chatRoomContent on resize
2011-12-11 22:29:43 +00:00
var chatRoomContent = $('#chatRoomContent');
var chatBox = $('#chatBox');
$(window).resize(function() {
chatRoomContent.width(chatBox.width() - 400);
});
2011-12-02 22:16:55 +00:00
// $(window).bind('beforeunload', function() {
// return false;
// });
2011-12-03 14:01:54 +00:00
$('.chatRoom').click($.proxy(function (event) {
if (typeof window.history.replaceState != 'undefined') {
event.preventDefault();
this.changeRoom($(event.target));
2011-12-03 14:01:54 +00:00
}
}, this));
2011-12-04 10:52:56 +00:00
$('.chatUser .chatUserLink').click($.proxy(function (event) {
event.preventDefault();
this.toggleUserMenu($(event.target));
}, this));
2011-12-13 20:40:39 +00:00
$('#chatForm').submit($.proxy(function (event) {
event.preventDefault();
2011-12-13 20:52:32 +00:00
this.submit($(event.target));
}, this));
2011-12-15 17:55:44 +00:00
$('button').click(function (event) {
window.location.href = $(event.target).attr('value');
});
2011-12-03 14:01:54 +00:00
},
2011-12-13 20:48:13 +00:00
/**
* Changes the chat-room.
*
* @param object target
*/
changeRoom: function (target) {
window.history.replaceState({}, '', target.attr('href'));
2011-12-03 17:07:57 +00:00
// actually change the room
2011-12-03 16:37:20 +00:00
$.ajax(target.attr('href'), {
dataType: 'json',
data: { ajax: 1 },
type: 'POST',
2011-12-03 17:07:57 +00:00
success: $.proxy(function (data, textStatus, jqXHR) {
this.loading = false;
2011-12-11 22:29:43 +00:00
target.parent().removeClass('ajaxLoad');
2011-12-04 18:06:34 +00:00
// mark as active;
$('.activeMenuItem .chatRoom').parent().removeClass('activeMenuItem');
target.parent().addClass('activeMenuItem');
2011-12-03 17:07:57 +00:00
// set new topic
2011-12-03 20:23:13 +00:00
if (data.topic == '') {
2011-12-04 18:06:34 +00:00
if (data.topic == '' && $('#topic').text().trim() == '') return;
$('#topic').wcfBlindOut('vertical', function () {
2011-12-04 18:06:34 +00:00
$(this).text('');
2011-12-03 20:23:13 +00:00
});
2011-12-04 18:06:34 +00:00
}
else {
if ($('#topic').text().trim() != '') $('#topic').text(data.topic);
else {
2011-12-03 20:23:13 +00:00
$('#topic').text(data.topic);
$('#topic').wcfBlindIn();
}
}
2011-12-03 17:07:57 +00:00
// set page-title
$('title').text(this.titleTemplate.fetch(data));
}, this),
2011-12-11 11:57:20 +00:00
error: function() {
// reload page to change the room the old fashion-way
// inclusive the error-message :)
window.location.reload(true);
},
beforeSend: $.proxy(function () {
2011-12-04 18:06:34 +00:00
if (this.loading || target.parent().hasClass('activeMenuItem')) return false;
2011-12-04 00:27:21 +00:00
this.loading = true;
2011-12-04 18:06:34 +00:00
2011-12-11 22:29:43 +00:00
target.parent().addClass('ajaxLoad');
2011-12-03 17:07:57 +00:00
}, this)
2011-12-03 16:37:20 +00:00
});
2011-12-13 20:40:39 +00:00
},
getMessages: function (id) {
2011-12-04 10:52:56 +00:00
},
2011-12-13 20:48:13 +00:00
/**
* Appends the messages.
*
* @param array<object> messages
*/
handleMessages: function (messages) {
for (message in messages) {
message = messages[message];
output = this.messageTemplate.fetch(message);
li = $('<li></li>');
2011-12-11 22:29:43 +00:00
li.addClass('chatMessage chatMessage'+message.type);
if (message.sender == WCF.User.userID) li.addClass('ownMessage');
li.append(output);
2011-12-12 14:52:36 +00:00
$('.chatMessageContainer ul').append(li);
}
2011-12-12 14:52:36 +00:00
$('.chatMessageContainer').animate({scrollTop: $('.chatMessageContainer ul').height()}, 10000);
2011-12-11 11:57:20 +00:00
},
2011-12-13 20:48:13 +00:00
/**
* Inserts text into the chat-input.
*
* @param string text
* @param object options
*/
2011-12-12 15:16:24 +00:00
insertText: function (text, options) {
options = $.extend({
append: true,
submit: false
}, options || {});
if (options.append) {
text = $('#chatInput').val() + text;
}
$('#chatInput').val(text);
if (options.submit) $('#chatForm').submit();
2011-12-13 20:44:31 +00:00
else $('#chatInput').focus();
2011-12-11 12:01:35 +00:00
},
2011-12-13 20:52:32 +00:00
submit: function (target) {
// break if input contains only whitespace
if ($('#chatInput').val().trim().length === 0) return false;
submitButton = target.find('input[type=image]');
$.ajax($('#chatForm').attr('action'), {
data: {
text: $('#chatInput').val()
},
type: 'POST',
beforeSend: $.proxy(function (jqXHR) {
submitButton.attr('src', WCF.Icon.get('wcf.icon.loading'));
}),
success: $.proxy(function (data, textStatus, jqXHR) {
this.getMessages();
$('#chatInput').val('').focus();
}, this),
complete: function() {
submitButton.attr('src', WCF.Icon.get('wcf.icon.toRight1'));
}
});
2011-12-13 21:34:05 +00:00
},
2011-12-11 11:57:20 +00:00
toggleUserMenu: function (target) {
2011-12-11 22:29:43 +00:00
liUserID = '#' + target.parent().parent().attr('id');
2011-12-11 11:57:20 +00:00
if ($(liUserID).hasClass('activeMenuItem')) {
2011-12-11 12:01:35 +00:00
$(liUserID + ' .chatUserMenu').wcfBlindOut('vertical', function () {
2011-12-11 11:57:20 +00:00
$(liUserID).removeClass('activeMenuItem');
});
}
else {
$(liUserID).addClass('activeMenuItem');
$(liUserID + ' .chatUserMenu').wcfBlindIn();
}
}
};
2011-11-26 14:17:17 +00:00
})(jQuery, document);