mirror of
https://github.com/wbbaddons/Tims-Chat.git
synced 2024-12-22 21:40:08 +00:00
Optimizing use of chatMessage-Template. We can now insert messages via JS
This commit is contained in:
parent
725a94aeb2
commit
f76fcca04d
@ -12,7 +12,8 @@ if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {};
|
|||||||
|
|
||||||
(function ($, document) {
|
(function ($, document) {
|
||||||
TimWolla.WCF.Chat = {
|
TimWolla.WCF.Chat = {
|
||||||
titleTemplate: '',
|
titleTemplate: null,
|
||||||
|
messageTemplate: null,
|
||||||
init: function(roomID, messageID) {
|
init: function(roomID, messageID) {
|
||||||
this.bindEvents();
|
this.bindEvents();
|
||||||
},
|
},
|
||||||
@ -107,6 +108,19 @@ if (typeof TimWolla.WCF == 'undefined') TimWolla.WCF = {};
|
|||||||
$(liUserID).addClass('activeMenuItem');
|
$(liUserID).addClass('activeMenuItem');
|
||||||
$(liUserID + ' .chatUserMenu').wcfBlindIn();
|
$(liUserID + ' .chatUserMenu').wcfBlindIn();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
handleMessages: function (messages) {
|
||||||
|
for (message in messages) {
|
||||||
|
message = messages[message];
|
||||||
|
output = this.messageTemplate.fetch(message);
|
||||||
|
|
||||||
|
li = $('<li></li>');
|
||||||
|
li.addClass('chatMessage'+message.type);
|
||||||
|
if (message.sender == WCF.User.userID) li.addClass('ownMessage');
|
||||||
|
li.append(output);
|
||||||
|
|
||||||
|
$('.chatMessage ul').append(li);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})(jQuery, document);
|
})(jQuery, document);
|
@ -41,7 +41,7 @@ class ChatMessage extends \wcf\data\DatabaseObject {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function __toString() {
|
public function __toString() {
|
||||||
return $this->getFormattedMessage;
|
return $this->getFormattedMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,9 +66,9 @@ public function getFormattedMessage() {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getFormattedUsername() {
|
public function getFormattedUsername() {
|
||||||
if ($this->type == self::TYPE_INFORMATION) return '<strong>'.WCF::getLanguage()->get('wcf.chat.information').'</strong>';
|
if ($this->type == self::TYPE_INFORMATION) return '<strong>'.$this->getUsername().'</strong>';
|
||||||
|
|
||||||
$string = str_split($this->username);
|
$string = str_split($this->getUsername());
|
||||||
$r = (int) (($this->color1 >> 16 & 255) - ($this->color2 >> 16 & 255)) / (count($string) - 1);
|
$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);
|
$g = (int) (($this->color1 >> 8 & 255) - ($this->color2 >> 8 & 255)) / (count($string) - 1);
|
||||||
$b = (int) (($this->color1 & 255) - ($this->color2 & 255)) / (count($string) - 1);
|
$b = (int) (($this->color1 & 255) - ($this->color2 & 255)) / (count($string) - 1);
|
||||||
@ -79,4 +79,27 @@ public function getFormattedUsername() {
|
|||||||
|
|
||||||
return '<strong>'.$result.'</strong>';
|
return '<strong>'.$result.'</strong>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -204,15 +204,6 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="chatMessage border content">
|
<div class="chatMessage border content">
|
||||||
<ul>
|
<ul>
|
||||||
{foreach from=$newestMessages item='message'}
|
|
||||||
<li>
|
|
||||||
{* TODO: Use an own time-function to display a short timestamp *}
|
|
||||||
{assign var='time' value=$message->time|time}
|
|
||||||
{assign var='username' value=$message->getFormattedUsername()|concat:': '}
|
|
||||||
{assign var='message' value=$message->getFormattedMessage()}
|
|
||||||
{include file='chatMessage'}
|
|
||||||
</li>
|
|
||||||
{/foreach}
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<form style="margin-top: 10px;" id="chatForm" action="index.php?form=Chat" method="post">
|
<form style="margin-top: 10px;" id="chatForm" action="index.php?form=Chat" method="post">
|
||||||
@ -269,12 +260,14 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
//<![CDATA[
|
//<![CDATA[
|
||||||
TimWolla.WCF.Chat.titleTemplate = new WCF.Template('{ldelim}$title} - {'wcf.chat.title'|language|encodeJS} - {PAGE_TITLE|language|encodeJS}');
|
TimWolla.WCF.Chat.titleTemplate = new WCF.Template('{ldelim}$title} - {'wcf.chat.title'|language|encodeJS} - {PAGE_TITLE|language|encodeJS}');
|
||||||
{capture assign='time'}{literal}{@$time}{/literal}{/capture}
|
|
||||||
{capture assign='username'}{literal}{@$username}{/literal}{/capture}
|
|
||||||
{capture assign='message'}{literal}{@$message}{/literal}{/capture}
|
|
||||||
{capture assign='chatMessageTemplate'}{include file='chatMessage'}{/capture}
|
{capture assign='chatMessageTemplate'}{include file='chatMessage'}{/capture}
|
||||||
TimWolla.WCF.Chat.messageTemplate = new WCF.Template('{$chatMessageTemplate|encodeJS}');
|
TimWolla.WCF.Chat.messageTemplate = new WCF.Template('{$chatMessageTemplate|encodeJS}');
|
||||||
TimWolla.WCF.Chat.init({$room->roomID}, 1);
|
TimWolla.WCF.Chat.init({$room->roomID}, 1);
|
||||||
|
TimWolla.WCF.Chat.handleMessages([
|
||||||
|
{implode from=$newestMessages item='message'}
|
||||||
|
{@$message->jsonify()}
|
||||||
|
{/implode}
|
||||||
|
]);
|
||||||
//]]>
|
//]]>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
{@$time} {@$username}{@$message}
|
{literal}{@$time} {@$formattedUsername}{@$formattedMessage}{/literal}
|
||||||
|
Loading…
Reference in New Issue
Block a user