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
|
|
|
|
*/
|
2011-11-26 16:19:34 +00:00
|
|
|
if (typeof TimWolla == 'undefined') var TimWolla = {};
|
|
|
|
if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {};
|
2011-11-26 14:17:17 +00:00
|
|
|
|
2011-11-26 16:19:34 +00:00
|
|
|
|
2011-11-26 14:17:17 +00:00
|
|
|
(function ($, document) {
|
2011-11-27 12:13:59 +00:00
|
|
|
TimWolla.WCF.Chat = {
|
2011-12-05 13:45:50 +00:00
|
|
|
titleTemplate: null,
|
|
|
|
messageTemplate: null,
|
2011-11-26 16:19:34 +00:00
|
|
|
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.
|
|
|
|
*/
|
2011-12-05 13:45:50 +00:00
|
|
|
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
|
|
|
|
2011-12-03 23:44:16 +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();
|
2011-12-03 14:06:36 +00:00
|
|
|
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
|
|
|
|
2011-12-11 12:27:39 +00:00
|
|
|
$('#chatForm').submit($.proxy(function (event) {
|
|
|
|
event.preventDefault();
|
2011-12-13 20:52:32 +00:00
|
|
|
this.submit($(event.target));
|
2011-12-11 12:27:39 +00:00
|
|
|
}, this));
|
2011-12-03 14:01:54 +00:00
|
|
|
},
|
2011-12-13 20:48:13 +00:00
|
|
|
/**
|
|
|
|
* Changes the chat-room.
|
|
|
|
*
|
|
|
|
* @param object target
|
|
|
|
*/
|
2011-12-05 13:45:50 +00:00
|
|
|
changeRoom: function (target) {
|
2011-12-03 14:06:36 +00:00
|
|
|
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) {
|
2011-12-03 23:44:16 +00:00
|
|
|
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 23:44:16 +00:00
|
|
|
|
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;
|
|
|
|
|
2011-12-05 13:45:50 +00:00
|
|
|
$('#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));
|
2011-12-03 23:44:16 +00:00
|
|
|
}, 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);
|
|
|
|
},
|
2011-12-03 23:44:16 +00:00
|
|
|
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
|
|
|
|
2011-12-03 23:44:16 +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
|
|
|
|
*/
|
2011-12-05 13:45:50 +00:00
|
|
|
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);
|
2011-12-05 13:45:50 +00:00
|
|
|
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-05 13:45:50 +00:00
|
|
|
}
|
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'), {
|
|
|
|
dataType: 'json',
|
|
|
|
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-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 16:19:34 +00:00
|
|
|
}
|
|
|
|
};
|
2011-11-26 14:17:17 +00:00
|
|
|
})(jQuery, document);
|