Upload files to "kronic-chat/js"
This commit is contained in:
@@ -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)
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user