`;
}
function getConfirmFunction(): string {
switch (currentMode) {
case 'wallet':
return 'window.confirmWalletRow()';
case 'process':
return 'window.confirmProcessRow()';
default:
return 'window.confirmRow()';
}
}
function getCancelFunction(): string {
switch (currentMode) {
case 'wallet':
return 'window.cancelWalletRow()';
case 'process':
return 'window.cancelProcessRow()';
default:
return 'window.cancelRow()';
}
}
// Fonctions de gestion des tableaux
function addRow(): void {
if (isAddingRow) return;
isAddingRow = true;
const table = document.querySelector('#pairing-table tbody');
if (!table) return;
currentRow = table.insertRow();
const cells = ['SP Address', 'Device Name', 'SP Emojis'];
cells.forEach((_, index) => {
const cell = currentRow!.insertCell();
const input = document.createElement('input');
input.type = 'text';
input.className = 'edit-input';
// Ajouter un événement pour mettre à jour automatiquement les emojis
if (index === 0) { // SP Address input
input.addEventListener('input', (e) => {
const addressInput = e.target as HTMLInputElement;
const emojiCell = currentRow!.cells[2]; // Changé à 2 pour la troisième colonne
const emojis = addressToEmoji(addressInput.value);
if (emojiCell.querySelector('input')) {
(emojiCell.querySelector('input') as HTMLInputElement).value = emojis;
}
});
}
// Désactiver l'input des emojis car il est automatique
if (index === 2) { // SP Emojis input (troisième colonne)
input.readOnly = true;
}
cell.appendChild(input);
});
const deleteCell = currentRow.insertCell();
deleteCell.style.width = '40px';
updateActionButtons();
}
function confirmRow(): void {
if (!currentRow) return;
const inputs = currentRow.getElementsByTagName('input');
const values: string[] = Array.from(inputs).map(input => input.value.trim());
// Vérification des champs vides
if (values.some(value => value === '')) {
showAlert('Please fill in all fields');
return;
}
// Vérification de la longueur de l'adresse SP
if (values[0].length !== 118) {
showAlert('SP Address must be exactly 118 characters long');
return;
}
const newRow: Row = {
column1: values[0],
column2: values[1],
column3: values[2]
};
const storageKey = STORAGE_KEYS[currentMode];
const rows: Row[] = JSON.parse(localStorage.getItem(storageKey) || '[]');
rows.push(newRow);
localStorage.setItem(storageKey, JSON.stringify(rows));
isAddingRow = false;
currentRow = null;
resetButtonContainer();
updateTableContent(rows);
}
function cancelRow(): void {
if (!currentRow) return;
currentRow.remove();
isAddingRow = false;
currentRow = null;
resetButtonContainer();
}
function resetButtonContainer(): void {
const buttonContainer = document.querySelector('.button-container');
if (!buttonContainer) return;
buttonContainer.innerHTML = `
`;
}
function deleteRow(button: HTMLButtonElement): void {
const row = button.closest('tr');
if (!row) return;
const table = row.closest('tbody');
if (!table) return;
// Vérifier le nombre de lignes restantes
const remainingRows = table.getElementsByTagName('tr').length;
if (remainingRows <= 2) {
showAlert('You must keep at least 2 devices paired');
return;
}
// Animation de suppression
row.style.transition = 'opacity 0.3s';
row.style.opacity = '0';
setTimeout(() => {
// Obtenir l'index avant la suppression
const index = Array.from(table.children).indexOf(row);
// Supprimer la ligne du DOM
row.remove();
// Mettre à jour le localStorage
const storageKey = STORAGE_KEYS[currentMode];
const rows = JSON.parse(localStorage.getItem(storageKey) || '[]');
rows.splice(index, 1);
localStorage.setItem(storageKey, JSON.stringify(rows));
}, 300);
}
function editDeviceName(cell: HTMLTableCellElement): void {
if (cell.classList.contains('editing')) return;
const currentValue = cell.textContent || '';
const input = document.createElement('input');
input.type = 'text';
input.value = currentValue;
input.className = 'edit-input';
input.addEventListener('blur', () => finishEditing(cell, input));
input.addEventListener('keypress', (e: KeyboardEvent) => {
if (e.key === 'Enter') {
finishEditing(cell, input);
}
});
cell.textContent = '';
cell.appendChild(input);
cell.classList.add('editing');
input.focus();
}
function finishEditing(cell: HTMLTableCellElement, input: HTMLInputElement): void {
const newValue = input.value.trim();
if (newValue === '') {
cell.textContent = cell.getAttribute('data-original-value') || '';
cell.classList.remove('editing');
return;
}
const row = cell.closest('tr');
if (!row) return;
const table = row.closest('tbody');
if (!table) return;
const index = Array.from(table.children).indexOf(row);
const storageKey = STORAGE_KEYS[currentMode];
const rows: Row[] = JSON.parse(localStorage.getItem(storageKey) || '[]');
if (rows[index]) {
rows[index].column3 = newValue;
localStorage.setItem(storageKey, JSON.stringify(rows));
}
cell.textContent = newValue;
cell.classList.remove('editing');
}
function showAlert(message: string): void {
// Créer la popup si elle n'existe pas
let alertPopup = document.querySelector('.alert-popup');
if (!alertPopup) {
alertPopup = document.createElement('div');
alertPopup.className = 'alert-popup';
document.body.appendChild(alertPopup);
}
// Définir le message et afficher la popup
alertPopup.textContent = message;
(alertPopup as HTMLElement).style.display = 'block';
// Cacher la popup après 3 secondes
setTimeout(() => {
(alertPopup as HTMLElement).style.display = 'none';
}, 3000);
}
// Fonction pour gérer les événements d'édition
function initializeEventListeners(): void {
const editableFields = document.querySelectorAll('.editable');
editableFields.forEach(field => {
field.addEventListener('click', () => {
if (!field.classList.contains('editing')) {
const currentValue = field.textContent || '';
const input = document.createElement('input');
input.type = 'text';
input.value = currentValue;
input.className = 'edit-input';
field.textContent = '';
field.appendChild(input);
field.classList.add('editing');
input.focus();
}
});
});
const avatarInput = document.getElementById('avatar-upload') as HTMLInputElement;
if (avatarInput) {
avatarInput.addEventListener('change', handleAvatarUpload);
}
}
// Fonction pour gérer le téléchargement de l'avatar
function handleAvatarUpload(event: Event): void {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
if (file) {
const reader = new FileReader();
reader.onload = (e: ProgressEvent) => {
const result = e.target?.result as string;
const popupAvatar = document.getElementById('popup-avatar-img') as HTMLImageElement;
const navAvatar = document.querySelector('.nav-wrapper .avatar') as HTMLImageElement;
if (popupAvatar) popupAvatar.src = result;
if (navAvatar) navAvatar.src = result;
localStorage.setItem('userAvatar', result);
};
reader.readAsDataURL(file);
}
}
// Initialisation au chargement de la page
if (!localStorage.getItem('rows')) {
localStorage.setItem('rows', JSON.stringify(defaultRows));
}
// Event Listeners et initialisation
document.addEventListener('DOMContentLoaded', () => {
//console.log("DOM Content Loaded");
initAccount();
showPairing();
});
// Ajout des fonctions manquantes
function showProcess(): void {
//console.log("showProcess called");
currentMode = 'process';
hideAllContent();
const headerTitle = document.getElementById('header-title');
if (headerTitle) headerTitle.textContent = 'Process';
const processContent = document.getElementById('process-content');
if (processContent) {
processContent.style.display = 'block';
processContent.innerHTML = `
Process
Process Name
Role
Notifications
`;
// Utiliser mockProcessRows au lieu du localStorage
updateProcessTableContent(mockProcessRows);
}
}
// Fonction utilitaire pour mettre à jour le contenu du tableau Process
function updateProcessTableContent(rows: any[]): void {
const tbody = document.querySelector('#process-table tbody');
if (!tbody) return;
tbody.innerHTML = rows.map(row => `
`).join('');
}
function showProcessNotifications(processName: string): void {
const process = mockProcessRows.find(p => p.process === processName);
if (!process) return;
const modal = document.createElement('div');
modal.className = 'notifications-modal';
let notificationsList = process.notification.messages.map(msg => `
${msg.message}
${msg.date}
`).join('');
if (process.notification.messages.length === 0) {
notificationsList = '
No notifications
';
}
modal.innerHTML = `
${processName} Notifications
${notificationsList}
`;
document.body.appendChild(modal);
// Mettre à jour le compteur de notifications
const countElement = document.querySelector(`.notification-count[data-process="${processName}"]`);
if (countElement) {
const notifCount = calculateNotifications(process.notification.messages);
countElement.textContent = `${notifCount.unread}/${notifCount.total}`;
}
const closeButton = modal.querySelector('.close-notifications');
closeButton?.addEventListener('click', () => {
modal.remove();
showProcess(); // Rafraîchir l'affichage pour mettre à jour les compteurs
});
}
function generateRandomNotification(): void {
const processes = JSON.parse(localStorage.getItem(STORAGE_KEYS.process) || '[]');
if (processes.length === 0) return;
const randomProcess = processes[Math.floor(Math.random() * processes.length)].column1;
const notification = {
message: notificationMessages[Math.floor(Math.random() * notificationMessages.length)],
timestamp: new Date().toLocaleString(),
isRead: false
};
if (!mockNotifications[randomProcess]) {
mockNotifications[randomProcess] = [];
}
mockNotifications[randomProcess].push(notification);
if (currentMode === 'process') {
showProcess();
}
}
function handleLogout(): void {
localStorage.clear();
window.location.href = '../login/login.html';
}
// Initialisation des notifications
document.addEventListener('DOMContentLoaded', () => {
loadUserInfo();
setInterval(generateRandomNotification, 60000);
});
// Fonctions de gestion des contrats
function showContractPopup(contractId: string): void {
const contract = mockContracts[contractId as keyof typeof mockContracts];
if (!contract) {
showAlert('Contract not found');
return;
}
const popupHTML = `
${contract.title}
Date: ${contract.date}
Parties involved:
${contract.parties.map(party => `
${party}
`).join('')}
Terms and Conditions:
${contract.terms.map(term => `
${term}
`).join('')}
Details:
${contract.content}
`;
document.body.insertAdjacentHTML('beforeend', popupHTML);
}
// Fonction utilitaire pour cacher tous les contenus
function hideAllContent(): void {
const contents = ['pairing-content', 'wallet-content', 'process-content', 'data-content'];
contents.forEach(id => {
const element = document.getElementById(id);
if (element) {
element.style.display = 'none';
}
});
}
// Fonctions d'affichage des sections
function showPairing(): void {
isAddingRow = false;
currentRow = null;
currentMode = 'pairing';
// Cacher tous les contenus
hideAllContent();
// Mettre à jour le titre
const headerElement = document.getElementById('parameter-header');
if (headerElement) {
headerElement.textContent = 'Pairing';
}
// Afficher le contenu de pairing
const pairingContent = document.getElementById('pairing-content');
if (pairingContent) {
pairingContent.style.display = 'block';
pairingContent.innerHTML = `
Pairing
SP Address
Device Name
SP Emojis
`;
// Mettre à jour le contenu du tableau
const rows = JSON.parse(localStorage.getItem(STORAGE_KEYS.pairing) || '[]');
updateTableContent(rows);
}
}
function showWallet(): void {
isAddingRow = false;
currentRow = null;
currentMode = 'wallet';
hideAllContent();
// Mettre à jour le titre
const headerTitle = document.getElementById('header-title');
if (headerTitle) headerTitle.textContent = 'Wallet';
const walletContent = document.getElementById('wallet-content');
if (!walletContent) return;
walletContent.style.display = 'block'; // Ajout de cette ligne pour rendre visible
walletContent.innerHTML = `