ami/src/ami.js/ts_chat.js

635 lines
21 KiB
JavaScript

#include ts_20_ui.js
#include ts_10_user.js
#include eeprom.js
#include z_eepromv1.js
#include utility.js
#include sidebars/channelssb.js
#include sidebars/helpsb.js
#include sidebars/settingssb.js
#include sidebars/uploadssb.js
#include sidebars/userssb.js
var Chat = (function () {
// pulling all the env shit in atop here for now
var chatSettings = [
{
id: "style",
name: 'style',
type: "select",
load: function(input) {
ami.styles.forEach(function(style) {
input.appendChild($e({
tag: 'option',
child: style.getTitle(),
attrs: {
value: style.getName(),
className: style.getOptionClass(),
},
}));
if(style.isDefault() && !ami.settings.has('style'))
ami.settings.set('style', style.getName());
});
},
},
{
id: "bbcode",
name: 'bbParse',
type: "checkbox",
},
{
id: "emotes",
name: 'parseEmoticons',
type: "checkbox",
},
{
id: "links",
name: 'parseLinks',
type: "checkbox",
},
{
id: "spack",
name: 'soundPack',
type: "select",
load: function(input) {
ami.sound.forEachPack(function(pack) {
var opt = document.createElement('option');
opt.value = pack.getName();
opt.innerHTML = pack.getTitle();
input.appendChild(opt);
if(ami.settings.get('soundPack') === '')
ami.settings.set('soundPack', pack.getName());
});
},
},
{
id: "volume",
name: 'soundVolume',
type: "select",
as: 'float',
options: [
[1.0, '100%'],
[0.9, '90%'],
[0.8, '80%'],
[0.7, '70%'],
[0.6, '60%'],
[0.5, '50%'],
[0.4, '40%'],
[0.3, '30%'],
[0.2, '20%'],
[0.1, '10%'],
[0.0, '0%'],
],
},
{
id: "shownotifs",
name: 'enableNotifs',
type: "checkbox",
},
{
id: "runjoketrig",
name: 'runJokeTriggers',
type: "checkbox",
},
{
id: "dumppackets",
name: 'dumpPackets',
type: "checkbox",
},
{
id: "autoembedmedia",
name: 'bbAutoEmbedV1',
type: "checkbox",
},
{
id: "skipdomainpopupthing",
name: 'tmpSkipDomainPopUpThing',
type: "checkbox",
},
];
var userMenu = [
{
self: true,
others: false,
modonly: false,
langid: "ctxlou",
action: 1,
value: "/who"
},
{
self: true,
others: false,
modonly: false,
langid: "ctxlchan",
action: 1,
value: "/channels"
},
{
self: true,
others: false,
modonly: false,
langid: "ctxjchan",
action: 0,
value: "/join"
},
{
self: true,
others: false,
modonly: false,
langid: "ctxcchan",
action: 0,
value: "/create"
},
{
self: true,
others: false,
modonly: false,
langid: "ctxda",
action: 0,
value: "/me"
},
{
self: true,
others: false,
modonly: false,
langid: "ctxnick",
action: 0,
value: "/nick"
},
{
self: false,
others: true,
modonly: false,
langid: "ctxpm",
action: 0,
value: "/msg {1}"
},
{
self: false,
others: true,
modonly: true,
langid: "ctxkick",
action: 0,
value: "/kick {1}"
},
{
self: false,
others: true,
modonly: true,
langid: "ctxban",
action: 0,
value: "/ban {1}"
},
{
self: false,
others: true,
modonly: true,
langid: "ctxwho",
action: 1,
value: "/whois {1}"
},
];
var bbcodes = [
{
tag: "b",
arg: false,
swap: "<b>{0}</b>",
button: 'b',
buttonStyle: { fontWeight: 'bold' },
persistSetting: 'bbPersistBold',
persistStyleAttr: 'fontWeight',
persistStyleValue: 'bold',
},
{
tag: "i",
arg: false,
swap: "<i>{0}</i>",
button: 'i',
buttonStyle: { fontStyle: 'italic' },
persistSetting: 'bbPersistItalics',
persistStyleAttr: 'fontStyle',
persistStyleValue: 'italic',
},
{
tag: "u",
arg: false,
swap: "<u>{0}</u>",
button: 'u',
buttonStyle: { textDecoration: 'underline' },
persistSetting: 'bbPersistUnderline',
persistStyleAttr: 'textDecoration',
persistStyleValue: 'underline',
},
{
tag: "s",
arg: false,
swap: "<del>{0}</del>",
button: 's',
buttonStyle: { textDecoration: 'line-through' },
persistSetting: 'bbPersistStrike',
persistStyleAttr: 'textDecoration',
persistStyleValue: 'line-through',
},
{
tag: "quote",
arg: false,
swap: "<q style='font-variant: small-caps;'>{0}</q>",
button: true
},
{
tag: "code",
arg: false,
swap: "<span style='white-space: pre-wrap; font-family: monospace; font-size: 1.2em;'>{0}</span>",
button: true
},
{
tag: "sjis",
arg: false,
swap: "<span class='sjis'>{0}</span>",
button: true,
persistSetting: 'bbPersistSjis',
persistClassName: 'sjis',
},
{
tag: "color",
arg: true,
rmarg: ";:{}<>&|\\/~'\"",
swap: "<span style='color:{0};'>{1}</span>",
button: true,
bhandle: function(bb, ev) {
ami.chat.colourPicker.show(ev, function(colour) {
Chat.InsertBBCode('color', colour.hex);
});
},
enableSetting: 'bbParseColour',
persistSetting: 'bbPersistColour',
persistStyleAttr: 'color',
persistStyleSingle: true,
},
{
tag: "url",
arg: false,
rmin: "\"'",
swap: "<a href='{0}' target='_blank'>{0}</a>"
},
{
tag: "url",
arg: true,
rmarg: "\"'",
swap: "<a href='{0}' target='_blank'>{1}</a>",
button: true,
bprompt: "urlprompt"
},
{
tag: "img",
arg: false,
rmin: "\"'",
swap: "<span title='{0}'><span title='link'><a href='{0}' target='_blank'>{0}</a></span>&nbsp;[<a href='#' onclick='Utils.EmbedImage(this);return false;' class='js-embed-btn'>Embed</a>]</span>",
button: true,
enableSetting: 'bbParseImage',
},
{
tag: "video",
arg: false,
rmin: "\"'",
swap: "<span title='{0}'><span title='link'><a href='{0}' target='_blank'>{0}</a></span>&nbsp;[<a href='#' onclick='Utils.EmbedVideo(this);return false;' class='js-embed-btn'>Embed</a>]</span>",
button: true,
enableSetting: 'bbParseVideo',
},
{
tag: "audio",
arg: false,
rmin: "\"'",
swap: "<span title='{0}'><span title='link'><a href='{0}' target='_blank'>{0}</a></span>&nbsp;[<a href='#' onclick='Utils.EmbedAudio(this);return false;' class='js-embed-btn'>Embed</a>]</span>",
button: true,
enableSetting: 'bbParseAudio',
},
{
tag: "spoiler",
arg: false,
rmin: "\"'",
swap: "<span data-shit='{0}'><span>*** HIDDEN ***</span>&nbsp;[<a href='#' onclick='Utils.ToggleSpoiler(this);return false;'>Reveal</a>]</span>",
button: true
}
];
function Chat() {}
Chat.Main = function(auth) {
bbcodes.forEach(function(item) {
UI.bbcode.push(item);
});
userMenu.forEach(function(item) {
UI.contextMenuFields.push(item);
});
ami.chat.sidebars.create(new AmiUsersSidebar);
ami.chat.sidebars.create(new AmiChannelsSidebar);
var sbSettings = new AmiSettingsSidebar;
ami.chat.sidebars.create(sbSettings);
ami.chat.sidebars.create(new AmiHelpSidebar);
ami.chat.sidebars.create(new AmiUploadsSidebar);
if(window.innerWidth > 800)
ami.chat.sidebars.show('userList');
var table = document.getElementById("settingsList").getElementsByTagName("table")[0];
var cnt = 0;
chatSettings.forEach(function (elt, i, arr) {
var row = table.insertRow(i);
row.className = cnt % 2 == 0 ? "rowOdd" : "rowEven";
row.setAttribute("name", elt.id);
var cell = row.insertCell(0);
cell.innerHTML = elt.id;
cell = row.insertCell(1);
cell.className = "setting";
var input = null;
switch(elt.type) {
case "select":
input = document.createElement("select");
input.id = 'chatSetting-' + elt.id;
input.onchange = function(e) {
var value = this.value;
if(elt.as === 'float')
value = parseFloat(value);
ami.settings.set(elt.name, value);
};
if(elt.options !== undefined) {
for(var val in elt.options) {
var optionInfo = elt.options[val];
var option = document.createElement("option");
option.value = optionInfo[0];
option.innerHTML = optionInfo[1];
input.appendChild(option);
}
}
ami.settings.watch(elt.name, function(value) {
input.value = value;
});
cell.appendChild(input);
break;
case "checkbox":
input = document.createElement("input");
input.id = 'chatSetting-' + elt.id;
input.type = 'checkbox';
input.onchange = function(e) {
ami.settings.set(elt.name, this.checked);
};
ami.settings.watch(elt.name, function(value) {
input.checked = value;
});
cell.appendChild(input);
break;
}
if(input !== null) {
if(elt.load) {
elt.load.call(this, input);
input.value = ami.settings.get(elt.name);
}
++cnt;
}
});
UI.bbcode.forEach(function (elt, i, arr) {
if(elt.enableSetting) {
var row = table.insertRow(cnt);
row.className = cnt % 2 == 0 ? "rowOdd" : "rowEven";
row.setAttribute("name", "||" + elt["tag"]);
var cell = row.insertCell(0);
cell.innerHTML = "enable " + elt["tag"];
cell = row.insertCell(1);
cell.className = "setting";
input = document.createElement("input");
input.setAttribute("type", "checkbox");
input.checked = ami.settings.get(elt.enableSetting);
input.onchange = function (e) {
ami.settings.set(elt.enableSetting, this.checked);
};
cell.appendChild(input);
++cnt;
}
if(elt.persistSetting) {
var row = table.insertRow(cnt);
row.className = cnt % 2 == 0 ? "rowOdd" : "rowEven";
row.setAttribute("name", ";;" + elt["tag"]);
var cell = row.insertCell(0);
cell.innerHTML = "persist " + elt["tag"];
cell = row.insertCell(1);
cell.className = "setting";
input = document.createElement("input");
input.setAttribute("type", "checkbox");
input.checked = ami.settings.get(elt.persistSetting);
input.onchange = function(e) {
var settingValue = this.checked;
if(!elt.persistClassName && !elt.persistStyleValue)
settingValue = settingValue ? '' : null;
ami.settings.set(elt.persistSetting, settingValue);
};
ami.settings.watch(elt.persistSetting, function(value) {
if(elt.persistClassName)
ami.chat.inputBox.setClassName(elt.persistClassName, value);
if(elt.persistStyleAttr)
ami.chat.inputBox.setStyleValue(elt.persistStyleAttr, elt.persistStyleValue || value, !!value, !!elt.persistStyleSingle);
});
cell.appendChild(input);
++cnt;
}
});
// move into ctx when the sidebars code doesn't suck
ami.settings.watch('soundMute', function(value) {
if(!value && (!ami.sound.hasPacks() || !ami.sound.hasSoundLibrary())) {
ami.sound.clearPacks();
ami.sound.clearSoundLibrary();
futami.getJson('sounds2', function(sounds) {
if(typeof sounds !== 'object')
return;
if(Array.isArray(sounds.library))
for(var i in sounds.library)
ami.sound.registerLibrarySound(sounds.library[i]);
if(Array.isArray(sounds.packs)) {
for(var i in sounds.packs)
ami.sound.registerSoundPack(sounds.packs[i]);
if(sounds.packs.length > 0 && !ami.settings.has('soundPack'))
ami.settings.set('soundPack', sounds.packs[0].name);
}
var soundPackSetting = $i('chatSetting-spack');
if(soundPackSetting) {
while(soundPackSetting.options.length > 0)
soundPackSetting.options.remove(0);
ami.sound.forEachPack(function(pack) {
var opt = document.createElement('option');
opt.value = pack.getName();
opt.innerHTML = pack.getTitle();
soundPackSetting.appendChild(opt);
if(ami.settings.get('soundPack') === '')
ami.settings.set('soundPack', pack.getName());
});
soundPackSetting.value = ami.settings.get('soundPack');
}
});
}
ami.sound.setMuted(value);
});
for(var i in bbcodes)
(function(bbcodeInfo) {
if(!bbcodeInfo.button)
return;
var langKey = typeof bbcodeInfo.button === 'string' ? null : bbcodeInfo.tag;
var button = ami.chat.optionButtons.create(bbcodeInfo.tag, bbcodeInfo.tag, langKey, bbcodeInfo.buttonStyle, function(buttonInfo, ev) {
if(!bbcodeInfo.arg) {
Chat.InsertBBCode(bbcodeInfo.tag);
return;
}
if(bbcodeInfo.bhandle) {
bbcodeInfo.bhandle.call(buttonInfo, buttonInfo, ev);
return;
}
var tagArg = prompt(buttonInfo.bprompt && AmiStrings.hasMenuString(buttonInfo.bprompt) ? AmiStrings.getMenuString(buttonInfo.bprompt) : AmiStrings.getMenuString('bprompt'));
if(tagArg === null || tagArg === undefined)
return;
Chat.InsertBBCode(bbcodeInfo.tag, tagArg);
});
if(bbcodeInfo.enableSetting)
ami.settings.watch(bbcodeInfo.enableSetting, function(value) {
button[value ? 'show' : 'hide']();
});
})(bbcodes[i]);
ami.chat.inputBox.watch('tab', function(ev) {
if('preventDefault' in ev)
ev.preventDefault();
if(ami.chat.inputBox.isEmpty())
return;
var cursorWord = ami.chat.inputBox.getWordAtCursor(),
snippet = cursorWord.word.toLowerCase(),
insertText = undefined;
if(snippet.length > 0) {
if(snippet.indexOf(':') === 0) {
var emoteRank = 0;
if(UserContext.self !== undefined)
emoteRank = parseInt(UserContext.self.perms[0]);
var emotes = ami.emoticons.findByName(emoteRank, snippet.substring(1), true);
if(emotes.length > 0)
insertText = ':' + emotes[0] + ':';
} else {
var findUserNames = [];
for(var i in UserContext.users) {
var findUserName = UserContext.users[i].username;
if(findUserName.toLowerCase().indexOf(snippet) >= 0)
findUserNames.push(findUserName);
}
if(findUserNames.length === 1)
insertText = findUserNames[0];
}
}
if(insertText !== undefined)
ami.chat.inputBox.insertAt(cursorWord.start, insertText, -snippet.length);
});
ami.chat.inputBox.watch('enter', function(ev) {
if('preventDefault' in ev)
ev.preventDefault();
var text = ami.chat.inputBox.getValue();
text = text.replace(/\t/g, ' ');
if(text.trim().charAt(0) != '/') {
for(var i in bbcodes) {
var bbcode = bbcodes[i];
if(bbcode.persistSetting) {
var settingValue = ami.settings.get(bbcode.persistSetting);
if(settingValue) {
if(typeof settingValue === 'boolean')
text = '[' + bbcode.tag + ']' + text + '[/' + bbcode.tag + ']';
else
text = '[' + bbcode.tag + '=' + settingValue + ']' + text + '[/' + bbcode.tag + ']';
}
}
}
}
if(text.trim() !== '')
ami.sockChat.sendMessage(text);
ami.chat.inputBox.clear();
ami.chat.inputBox.focus();
});
UI.RenderLanguage();
AmiEEPROM.init(function(success) {
if(!success)
return;
// set up old eeprom code
eepromInitialise(auth);
});
};
Chat.InsertBBCode = function (tag, arg) {
if (arg === void 0) { arg = null; }
var bbcode = null;
for(var i in bbcodes) {
var info = bbcodes[i];
if(info.tag === tag) {
bbcode = info;
break;
}
}
if(bbcode && bbcode.persistSetting) {
var persistValue = ami.settings.get(bbcode.persistSetting);
if(persistValue !== null && persistValue !== false) {
ami.settings.set(bbcode.persistSetting, arg);
ami.chat.inputBox.focus();
return;
}
}
if (arg == null)
ami.chat.inputBox.insertAround('[' + tag + ']', '[/' + tag + ']');
else
ami.chat.inputBox.insertAround('[' + tag + '=' + arg + ']', '[/' + tag + ']');
ami.chat.inputBox.focus();
};
return Chat;
})();