Nachrichten Popup und Positionsmarker im mobilen View
This commit is contained in:
1 parent
6e24c79008
commit
18278fdf43
4 files changed
+211
-49
No files matched your search
@@ -2539,10 +2539,12 @@ function openChat(groupId) {
|
||||
dispatcherState.chatGroupId = Number(groupId);
|
||||
renderChat();
|
||||
const dialog = document.getElementById('chatModal');
|
||||
if (dialog && typeof dialog.showModal === 'function') {
|
||||
dialog.showModal();
|
||||
} else if (dialog) {
|
||||
dialog.show();
|
||||
if (dialog && !dialog.open) {
|
||||
if (typeof dialog.showModal === 'function') {
|
||||
dialog.showModal();
|
||||
} else {
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
const container = document.getElementById('chatMessages');
|
||||
if (container) {
|
||||
@@ -2850,10 +2852,12 @@ function showInfoMessage(message) {
|
||||
|
||||
renderInfoMessages();
|
||||
|
||||
if (typeof dialog.showModal === 'function') {
|
||||
dialog.showModal();
|
||||
} else {
|
||||
dialog.show();
|
||||
if (!dialog.open) {
|
||||
if (typeof dialog.showModal === 'function') {
|
||||
dialog.showModal();
|
||||
} else {
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
const container = document.getElementById('infoMessages');
|
||||
if (container) {
|
||||
@@ -2948,7 +2952,12 @@ function handleGroupChatMessage(message) {
|
||||
const payload = parsePayload(message.msg) || {};
|
||||
const groupId = Number(payload.group_id ?? message.receiver_id);
|
||||
const group = dispatcherState.groupById.get(groupId);
|
||||
if (!group) return;
|
||||
if (!group) {
|
||||
if (Number(message.sender_id) !== currentAccountId) {
|
||||
showInfoMessage(message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const text = extractMessageText(payload.text ?? payload.msg ?? '');
|
||||
|
||||
@@ -2963,6 +2972,11 @@ function handleGroupChatMessage(message) {
|
||||
ts: normalizeTimestamp(message.ts) || Date.now(),
|
||||
text
|
||||
});
|
||||
|
||||
if (Number(message.sender_id) !== currentAccountId) {
|
||||
playNotificationSound();
|
||||
openChat(group.id);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
@@ -3160,8 +3174,9 @@ function handleWsMessage(event) {
|
||||
break;
|
||||
}
|
||||
case 1100: {
|
||||
const groupId = Number(message.receiver_id) || Number((message.msg && typeof message.msg === 'object' && message.msg.group_id));
|
||||
const text = extractMessageText(message.msg);
|
||||
const payload = parsePayload(message.msg) || {};
|
||||
const groupId = Number(message.receiver_id) || Number(payload.group_id);
|
||||
const text = extractMessageText(payload.text ?? message.msg);
|
||||
if (window.CMBNET_VIEW_MODE !== 'observer') {
|
||||
if (groupId) {
|
||||
addChatMessage(groupId, {
|
||||
@@ -3181,13 +3196,18 @@ function handleWsMessage(event) {
|
||||
ts: message.ts
|
||||
});
|
||||
} else if (Number(message.sender_id) !== currentAccountId) {
|
||||
showInfoMessage({
|
||||
code: message.code,
|
||||
sender_id: message.sender_id,
|
||||
receiver_id: groupId || message.receiver_id,
|
||||
msg: text,
|
||||
ts: message.ts
|
||||
});
|
||||
playNotificationSound();
|
||||
if (groupId) {
|
||||
openChat(groupId);
|
||||
} else {
|
||||
showInfoMessage({
|
||||
code: message.code,
|
||||
sender_id: message.sender_id,
|
||||
receiver_id: message.receiver_id,
|
||||
msg: text,
|
||||
ts: message.ts
|
||||
});
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,15 @@ const state = {
|
||||
chatMessages: [],
|
||||
chatKeys: new Set(),
|
||||
userMarker: null,
|
||||
userArrow: null,
|
||||
userHeadingLine: null,
|
||||
userHeading: null,
|
||||
lastCompassHeading: null,
|
||||
lastCompassAt: 0,
|
||||
absoluteCompassAvailable: false,
|
||||
lastGpsHeading: null,
|
||||
lastGpsAt: 0,
|
||||
orientationListenersBound: false,
|
||||
orientationPermissionRequested: false,
|
||||
watchId: null,
|
||||
positionSendTimerId: null,
|
||||
navigationTimerId: null,
|
||||
@@ -426,11 +434,13 @@ function sendPositionNow() {
|
||||
navigator.geolocation.getCurrentPosition((position) => {
|
||||
const lat = Number(position.coords.latitude);
|
||||
const lon = Number(position.coords.longitude);
|
||||
const previous = state.lastKnownPosition;
|
||||
state.lastKnownPosition = {
|
||||
lat,
|
||||
lon,
|
||||
accuracy: position.coords.accuracy
|
||||
};
|
||||
updateHeadingFromGps(position, previous);
|
||||
updateOwnPositionMarker(lat, lon);
|
||||
|
||||
const sent = sendOwnPosition(lat, lon, false);
|
||||
@@ -1031,27 +1041,31 @@ function startGeolocation() {
|
||||
map.removeLayer(state.userMarker);
|
||||
state.userMarker = null;
|
||||
}
|
||||
if (state.userArrow) {
|
||||
map.removeLayer(state.userArrow);
|
||||
state.userArrow = null;
|
||||
}
|
||||
state.userHeadingLine = null;
|
||||
|
||||
const icon = L.divIcon({
|
||||
className: 'mobile-user-marker',
|
||||
html: '<div class="mobile-user-dot"></div><div class="mobile-user-arrow" id="mobileUserArrow"></div>',
|
||||
html: '<div class="mobile-user-heading" id="mobileUserHeading" hidden></div><div class="mobile-user-dot"></div>',
|
||||
iconSize: [16, 16],
|
||||
iconAnchor: [8, 8]
|
||||
});
|
||||
state.userMarker = L.marker(map.getCenter(), {icon, interactive: false}).addTo(map);
|
||||
state.userArrow = document.getElementById('mobileUserArrow');
|
||||
|
||||
state.userHeadingLine = document.getElementById('mobileUserHeading');
|
||||
applyUserHeading(state.userHeading);
|
||||
|
||||
state.watchId = navigator.geolocation.watchPosition((pos) => {
|
||||
const lat = Number(pos.coords.latitude);
|
||||
const lon = Number(pos.coords.longitude);
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return;
|
||||
|
||||
const previous = state.lastKnownPosition;
|
||||
state.lastKnownPosition = {
|
||||
lat,
|
||||
lon,
|
||||
accuracy: pos.coords.accuracy
|
||||
};
|
||||
|
||||
updateHeadingFromGps(pos, previous);
|
||||
updateOwnPositionMarker(lat, lon);
|
||||
|
||||
if (viewMode === 'mobile_group') {
|
||||
@@ -1069,21 +1083,123 @@ function startGeolocation() {
|
||||
AUTO_POSITION_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
|
||||
if (window.DeviceOrientationEvent) {
|
||||
window.addEventListener('deviceorientationabsolute', handleOrientation, true);
|
||||
window.addEventListener('deviceorientation', handleOrientation, true);
|
||||
|
||||
setupDeviceOrientation();
|
||||
}
|
||||
|
||||
function normalizeHeading(value) {
|
||||
if (value === null || value === undefined || value === '') return null;
|
||||
const heading = Number(value);
|
||||
if (!Number.isFinite(heading)) return null;
|
||||
return ((heading % 360) + 360) % 360;
|
||||
}
|
||||
|
||||
function applyUserHeading(heading) {
|
||||
const normalized = normalizeHeading(heading);
|
||||
if (normalized === null) return;
|
||||
|
||||
state.userHeading = normalized;
|
||||
const line = state.userHeadingLine;
|
||||
if (!line) return;
|
||||
|
||||
line.hidden = false;
|
||||
line.style.transform = `translate(-50%, -100%) rotate(${normalized}deg)`;
|
||||
}
|
||||
|
||||
function bearingBetween(from, to) {
|
||||
if (!from || !to) return null;
|
||||
const lat1 = Number(from.lat);
|
||||
const lon1 = Number(from.lon);
|
||||
const lat2 = Number(to.lat);
|
||||
const lon2 = Number(to.lon);
|
||||
if (![lat1, lon1, lat2, lon2].every(Number.isFinite)) return null;
|
||||
|
||||
const toRad = (value) => value * Math.PI / 180;
|
||||
const y = Math.sin(toRad(lon2 - lon1)) * Math.cos(toRad(lat2));
|
||||
const x = Math.cos(toRad(lat1)) * Math.sin(toRad(lat2))
|
||||
- Math.sin(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.cos(toRad(lon2 - lon1));
|
||||
return normalizeHeading(Math.atan2(y, x) * 180 / Math.PI);
|
||||
}
|
||||
|
||||
function updateHeadingFromGps(position, previous) {
|
||||
const coords = position && position.coords;
|
||||
if (!coords) return;
|
||||
|
||||
let heading = normalizeHeading(coords.heading);
|
||||
if (heading === null && previous && state.lastKnownPosition) {
|
||||
const moved = L.latLng(previous.lat, previous.lon)
|
||||
.distanceTo(L.latLng(state.lastKnownPosition.lat, state.lastKnownPosition.lon));
|
||||
if (moved >= 3) {
|
||||
heading = bearingBetween(previous, state.lastKnownPosition);
|
||||
}
|
||||
}
|
||||
if (heading === null) return;
|
||||
|
||||
state.lastGpsHeading = heading;
|
||||
state.lastGpsAt = Date.now();
|
||||
|
||||
// Der Kompass hat Vorrang. GPS dient als Rückfallebene, solange keine
|
||||
// aktuelle Kompassrichtung vorliegt (z. B. ohne Sensorfreigabe).
|
||||
if (Date.now() - state.lastCompassAt > 10000) {
|
||||
applyUserHeading(heading);
|
||||
}
|
||||
}
|
||||
|
||||
function handleOrientation(e) {
|
||||
let heading = e.alpha;
|
||||
if (typeof e.webkitCompassHeading === 'number') {
|
||||
heading = e.webkitCompassHeading;
|
||||
function handleOrientation(event) {
|
||||
const hasCompassHeading = typeof event.webkitCompassHeading === 'number'
|
||||
&& Number.isFinite(event.webkitCompassHeading);
|
||||
const isAbsolute = hasCompassHeading
|
||||
|| event.type === 'deviceorientationabsolute'
|
||||
|| event.absolute === true;
|
||||
|
||||
let heading = null;
|
||||
if (hasCompassHeading) {
|
||||
heading = event.webkitCompassHeading;
|
||||
} else if (typeof event.alpha === 'number'
|
||||
&& Number.isFinite(event.alpha)
|
||||
&& (isAbsolute || !state.absoluteCompassAvailable)) {
|
||||
// DeviceOrientationEvent.alpha dreht gegen den Uhrzeigersinn.
|
||||
// Relative Werte dienen nur als Rückfall, solange keine absolute
|
||||
// Kompassrichtung verfügbar ist.
|
||||
heading = 360 - event.alpha;
|
||||
}
|
||||
if (typeof heading !== 'number' || isNaN(heading)) return;
|
||||
if (state.userArrow) {
|
||||
state.userArrow.style.transform = `translate(-50%, -100%) rotate(${heading}deg)`;
|
||||
|
||||
const normalized = normalizeHeading(heading);
|
||||
if (normalized === null) return;
|
||||
|
||||
if (isAbsolute) state.absoluteCompassAvailable = true;
|
||||
state.lastCompassHeading = normalized;
|
||||
state.lastCompassAt = Date.now();
|
||||
applyUserHeading(normalized);
|
||||
}
|
||||
|
||||
function setupDeviceOrientation() {
|
||||
if (!window.DeviceOrientationEvent || state.orientationListenersBound) return;
|
||||
|
||||
const addListeners = () => {
|
||||
if (state.orientationListenersBound) return;
|
||||
window.addEventListener('deviceorientationabsolute', handleOrientation, true);
|
||||
window.addEventListener('deviceorientation', handleOrientation, true);
|
||||
state.orientationListenersBound = true;
|
||||
};
|
||||
|
||||
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
|
||||
// iOS verlangt die Freigabe aus einer Nutzergeste. Die erste Berührung
|
||||
// der Karte bzw. Seite wird dafür verwendet.
|
||||
const requestPermission = () => {
|
||||
if (state.orientationPermissionRequested) return;
|
||||
state.orientationPermissionRequested = true;
|
||||
DeviceOrientationEvent.requestPermission()
|
||||
.then((result) => {
|
||||
if (result === 'granted') addListeners();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn('Kompassfreigabe nicht erteilt.', error);
|
||||
});
|
||||
};
|
||||
document.addEventListener('pointerdown', requestPermission, {once: true, passive: true});
|
||||
} else {
|
||||
addListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1100,11 +1216,13 @@ function showAllGroupPositions() {
|
||||
const icon = L.divIcon({
|
||||
className: 'mobile-group-marker',
|
||||
html: `<div class="mobile-group-dot" style="--marker-color:${color}"></div>`,
|
||||
iconSize: [14, 14],
|
||||
iconAnchor: [7, 7]
|
||||
iconSize: [32, 32],
|
||||
iconAnchor: [16, 16]
|
||||
});
|
||||
const marker = L.marker([lat, lon], {icon}).addTo(map);
|
||||
marker.bindTooltip(group.aliasname || String(group.id));
|
||||
if (viewMode === 'mobile_dispatcher') {
|
||||
marker.on('click', () => focusGroupFromMarker(group.id));
|
||||
}
|
||||
state.groupMarkers.set(group.id, marker);
|
||||
}
|
||||
});
|
||||
@@ -1112,6 +1230,21 @@ function showAllGroupPositions() {
|
||||
updateNavigationButton();
|
||||
}
|
||||
|
||||
async function focusGroupFromMarker(groupId) {
|
||||
if (viewMode !== 'mobile_dispatcher') return;
|
||||
|
||||
const group = state.groupById.get(Number(groupId));
|
||||
if (!group) return;
|
||||
|
||||
if (Number(state.currentGroupId) !== Number(group.id)) {
|
||||
await loadGroupData(group.id);
|
||||
}
|
||||
|
||||
const coordinates = getGroupCoordinates(group);
|
||||
if (!coordinates) return;
|
||||
map.setView([coordinates.lat, coordinates.lon], Math.max(map.getZoom(), 15));
|
||||
}
|
||||
|
||||
function getGroupColor(id) {
|
||||
const hue = Math.round((Number(id) * 137.508) % 360);
|
||||
return `hsl(${hue}, 72%, 45%)`;
|
||||
|
||||
Reference in new issue
Block a user