Files
docuseal/app/javascript/template_builder/fields.vue
T

197 lines
5.3 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
2023-08-19 21:05:54 +03:00
<div :class="withStickySubmitters ? 'sticky top-0 z-10' : ''">
2023-06-16 00:04:05 +03:00
<FieldSubmitter
:model-value="selectedSubmitter.uuid"
2023-08-19 21:05:54 +03:00
class="w-full rounded-lg"
:class="{ 'bg-base-100': withStickySubmitters }"
2023-06-16 00:04:05 +03:00
:submitters="submitters"
2023-06-24 16:17:07 +03:00
@new-submitter="save"
2023-06-16 00:04:05 +03:00
@remove="removeSubmitter"
2023-06-24 16:17:07 +03:00
@name-change="save"
2023-06-16 00:04:05 +03:00
@update:model-value="$emit('change-submitter', submitters.find((s) => s.uuid === $event))"
/>
</div>
2023-06-07 01:16:43 +03:00
<div class="mb-1 mt-2">
2023-05-21 17:59:36 +03:00
<Field
2023-06-07 01:16:43 +03:00
v-for="field in submitterFields"
2023-05-21 17:59:36 +03:00
:key="field.uuid"
:field="field"
2023-06-03 00:53:46 +03:00
:type-index="fields.filter((f) => f.type === field.type).indexOf(field)"
2023-06-07 01:16:43 +03:00
@remove="removeField"
2023-06-03 00:53:46 +03:00
@move-up="move(field, -1)"
@move-down="move(field, 1)"
@scroll-to="$emit('scroll-to-area', $event)"
2023-05-21 17:59:36 +03:00
@set-draw="$emit('set-draw', $event)"
/>
</div>
2023-06-16 00:04:05 +03:00
<div class="grid grid-cols-3 gap-1 pb-2">
2023-06-03 00:53:46 +03:00
<button
2023-06-04 01:08:45 +03:00
v-for="(icon, type) in fieldIcons"
:key="type"
2023-06-03 00:53:46 +03:00
draggable="true"
2023-08-19 21:05:54 +03:00
class="flex items-center justify-center border border-dashed border-base-300 w-full rounded relative"
2023-06-04 01:08:45 +03:00
@dragstart="onDragstart(type)"
2023-06-03 00:53:46 +03:00
@dragend="$emit('drag-end')"
2023-06-04 01:08:45 +03:00
@click="addField(type)"
2023-05-23 00:03:26 +03:00
>
2023-06-03 00:53:46 +03:00
<div class="w-0 absolute left-0">
<svg
xmlns="http://www.w3.org/2000/svg"
class="cursor-grab"
width="18"
height="18"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
fill="none"
stroke-linecap="round"
stroke-linejoin="round"
>
<path
stroke="none"
d="M0 0h24v24H0z"
fill="none"
/>
<path d="M9 5m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M9 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M9 19m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M15 5m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M15 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M15 19m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
</svg>
</div>
<div class="flex items-center flex-col px-2 py-2">
2023-06-04 01:08:45 +03:00
<component :is="icon" />
2023-06-03 00:53:46 +03:00
<span class="text-xs mt-1">
2023-06-27 00:57:29 +03:00
{{ fieldNames[type] }}
2023-06-03 00:53:46 +03:00
</span>
</div>
</button>
</div>
2023-07-08 17:07:07 +03:00
<div
v-if="fields.length < 4"
class="text-xs p-2 border border-base-200 rounded"
>
2023-07-12 23:56:42 +03:00
<ul class="list-disc list-outside ml-3">
2023-07-08 17:07:07 +03:00
<li>
Draw a text field on the page with a mouse
</li>
<li>
2023-08-06 18:51:02 +03:00
Single click on the page to add a checkbox
2023-07-08 17:07:07 +03:00
</li>
<li>
Drag &amp; drop any other field type on the page
</li>
<li>
Click on the field type above to add it to the form without drawing it on the document
</li>
2023-07-12 23:56:42 +03:00
</ul>
2023-07-08 17:07:07 +03:00
</div>
2023-05-21 17:59:36 +03:00
</template>
<script>
import Field from './field'
import { v4 } from 'uuid'
2023-06-04 01:08:45 +03:00
import FieldType from './field_type'
2023-06-07 01:16:43 +03:00
import FieldSubmitter from './field_submitter'
2023-05-21 17:59:36 +03:00
export default {
2023-05-31 23:19:20 +03:00
name: 'TemplateFields',
2023-05-21 17:59:36 +03:00
components: {
2023-06-07 01:16:43 +03:00
Field,
FieldSubmitter
2023-05-21 17:59:36 +03:00
},
2023-06-24 16:17:07 +03:00
inject: ['save'],
2023-05-21 17:59:36 +03:00
props: {
fields: {
type: Array,
required: true
2023-06-07 01:16:43 +03:00
},
2023-08-19 21:05:54 +03:00
withStickySubmitters: {
type: Boolean,
required: false,
default: true
},
2023-06-07 01:16:43 +03:00
submitters: {
type: Array,
required: true
},
selectedSubmitter: {
type: Object,
required: true
2023-05-21 17:59:36 +03:00
}
},
2023-06-07 01:16:43 +03:00
emits: ['set-draw', 'set-drag', 'drag-end', 'scroll-to-area', 'change-submitter'],
2023-05-21 17:59:36 +03:00
computed: {
2023-06-27 00:57:29 +03:00
fieldNames: FieldType.computed.fieldNames,
2023-06-07 01:16:43 +03:00
fieldIcons: FieldType.computed.fieldIcons,
submitterFields () {
return this.fields.filter((f) => f.submitter_uuid === this.selectedSubmitter.uuid)
}
2023-05-21 17:59:36 +03:00
},
methods: {
2023-05-23 00:03:26 +03:00
onDragstart (fieldType) {
this.$emit('set-drag', fieldType)
},
2023-06-07 01:16:43 +03:00
removeSubmitter (submitter) {
[...this.fields].forEach((field) => {
if (field.submitter_uuid === submitter.uuid) {
this.removeField(field)
}
})
this.submitters.splice(this.submitters.indexOf(submitter), 1)
if (this.selectedSubmitter === submitter) {
this.$emit('change-submitter', this.submitters[0])
}
2023-06-24 16:17:07 +03:00
this.save()
2023-06-07 01:16:43 +03:00
},
2023-06-03 00:53:46 +03:00
move (field, direction) {
2023-06-08 00:20:42 +03:00
const currentIndex = this.submitterFields.indexOf(field)
const fieldsIndex = this.fields.indexOf(field)
2023-06-03 00:53:46 +03:00
2023-06-08 00:20:42 +03:00
this.fields.splice(fieldsIndex, 1)
2023-06-03 00:53:46 +03:00
2023-06-08 00:20:42 +03:00
if (currentIndex + direction > this.submitterFields.length) {
const firstIndex = this.fields.indexOf(this.submitterFields[0])
this.fields.splice(firstIndex, 0, field)
2023-06-03 00:53:46 +03:00
} else if (currentIndex + direction < 0) {
2023-06-08 00:20:42 +03:00
const lastIndex = this.fields.indexOf(this.submitterFields[this.submitterFields.length - 1])
this.fields.splice(lastIndex + 1, 0, field)
2023-06-03 00:53:46 +03:00
} else {
2023-06-08 00:20:42 +03:00
this.fields.splice(fieldsIndex + direction, 0, field)
2023-06-03 00:53:46 +03:00
}
2023-06-24 16:17:07 +03:00
this.save()
2023-06-03 00:53:46 +03:00
},
2023-06-07 01:16:43 +03:00
removeField (field) {
this.fields.splice(this.fields.indexOf(field), 1)
2023-06-24 16:17:07 +03:00
this.save()
2023-06-07 01:16:43 +03:00
},
2023-05-23 00:03:26 +03:00
addField (type, area = null) {
2023-05-21 17:59:36 +03:00
const field = {
2023-06-04 01:08:45 +03:00
name: '',
2023-05-21 17:59:36 +03:00
uuid: v4(),
2023-07-22 20:00:28 +03:00
required: type !== 'checkbox',
areas: [],
2023-06-07 01:16:43 +03:00
submitter_uuid: this.selectedSubmitter.uuid,
2023-05-21 17:59:36 +03:00
type
}
2023-06-16 00:04:05 +03:00
if (['select', 'multiple', 'radio'].includes(type)) {
2023-05-21 17:59:36 +03:00
field.options = ['']
}
this.fields.push(field)
2023-06-24 16:17:07 +03:00
this.save()
2023-05-21 17:59:36 +03:00
}
}
}
</script>