- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
272 lines
12 KiB
JavaScript
Executable File
272 lines
12 KiB
JavaScript
Executable File
const CLASSES = ['Economy', 'Business', 'Excelsior'];
|
|
|
|
const app = {
|
|
user: null,
|
|
estimates: {},
|
|
selectedClass: null,
|
|
booking: false,
|
|
estimateSeq: 0,
|
|
|
|
init: () => {
|
|
app.user = {
|
|
name: localStorage.getItem('delamain_username'),
|
|
token: localStorage.getItem('delamain_token'),
|
|
};
|
|
app.bindEvents();
|
|
app.updateAuthUI();
|
|
app.loadTrips();
|
|
},
|
|
|
|
updateAuthUI: () => {
|
|
const logged = !!(app.user && app.user.name);
|
|
document.getElementById('btn-login').style.display = logged ? 'none' : 'block';
|
|
document.getElementById('btn-logout').style.display = logged ? 'block' : 'none';
|
|
document.getElementById('auth-hello').textContent = logged ? `Welcome, ${app.user.name}` : '';
|
|
},
|
|
|
|
bindEvents: () => {
|
|
document.querySelectorAll('.nav-btn').forEach((btn) => {
|
|
btn.addEventListener('click', () => app.switchSection(btn.dataset.section));
|
|
});
|
|
document.querySelector('.brand').addEventListener('click', () => app.switchSection('section-rides'));
|
|
document.querySelectorAll('.close').forEach((c) =>
|
|
c.addEventListener('click', () => utils.hideModal(c.closest('.modal').id)));
|
|
window.addEventListener('click', (e) => {
|
|
if (e.target.classList.contains('modal')) utils.hideModal(e.target.id);
|
|
});
|
|
|
|
document.getElementById('btn-login').addEventListener('click', () => utils.showModal('login-modal'));
|
|
document.getElementById('btn-logout').addEventListener('click', app.logout);
|
|
document.getElementById('btn-register').addEventListener('click', app.register);
|
|
document.getElementById('login-form').addEventListener('submit', app.login);
|
|
|
|
document.getElementById('btn-refresh-trips').addEventListener('click', app.loadTrips);
|
|
document.getElementById('btn-book').addEventListener('click', app.bookRide);
|
|
document.getElementById('btn-new-ride').addEventListener('click', app.newRide);
|
|
|
|
|
|
document.getElementById('trips-list').addEventListener('click', (e) => {
|
|
const btn = e.target.closest('button[data-act]');
|
|
if (!btn) return;
|
|
const id = btn.dataset.id;
|
|
if (btn.dataset.act === 'view') app.showTripDetails(id);
|
|
if (btn.dataset.act === 'versions') app.showVersions(id);
|
|
if (btn.dataset.act === 'export') app.exportTrip(id);
|
|
});
|
|
|
|
|
|
NightCityMap.init(document.getElementById('nc-map'), app.onPointsChanged);
|
|
},
|
|
|
|
switchSection: (id) => {
|
|
document.querySelectorAll('.nav-btn').forEach((b) => b.classList.toggle('active', b.dataset.section === id));
|
|
document.querySelectorAll('.section').forEach((s) => s.classList.toggle('active', s.id === id));
|
|
if (id === 'section-trips') app.loadTrips();
|
|
if (id === 'section-rides') window.setTimeout(() => NightCityMap.refresh(), 0);
|
|
},
|
|
|
|
onPointsChanged: async ({ A, B }) => {
|
|
const estimateBlock = document.getElementById('estimate-block');
|
|
const estimateLoading = document.getElementById('estimate-loading');
|
|
const noPoints = document.getElementById('no-points');
|
|
const booked = document.getElementById('booked-block');
|
|
const pickup = document.getElementById('pickup-display');
|
|
const destination = document.getElementById('destination-display');
|
|
const hint = document.getElementById('map-hint-text');
|
|
const seq = ++app.estimateSeq;
|
|
|
|
pickup.textContent = A ? A.name : 'NOT SELECTED';
|
|
destination.textContent = B ? B.name : 'NOT SELECTED';
|
|
pickup.classList.toggle('is-set', !!A);
|
|
destination.classList.toggle('is-set', !!B);
|
|
hint.textContent = !A ? 'SET PICKUP POINT' : !B ? 'SET DESTINATION' : 'CLICK TO START A NEW ROUTE';
|
|
app.estimates = {};
|
|
app.selectedClass = null;
|
|
document.getElementById('btn-book').disabled = true;
|
|
|
|
if (!A || !B) {
|
|
estimateBlock.style.display = 'none';
|
|
estimateLoading.style.display = 'none';
|
|
booked.style.display = 'none';
|
|
noPoints.style.display = 'block';
|
|
return;
|
|
}
|
|
|
|
booked.style.display = 'none';
|
|
noPoints.style.display = 'none';
|
|
estimateBlock.style.display = 'none';
|
|
estimateLoading.style.display = 'block';
|
|
document.getElementById('route-summary').textContent = `${A.name} → ${B.name}`;
|
|
|
|
const results = await Promise.all(
|
|
CLASSES.map((cls) => api.estimate(A.x, A.y, B.x, B.y, cls).catch(() => null))
|
|
);
|
|
if (seq !== app.estimateSeq) return;
|
|
estimateLoading.style.display = 'none';
|
|
estimateBlock.style.display = 'block';
|
|
const container = document.getElementById('class-cards');
|
|
container.innerHTML = '';
|
|
results.forEach((r, i) => {
|
|
if (!r || r.status !== 0) return;
|
|
app.estimates[CLASSES[i]] = r;
|
|
const card = document.createElement('div');
|
|
card.className = 'class-card';
|
|
card.dataset.cls = CLASSES[i];
|
|
const serviceNames = ['Standard', 'Executive', 'Excelsior'];
|
|
card.innerHTML = `
|
|
<div class="class-name">${serviceNames[i]}</div>
|
|
<div class="class-meta">${r.duration_min} min · ${r.distance_km} km</div>
|
|
<div class="class-price">${r.price} ¢</div>`;
|
|
card.addEventListener('click', () => {
|
|
app.selectedClass = CLASSES[i];
|
|
container.querySelectorAll('.class-card').forEach((c) =>
|
|
c.classList.toggle('selected', c.dataset.cls === CLASSES[i]));
|
|
document.getElementById('btn-book').disabled = app.booking;
|
|
});
|
|
container.appendChild(card);
|
|
});
|
|
},
|
|
|
|
bookRide: async () => {
|
|
const { A, B } = NightCityMap.points;
|
|
const cls = app.selectedClass;
|
|
if (!A || !B || !cls || app.booking) return;
|
|
app.booking = true;
|
|
document.getElementById('btn-book').disabled = true;
|
|
|
|
try {
|
|
const resp = await api.bookRide({
|
|
origin: A.name,
|
|
destination: B.name,
|
|
from_x: A.x, from_y: A.y,
|
|
to_x: B.x, to_y: B.y,
|
|
class: cls,
|
|
stops: [A.name, B.name],
|
|
instruction: document.getElementById('ride-instruction').value,
|
|
});
|
|
const est = app.estimates[cls] || {};
|
|
document.getElementById('estimate-block').style.display = 'none';
|
|
const booked = document.getElementById('booked-block');
|
|
booked.style.display = 'block';
|
|
document.getElementById('booked-card').innerHTML = `
|
|
<div class="booked-id">${utils.escapeHtml(resp.trip_id)}</div>
|
|
<div class="booked-route">${utils.escapeHtml(A.name)} → ${utils.escapeHtml(B.name)}</div>
|
|
<div class="booked-meta">${cls} · ${est.duration_min || '?'} min · ${est.price || '?'} ¢</div>
|
|
<div class="booked-driver">Delamain cab #${Math.floor(Math.random() * 900 + 100)} is on the way</div>
|
|
<div class="dim mono">instruction version: ${utils.escapeHtml(resp.version_token || '')}</div>`;
|
|
utils.notify(`Ride ${resp.trip_id} booked!`);
|
|
} catch (err) {
|
|
utils.showError(err);
|
|
document.getElementById('btn-book').disabled = false;
|
|
} finally {
|
|
app.booking = false;
|
|
}
|
|
},
|
|
|
|
newRide: () => {
|
|
NightCityMap.reset();
|
|
document.getElementById('booked-block').style.display = 'none';
|
|
document.getElementById('ride-instruction').value = '';
|
|
utils.notify('Route cleared');
|
|
},
|
|
|
|
loadTrips: async () => {
|
|
const list = document.getElementById('trips-list');
|
|
try {
|
|
const resp = await api.getAllTrips();
|
|
list.innerHTML = '';
|
|
if (resp.trips && resp.trips.length) {
|
|
resp.trips.forEach((t) => list.appendChild(utils.tripCard(t)));
|
|
} else {
|
|
utils.empty('trips-list', 'No rides yet. Book your first Delamain ride!');
|
|
}
|
|
} catch (err) {
|
|
utils.empty('trips-list', 'Failed to load trips. Is the service running?');
|
|
}
|
|
},
|
|
|
|
showTripDetails: async (tripId) => {
|
|
try {
|
|
const trip = await api.getTrip(tripId);
|
|
const stops = (trip.stops || [])
|
|
.map((s, i) => `${i + 1}. ${s.name} (${Math.round(s.x)},${Math.round(s.y)})${s.is_pickup ? ' — pickup' : ''}`)
|
|
.join('\n');
|
|
alert(
|
|
`Ride ${trip.trip_id}\nClass: ${trip.trip.class}\nStatus: ${trip.trip.status}\n\n` +
|
|
`Route:\n${stops}\n\nPrivate instruction:\n${trip.instruction}\n\nVersion token: ${trip.version_token}`
|
|
);
|
|
} catch (err) {
|
|
utils.showError(err);
|
|
}
|
|
},
|
|
|
|
showVersions: async (tripId) => {
|
|
try {
|
|
const resp = await api.getVersions(tripId);
|
|
const rows = (resp.versions || [])
|
|
.map((v) => `${v.version_token} ${v.size}b ${utils.formatDate(v.created_at)}`)
|
|
.join('\n');
|
|
alert(`Instruction versions for ${tripId}:\n\n${rows || '(none)'}`);
|
|
} catch (err) {
|
|
utils.showError(err);
|
|
}
|
|
},
|
|
|
|
exportTrip: async (tripId) => {
|
|
try {
|
|
const pkg = await api.createPackage(tripId);
|
|
const rows = (pkg.notes || []).map((n) => {
|
|
if (n.skipped) {
|
|
return `<div class="trip-detail"><span class="label">${utils.escapeHtml(n.source || 'note')}</span><span class="error-text">${utils.escapeHtml(n.skipped)}</span></div>`;
|
|
}
|
|
return `<div class="trip-detail"><span class="label">Private instruction</span><span class="mono">${utils.escapeHtml(n.data || '')}</span></div>`;
|
|
}).join('');
|
|
document.getElementById('export-result').innerHTML = rows || '<div class="empty">No note segments</div>';
|
|
utils.showModal('export-modal');
|
|
} catch (err) {
|
|
utils.showError(err);
|
|
}
|
|
},
|
|
|
|
login: async (e) => {
|
|
e.preventDefault();
|
|
const username = document.getElementById('username').value.trim();
|
|
const password = document.getElementById('password').value;
|
|
try {
|
|
const resp = await api.login(username, password);
|
|
app.user = { name: resp.username, token: resp.token };
|
|
localStorage.setItem('delamain_username', resp.username);
|
|
localStorage.setItem('delamain_token', resp.token);
|
|
utils.hideModal('login-modal');
|
|
app.updateAuthUI();
|
|
utils.notify(`Welcome back, ${resp.username}! Ride in style.`);
|
|
app.loadTrips();
|
|
} catch (err) {
|
|
utils.showError(err);
|
|
}
|
|
},
|
|
|
|
register: async () => {
|
|
const username = document.getElementById('username').value.trim();
|
|
const password = document.getElementById('password').value;
|
|
if (!username || !password) return utils.notify('Enter username and password', 'error');
|
|
try {
|
|
await api.createUser(username, password);
|
|
utils.notify('Account created, logging in...');
|
|
await app.login({ preventDefault: () => {} });
|
|
} catch (err) {
|
|
utils.showError(err);
|
|
}
|
|
},
|
|
|
|
logout: () => {
|
|
app.user = null;
|
|
localStorage.removeItem('delamain_username');
|
|
localStorage.removeItem('delamain_token');
|
|
app.updateAuthUI();
|
|
utils.notify('Logged out');
|
|
},
|
|
};
|
|
|
|
document.addEventListener('DOMContentLoaded', app.init);
|