koel/resources/assets/js/composables/useSmartPlaylistForm.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

import { ref } from 'vue'
2022-04-15 17:00:08 +00:00
import { playlistStore } from '@/stores'
2024-04-04 22:20:42 +00:00
import Btn from '@/components/ui/form/Btn.vue'
import FormBase from '@/components/playlist/smart-playlist/SmartPlaylistFormBase.vue'
import RuleGroup from '@/components/playlist/smart-playlist/SmartPlaylistRuleGroup.vue'
2022-08-01 08:58:25 +00:00
import SoundBars from '@/components/ui/SoundBars.vue'
2022-04-15 17:00:08 +00:00
export const useSmartPlaylistForm = (initialRuleGroups: SmartPlaylistRuleGroup[] = []) => {
const collectedRuleGroups = ref<SmartPlaylistRuleGroup[]>(initialRuleGroups)
2022-04-15 17:00:08 +00:00
const addGroup = () => collectedRuleGroups.value.push(playlistStore.createEmptySmartPlaylistRuleGroup())
2022-04-15 17:00:08 +00:00
const onGroupChanged = (data: SmartPlaylistRuleGroup) => {
2024-01-24 22:39:47 +00:00
const changedGroup = Object.assign(collectedRuleGroups.value.find(({ id }) => id === data.id)!, data)
2022-04-15 17:00:08 +00:00
// Remove empty group
if (changedGroup.rules.length === 0) {
2024-01-24 22:39:47 +00:00
collectedRuleGroups.value = collectedRuleGroups.value.filter(({ id }) => id !== changedGroup.id)
2022-04-15 17:00:08 +00:00
}
}
return {
Btn,
FormBase,
RuleGroup,
2022-08-01 08:58:25 +00:00
SoundBars,
collectedRuleGroups,
2022-04-15 17:00:08 +00:00
addGroup,
onGroupChanged,
2022-04-15 17:00:08 +00:00
}
}