Upload files to "kronic-chat/js"
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
window.Kronick = window.Kronick || {};
|
||||||
|
|
||||||
|
const installConfig =
|
||||||
|
window.KRONICK_INSTALL_CONFIG || {};
|
||||||
|
|
||||||
|
Kronick.config = {
|
||||||
|
LIVEKIT_URL:
|
||||||
|
installConfig.LIVEKIT_URL ||
|
||||||
|
"wss://livkit.kronickrusaders.ca",
|
||||||
|
|
||||||
|
TOKEN_URL:
|
||||||
|
`${window.location.origin}/borg-chat/get-token`,
|
||||||
|
|
||||||
|
APP_ORIGIN:
|
||||||
|
window.location.origin,
|
||||||
|
|
||||||
|
DATA_TOPIC_MEDIA:
|
||||||
|
'kronick-media',
|
||||||
|
|
||||||
|
DATA_TOPIC_CHAT:
|
||||||
|
'kronick-chat'
|
||||||
|
};
|
||||||
|
|
||||||
|
Kronick.state = {
|
||||||
|
room: null,
|
||||||
|
currentRoomName: '',
|
||||||
|
nextTileX: 0,
|
||||||
|
nextTileY: 0,
|
||||||
|
giphyTargetMode: 'room',
|
||||||
|
highestWindowZIndex: 6000,
|
||||||
|
|
||||||
|
participantStatsIntervals: new Map(),
|
||||||
|
participantLevels: new Map(),
|
||||||
|
participantRtts: new Map(),
|
||||||
|
participantAudioElements: new Map()
|
||||||
|
};
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/* Borg stage 2: navigation shell only. Does not modify legacy Media app.js. */
|
||||||
|
(() => {
|
||||||
|
const wrap=document.getElementById('borgWindowWrap');
|
||||||
|
const header=document.getElementById('borgWindowHeader');
|
||||||
|
const title=document.getElementById('borgWindowTitle');
|
||||||
|
if(!wrap || !header)return;
|
||||||
|
let pos=null;
|
||||||
|
const open=(name='Media Library')=>{wrap.hidden=false;title.textContent=name;};
|
||||||
|
const close=()=>{wrap.hidden=true;};
|
||||||
|
document.querySelectorAll('[data-borg]').forEach(btn=>btn.addEventListener('click',()=>{
|
||||||
|
document.querySelectorAll('[data-borg]').forEach(b=>b.classList.toggle('borg-selected',b===btn));
|
||||||
|
const kind=btn.dataset.borg;
|
||||||
|
if(kind==='room'){close();return;}
|
||||||
|
const labels={libraries:'Libraries',movies:'Movies',shows:'TV Shows',music:'Music',iptv:'IPTV',youtube:'Tube',twitch:'Twitch',settings:'Manage Libraries'};
|
||||||
|
open(labels[kind]||'Media Library');
|
||||||
|
// Trigger existing application actions when they can be identified safely.
|
||||||
|
if(kind==='libraries'){document.getElementById('librariesButton')?.click();}
|
||||||
|
if(kind==='settings'){document.getElementById('manageButton')?.click();}
|
||||||
|
// Other tabs intentionally show the current Media view until mapped to real app.js navigation.
|
||||||
|
}));
|
||||||
|
document.getElementById('borgClose')?.addEventListener('click',close);
|
||||||
|
document.getElementById('borgMinimize')?.addEventListener('click',close);
|
||||||
|
document.getElementById('borgMaximize')?.addEventListener('click',()=>wrap.classList.toggle('borg-max'));
|
||||||
|
header.addEventListener('pointerdown',e=>{
|
||||||
|
if(e.target.closest('button') || wrap.classList.contains('borg-max') || matchMedia('(max-width:700px)').matches)return;
|
||||||
|
pos={x:e.clientX,y:e.clientY,left:wrap.offsetLeft,top:wrap.offsetTop};
|
||||||
|
header.setPointerCapture(e.pointerId);
|
||||||
|
});
|
||||||
|
header.addEventListener('pointermove',e=>{
|
||||||
|
if(!pos)return;
|
||||||
|
const l=Math.max(0,Math.min(window.innerWidth-90,pos.left+e.clientX-pos.x));
|
||||||
|
const t=Math.max(0,Math.min(window.innerHeight-90,pos.top+e.clientY-pos.y));
|
||||||
|
wrap.style.left=l+'px';wrap.style.top=t+'px';
|
||||||
|
});
|
||||||
|
const stop=()=>{pos=null;};header.addEventListener('pointerup',stop);header.addEventListener('pointercancel',stop);
|
||||||
|
document.getElementById('homeButton')?.addEventListener('click',()=>open('Libraries'));
|
||||||
|
// Initial welcome room; Media contents are already mounted and retain their state.
|
||||||
|
close();
|
||||||
|
})();
|
||||||
@@ -0,0 +1,735 @@
|
|||||||
|
window.Kronick =
|
||||||
|
window.Kronick || {};
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Chat notification state
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.chatUnreadCount =
|
||||||
|
Number(
|
||||||
|
Kronick.chatUnreadCount
|
||||||
|
|| 0
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.getChatWindow =
|
||||||
|
function getChatWindow() {
|
||||||
|
return document.getElementById(
|
||||||
|
'chatFrameWrap'
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.getChatButton =
|
||||||
|
function getChatButton() {
|
||||||
|
return document.getElementById(
|
||||||
|
'chatToolButton'
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.getChatUnreadBadge =
|
||||||
|
function getChatUnreadBadge() {
|
||||||
|
return document.getElementById(
|
||||||
|
'chatUnreadCount'
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.chatWindowIsVisible =
|
||||||
|
function chatWindowIsVisible() {
|
||||||
|
const chatWrap =
|
||||||
|
Kronick.getChatWindow();
|
||||||
|
|
||||||
|
if (!chatWrap) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
chatWrap.classList.contains(
|
||||||
|
'window-minimized'
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const style =
|
||||||
|
window.getComputedStyle(
|
||||||
|
chatWrap
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
style.display !== 'none'
|
||||||
|
&& style.visibility !== 'hidden'
|
||||||
|
&& style.opacity !== '0'
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.isOwnChatMessage =
|
||||||
|
function isOwnChatMessage(
|
||||||
|
message
|
||||||
|
) {
|
||||||
|
const localIdentity =
|
||||||
|
Kronick.state
|
||||||
|
?.room
|
||||||
|
?.localParticipant
|
||||||
|
?.identity
|
||||||
|
|| '';
|
||||||
|
|
||||||
|
if (!localIdentity) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
String(
|
||||||
|
message?.sender
|
||||||
|
|| ''
|
||||||
|
)
|
||||||
|
===
|
||||||
|
String(
|
||||||
|
localIdentity
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.renderChatUnreadState =
|
||||||
|
function renderChatUnreadState() {
|
||||||
|
const badge =
|
||||||
|
Kronick.getChatUnreadBadge();
|
||||||
|
|
||||||
|
const button =
|
||||||
|
Kronick.getChatButton();
|
||||||
|
|
||||||
|
if (!badge || !button) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const count =
|
||||||
|
Math.max(
|
||||||
|
0,
|
||||||
|
Number(
|
||||||
|
Kronick.chatUnreadCount
|
||||||
|
|| 0
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
const hasUnread =
|
||||||
|
count > 0;
|
||||||
|
|
||||||
|
badge.dataset.count =
|
||||||
|
String(count);
|
||||||
|
|
||||||
|
badge.textContent =
|
||||||
|
count > 99
|
||||||
|
? '99+'
|
||||||
|
: String(count);
|
||||||
|
|
||||||
|
badge.hidden =
|
||||||
|
!hasUnread;
|
||||||
|
|
||||||
|
button.classList.toggle(
|
||||||
|
'chat-alert',
|
||||||
|
hasUnread
|
||||||
|
);
|
||||||
|
|
||||||
|
button.setAttribute(
|
||||||
|
'aria-label',
|
||||||
|
hasUnread
|
||||||
|
? (
|
||||||
|
`Open chat, ${count} unread `
|
||||||
|
+ (
|
||||||
|
count === 1
|
||||||
|
? 'message'
|
||||||
|
: 'messages'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
: 'Open chat'
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.handleIncomingChatNotification =
|
||||||
|
function handleIncomingChatNotification(
|
||||||
|
message
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
!message
|
||||||
|
|| typeof message !== 'object'
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Never notify for our own message.
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
Kronick.isOwnChatMessage(
|
||||||
|
message
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If chat is already visible, the user can see
|
||||||
|
* the incoming message immediately.
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
Kronick.chatWindowIsVisible()
|
||||||
|
) {
|
||||||
|
Kronick.clearChatUnreadCount();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Kronick.chatUnreadCount +=
|
||||||
|
1;
|
||||||
|
|
||||||
|
Kronick.renderChatUnreadState();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.clearChatUnreadCount =
|
||||||
|
function clearChatUnreadCount() {
|
||||||
|
Kronick.chatUnreadCount =
|
||||||
|
0;
|
||||||
|
|
||||||
|
Kronick.renderChatUnreadState();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compatibility alias for any code that was already
|
||||||
|
* calling the newer name.
|
||||||
|
*/
|
||||||
|
Kronick.clearChatNotification =
|
||||||
|
Kronick.clearChatUnreadCount;
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Send chat
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.sendChatToRoom =
|
||||||
|
function sendChatToRoom(
|
||||||
|
chatPayload
|
||||||
|
) {
|
||||||
|
const room =
|
||||||
|
Kronick.state.room;
|
||||||
|
|
||||||
|
if (!room?.localParticipant) {
|
||||||
|
alert(
|
||||||
|
'Join a room first.'
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
id:
|
||||||
|
chatPayload.id
|
||||||
|
|| Kronick.createId(),
|
||||||
|
|
||||||
|
sender:
|
||||||
|
room.localParticipant.identity
|
||||||
|
|| 'Unknown',
|
||||||
|
|
||||||
|
text:
|
||||||
|
chatPayload.text
|
||||||
|
|| '',
|
||||||
|
|
||||||
|
gifUrl:
|
||||||
|
chatPayload.gifUrl
|
||||||
|
|| null,
|
||||||
|
|
||||||
|
imageUrl:
|
||||||
|
chatPayload.imageUrl
|
||||||
|
|| null,
|
||||||
|
|
||||||
|
fileId:
|
||||||
|
chatPayload.fileId
|
||||||
|
|| null,
|
||||||
|
|
||||||
|
filename:
|
||||||
|
chatPayload.filename
|
||||||
|
|| null,
|
||||||
|
|
||||||
|
fileSize:
|
||||||
|
chatPayload.fileSize
|
||||||
|
|| null,
|
||||||
|
|
||||||
|
fileUrl:
|
||||||
|
chatPayload.fileUrl
|
||||||
|
|| null,
|
||||||
|
|
||||||
|
createdAt:
|
||||||
|
chatPayload.createdAt
|
||||||
|
|| Date.now()
|
||||||
|
};
|
||||||
|
|
||||||
|
const payload =
|
||||||
|
new TextEncoder().encode(
|
||||||
|
JSON.stringify(
|
||||||
|
message
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
room.localParticipant
|
||||||
|
.publishData(
|
||||||
|
payload,
|
||||||
|
{
|
||||||
|
reliable:
|
||||||
|
true,
|
||||||
|
|
||||||
|
topic:
|
||||||
|
Kronick.config
|
||||||
|
.DATA_TOPIC_CHAT
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.catch(error => {
|
||||||
|
console.error(
|
||||||
|
'Could not publish chat message:',
|
||||||
|
error
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Save and display locally immediately.
|
||||||
|
* LiveKit normally does not echo the packet
|
||||||
|
* back to the sending participant.
|
||||||
|
*/
|
||||||
|
Kronick.saveChatMessageToServer(
|
||||||
|
Kronick.state
|
||||||
|
.currentRoomName,
|
||||||
|
message
|
||||||
|
);
|
||||||
|
|
||||||
|
Kronick.receiveChatMessage(
|
||||||
|
message
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Receive chat
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.receiveChatMessage =
|
||||||
|
function receiveChatMessage(
|
||||||
|
message
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
!message
|
||||||
|
|| typeof message !== 'object'
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!message.id) {
|
||||||
|
message.id =
|
||||||
|
Kronick.createId();
|
||||||
|
}
|
||||||
|
|
||||||
|
Kronick.saveChatToHistory(
|
||||||
|
Kronick.state
|
||||||
|
.currentRoomName,
|
||||||
|
message
|
||||||
|
);
|
||||||
|
|
||||||
|
const chatFrame =
|
||||||
|
document.getElementById(
|
||||||
|
'chatFrame'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
chatFrame?.contentWindow
|
||||||
|
) {
|
||||||
|
chatFrame.contentWindow
|
||||||
|
.postMessage(
|
||||||
|
{
|
||||||
|
type:
|
||||||
|
'CHAT_MESSAGE',
|
||||||
|
|
||||||
|
message
|
||||||
|
},
|
||||||
|
Kronick.config
|
||||||
|
.APP_ORIGIN
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Notification ownership lives here.
|
||||||
|
* One incoming message = one notification check.
|
||||||
|
*/
|
||||||
|
Kronick
|
||||||
|
.handleIncomingChatNotification(
|
||||||
|
message
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Persist chat
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.saveChatMessageToServer =
|
||||||
|
async function saveChatMessageToServer(
|
||||||
|
roomName,
|
||||||
|
message
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
!roomName
|
||||||
|
|| !message
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response =
|
||||||
|
await fetch(
|
||||||
|
'/api/chat/message',
|
||||||
|
{
|
||||||
|
method:
|
||||||
|
'POST',
|
||||||
|
|
||||||
|
headers: {
|
||||||
|
'Content-Type':
|
||||||
|
'application/json'
|
||||||
|
},
|
||||||
|
|
||||||
|
body:
|
||||||
|
JSON.stringify({
|
||||||
|
...message,
|
||||||
|
room:
|
||||||
|
roomName
|
||||||
|
})
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
'Chat save failed: '
|
||||||
|
+ response.status
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(
|
||||||
|
'Could not save chat message:',
|
||||||
|
error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Load history
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.loadRoomChatHistory =
|
||||||
|
async function loadRoomChatHistory(
|
||||||
|
roomName
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const response =
|
||||||
|
await fetch(
|
||||||
|
'/api/chat/history/'
|
||||||
|
+ encodeURIComponent(
|
||||||
|
roomName
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
`History failed: ${
|
||||||
|
response.status
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data =
|
||||||
|
await response.json();
|
||||||
|
|
||||||
|
const messages =
|
||||||
|
Array.isArray(
|
||||||
|
data.messages
|
||||||
|
)
|
||||||
|
? data.messages
|
||||||
|
: [];
|
||||||
|
|
||||||
|
messages.forEach(
|
||||||
|
message => {
|
||||||
|
Kronick.saveChatToHistory(
|
||||||
|
roomName,
|
||||||
|
message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Kronick.sendMessagesToChatFrame(
|
||||||
|
'CHAT_HISTORY',
|
||||||
|
messages
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(
|
||||||
|
'Could not load chat history:',
|
||||||
|
error
|
||||||
|
);
|
||||||
|
|
||||||
|
Kronick
|
||||||
|
.sendChatHistoryToFrame();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Local history storage
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.saveChatToHistory =
|
||||||
|
function saveChatToHistory(
|
||||||
|
roomName,
|
||||||
|
message
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
!roomName
|
||||||
|
|| !message
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const key =
|
||||||
|
`chatHistory-${roomName}`;
|
||||||
|
|
||||||
|
let history = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
history =
|
||||||
|
JSON.parse(
|
||||||
|
localStorage.getItem(
|
||||||
|
key
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|| [];
|
||||||
|
} catch {
|
||||||
|
history = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const exists =
|
||||||
|
history.some(
|
||||||
|
item =>
|
||||||
|
item.id
|
||||||
|
=== message.id
|
||||||
|
);
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
history.push(
|
||||||
|
message
|
||||||
|
);
|
||||||
|
|
||||||
|
history =
|
||||||
|
history.slice(
|
||||||
|
-100
|
||||||
|
);
|
||||||
|
|
||||||
|
localStorage.setItem(
|
||||||
|
key,
|
||||||
|
JSON.stringify(
|
||||||
|
history
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.getChatHistory =
|
||||||
|
function getChatHistory(
|
||||||
|
roomName
|
||||||
|
) {
|
||||||
|
if (!roomName) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const key =
|
||||||
|
`chatHistory-${roomName}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return (
|
||||||
|
JSON.parse(
|
||||||
|
localStorage.getItem(
|
||||||
|
key
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|| []
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Parent -> chat iframe
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.sendMessagesToChatFrame =
|
||||||
|
function sendMessagesToChatFrame(
|
||||||
|
type,
|
||||||
|
messages
|
||||||
|
) {
|
||||||
|
const chatFrame =
|
||||||
|
document.getElementById(
|
||||||
|
'chatFrame'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
!chatFrame
|
||||||
|
?.contentWindow
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
chatFrame.contentWindow
|
||||||
|
.postMessage(
|
||||||
|
{
|
||||||
|
type,
|
||||||
|
messages
|
||||||
|
},
|
||||||
|
Kronick.config
|
||||||
|
.APP_ORIGIN
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kronick.sendChatHistoryToFrame =
|
||||||
|
function sendChatHistoryToFrame() {
|
||||||
|
const roomName =
|
||||||
|
Kronick.state
|
||||||
|
.currentRoomName;
|
||||||
|
|
||||||
|
if (!roomName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const history =
|
||||||
|
Kronick.getChatHistory(
|
||||||
|
roomName
|
||||||
|
);
|
||||||
|
|
||||||
|
Kronick.sendMessagesToChatFrame(
|
||||||
|
'CHAT_HISTORY',
|
||||||
|
history
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* =====================================================
|
||||||
|
Unread UI lifecycle
|
||||||
|
===================================================== */
|
||||||
|
|
||||||
|
Kronick.initChatUnreadUi =
|
||||||
|
function initChatUnreadUi() {
|
||||||
|
Kronick
|
||||||
|
.renderChatUnreadState();
|
||||||
|
|
||||||
|
const chatButton =
|
||||||
|
Kronick.getChatButton();
|
||||||
|
|
||||||
|
const chatWrap =
|
||||||
|
Kronick.getChatWindow();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Opening chat clears unread immediately.
|
||||||
|
*/
|
||||||
|
chatButton?.addEventListener(
|
||||||
|
'click',
|
||||||
|
() => {
|
||||||
|
window.setTimeout(
|
||||||
|
() => {
|
||||||
|
if (
|
||||||
|
Kronick
|
||||||
|
.chatWindowIsVisible()
|
||||||
|
) {
|
||||||
|
Kronick
|
||||||
|
.clearChatUnreadCount();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This also catches:
|
||||||
|
* - restore from minimized
|
||||||
|
* - opening through another control
|
||||||
|
* - window manager restore
|
||||||
|
*/
|
||||||
|
if (chatWrap) {
|
||||||
|
const observer =
|
||||||
|
new MutationObserver(
|
||||||
|
() => {
|
||||||
|
if (
|
||||||
|
Kronick
|
||||||
|
.chatWindowIsVisible()
|
||||||
|
) {
|
||||||
|
Kronick
|
||||||
|
.clearChatUnreadCount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
observer.observe(
|
||||||
|
chatWrap,
|
||||||
|
{
|
||||||
|
attributes:
|
||||||
|
true,
|
||||||
|
|
||||||
|
attributeFilter:
|
||||||
|
[
|
||||||
|
'class',
|
||||||
|
'style',
|
||||||
|
'hidden'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If the browser regains focus while chat is
|
||||||
|
* visibly open, there is nothing unread.
|
||||||
|
*/
|
||||||
|
window.addEventListener(
|
||||||
|
'focus',
|
||||||
|
() => {
|
||||||
|
if (
|
||||||
|
Kronick
|
||||||
|
.chatWindowIsVisible()
|
||||||
|
) {
|
||||||
|
Kronick
|
||||||
|
.clearChatUnreadCount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
if (
|
||||||
|
document.readyState
|
||||||
|
=== 'loading'
|
||||||
|
) {
|
||||||
|
document.addEventListener(
|
||||||
|
'DOMContentLoaded',
|
||||||
|
Kronick.initChatUnreadUi,
|
||||||
|
{
|
||||||
|
once:
|
||||||
|
true
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Kronick
|
||||||
|
.initChatUnreadUi();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user