Neue Verwaltung der Gruppenzuordnung 1:n
This commit is contained in:
1 parent
fa02a7ade0
commit
02c017d53d
34 files changed
+1306
-251
No files matched your search
@@ -52,14 +52,12 @@ function deleteGruppe(groupId) {
|
||||
|
||||
function openCreateGruppeModal() {
|
||||
document.getElementById('gruppeAlias').value = '';
|
||||
document.getElementById('gruppeAccount').value = '';
|
||||
document.getElementById('createGruppeModal').showModal();
|
||||
}
|
||||
|
||||
function closeCreateGruppeModal() {
|
||||
document.getElementById('createGruppeModal').close();
|
||||
document.getElementById('gruppeAlias').value = '';
|
||||
document.getElementById('gruppeAccount').value = '';
|
||||
}
|
||||
|
||||
function buildStateSelect(state) {
|
||||
@@ -74,19 +72,6 @@ function buildStateSelect(state) {
|
||||
return select;
|
||||
}
|
||||
|
||||
function buildAccountSelect(accountId) {
|
||||
const select = document.createElement('select');
|
||||
const source = document.getElementById('gruppeAccount');
|
||||
Array.from(source.options).forEach(option => {
|
||||
const copy = document.createElement('option');
|
||||
copy.value = option.value;
|
||||
copy.textContent = option.textContent;
|
||||
copy.selected = String(option.value) === String(accountId ?? '');
|
||||
select.appendChild(copy);
|
||||
});
|
||||
return select;
|
||||
}
|
||||
|
||||
function addGruppeRow(gruppe) {
|
||||
const tbody = document.querySelector('#gruppenTable tbody');
|
||||
const row = document.createElement('tr');
|
||||
@@ -112,13 +97,6 @@ function addGruppeRow(gruppe) {
|
||||
stateCell.appendChild(stateSelect);
|
||||
row.appendChild(stateCell);
|
||||
|
||||
const accountCell = document.createElement('td');
|
||||
const accountSelect = buildAccountSelect(gruppe.account_id);
|
||||
accountSelect.addEventListener('change',
|
||||
() => updateGruppe(gruppe.id, {account_id: accountSelect.value}));
|
||||
accountCell.appendChild(accountSelect);
|
||||
row.appendChild(accountCell);
|
||||
|
||||
const positionCell = document.createElement('td');
|
||||
positionCell.className = 'group-position';
|
||||
positionCell.textContent = formatPosition(gruppe.last_lat, gruppe.last_lon);
|
||||
@@ -141,12 +119,11 @@ function addGruppeRow(gruppe) {
|
||||
|
||||
function createGruppe() {
|
||||
const aliasname = document.getElementById('gruppeAlias').value;
|
||||
const accountId = document.getElementById('gruppeAccount').value;
|
||||
|
||||
fetch(`${apiBase}/gruppen`, {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({aliasname, account_id: accountId})
|
||||
body: JSON.stringify({aliasname})
|
||||
}).then(r => r.json()).then(data => {
|
||||
if (!data.success) {
|
||||
showError(data);
|
||||
@@ -155,7 +132,6 @@ function createGruppe() {
|
||||
addGruppeRow({
|
||||
id: data.id,
|
||||
aliasname: aliasname,
|
||||
account_id: accountId || null,
|
||||
state: 'offen',
|
||||
last_lat: null,
|
||||
last_lon: null
|
||||
|
||||
@@ -577,6 +577,15 @@ function groupDisplayName(group) {
|
||||
: String(group.id);
|
||||
}
|
||||
|
||||
function findGroupForAccount(accountId) {
|
||||
const id = Number(accountId);
|
||||
if (!Number.isFinite(id)) return null;
|
||||
return dispatcherState.groups.find(group =>
|
||||
Array.isArray(group.account_ids)
|
||||
&& group.account_ids.some(value => Number(value) === id)
|
||||
) || null;
|
||||
}
|
||||
|
||||
function groupColor(group) {
|
||||
const id = Number(group.id) || 0;
|
||||
const hue = Math.round((id * 137.508) % 360);
|
||||
@@ -829,8 +838,8 @@ function createGroupElement(group) {
|
||||
const content = document.createElement('div');
|
||||
content.className = 'group-details';
|
||||
|
||||
const positionRow = document.createElement('div');
|
||||
positionRow.className = 'group-position-row';
|
||||
const controls = document.createElement('div');
|
||||
controls.className = 'group-controls';
|
||||
|
||||
const locateButton = document.createElement('button');
|
||||
locateButton.type = 'button';
|
||||
@@ -840,19 +849,19 @@ function createGroupElement(group) {
|
||||
locateButton.setAttribute('aria-label', 'Karte auf Gruppenposition zentrieren');
|
||||
locateButton.addEventListener('click', () => locateGroup(group.id));
|
||||
|
||||
const progress = document.createElement('progress');
|
||||
progress.className = 'group-progress';
|
||||
progress.max = 100;
|
||||
progress.value = 0;
|
||||
const progressCircle = document.createElement('span');
|
||||
progressCircle.className = 'group-progress-circle';
|
||||
progressCircle.setAttribute('role', 'progressbar');
|
||||
progressCircle.setAttribute('aria-valuemin', '0');
|
||||
progressCircle.setAttribute('aria-valuemax', '100');
|
||||
progressCircle.setAttribute('aria-valuenow', '0');
|
||||
progressCircle.setAttribute('aria-label', 'Position unbekannt');
|
||||
progressCircle.title = 'Position unbekannt';
|
||||
|
||||
positionRow.append(locateButton, progress);
|
||||
|
||||
const meta = document.createElement('div');
|
||||
meta.className = 'group-position-meta';
|
||||
meta.textContent = 'Position unbekannt';
|
||||
|
||||
const actions = document.createElement('div');
|
||||
actions.className = 'group-actions';
|
||||
const progressText = document.createElement('span');
|
||||
progressText.className = 'group-progress-text';
|
||||
progressText.textContent = '–';
|
||||
progressCircle.appendChild(progressText);
|
||||
|
||||
const crosshairButton = document.createElement('button');
|
||||
crosshairButton.type = 'button';
|
||||
@@ -895,7 +904,14 @@ function createGroupElement(group) {
|
||||
});
|
||||
stateSelect.addEventListener('change', () => changeGroupState(group.id, stateSelect));
|
||||
|
||||
actions.append(crosshairButton, streetsButton, aliasButton, stateSelect);
|
||||
controls.append(
|
||||
locateButton,
|
||||
progressCircle,
|
||||
stateSelect,
|
||||
crosshairButton,
|
||||
streetsButton,
|
||||
aliasButton
|
||||
);
|
||||
|
||||
const messageInput = document.createElement('input');
|
||||
messageInput.type = 'text';
|
||||
@@ -905,7 +921,7 @@ function createGroupElement(group) {
|
||||
messageInput.title = 'Chatverlauf öffnen';
|
||||
messageInput.addEventListener('click', () => openChat(group.id));
|
||||
|
||||
content.append(positionRow, meta, actions, messageInput);
|
||||
content.append(controls, messageInput);
|
||||
details.appendChild(content);
|
||||
|
||||
return details;
|
||||
@@ -941,8 +957,8 @@ function refreshGroupControls(now = Date.now()) {
|
||||
if (!item) return;
|
||||
|
||||
const locateButton = item.querySelector('.group-locate');
|
||||
const progress = item.querySelector('.group-progress');
|
||||
const meta = item.querySelector('.group-position-meta');
|
||||
const progressCircle = item.querySelector('.group-progress-circle');
|
||||
const progressText = item.querySelector('.group-progress-text');
|
||||
|
||||
const known = hasPosition(group);
|
||||
const positionTs = group.last_pos_ts;
|
||||
@@ -961,13 +977,37 @@ function refreshGroupControls(now = Date.now()) {
|
||||
}
|
||||
}
|
||||
|
||||
locateButton.disabled = !enabled;
|
||||
progress.value = Math.round(value);
|
||||
const rounded = Math.round(value);
|
||||
const isLive = rounded >= 100;
|
||||
|
||||
if (known) {
|
||||
meta.textContent = positionAgeLabel(ageSeconds);
|
||||
} else {
|
||||
meta.textContent = 'Position unbekannt';
|
||||
locateButton.disabled = !enabled;
|
||||
|
||||
if (progressCircle) {
|
||||
progressCircle.style.setProperty('--progress', String(rounded));
|
||||
progressCircle.classList.toggle('is-live', isLive);
|
||||
progressCircle.setAttribute('aria-valuenow', String(rounded));
|
||||
progressCircle.setAttribute(
|
||||
'aria-valuetext',
|
||||
isLive
|
||||
? 'Live'
|
||||
: (known ? positionAgeLabel(ageSeconds) : 'Position unbekannt')
|
||||
);
|
||||
|
||||
const label = !known
|
||||
? 'Position unbekannt'
|
||||
: (isLive ? 'Position: Live' : `Position: ${positionAgeLabel(ageSeconds)}`);
|
||||
progressCircle.title = label;
|
||||
progressCircle.setAttribute('aria-label', label);
|
||||
}
|
||||
|
||||
if (progressText) {
|
||||
if (!known || ageSeconds === null) {
|
||||
progressText.textContent = '–';
|
||||
} else if (isLive) {
|
||||
progressText.textContent = 'Live';
|
||||
} else {
|
||||
progressText.textContent = `${Math.max(1, Math.floor(ageSeconds / 60))}m`;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1129,6 +1169,205 @@ async function saveGroupAlias() {
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Gruppenzuordnungen */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
function openGroupAssignmentModal() {
|
||||
if (window.CMBNET_VIEW_MODE === 'observer') return;
|
||||
renderGroupAssignmentTable();
|
||||
const dialog = document.getElementById('groupAssignmentModal');
|
||||
if (dialog && !dialog.open) dialog.showModal();
|
||||
}
|
||||
|
||||
function closeGroupAssignmentModal() {
|
||||
const dialog = document.getElementById('groupAssignmentModal');
|
||||
if (dialog && dialog.open) dialog.close();
|
||||
}
|
||||
|
||||
function buildAssignmentHeaderCell(label, className, rotated = false) {
|
||||
const cell = document.createElement('th');
|
||||
cell.scope = 'col';
|
||||
cell.className = className;
|
||||
if (rotated) {
|
||||
const span = document.createElement('span');
|
||||
span.className = 'group-column-label';
|
||||
span.textContent = label;
|
||||
cell.title = label;
|
||||
cell.appendChild(span);
|
||||
} else {
|
||||
cell.textContent = label;
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
|
||||
function buildAssignmentRadioCell(accountId, accountName, value, checked, className, label) {
|
||||
const cell = document.createElement('td');
|
||||
cell.className = className;
|
||||
const input = document.createElement('input');
|
||||
input.type = 'radio';
|
||||
input.name = `group-assignment-${accountId}`;
|
||||
input.value = String(value);
|
||||
input.checked = checked;
|
||||
input.setAttribute('aria-label', `${label} für ${accountName}`);
|
||||
cell.appendChild(input);
|
||||
return cell;
|
||||
}
|
||||
|
||||
function renderGroupAssignmentTable() {
|
||||
const head = document.getElementById('groupAssignmentHead');
|
||||
const body = document.getElementById('groupAssignmentBody');
|
||||
const empty = document.getElementById('groupAssignmentEmpty');
|
||||
const scroll = document.querySelector('.group-assignment-scroll');
|
||||
const saveButton = document.getElementById('groupAssignmentSaveButton');
|
||||
const table = document.getElementById('groupAssignmentTable');
|
||||
if (!head || !body) return;
|
||||
|
||||
const accounts = dispatcherState.accounts
|
||||
.filter(account => account.role === 'Gruppe')
|
||||
.slice()
|
||||
.sort((a, b) => String(a.name || '').localeCompare(String(b.name || ''), 'de'));
|
||||
const groups = dispatcherState.groups
|
||||
.slice()
|
||||
.sort((a, b) => Number(a.id) - Number(b.id));
|
||||
|
||||
const assignmentByAccount = new Map();
|
||||
groups.forEach(group => {
|
||||
(Array.isArray(group.account_ids) ? group.account_ids : []).forEach(accountId => {
|
||||
assignmentByAccount.set(Number(accountId), Number(group.id));
|
||||
});
|
||||
});
|
||||
|
||||
head.innerHTML = '';
|
||||
body.innerHTML = '';
|
||||
|
||||
const headerRow = document.createElement('tr');
|
||||
const longestGroupLabel = groups.reduce(
|
||||
(maxLength, group) => Math.max(maxLength, groupDisplayName(group).length),
|
||||
0
|
||||
);
|
||||
headerRow.style.setProperty(
|
||||
'--group-header-height',
|
||||
`${Math.min(14, Math.max(6.2, longestGroupLabel * 0.32 + 1.1))}rem`
|
||||
);
|
||||
const lastHeaderLabel = groups.length
|
||||
? groupDisplayName(groups[groups.length - 1])
|
||||
: 'Nicht dabei';
|
||||
if (table) {
|
||||
const rightSpace = Math.min(14, Math.max(1.5, lastHeaderLabel.length * 0.26 - 3.1));
|
||||
table.style.setProperty('--assignment-right-space', `${rightSpace}rem`);
|
||||
}
|
||||
headerRow.appendChild(buildAssignmentHeaderCell('Account', 'assignment-account-header'));
|
||||
headerRow.appendChild(buildAssignmentHeaderCell(
|
||||
'Nicht dabei', 'not-participating-header', true
|
||||
));
|
||||
groups.forEach(group => {
|
||||
headerRow.appendChild(buildAssignmentHeaderCell(
|
||||
groupDisplayName(group), 'group-column-header', true
|
||||
));
|
||||
});
|
||||
const spacerHeader = document.createElement('th');
|
||||
spacerHeader.className = 'assignment-spacer-header';
|
||||
spacerHeader.setAttribute('aria-hidden', 'true');
|
||||
headerRow.appendChild(spacerHeader);
|
||||
head.appendChild(headerRow);
|
||||
|
||||
accounts.forEach(account => {
|
||||
const accountId = Number(account.id);
|
||||
const accountName = account.name || `Account ${accountId}`;
|
||||
const currentGroupId = assignmentByAccount.has(accountId)
|
||||
? assignmentByAccount.get(accountId)
|
||||
: null;
|
||||
|
||||
const row = document.createElement('tr');
|
||||
row.dataset.accountId = String(accountId);
|
||||
const rowHeader = document.createElement('th');
|
||||
rowHeader.scope = 'row';
|
||||
rowHeader.className = 'assignment-account-name';
|
||||
rowHeader.textContent = accountName;
|
||||
row.appendChild(rowHeader);
|
||||
|
||||
row.appendChild(buildAssignmentRadioCell(
|
||||
accountId, accountName, 'none', currentGroupId === null,
|
||||
'not-participating-cell', 'Nicht dabei'
|
||||
));
|
||||
groups.forEach(group => {
|
||||
row.appendChild(buildAssignmentRadioCell(
|
||||
accountId, accountName, group.id,
|
||||
currentGroupId === Number(group.id),
|
||||
'group-assignment-cell', groupDisplayName(group)
|
||||
));
|
||||
});
|
||||
const spacerCell = document.createElement('td');
|
||||
spacerCell.className = 'assignment-spacer-cell';
|
||||
spacerCell.setAttribute('aria-hidden', 'true');
|
||||
row.appendChild(spacerCell);
|
||||
body.appendChild(row);
|
||||
});
|
||||
|
||||
if (empty) empty.hidden = accounts.length > 0;
|
||||
if (scroll) scroll.hidden = accounts.length === 0;
|
||||
if (saveButton) saveButton.disabled = accounts.length === 0;
|
||||
}
|
||||
|
||||
function updateLocalGroupAssignments(assignments) {
|
||||
dispatcherState.groups.forEach(group => {
|
||||
group.account_ids = [];
|
||||
});
|
||||
Object.entries(assignments).forEach(([accountId, groupId]) => {
|
||||
if (groupId === null || groupId === undefined) return;
|
||||
const group = dispatcherState.groupById.get(Number(groupId));
|
||||
if (!group) return;
|
||||
if (!Array.isArray(group.account_ids)) group.account_ids = [];
|
||||
group.account_ids.push(Number(accountId));
|
||||
});
|
||||
}
|
||||
|
||||
async function saveGroupAssignments() {
|
||||
const saveButton = document.getElementById('groupAssignmentSaveButton');
|
||||
if (!saveButton || saveButton.disabled) return;
|
||||
|
||||
const assignments = {};
|
||||
document.querySelectorAll('#groupAssignmentBody tr[data-account-id]').forEach(row => {
|
||||
const checked = row.querySelector('input[type="radio"]:checked');
|
||||
const rawValue = checked ? checked.value : 'none';
|
||||
assignments[row.dataset.accountId] = rawValue === 'none'
|
||||
? null
|
||||
: Number(rawValue);
|
||||
});
|
||||
|
||||
if (!Object.keys(assignments).length) {
|
||||
closeGroupAssignmentModal();
|
||||
return;
|
||||
}
|
||||
|
||||
saveButton.disabled = true;
|
||||
try {
|
||||
const response = await fetch(`${apiBase}/groups/assignments`, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({assignments})
|
||||
});
|
||||
let data = {};
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (error) {
|
||||
data = {};
|
||||
}
|
||||
if (!response.ok || !data.success) {
|
||||
throw new Error(data.error || `HTTP ${response.status}`);
|
||||
}
|
||||
updateLocalGroupAssignments(assignments);
|
||||
closeGroupAssignmentModal();
|
||||
} catch (error) {
|
||||
console.error('Gruppenzuordnungen konnten nicht gespeichert werden:', error);
|
||||
alert(`Gruppenzuordnungen konnten nicht gespeichert werden: ${error.message}`);
|
||||
} finally {
|
||||
saveButton.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
function locateGroup(groupId) {
|
||||
const group = dispatcherState.groupById.get(Number(groupId));
|
||||
if (!group || !hasPosition(group)) return;
|
||||
@@ -2142,7 +2381,7 @@ function getSenderDisplay(senderId, messageOrPayload = null) {
|
||||
}
|
||||
|
||||
const account = dispatcherState.accountById.get(id);
|
||||
const group = dispatcherState.groups.find(item => Number(item.account_id) === id);
|
||||
const group = findGroupForAccount(id);
|
||||
const accountName = account && account.name ? account.name : `Account ${id}`;
|
||||
if (group) {
|
||||
const alias = group.aliasname && String(group.aliasname).trim();
|
||||
@@ -2546,6 +2785,21 @@ function handleWsMessage(event) {
|
||||
dispatcherState.ws.send(JSON.stringify({code: 2, msg: 'pong'}));
|
||||
}
|
||||
break;
|
||||
case 99: {
|
||||
if (window.CMBNET_VIEW_MODE === 'observer') {
|
||||
window.location.reload();
|
||||
break;
|
||||
}
|
||||
const text = typeof message.msg === 'string'
|
||||
? message.msg
|
||||
: extractMessageText(message.msg);
|
||||
if (typeof window.showReloadNotice === 'function') {
|
||||
window.showReloadNotice(text);
|
||||
} else {
|
||||
alert(`${text}\n\nBitte die Webseite neu laden.`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 100:
|
||||
handleStreetUpdate(message);
|
||||
break;
|
||||
@@ -2857,6 +3111,10 @@ function bindStaticUi() {
|
||||
if (speakerButton && window.CMBNET_VIEW_MODE !== 'observer') {
|
||||
speakerButton.addEventListener('click', openSpeakerModal);
|
||||
}
|
||||
const groupAssignmentButton = document.getElementById('groupAssignmentButton');
|
||||
if (groupAssignmentButton && window.CMBNET_VIEW_MODE !== 'observer') {
|
||||
groupAssignmentButton.addEventListener('click', openGroupAssignmentModal);
|
||||
}
|
||||
const streetStateSelect = document.getElementById('streetStateSelect');
|
||||
if (streetStateSelect && window.CMBNET_VIEW_MODE !== 'observer') {
|
||||
streetStateSelect.addEventListener('change', updateStreetGroupVisibility);
|
||||
|
||||
@@ -9,6 +9,7 @@ const storageKey = `cmbnet:${tenant}:mobile`;
|
||||
const viewMode = window.CMBNET_VIEW_MODE || 'mobile_dispatcher';
|
||||
const currentAccountId = Number(window.CMBNET_ACCOUNT_ID) || null;
|
||||
const currentRole = window.CMBNET_ROLE || null;
|
||||
const appTitle = window.CMBNET_APP_TITLE || '20*C+M+B+27';
|
||||
|
||||
const DEFAULT_SETTINGS = {
|
||||
opacity: 0.7,
|
||||
@@ -248,7 +249,7 @@ async function requestNotifications() {
|
||||
if (permission === 'granted' && navigator.serviceWorker) {
|
||||
const registration = await navigator.serviceWorker.getRegistration();
|
||||
if (registration && registration.showNotification) {
|
||||
await registration.showNotification('CMBNet', {
|
||||
await registration.showNotification(appTitle, {
|
||||
body: 'Benachrichtigungen sind aktiviert.',
|
||||
icon: window.CMBNET_ICON_URL || undefined,
|
||||
tag: 'cmbnet-permission'
|
||||
@@ -395,7 +396,11 @@ function groupDisplayName(group) {
|
||||
|
||||
function findGroupForAccount(accountId) {
|
||||
const id = Number(accountId);
|
||||
return state.groups.find(group => Number(group.account_id) === id) || null;
|
||||
if (!Number.isFinite(id)) return null;
|
||||
return state.groups.find(group =>
|
||||
Array.isArray(group.account_ids)
|
||||
&& group.account_ids.some(value => Number(value) === id)
|
||||
) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -490,8 +495,7 @@ function getReceiverName(message, payload) {
|
||||
group = state.groups[0];
|
||||
}
|
||||
if (group) {
|
||||
const accountName = getAccountDisplayName(group.account_id);
|
||||
return accountName || groupDisplayName(group) || 'Gruppe';
|
||||
return groupDisplayName(group) || 'Gruppe';
|
||||
}
|
||||
|
||||
const accountName = getAccountDisplayName(receiverId);
|
||||
@@ -927,11 +931,11 @@ async function showChatPopup(text, senderName) {
|
||||
if (navigator.serviceWorker) {
|
||||
const registration = await navigator.serviceWorker.getRegistration();
|
||||
if (registration && registration.showNotification) {
|
||||
await registration.showNotification(senderName || 'CMBNet', options);
|
||||
await registration.showNotification(senderName || appTitle, options);
|
||||
return;
|
||||
}
|
||||
}
|
||||
new Notification(senderName || 'CMBNet', options);
|
||||
new Notification(senderName || appTitle, options);
|
||||
} catch (error) {
|
||||
// Benachrichtigungen sind optional.
|
||||
}
|
||||
@@ -983,6 +987,15 @@ function handleWs(event) {
|
||||
if (state.ws && state.ws.readyState === WebSocket.OPEN) {
|
||||
state.ws.send(JSON.stringify({code: 2, msg: 'pong'}));
|
||||
}
|
||||
} else if (message.code === 99) {
|
||||
const text = typeof message.msg === 'string'
|
||||
? message.msg
|
||||
: extractText(message.msg);
|
||||
if (typeof window.showReloadNotice === 'function') {
|
||||
window.showReloadNotice(text);
|
||||
} else {
|
||||
alert(`${text}\n\nBitte die Webseite neu laden.`);
|
||||
}
|
||||
} else if (message.code === 100) {
|
||||
// Street update
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user