116 lines
4.2 KiB
JavaScript
116 lines
4.2 KiB
JavaScript
const { renderView } = require('../../../view');
|
|
|
|
function formatDateTime(value) {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) {
|
|
return '';
|
|
}
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
|
}
|
|
|
|
function formatTime(value) {
|
|
return value ? String(value).slice(0, 5) : '';
|
|
}
|
|
|
|
function formatDays(daysJson) {
|
|
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
let days = [];
|
|
if (Array.isArray(daysJson)) {
|
|
days = daysJson;
|
|
} else if (daysJson) {
|
|
try {
|
|
const parsed = JSON.parse(daysJson);
|
|
days = Array.isArray(parsed) ? parsed : [];
|
|
} catch (_error) {
|
|
days = [];
|
|
}
|
|
}
|
|
return days.map((day) => dayNames[Number(day)] || '').filter(Boolean).join(', ');
|
|
}
|
|
|
|
function scheduleSummary(item) {
|
|
const mode = String(item.schedule_mode || 'always');
|
|
if (mode === 'dates') {
|
|
const start = formatDateTime(item.schedule_start_datetime);
|
|
const end = formatDateTime(item.schedule_end_datetime);
|
|
return start && end ? `Dates: ${start} to ${end}` : 'Dates: not set';
|
|
}
|
|
if (mode === 'times') {
|
|
const days = formatDays(item.schedule_days_json);
|
|
const start = formatTime(item.schedule_start_time);
|
|
const end = formatTime(item.schedule_end_time);
|
|
return days && start && end ? `Times: ${days} ${start}-${end}` : 'Times: not set';
|
|
}
|
|
return 'Always visible';
|
|
}
|
|
|
|
function getCanvasSignature(width, height) {
|
|
const normalizedWidth = Number(width);
|
|
const normalizedHeight = Number(height);
|
|
if (!Number.isFinite(normalizedWidth) || !Number.isFinite(normalizedHeight)) {
|
|
return null;
|
|
}
|
|
return normalizedWidth + 'x' + normalizedHeight;
|
|
}
|
|
|
|
module.exports = function renderPlaylistEditPage(playlist, data, message, currentUser) {
|
|
const playlistSlides = (data.playlistSlides || [])
|
|
.filter((item) => item.playlist_id === playlist.id)
|
|
.sort((a, b) => {
|
|
if (a.position !== b.position) {
|
|
return a.position - b.position;
|
|
}
|
|
return a.id - b.id;
|
|
})
|
|
.map((item, index, items) => Object.assign({}, item, {
|
|
displayOrder: index + 1,
|
|
isFirst: index === 0,
|
|
isLast: index === items.length - 1,
|
|
canvasSignature: getCanvasSignature(item.canvas_width, item.canvas_height) || '',
|
|
scheduleSummary: scheduleSummary(item)
|
|
}));
|
|
const assignedSlideIds = new Set(playlistSlides.map((item) => item.slide_id));
|
|
const playlistCanvasSignatures = Array.from(new Set(playlistSlides.map((item) => getCanvasSignature(item.canvas_width, item.canvas_height)).filter(Boolean)));
|
|
const playlistCanvasSignature = playlistCanvasSignatures.length === 1 ? playlistCanvasSignatures[0] : null;
|
|
const playlistCanvasMismatch = playlistCanvasSignatures.length > 1;
|
|
const selectableSlides = (data.slides || []).filter((slide) => {
|
|
if (playlistCanvasMismatch) {
|
|
return false;
|
|
}
|
|
if (!playlistCanvasSignature) {
|
|
return true;
|
|
}
|
|
return getCanvasSignature(slide.canvas_width, slide.canvas_height) === playlistCanvasSignature;
|
|
}).map((slide) => Object.assign({}, slide, {
|
|
canvasSignature: getCanvasSignature(slide.canvas_width, slide.canvas_height) || '',
|
|
isAssigned: assignedSlideIds.has(slide.id)
|
|
}));
|
|
const hasAddableSlides = selectableSlides.some((slide) => !slide.isAssigned);
|
|
|
|
return renderView('playlists/edit', {
|
|
title: 'Edit playlist',
|
|
active: 'playlists',
|
|
message: message,
|
|
currentUser: currentUser || null,
|
|
playlist: playlist,
|
|
playlistFadeBetweenSlides: Boolean(playlist.fade_between_slides),
|
|
playlistSlides: playlistSlides,
|
|
slides: data.slides || [],
|
|
hasSlides: (data.slides || []).length > 0,
|
|
hasSelectableSlides: selectableSlides.length > 0,
|
|
hasAddableSlides: hasAddableSlides,
|
|
selectableSlides: selectableSlides,
|
|
playlistCanvasSignature: playlistCanvasSignature,
|
|
playlistCanvasMismatch: playlistCanvasMismatch,
|
|
scripts: ['js/vendor/sortable.min.js', 'js/playlists/playlist-schedule.js']
|
|
});
|
|
};
|