ami/src/ami.js/messages.js

138 lines
4.5 KiB
JavaScript

#include utility.js
#include watcher.js
var AmiMessageList = function(parent) {
var container = $e({ attrs: { id: 'chatList', className: 'fullWidth' } }),
isEven = true,
idPfx = 'sock_msg_',
watchers = new AmiWatcherCollection;
parent.appendChild(container);
watchers.define(['nameInsert', 'delete']);
var reflow = function() {
isEven = true;
for(var i = 0; i < container.children.length; ++i) {
var child = container.children[i];
child.classList[isEven ? 'remove' : 'add']('rowOdd');
child.classList[isEven ? 'add' : 'remove']('rowEven');
isEven = !isEven;
}
};
return {
add: function(msgInfo) {
var nameStyle = { color: msgInfo.sender.color };
if(msgInfo.nameBold) nameStyle.fontWeight = 'bold';
if(msgInfo.nameItalics) nameStyle.fontStyle = 'italic';
if(msgInfo.nameUnderline) nameStyle.textDecoration = 'underline';
var nameBody = msgInfo.sender.username;
if(msgInfo.sender.isBot())
nameBody = {
tag: 'span',
attrs: { className: 'botName' },
child: nameBody,
};
var messageBody = [
{
tag: 'span',
attrs: { className: 'date' },
child: '(' + msgInfo.created.toLocaleTimeString() + ')',
},
' ',
{
tag: 'span',
attrs: {
style: nameStyle,
onclick: function() {
watchers.call('nameInsert', msgInfo, [msgInfo]);
},
},
child: nameBody,
},
{
tag: 'span',
attrs: { className: 'msgColon' },
child: msgInfo.showColon ? ':' : '',
},
' ',
{
tag: 'span',
attrs: { className: 'msgBreak' },
child: { tag: 'br' },
},
];
if(msgInfo.isPM) {
if(msgInfo.pmTargetName)
messageBody.push({
tag: 'i',
child: AmiStrings.getMenuString('whisperto', msgInfo.pmTargetName),
});
else
messageBody.push({
tag: 'i',
child: AmiStrings.getMenuString('whisper'),
});
messageBody.push(' ');
}
var message = $e({
attrs: {
id: idPfx + msgInfo.id,
className: isEven ? 'rowEven' : 'rowOdd'
},
child: messageBody,
});
message.insertAdjacentHTML('beforeend', msgInfo.bodyRaw);
if(msgInfo.canDelete)
message.appendChild($e({
tag: 'a',
attrs: {
title: 'Delete this message',
className: 'sprite-small sprite-delete',
style: {
float: 'right',
margin: '2px 0 0 0',
},
onclick: function() {
watchers.call('delete', msgInfo, [msgInfo]);
},
},
}));
isEven = !isEven;
container.appendChild(message);
return message;
},
remove: function(msgId) {
$ri(idPfx + msgId);
reflow();
},
clear: function() {
$rc(container);
isEven = true;
},
reflow: reflow,
showSideBar: function(type) {
container.classList[type === 'wide' ? 'add' : 'remove']('wideSideVisible');
container.classList[type === 'thin' ? 'add' : 'remove']('userListVisible');
container.classList[type === 'none' ? 'add' : 'remove']('fullWidth');
},
scrollToBottom: function() {
container.scrollTop = container.scrollHeight;
},
watch: function(name, watcher) {
watchers.watch(name, watcher);
},
unwatch: function(name, watcher) {
watchers.unwatch(name, watcher);
},
};
};