77 lines
3.8 KiB
JavaScript
77 lines
3.8 KiB
JavaScript
// Shared announcement form view-model builder.
|
|
|
|
const { DEFAULT_ANNOUNCEMENT_ICON } = require('#src/data/announcement-icons');
|
|
|
|
function buildDefaultAnnouncement() {
|
|
return {
|
|
id: null,
|
|
message: '',
|
|
short_label: '',
|
|
announcement_type: 'lower-third',
|
|
color_key: 'primary',
|
|
icon_key: DEFAULT_ANNOUNCEMENT_ICON,
|
|
durationMode: 'duration',
|
|
duration_seconds: null,
|
|
durationValue: 5,
|
|
durationUnit: 'minutes',
|
|
durationLabel: 'Until disabled',
|
|
screen_ids: []
|
|
};
|
|
}
|
|
|
|
function buildAnnouncementFormViewModel(announcement, message, currentUser, options, isEdit) {
|
|
const announcementTypes = Array.isArray(options && options.announcementTypes) ? options.announcementTypes : [];
|
|
const announcementColors = Array.isArray(options && options.announcementColors) ? options.announcementColors : [];
|
|
const announcementIcons = Array.isArray(options && options.announcementIcons) ? options.announcementIcons : [];
|
|
const announcementScreens = Array.isArray(options && options.announcementScreens) ? options.announcementScreens : [];
|
|
const viewOptions = options || {};
|
|
const viewAnnouncement = Object.assign({}, buildDefaultAnnouncement(), announcement || {});
|
|
const canDelete = Boolean(currentUser && Array.isArray(currentUser.permissionKeys) && currentUser.permissionKeys.indexOf('announcements.delete') !== -1);
|
|
const hasTargets = Array.isArray(viewAnnouncement.screen_ids) && viewAnnouncement.screen_ids.length > 0;
|
|
const isActive = !(viewAnnouncement.expires_at && new Date(viewAnnouncement.expires_at).getTime() <= Date.now());
|
|
|
|
return {
|
|
title: isEdit ? 'Edit announcement' : 'Add announcement',
|
|
active: 'announcements',
|
|
message: message,
|
|
currentUser: currentUser || null,
|
|
announcement: viewAnnouncement,
|
|
isEdit: Boolean(isEdit),
|
|
showSaveSecondaryActions: true,
|
|
messageVariant: viewOptions.messageVariant || '',
|
|
actionLabel: isActive ? 'Stop' : 'Play',
|
|
actionIcon: isActive ? 'bi-stop-fill' : 'bi-play-fill',
|
|
actionDisabled: !isActive && !hasTargets,
|
|
actionClassName: !isActive && !hasTargets
|
|
? 'btn-outline-info'
|
|
: (isActive ? 'btn-outline-warning' : 'btn-info'),
|
|
actionDisabledTitle: !isActive && !hasTargets
|
|
? 'Select at least one screen group to play this announcement.'
|
|
: '',
|
|
actionConfirmMessage: isActive
|
|
? 'Stop this announcement on the selected screens now?'
|
|
: 'Send this announcement to the selected screens now?',
|
|
actionPath: isEdit && viewAnnouncement.id
|
|
? (isActive ? '/announcements/' + viewAnnouncement.id + '/stop' : '/announcements/' + viewAnnouncement.id + '/play')
|
|
: '',
|
|
showDeleteAction: Boolean(canDelete || !isEdit),
|
|
deleteDisabled: !isEdit || !Boolean(viewAnnouncement && viewAnnouncement.canDelete),
|
|
deleteConfirmMessage: isEdit ? 'Delete this announcement?' : '',
|
|
deleteTitle: !isEdit
|
|
? 'Delete is available after the announcement has been created.'
|
|
: (!Boolean(viewAnnouncement && viewAnnouncement.canDelete) ? 'This announcement is currently active and cannot be deleted.' : ''),
|
|
formAction: isEdit && viewAnnouncement && viewAnnouncement.id ? '/announcements/' + viewAnnouncement.id : '/announcements',
|
|
formAttrs: 'data-async-save' + (isEdit ? ' data-async-save-close-url="/announcements"' : ' data-async-save-new-redirect="response-url"') + ' data-async-save-new-url="/announcements/new"',
|
|
cancelUrl: '/announcements',
|
|
deleteUrl: isEdit && viewAnnouncement && viewAnnouncement.id ? '/announcements/' + viewAnnouncement.id + '/delete' : '',
|
|
actionFormId: 'announcement-action-form',
|
|
announcementTypes: announcementTypes,
|
|
announcementColors: announcementColors,
|
|
announcementIcons: announcementIcons,
|
|
announcementScreens: announcementScreens
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
buildAnnouncementFormViewModel: buildAnnouncementFormViewModel
|
|
}; |