var $currentsb;
class xoborShoutbox {
defaults = {
target: '.scndrow',
title: 'Shoutbox',
boxid: 'xShoutbox',
showinput : true,
wow: 'fadeIn',
adminview : false,
user : false,
loader: "
",
initOnLoad: true,
emoji : "😀 😁 😂 🤣 😃 😄 😅 😆 😉 😊 😋 😎 😍 😘 😗 😙 😚 🙂 🤗 🤩 🤔 🤨 😐 😑 😶 🙄 😏 😣 😥 😮 🤐 😯 😪 😫 😴 😌 😛 😜 😝 🤤 😒 😓 😔 😕 🙃 🤑 😲 ☹️ 🙁 😖 😞 😟 😤 😢 😭 😦 😧 😨 😩 🤯 😬 😰 😱 🥵 🥶 😳 🤪 😵 😡 😠 🤬 😷 🤒 🤕 🤢 🤮 🤧 😇 🤠 🤡 🤥 🤫 🤭 🧐 🤓 😈 👿 👹 👺 💀 👻 👽 🤖 💩 😺 😸 😹 😻 😼 😽 🙀 😿 😾",
};
initialized = false;
skeletons = {};
constructor(options = {}) {
$currentsb = this;
this.config = $.extend({},this.defaults,options);
this.initSkeletons();
$(this.skeletons.box).prependTo(this.config.target);
this.config.sbtitle = $('#' + this.config.boxid).find(this.config.sbtitle_selector);
this.config.sbcontent = $('#' + this.config.boxid).find(this.config.sbcontent_selector);
this.config.sbfooter = $('#' + this.config.boxid).find(this.config.sbfooter_selector);
this.config.sbinput = $('#' + this.config.boxid).find(this.config.sbinput_selector);
if (this.config.initOnLoad) {
this.init();
}
return this;
}
init() {
$currentsb.log("Init Shoutbox");
var $curboxcontent = xobor.plugin('xshoutbox').getGlob('shoutboxdata');
$currentsb.config.sbcontent.html('');
$.each($curboxcontent, function() {$currentsb.drawMessage(this);});
$currentsb.initialized = true;
/*Emoji Bindings*/
$('.xsbemoji').on('click',function(){
$currentsb.config.sbinput.val($currentsb.config.sbinput.val() + " " + $(this).text());
$currentsb.hideEmoji();
});
document.addEventListener("plugin_xshoutbox_update", $currentsb.newMessageHandler, false);
$currentsb.log("Init Shoutbox Done");
}
newMessageHandler(e){
try{
if(e.detail.data.element && e.detail.data.element.user_id != $currentsb.config.user ) $currentsb.drawMessage(e.detail.data.element);
}catch(e){}
}
drawMessage(mes) {
mes.displayTime = $currentsb.dateFormatForMessage(mes.time);
$('#'+$currentsb.config.boxid).removeClass('emptysb');
if(mes.userlink){
if(mes.usercolor){
mes.displayLink = ''+mes.username+'';
}else{
mes.displayLink = ''+mes.username+'';
}
}else{
mes.displayLink = ''+mes.username+'';
}
if(mes.avatar){
mes.displayAvatar = '
';
}else{
mes.displayAvatar = ''+mes.username.substring(0,2)+'';
}
let effectclass = ($currentsb.initialized && $currentsb.config.wow) ? "wow "+ $currentsb.config.wow : "";
effectclass = false;
let parsedMessage = $currentsb.parseEmoji(mes.message);
let delmessagespan ='';
if($currentsb.config.adminview){
delmessagespan = '';
}else if($currentsb.config.user == mes.user_id){
delmessagespan = ''
}
let userclass = "sbm_u"+mes.user_id;
let rowskeleton = `
${mes.displayLink} ${delmessagespan}
${parsedMessage}
${mes.displayTime}
`;
$(rowskeleton).appendTo($currentsb.config.sbcontent);
$currentsb.config.sbcontent[0].scrollTop = $currentsb.config.sbcontent[0].scrollHeight;
}
addMessage() {
if($currentsb.config.sbinput.val().trim() == ""){return;}
var data = {
message: $currentsb.config.sbinput.val()
};
$currentsb.config.sbinput.val("");
xobor.plugin("xshoutbox").call("addShoutboxEntry", data, function(res) {
if(res && res.user_id == $currentsb.config.user){
$currentsb.drawMessage(res);
}else{
}
});
}
toggleEmoji(){
if($('#xShoutbox .emoji-bubble-cont').is(':visible')){
this.hideEmoji();
}else{
this.showEmoji();
}
}
showEmoji(){
$('#xShoutbox .emoji-bubble-cont').show();
}
hideEmoji(){
$('#xShoutbox .emoji-bubble-cont').hide();
}
clearAll(){
if(!confirm("Den gesamten Inhalt der Shoutbox löschen?")){return;}
xobor.plugin('xshoutbox').setGlob('shoutboxdata',"",function(){
$('#xShoutbox .xsbMessage').remove();
$('#'+$currentsb.config.boxid).addClass('emptysb');
});
}
clearLastMessage(){
xobor.plugin("xshoutbox").call("delLastShoutboxEntry", {del:true}, function(res) {
$('.xsbMessage').eq(-1).remove();
});
}
clearMessage(el){
var totallength = $('.xsbMessage').length;
var index_to_del = $('.xsbMessage').index($(el).closest('.xsbMessage'));
var el_to_del = totallength-index_to_del;
xobor.plugin('xshoutbox').getGlob('shoutboxdata',function(res){
var arrindex = res.length - el_to_del;
if(arrindex>0){
res.splice(arrindex, 1);
}
xobor.plugin('xshoutbox').setGlob('shoutboxdata',res,function(res){});
});
$(el).closest('.xsbMessage').remove();
}
/*
HELPER FUNCTIONS
*/
dateFormatForMessage(ts){
ts = ts * 1000;
var date = new Date(ts);
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
if(minutes < 10){minutes = "0" + minutes;}
if(hours < 10){hours = "0" + hours;}
if(day < 10){day = "0" + day;}
if(month < 10){month = "0" + month;}
if($currentsb.isToday(date)){
return hours+':'+minutes;
}else{
return day+'.'+month+' '+hours+':'+minutes;
}
}
isToday(someDate){
var today = new Date()
return someDate.getDate() == today.getDate() &&
someDate.getMonth() == today.getMonth() &&
someDate.getFullYear() == today.getFullYear()
}
log(content) {
xobor.log.dev('Shoutbox', content);
}
getEmoji(){
var $emoji = $currentsb.config.emoji.split(" ");
var $emojidiv = "";
$.each($emoji,function(){
$emojidiv += ""+this+"";
});
$emojidiv += "
";
return $emojidiv;
}
parseEmoji(str=""){
str = str + "";
return str.replace(/(\d+;){2}/g, function(match) {
var orig = match;
match = match.replace(//g,'').split(';');
var binFirst = (parseInt('0x' + parseInt(match[0]).toString(16)) - 0xd800).toString(2);
var binSecond = (parseInt('0x' + parseInt(match[1]).toString(16)) - 0xdc00).toString(2);
binFirst = '0000000000'.substr(binFirst.length) + binFirst;
binSecond = '0000000000'.substr(binSecond.length) + binSecond;
var temp = ('0x' + (parseInt(binFirst + binSecond, 2).toString(16))) - (-0x10000);
if(isNaN(temp)){return orig;}
return '' + temp.toString(16) + ';';
});
}
initSkeletons(){
let emojidiv = "" + $currentsb.getEmoji() + "
";
let clearspan = this.config.adminview ? ' leeren' : '';
let togglespan = `
`;
this.skeletons.emojidiv = emojidiv;
this.skeletons.clearspan = clearspan;
let visclass = "expanded";
try{
if(hidden_cards.length){
$.each(hidden_cards,function(){
if(this == $currentsb.config.boxid){
visclass = "compressed";
}
});
}
}catch(e){}
this.skeletons.footer = this.config.showinput ?
`` : '';
this.skeletons.box = `
${this.config.loader}
Keine Einträge
${this.skeletons.footer}
`;
this.config.sbtitle_selector = '.card-header h5';
this.config.sbcontent_selector = '.card-body';
this.config.sbfooter_selector = '.card-footer';
this.config.sbinput_selector = '.xSbNewMessage';
}
}