ami/src/ami.js/chat.js

150 lines
4.5 KiB
JavaScript

#include buttons.js
#include colourpick.js
#include emotes.js
#include messages.js
#include inputbox.js
#include opticons.js
#include sidebars.js
#include status.js
#include submitbox.js
#include title.js
#include utility.js
#include mami/settings.js
var AmiChat = function(chat, title, parent) {
var container = $e({ attrs: { id: 'chat' } });
var pub = {
getElement: function() { return container; },
appendTo: function(parent) { parent.appendChild(container); },
appendChild: function(child) {
if(typeof 'getElement' in child)
child = child.getElement();
container.appendChild(child);
},
};
pub.title = new AmiChatTitle(pub, title);
var copyright = new AmiCopyright(pub);
pub.copyright = copyright;
copyright.addLine([ ['Sock Chat', '//railgun.sh/sockchat'], ' © ', ['aroltd.com', 'http://aroltd.com'] ]);
copyright.addLine([ 'Ami © ', ['flash.moe', '//flash.moe'] ]);
var statusIndicator = new AmiStatusIndicator(pub);
pub.statusIndicator = statusIndicator;
var optionIcons = new AmiOptionIcons(pub);
pub.optionIcons = optionIcons;
var emoticonList = new AmiEmoticonList(pub);
pub.emoticonList = emoticonList;
var messageList = new AmiMessageList(pub);
pub.messageList = messageList;
var optionButtons = new AmiOptionButtons(pub);
pub.optionButtons = optionButtons;
chat.settings.watch('bbParse', function(value) {
optionButtons[value ? 'show' : 'hide']();
});
var sidebars = new AmiSidebars(pub);
pub.sidebars = sidebars;
sidebars.watch('show', function(sidebar) {
messageList.showSideBar(sidebar.isWide() ? 'wide' : 'thin');
});
sidebars.watch('hide', function() {
messageList.showSideBar('none');
});
var inputBox = new AmiInputBox(pub);
pub.inputBox = inputBox;
emoticonList.watch(function(emote) {
inputBox.insert(':' + emote.strings[0] + ':');
inputBox.focus();
});
var submitBox = new AmiSubmitBox(pub);
pub.submitBox = submitBox;
pub.colourPicker = new AmiColourPicker(pub);
if(parent !== undefined)
pub.appendTo(parent);
if(MamiSettings.isSupported())
optionIcons.create('export', 'Export modern client settings', 'export', () => MamiSettings.exportFile());
optionIcons.create('unembed', 'Unembed any embedded media', 'unembed', function() {
var buttons = $c('js-unembed-btn');
while(buttons.length > 0)
buttons[0].click();
});
optionIcons.create('uploads', 'Uploads', 'uploads', function() {
sidebars.toggle('uploadList');
});
optionIcons.create('help', 'Help', 'help', function() {
sidebars.toggle('helpList');
});
optionIcons.create('settings', 'Settings', 'settings', function() {
sidebars.toggle('settingsList');
});
optionIcons.create('users', 'Online users', 'users', function() {
sidebars.toggle('userList');
});
optionIcons.create('channels', 'Channels', 'channels', function() {
sidebars.toggle('chanList');
});
var muteToggle = optionIcons.create('audio', 'Mute sounds', 'sound', function() {
chat.settings.toggle('soundMute');
}, true);
chat.settings.watch('soundMute', function(value) {
muteToggle.setState(!value);
});
optionIcons.create('clear', 'Clear logs', 'clear', function() {
messageList.clear();
});
var scrollToggle = optionIcons.create('scroll', 'Auto scroll', 'autoscroll', function() {
chat.settings.toggle('msgAutoScroll');
}, true);
chat.settings.watch('msgAutoScroll', function(value) {
scrollToggle.setState(value);
});
submitBox.watch(function(ev) {
inputBox.enter(ev);
});
inputBox.watch('length', function(length) {
submitBox.setCurrentLength(length);
});
chat.sockChat.watch('session:start', function(info) {
inputBox.setMaxLength(info.ctx.maxMsgLength);
submitBox.setMaxLength(info.ctx.maxMsgLength);
});
chat.sockChat.watch('msg:remove', function(info) {
messageList.remove(info.msg.id);
});
chat.sockChat.watch('msg:clear', function() {
messageList.clear();
});
messageList.watch('nameInsert', function(msgInfo) {
inputBox.insert(msgInfo.sender.username);
inputBox.focus();
});
messageList.watch('delete', function(msgInfo) {
if(confirm(AmiStrings.getMenuString('delmsg')))
chat.sockChat.sendMessage('/delete ' + msgInfo.id);
});
return pub;
};