Ported array and html funcs from Misuzu.

This commit is contained in:
flash 2025-02-20 19:19:55 +00:00
commit 4638c60af4
35 changed files with 441 additions and 535 deletions
src/ami.js

View file

@ -1,11 +1,10 @@
#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;
var container = $element('div', { id: 'chatList', className: 'fullWidth' });
var isEven = true;
var idPfx = 'sock_msg_';
var watchers = new AmiWatcherCollection;
parent.appendChild(container);
watchers.define(['nameInsert', 'delete']);
@ -29,90 +28,80 @@ var AmiMessageList = function(parent) {
var nameBody = msgInfo.sender.username;
if(msgInfo.sender.isBot())
nameBody = {
tag: 'span',
attrs: { className: 'botName' },
child: nameBody,
};
nameBody = $element(
'span',
{ className: 'botName' },
nameBody,
);
var messageBody = [
{
tag: 'span',
attrs: { className: 'date' },
child: '(' + msgInfo.created.toLocaleTimeString() + ')',
},
$element(
'span',
{ className: 'date' },
'(' + msgInfo.created.toLocaleTimeString() + ')',
),
' ',
{
tag: 'span',
attrs: {
style: nameStyle,
onclick: function() {
watchers.call('nameInsert', msgInfo, [msgInfo]);
},
$element('span', {
style: nameStyle,
onclick: function() {
watchers.call('nameInsert', msgInfo, [msgInfo]);
},
child: nameBody,
},
}, nameBody),
];
if(msgInfo.showColon)
messageBody.push({
tag: 'span',
attrs: { className: 'msgColon' },
child: ':',
});
messageBody.push($element(
'span',
{ className: 'msgColon' },
':',
));
if(msgInfo.isAction) {
if(msgInfo.bodyText.indexOf("'") !== 0 || (msgInfo.bodyText.match(/\'/g).length % 2) === 0)
messageBody.push(' ');
} else {
messageBody.push(' ');
messageBody.push({
tag: 'span',
attrs: { className: 'msgBreak' },
child: { tag: 'br' },
});
messageBody.push($element(
'span',
{ className: 'msgBreak' },
$element('br'),
));
}
if(msgInfo.isPM) {
if(msgInfo.pmTargetName)
messageBody.push({
tag: 'i',
child: AmiStrings.getMenuString('whisperto', msgInfo.pmTargetName),
});
messageBody.push($element(
'i', {},
AmiStrings.getMenuString('whisperto', msgInfo.pmTargetName),
));
else
messageBody.push({
tag: 'i',
child: AmiStrings.getMenuString('whisper'),
});
messageBody.push($element(
'i', {},
AmiStrings.getMenuString('whisper'),
));
messageBody.push(' ');
}
var message = $e({
attrs: {
id: idPfx + msgInfo.id,
className: isEven ? 'rowEven' : 'rowOdd'
},
child: messageBody,
});
var message = $element('div', {
id: idPfx + msgInfo.id,
className: isEven ? 'rowEven' : 'rowOdd'
}, ...messageBody);
let msgBodyTarget = message;
if(msgInfo.isAction)
message.appendChild(msgBodyTarget = $e({ tag: 'i' }));
message.appendChild(msgBodyTarget = $element('i'));
msgBodyTarget.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]);
},
message.appendChild($element('a', {
title: 'Delete this message',
className: 'sprite-small sprite-delete',
style: {
float: 'right',
margin: '2px 0 0 0',
},
onclick: function() {
watchers.call('delete', msgInfo, [msgInfo]);
},
}));
@ -122,11 +111,13 @@ var AmiMessageList = function(parent) {
return message;
},
remove: function(msgId) {
$ri(idPfx + msgId);
const elem = $id(idPfx + msgId);
if(elem)
elem.parentNode.removeChild(elem);
reflow();
},
clear: function() {
$rc(container);
$removeChildren(container);
isEven = true;
},
reflow: reflow,