From f76fcca04d6207dd189173e63ac0ef3e718a73ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 5 Dec 2011 14:45:50 +0100 Subject: [PATCH] Optimizing use of chatMessage-Template. We can now insert messages via JS --- file/js/TimWolla.WCF.Chat.js | 28 +++++++++++++----- .../data/chat/message/ChatMessage.class.php | 29 +++++++++++++++++-- template/chat.tpl | 17 ++++------- template/chatMessage.tpl | 2 +- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/file/js/TimWolla.WCF.Chat.js b/file/js/TimWolla.WCF.Chat.js index 858b6e3..52c191d 100644 --- a/file/js/TimWolla.WCF.Chat.js +++ b/file/js/TimWolla.WCF.Chat.js @@ -12,12 +12,13 @@ if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {}; (function ($, document) { TimWolla.WCF.Chat = { - titleTemplate: '', + titleTemplate: null, + messageTemplate: null, init: function(roomID, messageID) { this.bindEvents(); }, - bindEvents: function() { - $('.smiley').click(function(event) { + bindEvents: function () { + $('.smiley').click(function (event) { alert($(event.target).attr('alt')); }); @@ -37,7 +38,7 @@ if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {}; this.toggleUserMenu($(event.target)); }, this)); }, - changeRoom: function(target) { + changeRoom: function (target) { window.history.replaceState({}, '', target.attr('href')); // actually change the room @@ -64,7 +65,7 @@ if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {}; if (data.topic == '') { if (data.topic == '' && $('#topic').text().trim() == '') return; - $('#topic').wcfBlindOut('vertical', function() { + $('#topic').wcfBlindOut('vertical', function () { $(this).text(''); }); } @@ -92,11 +93,11 @@ if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {}; }) .parent() .append('') - .css({'marginTop' : function(index) {return (target.parent().height() / 2) - ($(this).height() / 2)}}); + .css({'marginTop' : function (index) {return (target.parent().height() / 2) - ($(this).height() / 2)}}); }, this) }); }, - toggleUserMenu: function(target) { + toggleUserMenu: function (target) { liUserID = '#' + target.parent().attr('id'); if ($(liUserID).hasClass('activeMenuItem')) { $(liUserID + ' .chatUserMenu').wcfBlindOut('vertical', function() { @@ -107,6 +108,19 @@ if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {}; $(liUserID).addClass('activeMenuItem'); $(liUserID + ' .chatUserMenu').wcfBlindIn(); } + }, + handleMessages: function (messages) { + for (message in messages) { + message = messages[message]; + output = this.messageTemplate.fetch(message); + + li = $('
  • '); + li.addClass('chatMessage'+message.type); + if (message.sender == WCF.User.userID) li.addClass('ownMessage'); + li.append(output); + + $('.chatMessage ul').append(li); + } } }; })(jQuery, document); \ No newline at end of file diff --git a/file/lib/data/chat/message/ChatMessage.class.php b/file/lib/data/chat/message/ChatMessage.class.php index 2e245bd..4731cac 100644 --- a/file/lib/data/chat/message/ChatMessage.class.php +++ b/file/lib/data/chat/message/ChatMessage.class.php @@ -41,7 +41,7 @@ class ChatMessage extends \wcf\data\DatabaseObject { * @return string */ public function __toString() { - return $this->getFormattedMessage; + return $this->getFormattedMessage(); } /** @@ -66,9 +66,9 @@ public function getFormattedMessage() { * @return string */ public function getFormattedUsername() { - if ($this->type == self::TYPE_INFORMATION) return ''.WCF::getLanguage()->get('wcf.chat.information').''; + if ($this->type == self::TYPE_INFORMATION) return ''.$this->getUsername().''; - $string = str_split($this->username); + $string = str_split($this->getUsername()); $r = (int) (($this->color1 >> 16 & 255) - ($this->color2 >> 16 & 255)) / (count($string) - 1); $g = (int) (($this->color1 >> 8 & 255) - ($this->color2 >> 8 & 255)) / (count($string) - 1); $b = (int) (($this->color1 & 255) - ($this->color2 & 255)) / (count($string) - 1); @@ -79,4 +79,27 @@ public function getFormattedUsername() { return ''.$result.''; } + + /** + * Returns the unformatted username + * + * @return string + */ + public function getUsername() { + if ($this->type == self::TYPE_INFORMATION) return WCF::getLanguage()->get('wcf.chat.information'); + return $this->username; + } + + public function jsonify() { + return \wcf\util\JSON::encode(array( + 'formattedUsername' => $this->getFormattedUsername(), + 'formattedMessage' => $this->getFormattedMessage(), + 'time' => $this->time, + 'sender' => $this->sender, + 'username' => $this->getUsername(), + 'receiver' => $this->receiver, + 'type' => $this->type + )); + + } } diff --git a/template/chat.tpl b/template/chat.tpl index 65feaa2..2c56a8d 100644 --- a/template/chat.tpl +++ b/template/chat.tpl @@ -204,15 +204,6 @@
    @@ -269,12 +260,14 @@ diff --git a/template/chatMessage.tpl b/template/chatMessage.tpl index 1135bef..18fa9af 100644 --- a/template/chatMessage.tpl +++ b/template/chatMessage.tpl @@ -1 +1 @@ -{@$time} {@$username}{@$message} +{literal}{@$time} {@$formattedUsername}{@$formattedMessage}{/literal}