save
This commit is contained in:
641
js/app.js
641
js/app.js
@@ -11,44 +11,85 @@ class AccountManager {
|
||||
}
|
||||
|
||||
this.currentUser = currentUser;
|
||||
this.accounts = this.loadFromStorage('accounts') || [];
|
||||
this.applications = this.loadFromStorage('applications') || [
|
||||
{ id: 1, name: 'AWS', type: 'Cloud', status: 'online', icon: 'cloud' },
|
||||
{ id: 2, name: 'GitHub', type: 'VCS', status: 'online', icon: 'code' },
|
||||
{ id: 3, name: 'Google Workspace', type: 'Collaboration', status: 'online', icon: 'mail' },
|
||||
{ id: 4, name: 'Nginx Proxy', type: 'Infra', status: 'offline', icon: 'dns' },
|
||||
];
|
||||
this.accounts = [];
|
||||
this.applications = [];
|
||||
this.apiBase = '/api';
|
||||
this.currentPage = 'dashboard';
|
||||
this.init();
|
||||
this.initPromise = this.init();
|
||||
}
|
||||
|
||||
init() {
|
||||
getUserId() {
|
||||
const u = this.currentUser;
|
||||
const detected = u?.UserId ?? u?.userId ?? u?.id ?? u?.ID ?? u?.userid ?? u?.user_id ?? u?.user?.UserId ?? u?.user?.userId;
|
||||
// Fallback: if only username/role exist (no id), use default admin id = 1
|
||||
return detected ?? 1;
|
||||
}
|
||||
|
||||
async init() {
|
||||
await this.fetchApplications();
|
||||
await this.fetchAccounts();
|
||||
this.setupEventListeners();
|
||||
// Render dashboard content
|
||||
this.loadModals(); // Load modals từ file riêng
|
||||
// Render dashboard content (only for index.html)
|
||||
const mainContent = document.getElementById('mainContent');
|
||||
if (mainContent) {
|
||||
if (mainContent && window.location.pathname.endsWith('index.html')) {
|
||||
mainContent.innerHTML = this.renderDashboard();
|
||||
}
|
||||
}
|
||||
|
||||
async fetchApplications() {
|
||||
const res = await fetch(`${this.apiBase}/applications`);
|
||||
const data = await res.json();
|
||||
if (data.success) {
|
||||
this.applications = data.data;
|
||||
} else {
|
||||
console.error('Load applications failed:', data.message);
|
||||
}
|
||||
}
|
||||
|
||||
async fetchAccounts() {
|
||||
const userId = this.getUserId();
|
||||
if (!userId) return;
|
||||
const res = await fetch(`${this.apiBase}/accounts/user/${userId}`);
|
||||
const data = await res.json();
|
||||
if (data.success) {
|
||||
this.accounts = data.data;
|
||||
} else {
|
||||
console.error('Load accounts failed:', data.message);
|
||||
}
|
||||
}
|
||||
|
||||
async loadModals() {
|
||||
try {
|
||||
const response = await fetch('../modals.html');
|
||||
const modalsHTML = await response.text();
|
||||
const modalsContainer = document.getElementById('modalsContainer');
|
||||
if (modalsContainer) {
|
||||
modalsContainer.innerHTML = modalsHTML;
|
||||
// Re-attach event listeners after modals are loaded
|
||||
setTimeout(() => {
|
||||
this.setupAccountRowListeners();
|
||||
this.setupFormListeners();
|
||||
}, 50);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Lỗi load modals:', error);
|
||||
}
|
||||
}
|
||||
|
||||
setupEventListeners() {
|
||||
// Account modal
|
||||
const addAccountBtn = document.getElementById('addAccountBtn');
|
||||
if (addAccountBtn) {
|
||||
addAccountBtn.addEventListener('click', () => this.openAccountModal());
|
||||
}
|
||||
|
||||
// Application modal
|
||||
const addAppBtn = document.getElementById('addAppBtn');
|
||||
if (addAppBtn) {
|
||||
addAppBtn.addEventListener('click', () => this.openAppModal());
|
||||
}
|
||||
|
||||
// Modal close buttons
|
||||
document.querySelectorAll('[data-close-modal]').forEach(btn => {
|
||||
btn.addEventListener('click', () => this.closeModals());
|
||||
});
|
||||
|
||||
// Close with Escape key
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape') {
|
||||
this.closeModals();
|
||||
}
|
||||
});
|
||||
|
||||
// Form submissions
|
||||
const accountForm = document.getElementById('accountForm');
|
||||
if (accountForm) {
|
||||
@@ -73,13 +114,34 @@ class AccountManager {
|
||||
this.setupAccountRowListeners();
|
||||
}
|
||||
|
||||
setupFormListeners() {
|
||||
const accountForm = document.getElementById('accountForm');
|
||||
if (accountForm) {
|
||||
accountForm.addEventListener('submit', (e) => this.handleAccountSubmit(e));
|
||||
}
|
||||
|
||||
const appForm = document.getElementById('appForm');
|
||||
if (appForm) {
|
||||
appForm.addEventListener('submit', (e) => this.handleAppSubmit(e));
|
||||
}
|
||||
|
||||
// Close when clicking backdrop outside modal content
|
||||
document.querySelectorAll('.modal-backdrop').forEach(backdrop => {
|
||||
backdrop.addEventListener('click', (evt) => {
|
||||
if (evt.target === backdrop) {
|
||||
this.closeModals();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
updateAccountDisplay() {
|
||||
// Use the logged-in user from constructor
|
||||
const usernameEl = document.getElementById('accountUsername');
|
||||
const roleEl = document.getElementById('accountRole');
|
||||
|
||||
if (usernameEl) usernameEl.textContent = this.currentUser?.username || 'User';
|
||||
if (roleEl) roleEl.textContent = this.currentUser?.role || 'Administrator';
|
||||
|
||||
if (usernameEl) usernameEl.textContent = this.currentUser?.username || this.currentUser?.Username || 'User';
|
||||
if (roleEl) roleEl.textContent = this.currentUser?.role || this.currentUser?.Role || 'Administrator';
|
||||
}
|
||||
|
||||
handleLogout() {
|
||||
@@ -113,7 +175,7 @@ class AccountManager {
|
||||
<span class="text-[10px] font-bold text-on-surface-variant uppercase tracking-wider mb-2">Applications</span>
|
||||
<div class="flex items-baseline justify-between">
|
||||
<span class="text-2xl font-black text-on-surface">${this.applications.length}</span>
|
||||
<span class="text-[10px] font-bold text-on-surface-variant">${this.applications.filter(a => a.status === 'online').length} Active</span>
|
||||
<span class="text-[10px] font-bold text-on-surface-variant">${this.applications.filter(a => (a.Status || a.status) === 'online').length} Active</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-surface-container-lowest p-4 rounded-xl border border-outline-variant/15 flex flex-col">
|
||||
@@ -148,14 +210,18 @@ class AccountManager {
|
||||
</div>
|
||||
${this.accounts.length > 0 ? `
|
||||
<div class="flex-1 overflow-y-auto space-y-2">
|
||||
${this.accounts.slice(-5).reverse().map(acc => `
|
||||
${this.accounts.slice(-5).reverse().map(acc => {
|
||||
const username = acc.AccountUsername || acc.username || '-';
|
||||
const service = acc.AppName || acc.service || '-';
|
||||
const owner = acc.Email || acc.owner || this.currentUser?.Username || this.currentUser?.username || '-';
|
||||
return `
|
||||
<div class="flex gap-3 p-3 bg-surface-container-low/50 rounded-lg border-l-2 border-primary/50">
|
||||
<div class="flex-1 min-w-0">
|
||||
<p class="text-[11px] font-bold text-on-surface truncate">${acc.username}</p>
|
||||
<p class="text-[9px] text-on-surface-variant">${acc.service} • ${acc.owner}</p>
|
||||
<p class="text-[11px] font-bold text-on-surface truncate">${username}</p>
|
||||
<p class="text-[9px] text-on-surface-variant">${service} • ${owner}</p>
|
||||
</div>
|
||||
</div>
|
||||
`).join('')}
|
||||
`;}).join('')}
|
||||
</div>
|
||||
` : `
|
||||
<div class="flex-1 flex items-center justify-center text-center">
|
||||
@@ -188,7 +254,7 @@ class AccountManager {
|
||||
<span class="text-[10px] font-bold uppercase text-on-surface-variant">Service</span>
|
||||
<select id="serviceFilter" class="bg-surface-container-low border-slate-200 rounded-md text-[11px] py-1 px-2 pr-6 focus:ring-1 focus:ring-primary shadow-sm">
|
||||
<option value="">All Services</option>
|
||||
${this.applications.map(app => `<option value="${app.name}">${app.name}</option>`).join('')}
|
||||
${this.applications.map(app => `<option value="${app.AppId}">${app.Name}</option>`).join('')}
|
||||
</select>
|
||||
</div>
|
||||
<div class="flex-1"></div>
|
||||
@@ -208,18 +274,21 @@ class AccountManager {
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-slate-100">
|
||||
${this.accounts.map((acc, idx) => `
|
||||
<tr class="hover:bg-slate-50/80 transition-colors group account-row" data-account-id="${idx}">
|
||||
<td class="px-4 py-3 text-sm font-medium text-slate-900">${acc.owner}</td>
|
||||
<td class="px-4 py-3 text-sm text-slate-600">${acc.username}</td>
|
||||
${this.accounts.map(acc => `
|
||||
<tr class="hover:bg-slate-50/80 transition-colors group account-row" data-account-id="${acc.AccountId}">
|
||||
<td class="px-4 py-3 text-sm font-medium text-slate-900">${acc.Email || '-'}</td>
|
||||
<td class="px-4 py-3 text-sm text-slate-600">${acc.AccountUsername || '-'}</td>
|
||||
<td class="px-4 py-3 text-sm">
|
||||
<span class="px-2 py-1 bg-blue-100 text-blue-700 rounded text-xs font-semibold">${acc.service}</span>
|
||||
<span class="px-2 py-1 bg-blue-100 text-blue-700 rounded text-xs font-semibold">${acc.AppName || '-'}</span>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-right">
|
||||
<button class="p-1.5 text-slate-400 hover:text-primary transition-colors edit-account" data-account-id="${idx}">
|
||||
<button class="p-1.5 text-slate-400 hover:text-slate-600 transition-colors view-account" data-account-id="${acc.AccountId}" title="View Details">
|
||||
<span class="material-symbols-outlined text-lg">info</span>
|
||||
</button>
|
||||
<button class="p-1.5 text-slate-400 hover:text-primary transition-colors edit-account" data-account-id="${acc.AccountId}">
|
||||
<span class="material-symbols-outlined text-lg">edit</span>
|
||||
</button>
|
||||
<button class="p-1.5 text-slate-400 hover:text-error transition-colors delete-account" data-account-id="${idx}">
|
||||
<button class="p-1.5 text-slate-400 hover:text-error transition-colors delete-account" data-account-id="${acc.AccountId}">
|
||||
<span class="material-symbols-outlined text-lg">delete</span>
|
||||
</button>
|
||||
</td>
|
||||
@@ -242,42 +311,6 @@ class AccountManager {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Account Modal -->
|
||||
<div class="modal-backdrop fixed inset-0 z-[100] flex items-center justify-center bg-black/40 backdrop-blur-sm hidden" id="accountModal">
|
||||
<div class="modal-content w-full max-w-lg bg-white rounded-xl shadow-2xl border border-slate-200 overflow-hidden m-4">
|
||||
<div class="px-6 py-4 border-b border-slate-100 flex items-center justify-between bg-slate-50">
|
||||
<h3 class="text-base font-extrabold text-slate-900">Add New Account</h3>
|
||||
<button class="p-1.5 rounded-lg hover:bg-slate-200 text-slate-400 transition-colors" data-close-modal>
|
||||
<span class="material-symbols-outlined">close</span>
|
||||
</button>
|
||||
</div>
|
||||
<form id="accountForm" class="p-6 space-y-4">
|
||||
<div>
|
||||
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Service</label>
|
||||
<select id="accountService" class="w-full border-slate-200 rounded-lg text-sm py-2.5 px-3" required>
|
||||
<option value="">Select a service</option>
|
||||
${this.applications.map(app => `<option value="${app.name}">${app.name}</option>`).join('')}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Owner Name</label>
|
||||
<input type="text" id="accountOwner" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" required placeholder="John Doe">
|
||||
</div>
|
||||
<div>
|
||||
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Username</label>
|
||||
<input type="text" id="accountUsername" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" required placeholder="username">
|
||||
</div>
|
||||
<div>
|
||||
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Password</label>
|
||||
<input type="password" id="accountPassword" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" required placeholder="••••••••">
|
||||
</div>
|
||||
<div class="flex gap-3 pt-4">
|
||||
<button type="button" class="flex-1 px-4 py-2 text-xs font-bold text-slate-600 border border-slate-200 rounded-lg" data-close-modal>Cancel</button>
|
||||
<button type="submit" class="flex-1 px-4 py-2 bg-primary hover:bg-primary-dim text-on-primary rounded-lg text-xs font-bold">Save Account</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -335,36 +368,43 @@ class AccountManager {
|
||||
<tr class="bg-surface-container-low/30 border-b border-outline-variant/10">
|
||||
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Name</th>
|
||||
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Type</th>
|
||||
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Description</th>
|
||||
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">URL</th>
|
||||
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest">Status</th>
|
||||
<th class="px-6 py-2.5 text-[10px] font-bold text-on-surface-variant uppercase tracking-widest text-right">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-outline-variant/5">
|
||||
${this.applications.map((app, idx) => `
|
||||
${this.applications.map(app => `
|
||||
<tr class="hover:bg-surface-container-low/30 transition-colors group">
|
||||
<td class="px-6 py-3">
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="w-8 h-8 rounded bg-surface-container-highest flex items-center justify-center text-primary">
|
||||
<span class="material-symbols-outlined text-sm">${app.icon}</span>
|
||||
<span class="material-symbols-outlined text-sm">${app.Icon || 'apps'}</span>
|
||||
</div>
|
||||
<span class="font-bold text-sm text-on-surface">${app.name}</span>
|
||||
<span class="font-bold text-sm text-on-surface">${app.Name}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-3">
|
||||
<span class="px-2 py-0.5 rounded-full text-[9px] font-black uppercase bg-surface-container-highest text-on-surface-variant">${app.type}</span>
|
||||
<span class="px-2 py-0.5 rounded-full text-[9px] font-black uppercase bg-surface-container-highest text-on-surface-variant">${app.Type}</span>
|
||||
</td>
|
||||
<td class="px-6 py-3 text-sm text-on-surface-variant max-w-xs truncate" title="${app.Description || ''}">${app.Description || '-'}</td>
|
||||
<td class="px-6 py-3 text-sm text-primary max-w-xs truncate">${(app.Url || app.url) ? `<a href="${app.Url || app.url}" target="_blank" class="underline">${app.Url || app.url}</a>` : '-'}</td>
|
||||
<td class="px-6 py-3">
|
||||
<div class="flex items-center gap-1.5">
|
||||
<div class="w-1.5 h-1.5 rounded-full ${app.status === 'online' ? 'bg-primary' : 'bg-error'} ring-2 ${app.status === 'online' ? 'ring-primary/20' : 'ring-error/20'}"></div>
|
||||
<span class="text-xs font-medium ${app.status === 'online' ? 'text-on-primary-fixed-variant' : 'text-error'}">${app.status === 'online' ? 'Online' : 'Offline'}</span>
|
||||
<div class="w-1.5 h-1.5 rounded-full ${(app.Status || app.status) === 'online' ? 'bg-primary' : 'bg-error'} ring-2 ${(app.Status || app.status) === 'online' ? 'ring-primary/20' : 'ring-error/20'}"></div>
|
||||
<span class="text-xs font-medium ${(app.Status || app.status) === 'online' ? 'text-on-primary-fixed-variant' : 'text-error'}">${(app.Status || app.status) === 'online' ? 'Online' : 'Offline'}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-3 text-right">
|
||||
<div class="flex items-center justify-end gap-1">
|
||||
<button class="p-1.5 text-on-surface-variant hover:text-primary transition-colors edit-app" data-app-id="${idx}">
|
||||
<button class="p-1.5 text-on-surface-variant hover:text-on-surface transition-colors view-app" data-app-id="${app.AppId}" title="View Details">
|
||||
<span class="material-symbols-outlined text-lg">info</span>
|
||||
</button>
|
||||
<button class="p-1.5 text-on-surface-variant hover:text-primary transition-colors edit-app" data-app-id="${app.AppId}">
|
||||
<span class="material-symbols-outlined text-lg">edit</span>
|
||||
</button>
|
||||
<button class="p-1.5 text-on-surface-variant hover:text-error transition-colors delete-app" data-app-id="${idx}">
|
||||
<button class="p-1.5 text-on-surface-variant hover:text-error transition-colors delete-app" data-app-id="${app.AppId}">
|
||||
<span class="material-symbols-outlined text-lg">delete</span>
|
||||
</button>
|
||||
</div>
|
||||
@@ -377,86 +417,228 @@ class AccountManager {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- App Modal -->
|
||||
<div class="modal-backdrop fixed inset-0 z-[100] flex items-center justify-center bg-black/40 backdrop-blur-sm hidden" id="appModal">
|
||||
<div class="modal-content w-full max-w-lg bg-white rounded-xl shadow-2xl border border-slate-200 overflow-hidden m-4">
|
||||
<div class="px-6 py-4 border-b border-slate-100 flex items-center justify-between bg-slate-50">
|
||||
<h3 class="text-base font-extrabold text-slate-900">Add New Application</h3>
|
||||
<button class="p-1.5 rounded-lg hover:bg-slate-200 text-slate-400 transition-colors" data-close-modal>
|
||||
<span class="material-symbols-outlined">close</span>
|
||||
</button>
|
||||
</div>
|
||||
<form id="appForm" class="p-6 space-y-4">
|
||||
<div>
|
||||
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Application Name</label>
|
||||
<input type="text" id="appName" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" required placeholder="AWS Services">
|
||||
</div>
|
||||
<div>
|
||||
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Type</label>
|
||||
<input type="text" id="appType" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3" required placeholder="Cloud">
|
||||
</div>
|
||||
<div>
|
||||
<label class="text-[10px] font-bold uppercase text-slate-500 tracking-widest block mb-1">Status</label>
|
||||
<select id="appStatus" class="w-full border border-slate-200 rounded-lg text-sm py-2.5 px-3">
|
||||
<option value="online">Online</option>
|
||||
<option value="offline">Offline</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="flex gap-3 pt-4">
|
||||
<button type="button" class="flex-1 px-4 py-2 text-xs font-bold text-slate-600 border border-slate-200 rounded-lg" data-close-modal>Cancel</button>
|
||||
<button type="submit" class="flex-1 px-4 py-2 bg-primary hover:bg-primary-dim text-on-primary rounded-lg text-xs font-bold">Save Application</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
setupAccountRowListeners() {
|
||||
// View Account listeners
|
||||
document.querySelectorAll('.view-account').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const accountId = Number(btn.dataset.accountId);
|
||||
const account = this.accounts.find(a => a.AccountId === accountId);
|
||||
this.currentViewAccountId = accountId;
|
||||
this.currentViewAccount = account;
|
||||
document.getElementById('viewAccountService').textContent = account?.AppName || '-';
|
||||
document.getElementById('viewAccountOwner').textContent = account?.Email || '-';
|
||||
document.getElementById('viewAccountUsername').textContent = account?.AccountUsername || '-';
|
||||
document.getElementById('viewAccountPassword').textContent = '••••••••';
|
||||
document.getElementById('viewAccountPassword').dataset.visible = 'false';
|
||||
document.getElementById('toggleIcon').textContent = 'visibility';
|
||||
document.getElementById('viewAccountModal').classList.add('open');
|
||||
});
|
||||
});
|
||||
|
||||
// Delete Account listeners - show confirmation modal
|
||||
document.querySelectorAll('.delete-account').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const accountId = btn.dataset.accountId;
|
||||
if (confirm('Delete this account?')) {
|
||||
this.accounts.splice(accountId, 1);
|
||||
this.saveToStorage('accounts', this.accounts);
|
||||
location.href = './accounts.html';
|
||||
const accountId = Number(btn.dataset.accountId);
|
||||
const account = this.accounts.find(a => a.AccountId === accountId);
|
||||
this.pendingDeleteAccountId = accountId;
|
||||
document.getElementById('deleteAccountUsername').textContent = account?.AccountUsername || '';
|
||||
document.getElementById('deleteAccountModal').classList.add('open');
|
||||
});
|
||||
});
|
||||
|
||||
// Confirm Delete Account
|
||||
document.querySelectorAll('.confirm-delete-account').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
if (this.pendingDeleteAccountId !== undefined) {
|
||||
fetch(`${this.apiBase}/accounts/${this.pendingDeleteAccountId}`, { method: 'DELETE' })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Account deleted successfully');
|
||||
this.closeModals();
|
||||
location.href = './accounts.html';
|
||||
} else {
|
||||
alert(data.message || 'Delete account failed');
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
alert('Delete account failed');
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Edit Account listeners
|
||||
document.querySelectorAll('.edit-account').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const accountId = btn.dataset.accountId;
|
||||
const account = this.accounts[accountId];
|
||||
const accountId = Number(btn.dataset.accountId);
|
||||
const account = this.accounts.find(a => a.AccountId === accountId);
|
||||
// Populate form with existing data
|
||||
document.getElementById('accountUsername').value = account.username;
|
||||
document.getElementById('accountPassword').value = account.password;
|
||||
document.getElementById('accountOwner').value = account.owner;
|
||||
document.getElementById('accountService').value = account.service;
|
||||
this.editingAccountId = accountId;
|
||||
const form = document.getElementById('accountForm');
|
||||
if (form) {
|
||||
const userInput = form.querySelector('#accountUsername');
|
||||
const passInput = form.querySelector('#accountPassword');
|
||||
const ownerInput = form.querySelector('#accountOwner');
|
||||
const serviceSelect = form.querySelector('#accountService');
|
||||
if (userInput) userInput.value = account?.AccountUsername || '';
|
||||
if (passInput) passInput.value = account?.AccountPassword || '';
|
||||
if (ownerInput) ownerInput.value = this.currentUser?.Username || this.currentUser?.username || '';
|
||||
if (serviceSelect) serviceSelect.value = account?.AppId || '';
|
||||
}
|
||||
this.editingAccountId = account?.AccountId;
|
||||
this.closeModals();
|
||||
this.openAccountModal();
|
||||
});
|
||||
});
|
||||
|
||||
document.querySelectorAll('.delete-app').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const appId = btn.dataset.appId;
|
||||
if (confirm('Delete this application?')) {
|
||||
this.applications.splice(appId, 1);
|
||||
this.saveToStorage('applications', this.applications);
|
||||
location.href = './applications.html';
|
||||
// Edit from View modal
|
||||
document.querySelectorAll('.edit-account-from-view').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
const account = this.currentViewAccount;
|
||||
const form = document.getElementById('accountForm');
|
||||
if (form) {
|
||||
const userInput = form.querySelector('#accountUsername');
|
||||
const passInput = form.querySelector('#accountPassword');
|
||||
const ownerInput = form.querySelector('#accountOwner');
|
||||
const serviceSelect = form.querySelector('#accountService');
|
||||
if (userInput) userInput.value = account?.AccountUsername || '';
|
||||
if (passInput) passInput.value = account?.AccountPassword || '';
|
||||
if (ownerInput) ownerInput.value = this.currentUser?.Username || this.currentUser?.username || '';
|
||||
if (serviceSelect) serviceSelect.value = account?.AppId || '';
|
||||
}
|
||||
this.editingAccountId = account?.AccountId;
|
||||
this.closeModals();
|
||||
this.openAccountModal();
|
||||
});
|
||||
});
|
||||
|
||||
// Toggle Password Visibility
|
||||
document.querySelectorAll('.toggle-password').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
const passwordEl = document.getElementById('viewAccountPassword');
|
||||
const toggleIcon = document.getElementById('toggleIcon');
|
||||
const isVisible = passwordEl.dataset.visible === 'true';
|
||||
|
||||
if (isVisible) {
|
||||
passwordEl.textContent = '••••••••';
|
||||
passwordEl.dataset.visible = 'false';
|
||||
toggleIcon.textContent = 'visibility';
|
||||
} else {
|
||||
passwordEl.textContent = this.currentViewAccount?.AccountPassword || '';
|
||||
passwordEl.dataset.visible = 'true';
|
||||
toggleIcon.textContent = 'visibility_off';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// View App listeners
|
||||
document.querySelectorAll('.view-app').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const appId = Number(btn.dataset.appId);
|
||||
const app = this.applications.find(a => a.AppId === appId);
|
||||
this.currentViewAppId = appId;
|
||||
document.getElementById('viewAppName').textContent = app?.Name || '-';
|
||||
document.getElementById('viewAppType').textContent = app?.Type || '-';
|
||||
document.getElementById('viewAppDescription').textContent = app?.Description || '-';
|
||||
const urlEl = document.getElementById('viewAppUrl');
|
||||
const urlVal = app?.Url || app?.url;
|
||||
if (urlEl) {
|
||||
if (urlVal) {
|
||||
urlEl.innerHTML = `<a href="${urlVal}" target="_blank" class="text-primary underline">${urlVal}</a>`;
|
||||
} else {
|
||||
urlEl.textContent = '-';
|
||||
}
|
||||
}
|
||||
const statusValue = app?.Status || app?.status;
|
||||
document.getElementById('viewAppStatus').textContent = statusValue === 'online' ? 'Online' : 'Offline';
|
||||
document.getElementById('viewAppModal').classList.add('open');
|
||||
});
|
||||
});
|
||||
|
||||
// Delete App listeners - show confirmation modal
|
||||
document.querySelectorAll('.delete-app').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const appId = Number(btn.dataset.appId);
|
||||
const app = this.applications.find(a => a.AppId === appId);
|
||||
this.pendingDeleteAppId = appId;
|
||||
document.getElementById('deleteAppName').textContent = app?.Name || '';
|
||||
document.getElementById('deleteAppModal').classList.add('open');
|
||||
});
|
||||
});
|
||||
|
||||
// Confirm Delete App
|
||||
document.querySelectorAll('.confirm-delete-app').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
if (this.pendingDeleteAppId !== undefined) {
|
||||
fetch(`${this.apiBase}/applications/${this.pendingDeleteAppId}`, { method: 'DELETE' })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Application deleted successfully');
|
||||
this.closeModals();
|
||||
location.href = './applications.html';
|
||||
} else {
|
||||
alert(data.message || 'Delete application failed');
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
alert('Delete application failed');
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Edit App listeners
|
||||
document.querySelectorAll('.edit-app').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
const appId = btn.dataset.appId;
|
||||
const app = this.applications[appId];
|
||||
document.getElementById('appName').value = app.name;
|
||||
document.getElementById('appType').value = app.type;
|
||||
document.getElementById('appStatus').value = app.status;
|
||||
this.editingAppId = appId;
|
||||
const appId = Number(btn.dataset.appId);
|
||||
const app = this.applications.find(a => a.AppId === appId);
|
||||
document.getElementById('appName').value = app?.Name || '';
|
||||
document.getElementById('appType').value = app?.Type || '';
|
||||
document.getElementById('appStatus').value = app?.Status || 'online';
|
||||
document.getElementById('appDescription').value = app?.Description || '';
|
||||
document.getElementById('appUrl').value = app?.Url || app?.url || '';
|
||||
this.editingAppId = app?.AppId;
|
||||
this.closeModals();
|
||||
this.openAppModal();
|
||||
});
|
||||
});
|
||||
|
||||
// Edit App from View modal
|
||||
document.querySelectorAll('.edit-app-from-view').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
const appId = this.currentViewAppId;
|
||||
const app = this.applications.find(a => a.AppId === appId);
|
||||
document.getElementById('appName').value = app?.Name || '';
|
||||
document.getElementById('appType').value = app?.Type || '';
|
||||
document.getElementById('appStatus').value = app?.Status || 'online';
|
||||
document.getElementById('appDescription').value = app?.Description || '';
|
||||
document.getElementById('appUrl').value = app?.Url || '';
|
||||
this.editingAppId = app?.AppId;
|
||||
this.closeModals();
|
||||
this.openAppModal();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
setupAddButtonListeners() {
|
||||
// Add Account button
|
||||
document.querySelectorAll('#addAccountBtn').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
this.editingAccountId = undefined;
|
||||
this.openAccountModal();
|
||||
});
|
||||
});
|
||||
|
||||
// Add Application button
|
||||
document.querySelectorAll('#addAppBtn').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
this.editingAppId = undefined;
|
||||
this.openAppModal();
|
||||
});
|
||||
});
|
||||
@@ -464,67 +646,139 @@ class AccountManager {
|
||||
|
||||
handleAccountSubmit(e) {
|
||||
e.preventDefault();
|
||||
const newAccount = {
|
||||
service: document.getElementById('accountService').value,
|
||||
owner: document.getElementById('accountOwner').value,
|
||||
username: document.getElementById('accountUsername').value,
|
||||
password: document.getElementById('accountPassword').value,
|
||||
dateCreated: new Date().toLocaleDateString()
|
||||
const accountForm = document.getElementById('accountForm');
|
||||
const userId = this.getUserId();
|
||||
const appId = Number(accountForm?.querySelector('#accountService')?.value || 0);
|
||||
const accountUsername = (accountForm?.querySelector('#accountUsername')?.value || '').trim();
|
||||
const accountPassword = (accountForm?.querySelector('#accountPassword')?.value || '').trim();
|
||||
const accountEmail = ((accountForm?.querySelector('#accountOwner')?.value || '').trim()) || this.currentUser?.Username || this.currentUser?.username || '';
|
||||
if (!accountForm) {
|
||||
alert('Account form not found.');
|
||||
return;
|
||||
}
|
||||
if (!userId) {
|
||||
alert('User is not authenticated. Please login again.');
|
||||
return;
|
||||
}
|
||||
if (!appId) {
|
||||
alert('Please select a service.');
|
||||
return;
|
||||
}
|
||||
if (!accountUsername) {
|
||||
alert('Please enter a username.');
|
||||
return;
|
||||
}
|
||||
if (!accountPassword) {
|
||||
alert('Please enter a password.');
|
||||
return;
|
||||
}
|
||||
const payload = {
|
||||
userId,
|
||||
appId,
|
||||
accountUsername,
|
||||
accountPassword,
|
||||
email: accountEmail,
|
||||
accessLevel: 'user',
|
||||
notes: ''
|
||||
};
|
||||
|
||||
if (this.editingAccountId !== undefined) {
|
||||
this.accounts[this.editingAccountId] = newAccount;
|
||||
this.editingAccountId = undefined;
|
||||
} else {
|
||||
this.accounts.push(newAccount);
|
||||
}
|
||||
const isEdit = this.editingAccountId !== undefined;
|
||||
const url = isEdit ? `${this.apiBase}/accounts/${this.editingAccountId}` : `${this.apiBase}/accounts`;
|
||||
const method = isEdit ? 'PUT' : 'POST';
|
||||
|
||||
this.saveToStorage('accounts', this.accounts);
|
||||
this.closeModals();
|
||||
location.href = './accounts.html';
|
||||
fetch(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
}).then(res => res.json()).then(data => {
|
||||
if (data.success) {
|
||||
this.editingAccountId = undefined;
|
||||
alert(isEdit ? 'Account updated successfully' : 'Account created successfully');
|
||||
this.closeModals();
|
||||
location.href = './accounts.html';
|
||||
} else {
|
||||
alert(data.message || 'Save account failed');
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
alert('Save account failed');
|
||||
});
|
||||
}
|
||||
|
||||
handleAppSubmit(e) {
|
||||
e.preventDefault();
|
||||
const newApp = {
|
||||
const payload = {
|
||||
name: document.getElementById('appName').value,
|
||||
type: document.getElementById('appType').value,
|
||||
status: document.getElementById('appStatus').value,
|
||||
icon: 'cloud'
|
||||
icon: 'cloud',
|
||||
description: document.getElementById('appDescription')?.value || '',
|
||||
url: (document.getElementById('appUrl')?.value || '').trim()
|
||||
};
|
||||
|
||||
if (this.editingAppId !== undefined) {
|
||||
this.applications[this.editingAppId] = newApp;
|
||||
this.editingAppId = undefined;
|
||||
} else {
|
||||
this.applications.push(newApp);
|
||||
}
|
||||
const isEdit = this.editingAppId !== undefined;
|
||||
const url = isEdit ? `${this.apiBase}/applications/${this.editingAppId}` : `${this.apiBase}/applications`;
|
||||
const method = isEdit ? 'PUT' : 'POST';
|
||||
|
||||
this.saveToStorage('applications', this.applications);
|
||||
this.closeModals();
|
||||
location.href = './applications.html';
|
||||
fetch(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
}).then(res => res.json()).then(data => {
|
||||
if (data.success) {
|
||||
this.editingAppId = undefined;
|
||||
alert(isEdit ? 'Application updated successfully' : 'Application created successfully');
|
||||
this.closeModals();
|
||||
location.href = './applications.html';
|
||||
} else {
|
||||
alert(data.message || 'Save application failed');
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
alert('Save application failed');
|
||||
});
|
||||
}
|
||||
|
||||
openAccountModal() {
|
||||
this.editingAccountId = undefined;
|
||||
document.getElementById('accountService').value = '';
|
||||
document.getElementById('accountOwner').value = '';
|
||||
document.getElementById('accountUsername').value = '';
|
||||
document.getElementById('accountPassword').value = '';
|
||||
document.getElementById('accountModal').classList.remove('hidden');
|
||||
// Refresh service options so newly added applications appear
|
||||
const serviceSelect = document.getElementById('accountService');
|
||||
if (serviceSelect) {
|
||||
serviceSelect.innerHTML = `<option value="">Select a service</option>` +
|
||||
this.applications.map(app => `<option value="${app.AppId}">${app.Name}</option>`).join('');
|
||||
}
|
||||
|
||||
if (this.editingAccountId === undefined) {
|
||||
const form = document.getElementById('accountForm');
|
||||
if (form) {
|
||||
const serviceSelect = form.querySelector('#accountService');
|
||||
const ownerInput = form.querySelector('#accountOwner');
|
||||
const userInput = form.querySelector('#accountUsername');
|
||||
const passInput = form.querySelector('#accountPassword');
|
||||
if (serviceSelect) serviceSelect.value = '';
|
||||
if (ownerInput) ownerInput.value = this.currentUser?.Username || this.currentUser?.username || '';
|
||||
if (userInput) userInput.value = '';
|
||||
if (passInput) passInput.value = '';
|
||||
}
|
||||
}
|
||||
document.getElementById('accountModal').classList.add('open');
|
||||
}
|
||||
|
||||
openAppModal() {
|
||||
this.editingAppId = undefined;
|
||||
document.getElementById('appName').value = '';
|
||||
document.getElementById('appType').value = '';
|
||||
document.getElementById('appStatus').value = 'online';
|
||||
document.getElementById('appModal').classList.remove('hidden');
|
||||
if (this.editingAppId === undefined) {
|
||||
document.getElementById('appName').value = '';
|
||||
document.getElementById('appType').value = '';
|
||||
document.getElementById('appStatus').value = 'online';
|
||||
const desc = document.getElementById('appDescription');
|
||||
const url = document.getElementById('appUrl');
|
||||
if (desc) desc.value = '';
|
||||
if (url) url.value = '';
|
||||
}
|
||||
document.getElementById('appModal').classList.add('open');
|
||||
}
|
||||
|
||||
closeModals() {
|
||||
document.querySelectorAll('.modal-backdrop').forEach(modal => {
|
||||
modal.classList.add('hidden');
|
||||
modal.classList.remove('open');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -538,6 +792,37 @@ class AccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Global modal close functions
|
||||
function closeAllModals() {
|
||||
document.querySelectorAll('.modal-backdrop').forEach(modal => {
|
||||
modal.classList.remove('open');
|
||||
});
|
||||
}
|
||||
|
||||
function closeAccountModal() {
|
||||
document.getElementById('accountModal').classList.remove('open');
|
||||
}
|
||||
|
||||
function closeViewAccountModal() {
|
||||
document.getElementById('viewAccountModal').classList.remove('open');
|
||||
}
|
||||
|
||||
function closeDeleteAccountModal() {
|
||||
document.getElementById('deleteAccountModal').classList.remove('open');
|
||||
}
|
||||
|
||||
function closeAppModal() {
|
||||
document.getElementById('appModal').classList.remove('open');
|
||||
}
|
||||
|
||||
function closeViewAppModal() {
|
||||
document.getElementById('viewAppModal').classList.remove('open');
|
||||
}
|
||||
|
||||
function closeDeleteAppModal() {
|
||||
document.getElementById('deleteAppModal').classList.remove('open');
|
||||
}
|
||||
|
||||
// Initialize app when DOM is ready
|
||||
let app;
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
Reference in New Issue
Block a user