2022-07-10 15:25:43 +00:00
|
|
|
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'
|
2022-07-10 15:25:43 +00:00
|
|
|
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
|
|
|
|
2022-07-10 15:25:43 +00:00
|
|
|
export const useSmartPlaylistForm = (initialRuleGroups: SmartPlaylistRuleGroup[] = []) => {
|
2022-04-20 12:38:38 +00:00
|
|
|
const collectedRuleGroups = ref<SmartPlaylistRuleGroup[]>(initialRuleGroups)
|
2022-04-15 17:00:08 +00:00
|
|
|
|
2022-07-10 15:25:43 +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,
|
2022-04-20 12:38:38 +00:00
|
|
|
collectedRuleGroups,
|
2022-04-15 17:00:08 +00:00
|
|
|
addGroup,
|
2024-10-13 17:37:01 +00:00
|
|
|
onGroupChanged,
|
2022-04-15 17:00:08 +00:00
|
|
|
}
|
|
|
|
}
|