ffplayout/components/PlaylistGenerator.vue

426 lines
19 KiB
Vue
Raw Normal View History

2024-04-08 15:33:28 -04:00
<template>
2024-04-09 15:08:25 -04:00
<div
class="z-50 fixed top-0 bottom-0 w-full h-full left-0 right-0 flex justify-center items-center bg-black/30 overflow-y-auto"
>
<div
class="relative flex flex-col bg-base-100 w-[800px] min-w-[300px] max-w-[90vw] h-[680px] rounded-md p-5 shadow-xl"
>
<div class="font-bold text-lg">{{ t('player.generateProgram') }}</div>
2024-04-08 15:33:28 -04:00
<div class="h-[calc(100%-95px)] mt-3">
<div role="tablist" class="tabs tabs-bordered">
2024-04-08 15:33:28 -04:00
<input
type="radio"
name="my_tabs_2"
role="tab"
class="tab"
:aria-label="t('player.simple')"
2024-04-08 15:33:28 -04:00
checked
2024-04-16 12:13:28 -04:00
@change="advancedGenerator = false"
/>
2024-04-09 15:08:25 -04:00
<div role="tabpanel" class="tab-content w-full pt-3">
<div class="w-full">
2024-04-09 15:08:25 -04:00
<div class="grid">
<nav class="breadcrumbs px-3 pt-0">
<ul>
<li v-for="(crumb, index) in mediaStore.folderCrumbs" :key="index">
<button
v-if="
mediaStore.folderCrumbs.length > 1 &&
mediaStore.folderCrumbs.length - 1 > index
"
@click="mediaStore.getTree(crumb.path, true)"
>
<i class="bi-folder-fill me-1" />
{{ crumb.text }}
</button>
<span v-else><i class="bi-folder-fill me-1" />{{ crumb.text }}</span>
</li>
</ul>
</nav>
</div>
<ul class="h-[475px] border border-my-gray rounded overflow-auto bg-base-300 m-1 py-1">
2024-04-08 15:33:28 -04:00
<li
v-for="folder in mediaStore.folderList.folders"
:key="folder.uid"
2024-04-16 12:13:28 -04:00
class="even:bg-base-200 px-2 w-full"
2024-04-08 15:33:28 -04:00
>
<div class="grid grid-cols-[auto_24px]">
<button
class="truncate text-left"
@click="
;[
(selectedFolders = []),
mediaStore.getTree(
`/${mediaStore.folderList.source}/${folder.name}`.replace(
/\/[/]+/g,
'/'
2024-04-08 15:33:28 -04:00
),
true
),
]
"
>
<i class="bi-folder-fill" />
{{ folder.name }}
</button>
2024-04-08 15:33:28 -04:00
<div v-if="!generateFromAll" class="text-center">
<input
class="checkbox checkbox-xs rounded"
type="checkbox"
@change="
setSelectedFolder(
$event,
`/${mediaStore.folderList.source}/${folder.name}`.replace(
/\/[/]+/g,
'/'
)
)
"
/>
2024-04-08 15:33:28 -04:00
</div>
</div>
</li>
</ul>
</div>
</div>
<input
type="radio"
name="my_tabs_2"
role="tab"
class="tab"
:aria-label="t('player.advanced')"
2024-04-08 15:33:28 -04:00
@change=";(advancedGenerator = true), resetCheckboxes()"
/>
<div role="tabpanel" class="tab-content pt-3">
<div class="w-full">
2024-04-09 15:08:25 -04:00
<div class="grid grid-cols-[auto_48px] px-3 pt-0">
<nav class="breadcrumbs pt-0">
<ul>
<li v-for="(crumb, index) in mediaStore.folderCrumbs" :key="index">
<button
v-if="
mediaStore.folderCrumbs.length > 1 &&
mediaStore.folderCrumbs.length - 1 > index
"
@click="mediaStore.getTree(crumb.path, true)"
2024-04-08 15:33:28 -04:00
>
<i class="bi-folder-fill me-1" />
{{ crumb.text }}
</button>
<span v-else><i class="bi-folder-fill me-1" />{{ crumb.text }}</span>
</li>
</ul>
</nav>
<div class="flex justify-end">
<button
type="button"
class="btn btn-sm btn-primary"
:title="t('player.addBlock')"
@click="addTemplate()"
>
<i class="bi bi-folder-plus" />
2024-04-08 15:33:28 -04:00
</button>
</div>
</div>
<div
2024-04-09 15:08:25 -04:00
class="h-[475px] border border-my-gray rounded grid bg-base-300 m-1"
:class="width < 740 ? 'grid-cols-1' : 'grid-cols-[300px_auto]'"
>
<Sortable
:list="mediaStore.folderList.folders"
:options="templateBrowserSortOptions"
item-key="uid"
2024-04-09 15:08:25 -04:00
class="overflow-auto py-1 border-my-gray"
:class="width < 740 ? 'h-[240px] border-b' : 'border-e'"
tag="ul"
>
<template #item="{ element, index }">
<li
:id="`adv_folder_${index}`"
:key="element.uid"
2024-04-16 12:13:28 -04:00
class="even:bg-base-200 draggable px-2 w-full"
>
<button
class="w-full truncate text-left"
@click="
;[
(selectedFolders = []),
mediaStore.getTree(
`/${mediaStore.folderList.source}/${element.name}`.replace(
/\/[/]+/g,
'/'
),
true
),
]
"
>
<i class="bi-folder-fill" />
{{ element.name }}
</button>
</li>
</template>
</Sortable>
2024-04-09 15:08:25 -04:00
<ul class="overflow-auto px-1 pb-1">
<li
v-for="item in template.sources"
:key="item.start"
2024-04-09 15:08:25 -04:00
class="flex flex-col gap-1 justify-center items-center border border-my-gray rounded mt-1 p-1"
2024-04-08 15:33:28 -04:00
>
<div class="grid grid-cols-[60px_67px_70px_67px_70px] join">
<div
class="input input-sm input-bordered join-item px-2 text-center bg-base-200 leading-7"
2024-04-08 15:33:28 -04:00
>
{{ t('player.start') }}:
2024-04-08 15:33:28 -04:00
</div>
<input
2024-04-16 12:13:28 -04:00
v-model="item.start"
type="text"
class="input input-sm input-bordered join-item px-2 text-center"
/>
<div
class="input input-sm input-bordered join-item px-2 text-center bg-base-200 leading-7"
2024-04-08 15:33:28 -04:00
>
{{ t('player.duration') }}:
2024-04-08 15:33:28 -04:00
</div>
<input
2024-04-16 12:13:28 -04:00
v-model="item.duration"
type="text"
class="input input-sm input-bordered join-item px-2 text-center"
/>
<button
class="btn btn-sm input-bordered join-item"
:class="item.shuffle ? 'bg-base-100' : 'bg-base-300'"
@click="item.shuffle = !item.shuffle"
>
{{ item.shuffle ? t('player.shuffle') : t('player.sorted') }}
</button>
</div>
<Sortable
:list="item.paths"
item-key="index"
class="w-full border border-my-gray rounded"
:style="`height: ${item.paths ? item.paths.length * 23 + 31 : 300}px`"
tag="ul"
:options="templateTargetSortOptions"
@add="addFolderToTemplate($event, item)"
>
<template #item="{ element, index }">
2024-04-09 15:08:25 -04:00
<li
:id="`path_${index}`"
:key="index"
2024-04-16 12:13:28 -04:00
class="draggable grabbing py-0 even:bg-base-200 px-2"
2024-04-09 15:08:25 -04:00
>
<i class="bi-folder-fill" />
{{ element.split(/[\\/]+/).pop() }}
</li>
</template>
</Sortable>
<div class="w-full flex justify-end">
<button
type="button"
class="btn btn-sm bg-base-100"
@click="removeTemplate(item)"
>
<i class="bi-trash" />
</button>
</div>
</li>
</ul>
2024-04-08 15:33:28 -04:00
</div>
</div>
</div>
</div>
</div>
<div class="flex h-14 pt-6 justify-end items-center">
<div v-if="!advancedGenerator" class="form-control">
<label class="label cursor-pointer w-12">
<span class="label-text">{{ t('player.all') }}</span>
<input
v-model="generateFromAll"
2024-04-16 12:13:28 -04:00
type="checkbox"
class="checkbox checkbox-xs rounded"
@change="resetCheckboxes()"
/>
2024-04-08 15:33:28 -04:00
</label>
</div>
<div class="join ms-2">
<button
type="button"
class="btn btn-sm btn-primary join-item"
2024-04-08 15:42:12 -04:00
@click="resetCheckboxes(), resetTemplate(), close()"
2024-04-08 15:33:28 -04:00
>
{{ t('cancel') }}
2024-04-08 15:33:28 -04:00
</button>
2024-04-09 15:08:25 -04:00
<button type="button" class="btn btn-sm btn-primary join-item" @click="generatePlaylist(), close()">
{{ t('ok') }}
2024-04-08 15:33:28 -04:00
</button>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
const { $dayjs } = useNuxtApp()
const { t } = useI18n()
2024-04-09 15:08:25 -04:00
const { width } = useWindowSize({ initialWidth: 800 })
2024-04-08 15:33:28 -04:00
const authStore = useAuth()
const configStore = useConfig()
const indexStore = useIndex()
const mediaStore = useMedia()
const playlistStore = usePlaylist()
const { processPlaylist } = playlistOperations()
const prop = defineProps({
2024-04-08 15:42:12 -04:00
close: {
type: Function,
default() {
return ''
},
},
switchClass: {
type: Function,
default() {
return ''
},
},
2024-04-08 15:42:12 -04:00
})
2024-04-08 15:33:28 -04:00
const advancedGenerator = ref(false)
const selectedFolders = ref([] as string[])
const generateFromAll = ref(false)
const template = ref({
sources: [
{
start: configStore.playout.playlist.day_start,
duration: '02:00:00',
shuffle: false,
paths: [],
},
],
2024-04-08 15:33:28 -04:00
} as Template)
const templateBrowserSortOptions = {
group: { name: 'folder', pull: 'clone', put: false },
sort: false,
}
const templateTargetSortOptions = {
group: 'folder',
animation: 100,
handle: '.grabbing',
}
function setSelectedFolder(event: any, folder: string) {
if (event.target.checked) {
selectedFolders.value.push(folder)
} else {
const index = selectedFolders.value.indexOf(folder)
if (index > -1) {
selectedFolders.value.splice(index, 1)
}
}
}
function resetCheckboxes() {
selectedFolders.value = []
2024-04-16 12:13:28 -04:00
const checkboxes = document.getElementsByClassName('folder-check') as HTMLCollectionOf<HTMLInputElement>
2024-04-08 15:33:28 -04:00
if (checkboxes) {
for (const box of checkboxes) {
box.checked = false
}
}
}
function addFolderToTemplate(event: any, item: TemplateItem) {
const o = event.oldIndex
const n = event.newIndex
event.item.remove()
const storagePath = configStore.playout.storage.path
2024-04-08 15:33:28 -04:00
const navPath = mediaStore.folderCrumbs[mediaStore.folderCrumbs.length - 1].path
const sourcePath = `${storagePath}/${navPath}/${mediaStore.folderList.folders[o].name}`.replace(/\/[/]+/g, '/')
if (!item.paths.includes(sourcePath)) {
item.paths.splice(n, 0, sourcePath)
}
}
function resetTemplate() {
template.value.sources = []
}
function removeTemplate(item: TemplateItem) {
const index = template.value.sources.indexOf(item)
template.value.sources.splice(index, 1)
}
function addTemplate() {
const last = template.value.sources[template.value.sources.length - 1]
2024-04-16 12:13:28 -04:00
let start = $dayjs('2000-01-01T00:00:00')
2024-04-08 15:33:28 -04:00
if (last) {
2024-04-16 12:13:28 -04:00
const t = $dayjs(`2000-01-01T${last.duration}`)
start = $dayjs(`2000-01-01T${last.start}`)
.add(t.hour(), 'hour')
.add(t.minute(), 'minute')
.add(t.second(), 'second')
2024-04-08 15:33:28 -04:00
}
template.value.sources.push({
start: start.format('HH:mm:ss'),
duration: '02:00:00',
shuffle: false,
paths: [],
})
}
async function generatePlaylist() {
playlistStore.isLoading = true
let body = null as BodyObject | null
if (selectedFolders.value.length > 0 && !generateFromAll.value) {
body = { paths: selectedFolders.value }
}
if (advancedGenerator.value) {
if (body) {
body.template = template.value
} else {
body = { template: template.value }
}
}
await $fetch(`/api/playlist/${configStore.channels[configStore.id].id}/generate/${playlistStore.listDate}`, {
method: 'POST',
headers: { ...configStore.contentType, ...authStore.authHeader },
body,
})
.then((response: any) => {
playlistStore.playlist = processPlaylist(playlistStore.listDate, response.program, false)
prop.switchClass()
indexStore.msgAlert('success', t('player.generateDone'), 2)
})
.catch((e: any) => {
indexStore.msgAlert('error', e.data ? e.data : e, 4)
})
// reset selections
resetCheckboxes()
resetTemplate()
playlistStore.isLoading = false
}
2024-04-08 15:33:28 -04:00
</script>