Add journey sharing and collaboration
This commit is contained in:
1 parent
770ab0e5df
commit
c136f01f2e
4 files changed
+262
-17
No files matched your search
+160
-2
@@ -113,6 +113,52 @@
|
||||
.form-group {
|
||||
margin-bottom: var(--size-4);
|
||||
}
|
||||
.sharing-panel {
|
||||
display: none;
|
||||
background: var(--surface-2);
|
||||
border: 1px solid var(--surface-4);
|
||||
border-radius: var(--radius-2);
|
||||
padding: var(--size-4);
|
||||
margin-bottom: var(--size-4);
|
||||
}
|
||||
.sharing-panel.visible {
|
||||
display: block;
|
||||
}
|
||||
.sharing-panel h3 {
|
||||
margin-top: 0;
|
||||
margin-bottom: var(--size-2);
|
||||
font-size: var(--font-size-3);
|
||||
}
|
||||
.sharing-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||
gap: var(--size-4);
|
||||
}
|
||||
.sharing-user-list {
|
||||
display: grid;
|
||||
gap: var(--size-2);
|
||||
}
|
||||
.sharing-user {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--size-2);
|
||||
padding: var(--size-2);
|
||||
border-radius: var(--radius-2);
|
||||
background: var(--surface-1);
|
||||
}
|
||||
.sharing-user input {
|
||||
width: auto;
|
||||
}
|
||||
.sharing-help {
|
||||
color: var(--gray-6);
|
||||
font-size: var(--font-size-1);
|
||||
margin-top: 0;
|
||||
}
|
||||
.empty-share-list {
|
||||
color: var(--gray-6);
|
||||
font-style: italic;
|
||||
margin: 0;
|
||||
}
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: var(--size-2);
|
||||
@@ -302,6 +348,20 @@
|
||||
<option value="shared">Shared (with specific users)</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="sharing-panel" class="sharing-panel">
|
||||
<h3><i class="fas fa-user-group"></i> Share With Users</h3>
|
||||
<p class="sharing-help">Viewers can open the journey. Editors can also update its chapters, markers, and images.</p>
|
||||
<div class="sharing-grid">
|
||||
<div>
|
||||
<label>Can View</label>
|
||||
<div id="shared-read-users" class="sharing-user-list"></div>
|
||||
</div>
|
||||
<div>
|
||||
<label>Can Edit</label>
|
||||
<div id="shared-edit-users" class="sharing-user-list"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h3><i class="fas fa-map-marker-alt"></i> Chapters (Markers)</h3>
|
||||
<div id="markers-container"></div>
|
||||
@@ -323,7 +383,11 @@
|
||||
<script>
|
||||
// ==================== GLOBALS ====================
|
||||
let currentJourneyId = null;
|
||||
let currentJourneyOwnerId = null;
|
||||
let markersData = [];
|
||||
let availableUsers = [];
|
||||
let sharedReadIds = [];
|
||||
let sharedEditIds = [];
|
||||
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const journeyId = urlParams.get('id');
|
||||
@@ -359,6 +423,85 @@
|
||||
return { ...marker, images };
|
||||
}
|
||||
|
||||
function canManageSharing() {
|
||||
return !currentJourneyOwnerId || currentJourneyOwnerId === currentUser?.id;
|
||||
}
|
||||
|
||||
function uniqueIds(ids) {
|
||||
return [...new Set((ids || []).map((id) => parseInt(id)).filter(Number.isInteger))];
|
||||
}
|
||||
|
||||
async function loadUsers() {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/users`, { credentials: 'include' });
|
||||
if (!res.ok) throw new Error('Could not load users');
|
||||
availableUsers = await res.json();
|
||||
} catch (err) {
|
||||
availableUsers = [];
|
||||
showToast('Sharing users could not be loaded', true);
|
||||
}
|
||||
}
|
||||
|
||||
function getUserCheckboxHtml(user, group, checked, disabled) {
|
||||
return `
|
||||
<label class="sharing-user">
|
||||
<input type="checkbox" class="${group}-user" value="${user.id}" ${checked ? 'checked' : ''} ${disabled ? 'disabled' : ''}>
|
||||
<span>${escapeHtml(user.username)}</span>
|
||||
</label>
|
||||
`;
|
||||
}
|
||||
|
||||
function renderSharingControls() {
|
||||
const panel = document.getElementById('sharing-panel');
|
||||
const visibilitySelect = document.getElementById('journey-visibility');
|
||||
const visibility = visibilitySelect.value;
|
||||
const readContainer = document.getElementById('shared-read-users');
|
||||
const editContainer = document.getElementById('shared-edit-users');
|
||||
const isShared = visibility === 'shared';
|
||||
const disabled = !canManageSharing();
|
||||
|
||||
visibilitySelect.disabled = disabled;
|
||||
panel.classList.toggle('visible', isShared);
|
||||
if (!isShared) return;
|
||||
|
||||
if (!availableUsers.length) {
|
||||
readContainer.innerHTML = '<p class="empty-share-list">No other users found.</p>';
|
||||
editContainer.innerHTML = '<p class="empty-share-list">No other users found.</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
readContainer.innerHTML = availableUsers.map((user) =>
|
||||
getUserCheckboxHtml(user, 'shared-read', sharedReadIds.includes(user.id), disabled)
|
||||
).join('');
|
||||
editContainer.innerHTML = availableUsers.map((user) =>
|
||||
getUserCheckboxHtml(user, 'shared-edit', sharedEditIds.includes(user.id), disabled)
|
||||
).join('');
|
||||
|
||||
document.querySelectorAll('.shared-read-user').forEach((input) => {
|
||||
input.addEventListener('change', updateSharedUsersFromForm);
|
||||
});
|
||||
document.querySelectorAll('.shared-edit-user').forEach((input) => {
|
||||
input.addEventListener('change', updateSharedUsersFromForm);
|
||||
});
|
||||
}
|
||||
|
||||
function updateSharedUsersFromForm() {
|
||||
sharedReadIds = uniqueIds([...document.querySelectorAll('.shared-read-user:checked')].map((input) => input.value));
|
||||
sharedEditIds = uniqueIds([...document.querySelectorAll('.shared-edit-user:checked')].map((input) => input.value));
|
||||
sharedReadIds = uniqueIds([...sharedReadIds, ...sharedEditIds]);
|
||||
renderSharingControls();
|
||||
}
|
||||
|
||||
function getSharingPayload(visibility) {
|
||||
if (visibility !== 'shared') {
|
||||
return { shared_read: [], shared_edit: [] };
|
||||
}
|
||||
return {
|
||||
shared_read: uniqueIds([...sharedReadIds, ...sharedEditIds]),
|
||||
shared_edit: uniqueIds(sharedEditIds)
|
||||
};
|
||||
}
|
||||
|
||||
function renderMarkerImages(marker, idx) {
|
||||
const images = marker.images || [];
|
||||
if (!images.length) {
|
||||
@@ -509,11 +652,15 @@
|
||||
const journey = await res.json();
|
||||
console.log('Journey data:', journey);
|
||||
currentJourneyId = journey.id;
|
||||
currentJourneyOwnerId = journey.owner_id;
|
||||
document.getElementById('journey-title').value = journey.title;
|
||||
document.getElementById('journey-description').value = journey.description || '';
|
||||
document.getElementById('journey-visibility').value = journey.visibility || 'private';
|
||||
sharedReadIds = uniqueIds(journey.shared_read || []);
|
||||
sharedEditIds = uniqueIds(journey.shared_edit || []);
|
||||
markersData = (journey.markers || []).map(normalizeMarkerImages);
|
||||
console.log('Markers data loaded:', markersData);
|
||||
renderSharingControls();
|
||||
renderMarkers();
|
||||
document.getElementById('form-title').textContent = 'Edit Journey';
|
||||
} catch (err) {
|
||||
@@ -547,7 +694,11 @@
|
||||
videoUrl: m.videoUrl || ''
|
||||
}));
|
||||
|
||||
const payload = { title, description, markers, visibility };
|
||||
const payload = { title, description, markers };
|
||||
if (canManageSharing()) {
|
||||
payload.visibility = visibility;
|
||||
Object.assign(payload, getSharingPayload(visibility));
|
||||
}
|
||||
console.log('Saving payload:', payload);
|
||||
|
||||
try {
|
||||
@@ -601,11 +752,17 @@
|
||||
updateUserMenu();
|
||||
|
||||
if (!isNew && journeyId) {
|
||||
loadJourney(journeyId);
|
||||
await loadUsers();
|
||||
await loadJourney(journeyId);
|
||||
} else if (isNew) {
|
||||
currentJourneyId = null;
|
||||
currentJourneyOwnerId = currentUser.id;
|
||||
document.getElementById('form-title').textContent = 'New Journey';
|
||||
markersData = [];
|
||||
sharedReadIds = [];
|
||||
sharedEditIds = [];
|
||||
await loadUsers();
|
||||
renderSharingControls();
|
||||
renderMarkers();
|
||||
} else {
|
||||
window.location.href = 'blog-list.html';
|
||||
@@ -613,6 +770,7 @@
|
||||
|
||||
document.getElementById('journey-form').addEventListener('submit', saveJourney);
|
||||
document.getElementById('add-marker').addEventListener('click', addMarker);
|
||||
document.getElementById('journey-visibility').addEventListener('change', renderSharingControls);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in new issue
Block a user