2024-04-15 19:39:03 +02:00
|
|
|
import { defineStore } from "pinia"
|
2024-05-13 14:47:59 +02:00
|
|
|
import clonedeep from "clone-deep"
|
|
|
|
|
import { generateUUID } from "~/lib/utils.js"
|
2024-09-23 20:02:38 +02:00
|
|
|
import blocksTypes from "~/data/blocks_types.json"
|
|
|
|
|
import { useAlert } from '~/composables/useAlert'
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const useWorkingFormStore = defineStore("working_form", {
|
2023-12-09 15:47:03 +01:00
|
|
|
state: () => ({
|
|
|
|
|
content: null,
|
2024-09-23 20:02:38 +02:00
|
|
|
activeTab: 0,
|
|
|
|
|
formPageIndex: 0,
|
2023-12-09 15:47:03 +01:00
|
|
|
|
|
|
|
|
// Field being edited
|
|
|
|
|
selectedFieldIndex: null,
|
|
|
|
|
showEditFieldSidebar: null,
|
2024-04-15 19:39:03 +02:00
|
|
|
showAddFieldSidebar: null,
|
2024-05-13 14:47:59 +02:00
|
|
|
blockForm: null,
|
|
|
|
|
draggingNewBlock: false,
|
2023-12-09 15:47:03 +01:00
|
|
|
}),
|
|
|
|
|
actions: {
|
2024-04-15 19:39:03 +02:00
|
|
|
set(form) {
|
2023-12-09 15:47:03 +01:00
|
|
|
this.content = form
|
|
|
|
|
},
|
2024-04-15 19:39:03 +02:00
|
|
|
setProperties(properties) {
|
2024-03-12 12:48:51 +01:00
|
|
|
this.content.properties = [...properties]
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
2024-09-23 20:02:38 +02:00
|
|
|
objectToIndex(field) {
|
|
|
|
|
if (typeof field === 'object') {
|
|
|
|
|
return this.content.properties.findIndex(
|
|
|
|
|
prop => prop.id === field.id
|
2024-04-15 19:39:03 +02:00
|
|
|
)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
2024-09-23 20:02:38 +02:00
|
|
|
return field
|
|
|
|
|
},
|
|
|
|
|
setEditingField(field) {
|
|
|
|
|
this.selectedFieldIndex = this.objectToIndex(field)
|
|
|
|
|
},
|
|
|
|
|
openSettingsForField(field) {
|
|
|
|
|
this.setEditingField(field)
|
2023-12-09 15:47:03 +01:00
|
|
|
this.showEditFieldSidebar = true
|
|
|
|
|
this.showAddFieldSidebar = false
|
|
|
|
|
},
|
2024-04-15 19:39:03 +02:00
|
|
|
closeEditFieldSidebar() {
|
2023-12-09 15:47:03 +01:00
|
|
|
this.selectedFieldIndex = null
|
|
|
|
|
this.showEditFieldSidebar = false
|
|
|
|
|
this.showAddFieldSidebar = false
|
|
|
|
|
},
|
2024-09-23 20:02:38 +02:00
|
|
|
openAddFieldSidebar(field) {
|
|
|
|
|
if (field !== null) {
|
|
|
|
|
this.setEditingField(field)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
this.showAddFieldSidebar = true
|
|
|
|
|
this.showEditFieldSidebar = false
|
|
|
|
|
},
|
2024-04-15 19:39:03 +02:00
|
|
|
closeAddFieldSidebar() {
|
2023-12-09 15:47:03 +01:00
|
|
|
this.selectedFieldIndex = null
|
|
|
|
|
this.showAddFieldSidebar = false
|
|
|
|
|
this.showEditFieldSidebar = false
|
2024-01-17 14:52:32 +01:00
|
|
|
},
|
2024-04-15 19:39:03 +02:00
|
|
|
reset() {
|
2024-01-17 14:52:32 +01:00
|
|
|
this.content = null
|
|
|
|
|
this.selectedFieldIndex = null
|
|
|
|
|
this.showEditFieldSidebar = null
|
|
|
|
|
this.showAddFieldSidebar = null
|
2024-04-15 19:39:03 +02:00
|
|
|
},
|
2024-05-13 14:47:59 +02:00
|
|
|
|
|
|
|
|
resetBlockForm() {
|
|
|
|
|
this.blockForm = useForm({
|
|
|
|
|
type: null,
|
|
|
|
|
name: null,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
prefillDefault(data) {
|
|
|
|
|
if (data.type === "nf-text") {
|
|
|
|
|
data.content = "<p>This is a text block.</p>"
|
|
|
|
|
} else if (data.type === "nf-page-break") {
|
|
|
|
|
data.next_btn_text = "Next"
|
|
|
|
|
data.previous_btn_text = "Previous"
|
|
|
|
|
} else if (data.type === "nf-code") {
|
|
|
|
|
data.content =
|
|
|
|
|
'<div class="text-blue-500 italic">This is a code block.</div>'
|
|
|
|
|
} else if (data.type === "signature") {
|
|
|
|
|
data.help = "Draw your signature above"
|
|
|
|
|
}
|
|
|
|
|
return data
|
|
|
|
|
},
|
|
|
|
|
|
2024-09-23 20:15:41 +02:00
|
|
|
addBlock(type, index = null, openSettings = true) {
|
2024-05-13 14:47:59 +02:00
|
|
|
this.blockForm.type = type
|
2024-09-23 20:02:38 +02:00
|
|
|
this.blockForm.name = blocksTypes[type].default_block_name
|
2024-05-13 14:47:59 +02:00
|
|
|
const newBlock = this.prefillDefault(this.blockForm.data())
|
|
|
|
|
newBlock.id = generateUUID()
|
|
|
|
|
newBlock.hidden = false
|
|
|
|
|
if (["select", "multi_select"].includes(this.blockForm.type)) {
|
|
|
|
|
newBlock[this.blockForm.type] = { options: [] }
|
|
|
|
|
}
|
|
|
|
|
if (this.blockForm.type === "rating") {
|
|
|
|
|
newBlock.rating_max_value = 5
|
|
|
|
|
}
|
|
|
|
|
if (this.blockForm.type === "scale") {
|
|
|
|
|
newBlock.scale_min_value = 1
|
|
|
|
|
newBlock.scale_max_value = 5
|
|
|
|
|
newBlock.scale_step_value = 1
|
|
|
|
|
}
|
|
|
|
|
if (this.blockForm.type === "slider") {
|
|
|
|
|
newBlock.slider_min_value = 0
|
|
|
|
|
newBlock.slider_max_value = 50
|
|
|
|
|
newBlock.slider_step_value = 1
|
|
|
|
|
}
|
|
|
|
|
newBlock.help_position = "below_input"
|
|
|
|
|
if (
|
2024-05-22 09:38:22 +02:00
|
|
|
(this.selectedFieldIndex === null || this.selectedFieldIndex === undefined) &&
|
|
|
|
|
(index === null || index === undefined)
|
2024-05-13 14:47:59 +02:00
|
|
|
) {
|
|
|
|
|
const newFields = clonedeep(this.content.properties)
|
|
|
|
|
newFields.push(newBlock)
|
|
|
|
|
this.content.properties = newFields
|
2024-09-23 20:15:41 +02:00
|
|
|
if (openSettings) {
|
|
|
|
|
this.openSettingsForField(
|
|
|
|
|
this.content.properties.length - 1,
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-05-13 14:47:59 +02:00
|
|
|
} else {
|
|
|
|
|
const fieldIndex = typeof index === "number" ? index : this.selectedFieldIndex + 1
|
|
|
|
|
const newFields = clonedeep(this.content.properties)
|
|
|
|
|
newFields.splice(fieldIndex, 0, newBlock)
|
|
|
|
|
this.content.properties = newFields
|
2024-09-23 20:15:41 +02:00
|
|
|
if (openSettings) {
|
|
|
|
|
this.openSettingsForField(fieldIndex)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
removeField(field) {
|
|
|
|
|
this.internalRemoveField(field)
|
|
|
|
|
},
|
|
|
|
|
internalRemoveField(field) {
|
|
|
|
|
const index = this.objectToIndex(field)
|
|
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
useAlert().success('Ctrl + Z to undo',10000,{
|
|
|
|
|
title: 'Field removed',
|
|
|
|
|
actions: [{
|
|
|
|
|
label: 'Undo',
|
|
|
|
|
icon:"i-material-symbols-undo",
|
|
|
|
|
click: () => {
|
|
|
|
|
this.undo()
|
|
|
|
|
}
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
this.content.properties.splice(index, 1)
|
2024-05-13 14:47:59 +02:00
|
|
|
}
|
|
|
|
|
},
|
2024-09-23 20:02:38 +02:00
|
|
|
removeField(field) {
|
|
|
|
|
this.internalRemoveField(field)
|
|
|
|
|
},
|
|
|
|
|
internalRemoveField(field) {
|
|
|
|
|
const index = this.objectToIndex(field)
|
|
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
useAlert().success('Ctrl + Z to undo',10000,{
|
|
|
|
|
title: 'Field removed',
|
|
|
|
|
actions: [{
|
|
|
|
|
label: 'Undo',
|
|
|
|
|
icon:"i-material-symbols-undo",
|
|
|
|
|
click: () => {
|
|
|
|
|
this.undo()
|
|
|
|
|
}
|
|
|
|
|
}]
|
|
|
|
|
})
|
|
|
|
|
this.content.properties.splice(index, 1)
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-05-13 14:47:59 +02:00
|
|
|
|
|
|
|
|
moveField(oldIndex, newIndex) {
|
|
|
|
|
const newFields = clonedeep(this.content.properties)
|
2024-05-22 14:36:42 +02:00
|
|
|
const field = newFields.splice(oldIndex, 1)[0]
|
|
|
|
|
newFields.splice(newIndex, 0, field)
|
2024-05-13 14:47:59 +02:00
|
|
|
this.content.properties = newFields
|
|
|
|
|
}
|
2024-04-15 19:39:03 +02:00
|
|
|
},
|
2024-06-25 15:48:20 +02:00
|
|
|
history: {}
|
2023-12-09 15:47:03 +01:00
|
|
|
})
|