Upload files to "kronic-chat/js"
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,219 @@
|
||||
window.Kronick = window.Kronick || {};
|
||||
|
||||
Kronick.saveJoinInfo = function saveJoinInfo(
|
||||
roomName,
|
||||
userName,
|
||||
avatarUrl
|
||||
) {
|
||||
localStorage.setItem(
|
||||
'lastRoomName',
|
||||
roomName
|
||||
);
|
||||
|
||||
localStorage.setItem(
|
||||
'lastUserName',
|
||||
userName
|
||||
);
|
||||
|
||||
localStorage.setItem(
|
||||
'lastAvatarUrl',
|
||||
avatarUrl
|
||||
);
|
||||
|
||||
Kronick.addToSavedList(
|
||||
'savedRoomList',
|
||||
roomName
|
||||
);
|
||||
|
||||
Kronick.addToSavedList(
|
||||
'savedNameList',
|
||||
userName
|
||||
);
|
||||
|
||||
if (avatarUrl) {
|
||||
Kronick.addToSavedList(
|
||||
'savedAvatarList',
|
||||
avatarUrl
|
||||
);
|
||||
}
|
||||
|
||||
Kronick.populateSavedLists();
|
||||
};
|
||||
|
||||
Kronick.loadSavedJoinInfo =
|
||||
function loadSavedJoinInfo() {
|
||||
const roomInput =
|
||||
document.getElementById('roomName');
|
||||
|
||||
const userInput =
|
||||
document.getElementById('userName');
|
||||
|
||||
const avatarInput =
|
||||
document.getElementById('avatarUrl');
|
||||
|
||||
if (roomInput) {
|
||||
roomInput.value =
|
||||
localStorage.getItem(
|
||||
'lastRoomName'
|
||||
) || '';
|
||||
}
|
||||
|
||||
if (userInput) {
|
||||
userInput.value =
|
||||
localStorage.getItem(
|
||||
'lastUserName'
|
||||
) || '';
|
||||
}
|
||||
|
||||
if (avatarInput) {
|
||||
avatarInput.value =
|
||||
localStorage.getItem(
|
||||
'lastAvatarUrl'
|
||||
) || '';
|
||||
}
|
||||
};
|
||||
|
||||
Kronick.addToSavedList =
|
||||
function addToSavedList(key, value) {
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
|
||||
let list = [];
|
||||
|
||||
try {
|
||||
list = JSON.parse(
|
||||
localStorage.getItem(key)
|
||||
) || [];
|
||||
} catch {
|
||||
list = [];
|
||||
}
|
||||
|
||||
list = list.filter(
|
||||
item => item !== value
|
||||
);
|
||||
|
||||
list.unshift(value);
|
||||
list = list.slice(0, 10);
|
||||
|
||||
localStorage.setItem(
|
||||
key,
|
||||
JSON.stringify(list)
|
||||
);
|
||||
};
|
||||
|
||||
Kronick.populateSavedLists =
|
||||
function populateSavedLists() {
|
||||
Kronick.populateDatalist(
|
||||
'savedRooms',
|
||||
'savedRoomList'
|
||||
);
|
||||
|
||||
Kronick.populateDatalist(
|
||||
'savedNames',
|
||||
'savedNameList'
|
||||
);
|
||||
|
||||
Kronick.populateDatalist(
|
||||
'savedAvatars',
|
||||
'savedAvatarList'
|
||||
);
|
||||
};
|
||||
|
||||
Kronick.populateDatalist =
|
||||
function populateDatalist(
|
||||
datalistId,
|
||||
storageKey
|
||||
) {
|
||||
const datalist =
|
||||
document.getElementById(
|
||||
datalistId
|
||||
);
|
||||
|
||||
if (!datalist) {
|
||||
return;
|
||||
}
|
||||
|
||||
let list = [];
|
||||
|
||||
try {
|
||||
list = JSON.parse(
|
||||
localStorage.getItem(
|
||||
storageKey
|
||||
)
|
||||
) || [];
|
||||
} catch {
|
||||
list = [];
|
||||
}
|
||||
|
||||
datalist.innerHTML = '';
|
||||
|
||||
list.forEach(value => {
|
||||
const option =
|
||||
document.createElement(
|
||||
'option'
|
||||
);
|
||||
|
||||
option.value = value;
|
||||
datalist.appendChild(option);
|
||||
});
|
||||
};
|
||||
|
||||
Kronick.savePosition =
|
||||
function savePosition(key, position) {
|
||||
localStorage.setItem(
|
||||
`position-${key}`,
|
||||
JSON.stringify(position)
|
||||
);
|
||||
};
|
||||
|
||||
Kronick.getSavedPosition =
|
||||
function getSavedPosition(key) {
|
||||
const saved =
|
||||
localStorage.getItem(
|
||||
`position-${key}`
|
||||
);
|
||||
|
||||
if (!saved) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed =
|
||||
JSON.parse(saved);
|
||||
|
||||
if (
|
||||
typeof parsed.x !== 'number'
|
||||
|| typeof parsed.y !== 'number'
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parsed;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Kronick.getSavedVolume =
|
||||
function getSavedVolume(participantKey) {
|
||||
const saved =
|
||||
localStorage.getItem(
|
||||
`volume-${participantKey}`
|
||||
);
|
||||
|
||||
if (saved === null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const volume = Number(saved);
|
||||
|
||||
if (Number.isNaN(volume)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return Math.max(
|
||||
0,
|
||||
Math.min(1, volume)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,480 @@
|
||||
window.Kronick = window.Kronick || {};
|
||||
|
||||
Kronick.showToolWindow =
|
||||
function showToolWindow(elementId) {
|
||||
const windowEl =
|
||||
document.getElementById(elementId);
|
||||
|
||||
if (!windowEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
windowEl.classList.remove('window-minimized');
|
||||
windowEl.style.display = 'flex';
|
||||
Kronick.removeWindowTaskTab?.(elementId);
|
||||
|
||||
Kronick.bringToolWindowToFront(
|
||||
windowEl
|
||||
);
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
Kronick.keepWindowInViewport(
|
||||
windowEl
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
Kronick.hideToolWindow =
|
||||
function hideToolWindow(elementId) {
|
||||
const windowEl =
|
||||
document.getElementById(elementId);
|
||||
|
||||
if (!windowEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
windowEl.classList.remove('window-minimized', 'window-maximized');
|
||||
windowEl.style.display = 'none';
|
||||
Kronick.removeWindowTaskTab?.(elementId);
|
||||
};
|
||||
|
||||
Kronick.bringToolWindowToFront =
|
||||
function bringToolWindowToFront(windowEl) {
|
||||
if (!windowEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
Kronick.state.highestWindowZIndex += 1;
|
||||
|
||||
windowEl.style.zIndex =
|
||||
String(
|
||||
Kronick.state
|
||||
.highestWindowZIndex
|
||||
);
|
||||
};
|
||||
|
||||
Kronick.keepWindowInViewport =
|
||||
function keepWindowInViewport(windowEl) {
|
||||
if (
|
||||
!windowEl
|
||||
|| windowEl.style.display === 'none'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rect =
|
||||
windowEl.getBoundingClientRect();
|
||||
|
||||
const maxLeft = Math.max(
|
||||
0,
|
||||
window.innerWidth
|
||||
- Math.min(
|
||||
rect.width,
|
||||
window.innerWidth
|
||||
)
|
||||
);
|
||||
|
||||
const maxTop = Math.max(
|
||||
0,
|
||||
window.innerHeight - 44
|
||||
);
|
||||
|
||||
const newLeft = Math.max(
|
||||
0,
|
||||
Math.min(rect.left, maxLeft)
|
||||
);
|
||||
|
||||
const newTop = Math.max(
|
||||
0,
|
||||
Math.min(rect.top, maxTop)
|
||||
);
|
||||
|
||||
windowEl.style.left =
|
||||
`${newLeft}px`;
|
||||
|
||||
windowEl.style.top =
|
||||
`${newTop}px`;
|
||||
|
||||
windowEl.style.right = 'auto';
|
||||
windowEl.style.bottom = 'auto';
|
||||
windowEl.style.transform = 'none';
|
||||
};
|
||||
|
||||
Kronick.keepVisibleWindowsInViewport =
|
||||
function keepVisibleWindowsInViewport() {
|
||||
document
|
||||
.querySelectorAll(
|
||||
'.tool-frame-wrap, '
|
||||
+ '.shared-media-wrap'
|
||||
)
|
||||
.forEach(windowEl => {
|
||||
if (
|
||||
windowEl.style.display
|
||||
!== 'none'
|
||||
) {
|
||||
Kronick.keepWindowInViewport(
|
||||
windowEl
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Kronick.makeToolWindowDraggable =
|
||||
function makeToolWindowDraggable(
|
||||
windowEl,
|
||||
headerEl,
|
||||
storageKey
|
||||
) {
|
||||
if (!windowEl || !headerEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const saved =
|
||||
localStorage.getItem(storageKey);
|
||||
|
||||
if (saved) {
|
||||
try {
|
||||
const pos = JSON.parse(saved);
|
||||
|
||||
windowEl.style.left =
|
||||
`${Number(pos.left || 0)}px`;
|
||||
|
||||
windowEl.style.top =
|
||||
`${Number(pos.top || 0)}px`;
|
||||
|
||||
windowEl.style.right = 'auto';
|
||||
windowEl.style.bottom = 'auto';
|
||||
windowEl.style.transform = 'none';
|
||||
} catch {
|
||||
localStorage.removeItem(
|
||||
storageKey
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let dragging = false;
|
||||
let startX = 0;
|
||||
let startY = 0;
|
||||
let startLeft = 0;
|
||||
let startTop = 0;
|
||||
|
||||
headerEl.addEventListener(
|
||||
'mousedown',
|
||||
event => {
|
||||
if (
|
||||
event.button !== 0
|
||||
|| event.target.closest(
|
||||
'button'
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
dragging = true;
|
||||
|
||||
const rect =
|
||||
windowEl
|
||||
.getBoundingClientRect();
|
||||
|
||||
startX = event.clientX;
|
||||
startY = event.clientY;
|
||||
startLeft = rect.left;
|
||||
startTop = rect.top;
|
||||
|
||||
windowEl.style.left =
|
||||
`${startLeft}px`;
|
||||
|
||||
windowEl.style.top =
|
||||
`${startTop}px`;
|
||||
|
||||
windowEl.style.right = 'auto';
|
||||
windowEl.style.bottom = 'auto';
|
||||
windowEl.style.transform = 'none';
|
||||
|
||||
Kronick.bringToolWindowToFront(
|
||||
windowEl
|
||||
);
|
||||
|
||||
document.body.classList.add(
|
||||
'window-dragging'
|
||||
);
|
||||
|
||||
event.preventDefault();
|
||||
}
|
||||
);
|
||||
|
||||
document.addEventListener(
|
||||
'mousemove',
|
||||
event => {
|
||||
if (!dragging) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rect =
|
||||
windowEl
|
||||
.getBoundingClientRect();
|
||||
|
||||
const maxLeft = Math.max(
|
||||
0,
|
||||
window.innerWidth
|
||||
- rect.width
|
||||
);
|
||||
|
||||
const maxTop = Math.max(
|
||||
0,
|
||||
window.innerHeight - 44
|
||||
);
|
||||
|
||||
const newLeft = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
maxLeft,
|
||||
startLeft
|
||||
+ event.clientX
|
||||
- startX
|
||||
)
|
||||
);
|
||||
|
||||
const newTop = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
maxTop,
|
||||
startTop
|
||||
+ event.clientY
|
||||
- startY
|
||||
)
|
||||
);
|
||||
|
||||
windowEl.style.left =
|
||||
`${newLeft}px`;
|
||||
|
||||
windowEl.style.top =
|
||||
`${newTop}px`;
|
||||
}
|
||||
);
|
||||
|
||||
document.addEventListener(
|
||||
'mouseup',
|
||||
() => {
|
||||
if (!dragging) {
|
||||
return;
|
||||
}
|
||||
|
||||
dragging = false;
|
||||
|
||||
document.body.classList.remove(
|
||||
'window-dragging'
|
||||
);
|
||||
|
||||
localStorage.setItem(
|
||||
storageKey,
|
||||
JSON.stringify({
|
||||
left: parseInt(
|
||||
windowEl.style.left
|
||||
|| '0',
|
||||
10
|
||||
),
|
||||
|
||||
top: parseInt(
|
||||
windowEl.style.top
|
||||
|| '0',
|
||||
10
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
Kronick.openMusicBrowser =
|
||||
function openMusicBrowser() {
|
||||
Kronick.showToolWindow(
|
||||
'musicFrameWrap'
|
||||
);
|
||||
};
|
||||
|
||||
Kronick.closeMusicBrowser =
|
||||
function closeMusicBrowser() {
|
||||
Kronick.hideToolWindow(
|
||||
'musicFrameWrap'
|
||||
);
|
||||
};
|
||||
|
||||
Kronick.openGiphyBrowser =
|
||||
function openGiphyBrowser(
|
||||
targetMode = 'room'
|
||||
) {
|
||||
Kronick.state.giphyTargetMode =
|
||||
targetMode;
|
||||
|
||||
Kronick.showToolWindow(
|
||||
'giphyFrameWrap'
|
||||
);
|
||||
|
||||
const giphyFrame =
|
||||
document.getElementById(
|
||||
'giphyFrame'
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
if (giphyFrame?.contentWindow) {
|
||||
giphyFrame.contentWindow
|
||||
.postMessage(
|
||||
{
|
||||
type:
|
||||
'KRONICK_GIPHY_MODE',
|
||||
|
||||
mode:
|
||||
Kronick.state
|
||||
.giphyTargetMode
|
||||
},
|
||||
Kronick.config.APP_ORIGIN
|
||||
);
|
||||
}
|
||||
}, 100);
|
||||
};
|
||||
|
||||
Kronick.closeGiphyBrowser =
|
||||
function closeGiphyBrowser() {
|
||||
Kronick.hideToolWindow(
|
||||
'giphyFrameWrap'
|
||||
);
|
||||
|
||||
Kronick.state.giphyTargetMode =
|
||||
'room';
|
||||
};
|
||||
|
||||
Kronick.openChatWindow =
|
||||
function openChatWindow() {
|
||||
Kronick.showToolWindow(
|
||||
'chatFrameWrap'
|
||||
);
|
||||
|
||||
Kronick.sendChatHistoryToFrame();
|
||||
Kronick.clearChatUnreadCount();
|
||||
};
|
||||
|
||||
Kronick.closeChatWindow =
|
||||
function closeChatWindow() {
|
||||
Kronick.hideToolWindow(
|
||||
'chatFrameWrap'
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
/* Lightweight desktop-style window management. */
|
||||
Kronick.getWindowTaskbar = function getWindowTaskbar() {
|
||||
return document.getElementById('windowTaskbar');
|
||||
};
|
||||
|
||||
Kronick.removeWindowTaskTab = function removeWindowTaskTab(elementId) {
|
||||
document.querySelector(`[data-window-tab-for="${elementId}"]`)?.remove();
|
||||
};
|
||||
|
||||
Kronick.addWindowTaskTab = function addWindowTaskTab(windowEl) {
|
||||
const taskbar = Kronick.getWindowTaskbar();
|
||||
if (!taskbar || !windowEl || !windowEl.id) return;
|
||||
Kronick.removeWindowTaskTab(windowEl.id);
|
||||
const button = document.createElement('button');
|
||||
button.type = 'button';
|
||||
button.className = 'window-task-tab';
|
||||
button.dataset.windowTabFor = windowEl.id;
|
||||
button.innerHTML = `<span>${windowEl.dataset.windowIcon || '□'}</span><span>${windowEl.dataset.windowTitle || 'Window'}</span>`;
|
||||
button.addEventListener('click', () => Kronick.restoreToolWindow(windowEl.id));
|
||||
taskbar.appendChild(button);
|
||||
};
|
||||
|
||||
Kronick.minimizeToolWindow = function minimizeToolWindow(elementId) {
|
||||
const windowEl = document.getElementById(elementId);
|
||||
if (!windowEl) return;
|
||||
windowEl.classList.add('window-minimized');
|
||||
windowEl.style.display = 'none';
|
||||
Kronick.addWindowTaskTab(windowEl);
|
||||
};
|
||||
|
||||
Kronick.restoreToolWindow = function restoreToolWindow(elementId) {
|
||||
const windowEl = document.getElementById(elementId);
|
||||
if (!windowEl) return;
|
||||
windowEl.classList.remove('window-minimized');
|
||||
windowEl.style.display = 'flex';
|
||||
Kronick.removeWindowTaskTab(elementId);
|
||||
Kronick.bringToolWindowToFront(windowEl);
|
||||
requestAnimationFrame(() => Kronick.keepWindowInViewport(windowEl));
|
||||
if (elementId === 'chatFrameWrap') Kronick.clearChatUnreadCount?.();
|
||||
};
|
||||
|
||||
Kronick.toggleMaximizeToolWindow = function toggleMaximizeToolWindow(elementId) {
|
||||
const windowEl = document.getElementById(elementId);
|
||||
if (!windowEl) return;
|
||||
if (windowEl.classList.contains('window-maximized')) {
|
||||
const saved = windowEl.dataset.normalBounds;
|
||||
windowEl.classList.remove('window-maximized');
|
||||
if (saved) {
|
||||
try {
|
||||
const b = JSON.parse(saved);
|
||||
for (const key of ['left','top','width','height']) {
|
||||
if (b[key]) windowEl.style[key] = b[key];
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
Kronick.keepWindowInViewport(windowEl);
|
||||
return;
|
||||
}
|
||||
const r = windowEl.getBoundingClientRect();
|
||||
windowEl.dataset.normalBounds = JSON.stringify({left:`${r.left}px`,top:`${r.top}px`,width:`${r.width}px`,height:`${r.height}px`});
|
||||
windowEl.classList.add('window-maximized');
|
||||
windowEl.style.removeProperty('left');
|
||||
windowEl.style.removeProperty('top');
|
||||
windowEl.style.removeProperty('width');
|
||||
windowEl.style.removeProperty('height');
|
||||
Kronick.bringToolWindowToFront(windowEl);
|
||||
};
|
||||
|
||||
Kronick.makeToolWindowResizable = function makeToolWindowResizable(windowEl) {
|
||||
if (!windowEl || windowEl.querySelector(':scope > .window-resize-handle')) return;
|
||||
const handle = document.createElement('div');
|
||||
handle.className = 'window-resize-handle';
|
||||
handle.setAttribute('aria-hidden', 'true');
|
||||
windowEl.appendChild(handle);
|
||||
let active = false, sx = 0, sy = 0, sw = 0, sh = 0;
|
||||
handle.addEventListener('pointerdown', event => {
|
||||
if (windowEl.classList.contains('window-maximized')) return;
|
||||
active = true; sx = event.clientX; sy = event.clientY;
|
||||
const r = windowEl.getBoundingClientRect(); sw = r.width; sh = r.height;
|
||||
handle.setPointerCapture?.(event.pointerId);
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
});
|
||||
handle.addEventListener('pointermove', event => {
|
||||
if (!active) return;
|
||||
const maxW = Math.max(260, window.innerWidth - windowEl.getBoundingClientRect().left - 4);
|
||||
const maxH = Math.max(200, window.innerHeight - windowEl.getBoundingClientRect().top - 4);
|
||||
windowEl.style.width = `${Math.min(maxW, Math.max(280, sw + event.clientX - sx))}px`;
|
||||
windowEl.style.height = `${Math.min(maxH, Math.max(220, sh + event.clientY - sy))}px`;
|
||||
});
|
||||
const stop = () => { active = false; };
|
||||
handle.addEventListener('pointerup', stop);
|
||||
handle.addEventListener('pointercancel', stop);
|
||||
};
|
||||
|
||||
Kronick.initializeWindowManager = function initializeWindowManager() {
|
||||
document.querySelectorAll('.tool-frame-wrap, .shared-media-wrap').forEach(windowEl => {
|
||||
const header = windowEl.querySelector('.tool-frame-header');
|
||||
if (!header || header.querySelector('.window-manager-actions')) {
|
||||
Kronick.makeToolWindowResizable(windowEl);
|
||||
return;
|
||||
}
|
||||
const existingClose = header.querySelector('.window-close-button');
|
||||
const actions = document.createElement('div');
|
||||
actions.className = 'window-manager-actions';
|
||||
const min = document.createElement('button');
|
||||
min.type = 'button'; min.className = 'window-control-button'; min.title = 'Minimize'; min.textContent = '−';
|
||||
min.addEventListener('click', e => { e.stopPropagation(); Kronick.minimizeToolWindow(windowEl.id); });
|
||||
const max = document.createElement('button');
|
||||
max.type = 'button'; max.className = 'window-control-button'; max.title = 'Maximize / restore'; max.textContent = '▢';
|
||||
max.addEventListener('click', e => { e.stopPropagation(); Kronick.toggleMaximizeToolWindow(windowEl.id); });
|
||||
actions.append(min, max);
|
||||
if (existingClose) actions.appendChild(existingClose);
|
||||
header.appendChild(actions);
|
||||
Kronick.makeToolWindowResizable(windowEl);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,175 @@
|
||||
// =========================================================
|
||||
// Kronic UI fixes - 2026-10-31
|
||||
// =========================================================
|
||||
|
||||
window.Kronick =
|
||||
window.Kronick || {};
|
||||
|
||||
|
||||
/*
|
||||
* Flash only the top chat icon when a new chat notification
|
||||
* arrives while chat is not visibly open.
|
||||
*/
|
||||
(function setupChatNotificationFlash() {
|
||||
const chatButton =
|
||||
document.getElementById(
|
||||
'chatToolButton'
|
||||
);
|
||||
|
||||
const chatWrap =
|
||||
document.getElementById(
|
||||
'chatFrameWrap'
|
||||
);
|
||||
|
||||
const chatFrame =
|
||||
document.getElementById(
|
||||
'chatFrame'
|
||||
);
|
||||
|
||||
if (!chatButton) {
|
||||
return;
|
||||
}
|
||||
|
||||
function chatIsVisible() {
|
||||
if (!chatWrap) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const style =
|
||||
window.getComputedStyle(
|
||||
chatWrap
|
||||
);
|
||||
|
||||
return (
|
||||
style.display !== 'none'
|
||||
&& style.visibility !== 'hidden'
|
||||
&& !chatWrap.classList.contains(
|
||||
'window-minimized'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function clearChatFlash() {
|
||||
chatButton.classList.remove(
|
||||
'chat-notification-pulse'
|
||||
);
|
||||
}
|
||||
|
||||
function flashChatIcon() {
|
||||
if (
|
||||
!chatIsVisible()
|
||||
) {
|
||||
chatButton.classList.add(
|
||||
'chat-notification-pulse'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
chatButton.addEventListener(
|
||||
'click',
|
||||
clearChatFlash
|
||||
);
|
||||
|
||||
window.addEventListener(
|
||||
'message',
|
||||
event => {
|
||||
if (
|
||||
event.origin
|
||||
!== Kronick.config?.APP_ORIGIN
|
||||
&& event.origin
|
||||
!== window.location.origin
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
chatFrame?.contentWindow
|
||||
&& event.source
|
||||
!== chatFrame.contentWindow
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
event.data?.type
|
||||
=== 'KRONICK_CHAT_NEW_MESSAGE'
|
||||
) {
|
||||
flashChatIcon();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
* Existing chat-controller code may maintain the unread
|
||||
* badge. Observe it too, but display only the icon pulse.
|
||||
*/
|
||||
const unread =
|
||||
document.getElementById(
|
||||
'chatUnreadCount'
|
||||
);
|
||||
|
||||
if (unread) {
|
||||
const observer =
|
||||
new MutationObserver(
|
||||
() => {
|
||||
const count =
|
||||
Number(
|
||||
unread.dataset.count
|
||||
|| unread.textContent
|
||||
|| 0
|
||||
);
|
||||
|
||||
if (
|
||||
count > 0
|
||||
&& !chatIsVisible()
|
||||
) {
|
||||
flashChatIcon();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
observer.observe(
|
||||
unread,
|
||||
{
|
||||
attributes:
|
||||
true,
|
||||
childList:
|
||||
true,
|
||||
characterData:
|
||||
true,
|
||||
subtree:
|
||||
true
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* If another part of the app opens the chat window,
|
||||
* clear the pulse as soon as it becomes visible.
|
||||
*/
|
||||
if (chatWrap) {
|
||||
const observer =
|
||||
new MutationObserver(
|
||||
() => {
|
||||
if (
|
||||
chatIsVisible()
|
||||
) {
|
||||
clearChatFlash();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
observer.observe(
|
||||
chatWrap,
|
||||
{
|
||||
attributes:
|
||||
true,
|
||||
attributeFilter:
|
||||
[
|
||||
'style',
|
||||
'class'
|
||||
]
|
||||
}
|
||||
);
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,55 @@
|
||||
window.Kronick = window.Kronick || {};
|
||||
Kronick.state = Kronick.state || {};
|
||||
|
||||
Kronick.createId = function createId() {
|
||||
if (
|
||||
window.crypto
|
||||
&& typeof window.crypto.randomUUID === 'function'
|
||||
) {
|
||||
return window.crypto.randomUUID();
|
||||
}
|
||||
|
||||
return [
|
||||
Date.now(),
|
||||
Math.random().toString(36).slice(2)
|
||||
].join('-');
|
||||
};
|
||||
|
||||
Kronick.escapeHtml = function escapeHtml(value) {
|
||||
return String(value)
|
||||
.replaceAll('&', '&')
|
||||
.replaceAll('<', '<')
|
||||
.replaceAll('>', '>')
|
||||
.replaceAll('"', '"')
|
||||
.replaceAll("'", ''');
|
||||
};
|
||||
|
||||
Kronick.escapeAttribute = function escapeAttribute(value) {
|
||||
return Kronick.escapeHtml(value);
|
||||
};
|
||||
|
||||
Kronick.setStatus = function setStatus(
|
||||
message,
|
||||
state = ''
|
||||
) {
|
||||
const statusBar =
|
||||
document.getElementById('statusBar');
|
||||
|
||||
const statusIndicator =
|
||||
document.getElementById('statusIndicator');
|
||||
|
||||
if (statusBar) {
|
||||
statusBar.innerText = message;
|
||||
}
|
||||
|
||||
if (!statusIndicator) {
|
||||
return;
|
||||
}
|
||||
|
||||
statusIndicator.className =
|
||||
'status-indicator';
|
||||
|
||||
if (state) {
|
||||
statusIndicator.classList.add(state);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user