Files
docuseal/app/javascript/submission_form/form.vue
T

536 lines
17 KiB
Vue
Raw Normal View History

2023-05-25 22:47:52 +03:00
<template>
<FieldAreas
ref="areas"
2023-06-16 00:04:05 +03:00
:steps="stepFields"
2023-05-25 22:47:52 +03:00
:values="values"
:attachments-index="attachmentsIndex"
2023-07-31 00:48:51 +03:00
:with-label="!isAnonymousChecboxes"
2023-06-16 00:04:05 +03:00
:current-step="currentStepFields"
2023-07-22 14:31:13 +03:00
@focus-step="[saveStep(), goToStep($event, false, true), currentField.type !== 'checkbox' ? isFormVisible = true : '']"
2023-05-25 22:47:52 +03:00
/>
2023-07-17 22:48:01 +03:00
<button
v-if="!isFormVisible"
2023-08-12 12:34:02 +03:00
class="btn btn-neutral flex text-white absolute rounded-none border-x-0 md:border md:rounded-full bottom-0 w-full md:mb-4 text-base"
2023-07-17 22:48:01 +03:00
@click.prevent="isFormVisible = true"
>
2023-08-30 02:00:55 +03:00
{{ t('submit_form') }}
2023-07-17 22:48:01 +03:00
<IconArrowsDiagonal
class="absolute right-0 mr-4"
:width="20"
:height="20"
/>
</button>
<div
2023-07-22 14:31:13 +03:00
v-show="isFormVisible"
2023-07-17 22:48:01 +03:00
id="form_container"
class="shadow-md bg-base-100 absolute bottom-0 md:bottom-4 w-full border-base-200 border p-4 rounded"
2023-08-28 23:43:33 +03:00
:style="{ backgroundColor: backgroundColor }"
2023-07-17 22:48:01 +03:00
>
<button
2023-06-16 23:39:55 +03:00
v-if="!isCompleted"
2023-07-17 22:48:01 +03:00
class="absolute right-0 mr-2 mt-2 top-0 hidden md:block"
title="Minimize"
@click.prevent="isFormVisible = false"
2023-05-25 22:47:52 +03:00
>
2023-07-17 22:48:01 +03:00
<IconArrowsDiagonalMinimize2
:width="20"
:height="20"
/>
</button>
<div>
<form
v-if="!isCompleted"
ref="form"
:action="submitPath"
method="post"
class="md:mx-16"
@submit.prevent="submitStep"
2023-06-16 23:39:55 +03:00
>
2023-07-17 22:48:01 +03:00
<input
type="hidden"
name="authenticity_token"
:value="authenticityToken"
>
<input
value="put"
name="_method"
type="hidden"
>
<div class="md:mt-4">
<div v-if="['cells', 'text'].includes(currentField.type)">
<label
v-if="currentField.name"
:for="currentField.uuid"
class="label text-2xl mb-2"
>{{ currentField.name }}
2023-08-30 02:00:55 +03:00
<template v-if="!currentField.required">({{ t('optional') }})</template>
2023-07-17 22:48:01 +03:00
</label>
<div
v-else
class="py-1"
/>
<div>
<input
:id="currentField.uuid"
v-model="values[currentField.uuid]"
class="base-input !text-2xl w-full"
:required="currentField.required"
2023-08-30 02:00:55 +03:00
:placeholder="`${t('type_here')}...${currentField.required ? '' : ` (${t('optional')})`}`"
2023-07-17 22:48:01 +03:00
type="text"
:name="`values[${currentField.uuid}]`"
@focus="$refs.areas.scrollIntoField(currentField)"
>
</div>
2023-06-16 23:39:55 +03:00
</div>
2023-07-17 22:48:01 +03:00
<div v-else-if="currentField.type === 'date'">
<label
v-if="currentField.name"
:for="currentField.uuid"
class="label text-2xl mb-2"
>{{ currentField.name }}
2023-08-30 02:00:55 +03:00
<template v-if="!currentField.required">({{ t('optional') }})</template>
2023-07-17 22:48:01 +03:00
</label>
<div
v-else
class="py-1"
/>
<div class="text-center">
<input
:id="currentField.uuid"
v-model="values[currentField.uuid]"
class="base-input !text-2xl text-center w-full"
:required="currentField.required"
type="date"
:name="`values[${currentField.uuid}]`"
@focus="$refs.areas.scrollIntoField(currentField)"
>
</div>
</div>
<div v-else-if="currentField.type === 'select'">
<label
v-if="currentField.name"
:for="currentField.uuid"
class="label text-2xl mb-2"
>{{ currentField.name }}
2023-08-30 02:00:55 +03:00
<template v-if="!currentField.required">({{ t('optional') }})</template>
2023-07-17 22:48:01 +03:00
</label>
<div
v-else
class="py-1"
/>
<select
2023-06-16 23:39:55 +03:00
:id="currentField.uuid"
:required="currentField.required"
2023-07-17 22:48:01 +03:00
class="select base-input !text-2xl w-full text-center font-normal"
2023-06-16 23:39:55 +03:00
:name="`values[${currentField.uuid}]`"
2023-07-17 22:48:01 +03:00
@change="values[currentField.uuid] = $event.target.value"
2023-06-16 23:39:55 +03:00
@focus="$refs.areas.scrollIntoField(currentField)"
>
2023-07-17 22:48:01 +03:00
<option
value=""
:selected="!values[currentField.uuid]"
>
2023-08-30 02:00:55 +03:00
{{ t('select_your_option') }}
2023-07-17 22:48:01 +03:00
</option>
<option
v-for="(option, index) in currentField.options"
:key="index"
:selected="values[currentField.uuid] == option"
:value="option"
>
{{ option }}
</option>
</select>
2023-06-16 23:39:55 +03:00
</div>
2023-07-17 22:48:01 +03:00
<div v-else-if="currentField.type === 'radio'">
<label
v-if="currentField.name"
:for="currentField.uuid"
class="label text-2xl mb-2"
>{{ currentField.name }}
2023-08-30 02:00:55 +03:00
<template v-if="!currentField.required">({{ t('optional') }})</template>
2023-07-17 22:48:01 +03:00
</label>
<div class="flex w-full">
<div class="space-y-3.5 mx-auto">
<div
v-for="(option, index) in currentField.options"
:key="index"
>
<label
:for="currentField.uuid + option"
class="flex items-center space-x-3"
>
<input
:id="currentField.uuid + option"
v-model="values[currentField.uuid]"
type="radio"
class="base-radio !h-7 !w-7"
:name="`values[${currentField.uuid}]`"
:value="option"
:required="currentField.required"
>
<span class="text-xl">
{{ option }}
</span>
</label>
</div>
</div>
</div>
</div>
<MultiSelectStep
v-else-if="currentField.type === 'multiple'"
v-model="values[currentField.uuid]"
:field="currentField"
2023-06-30 01:26:13 +03:00
/>
2023-07-17 22:48:01 +03:00
<div
v-else-if="currentField.type === 'checkbox'"
2023-07-31 00:48:51 +03:00
class="flex w-full max-h-44 overflow-y-auto"
2023-05-25 22:47:52 +03:00
>
2023-07-17 22:48:01 +03:00
<input
type="hidden"
name="cast_boolean"
value="true"
2023-06-16 23:39:55 +03:00
>
2023-07-17 22:48:01 +03:00
<div
class="space-y-3.5 mx-auto"
2023-06-16 23:39:55 +03:00
>
2023-07-31 00:48:51 +03:00
<template v-if="isAnonymousChecboxes">
2023-08-12 12:34:02 +03:00
<span class="text-xl">
2023-08-30 02:00:55 +03:00
{{ t('complete_hightlighted_checkboxes_and_click') }} <span class="font-semibold">{{ stepFields.length === currentStep + 1 ? t('submit') : t('next') }}</span>.
2023-08-12 12:34:02 +03:00
</span>
2023-08-09 21:10:07 +03:00
<input
v-for="field in currentStepFields"
:key="field.uuid"
type="hidden"
:name="`values[${field.uuid}]`"
:value="!!values[field.uuid]"
>
2023-07-31 00:48:51 +03:00
</template>
<template v-else>
<div
v-for="(field, index) in currentStepFields"
:key="field.uuid"
2023-06-16 23:39:55 +03:00
>
2023-07-31 00:48:51 +03:00
<label
:for="field.uuid"
class="flex items-center space-x-3"
2023-06-16 23:39:55 +03:00
>
2023-07-31 00:48:51 +03:00
<input
type="hidden"
:name="`values[${field.uuid}]`"
:value="!!values[field.uuid]"
>
<input
:id="field.uuid"
type="checkbox"
class="base-checkbox !h-7 !w-7"
:checked="!!values[field.uuid]"
@click="[$refs.areas.scrollIntoField(field), values[field.uuid] = !values[field.uuid]]"
>
<span class="text-xl">
{{ field.name || field.type + ' ' + (index + 1) }}
</span>
</label>
</div>
</template>
2023-06-16 23:39:55 +03:00
</div>
</div>
2023-07-17 22:48:01 +03:00
<ImageStep
v-else-if="currentField.type === 'image'"
v-model="values[currentField.uuid]"
:field="currentField"
:is-direct-upload="isDirectUpload"
:attachments-index="attachmentsIndex"
:submitter-slug="submitterSlug"
@attached="[attachments.push($event), $refs.areas.scrollIntoField(currentField)]"
/>
<SignatureStep
v-else-if="currentField.type === 'signature'"
ref="currentStep"
v-model="values[currentField.uuid]"
:field="currentField"
:is-direct-upload="isDirectUpload"
:attachments-index="attachmentsIndex"
:submitter-slug="submitterSlug"
@attached="attachments.push($event)"
@start="$refs.areas.scrollIntoField(currentField)"
@minimize="isFormVisible = false"
/>
<AttachmentStep
v-else-if="currentField.type === 'file'"
v-model="values[currentField.uuid]"
:is-direct-upload="isDirectUpload"
:field="currentField"
:attachments-index="attachmentsIndex"
:submitter-slug="submitterSlug"
@attached="[attachments.push($event), $refs.areas.scrollIntoField(currentField)]"
/>
2023-06-16 23:39:55 +03:00
</div>
2023-07-17 22:48:01 +03:00
<div class="mt-6 md:mt-8">
<button
type="submit"
class="base-button w-full flex justify-center"
:disabled="isButtonDisabled"
2023-05-25 22:47:52 +03:00
>
2023-07-17 22:48:01 +03:00
<span class="flex">
<IconInnerShadowTop
v-if="isSubmitting"
class="mr-1 animate-spin"
/>
<span v-if="stepFields.length === currentStep + 1">
2023-08-30 02:00:55 +03:00
{{ t('submit') }}
2023-07-17 22:48:01 +03:00
</span>
<span v-else>
2023-08-30 02:00:55 +03:00
{{ t('next') }}
2023-07-17 22:48:01 +03:00
</span><span
v-if="isSubmitting"
class="w-6 flex justify-start mr-1"
><span>...</span></span>
2023-06-20 00:24:39 +03:00
</span>
2023-07-17 22:48:01 +03:00
</button>
</div>
</form>
<FormCompleted
v-else
:is-demo="isDemo"
2023-09-03 20:44:54 +03:00
:attribution="attribution"
2023-08-12 12:34:02 +03:00
:with-confetti="withConfetti"
2023-09-03 14:05:31 +03:00
:can-send-email="canSendEmail && submitter.email"
2023-07-17 22:48:01 +03:00
:submitter-slug="submitterSlug"
/>
<div class="flex justify-center">
<div class="flex items-center mt-5 mb-1">
<a
v-for="(step, index) in stepFields"
:key="step[0].uuid"
href="#"
class="inline border border-base-300 h-3 w-3 rounded-full mx-1"
:class="{ 'bg-base-300': index === currentStep, 'bg-base-content': index < currentStep || isCompleted, 'bg-white': index > currentStep }"
@click.prevent="isCompleted ? '' : [saveStep(), goToStep(step, true)]"
/>
</div>
2023-06-15 01:05:04 +03:00
</div>
</div>
2023-06-16 23:39:55 +03:00
</div>
2023-05-25 22:47:52 +03:00
</template>
<script>
import FieldAreas from './areas'
import ImageStep from './image_step'
import SignatureStep from './signature_step'
import AttachmentStep from './attachment_step'
2023-06-16 00:04:05 +03:00
import MultiSelectStep from './multi_select_step'
2023-05-25 22:47:52 +03:00
import FormCompleted from './completed'
2023-07-17 22:48:01 +03:00
import { IconInnerShadowTop, IconArrowsDiagonal, IconArrowsDiagonalMinimize2 } from '@tabler/icons-vue'
2023-08-30 02:00:55 +03:00
import { t } from './i18n'
2023-05-25 22:47:52 +03:00
export default {
2023-05-31 23:19:20 +03:00
name: 'SubmissionForm',
2023-05-25 22:47:52 +03:00
components: {
FieldAreas,
ImageStep,
SignatureStep,
AttachmentStep,
2023-06-16 00:04:05 +03:00
MultiSelectStep,
2023-06-20 00:24:39 +03:00
IconInnerShadowTop,
2023-07-17 22:48:01 +03:00
IconArrowsDiagonal,
IconArrowsDiagonalMinimize2,
2023-05-25 22:47:52 +03:00
FormCompleted
},
2023-08-12 12:34:02 +03:00
provide () {
return {
2023-08-30 02:00:55 +03:00
baseUrl: this.baseUrl,
t: this.t
2023-08-12 12:34:02 +03:00
}
},
2023-05-25 22:47:52 +03:00
props: {
2023-09-03 14:05:31 +03:00
submitter: {
type: Object,
2023-06-13 00:29:07 +03:00
required: true
},
2023-06-30 20:31:17 +03:00
canSendEmail: {
type: Boolean,
required: false,
default: false
},
2023-05-25 22:47:52 +03:00
attachments: {
type: Array,
required: false,
default: () => []
},
2023-08-12 12:34:02 +03:00
withConfetti: {
type: Boolean,
required: false,
default: false
},
baseUrl: {
type: String,
required: false,
default: ''
},
2023-05-25 22:47:52 +03:00
fields: {
type: Array,
required: false,
default: () => []
},
authenticityToken: {
type: String,
2023-08-12 12:34:02 +03:00
required: false,
default: ''
2023-05-25 22:47:52 +03:00
},
2023-08-28 23:43:33 +03:00
backgroundColor: {
type: String,
required: false,
default: ''
},
isDirectUpload: {
type: Boolean,
2023-08-12 12:34:02 +03:00
required: false,
default: false
},
2023-07-12 01:24:25 +03:00
isDemo: {
type: Boolean,
2023-08-12 12:34:02 +03:00
required: false,
2023-07-12 01:24:25 +03:00
default: false
},
2023-09-03 20:44:54 +03:00
attribution: {
type: Boolean,
required: false,
default: true
},
2023-05-25 22:47:52 +03:00
values: {
type: Object,
required: false,
default: () => ({})
}
},
data () {
return {
isCompleted: false,
2023-07-17 22:48:01 +03:00
isFormVisible: true,
2023-05-25 22:47:52 +03:00
currentStep: 0,
isSubmitting: false
}
},
computed: {
2023-06-16 00:04:05 +03:00
currentStepFields () {
return this.stepFields[this.currentStep]
},
2023-09-03 14:05:31 +03:00
submitterSlug () {
return this.submitter.slug
},
2023-07-31 00:48:51 +03:00
isAnonymousChecboxes () {
return this.currentField.type === 'checkbox' && this.currentStepFields.every((e) => !e.name) && this.currentStepFields.length > 4
},
2023-06-24 17:15:36 +03:00
isButtonDisabled () {
return this.isSubmitting ||
(this.currentField.required && ['image', 'file'].includes(this.currentField.type) && !this.values[this.currentField.uuid]?.length) ||
(this.currentField.required && this.currentField.type === 'signature' && !this.values[this.currentField.uuid]?.length && this.$refs.currentStep && !this.$refs.currentStep.isSignatureStarted)
},
2023-05-25 22:47:52 +03:00
currentField () {
2023-06-16 00:04:05 +03:00
return this.currentStepFields[0]
2023-06-13 00:29:07 +03:00
},
2023-06-16 00:04:05 +03:00
stepFields () {
2023-06-20 22:38:38 +03:00
return this.fields.reduce((acc, f) => {
2023-06-16 00:04:05 +03:00
const prevStep = acc[acc.length - 1]
if (f.type === 'checkbox' && Array.isArray(prevStep) && prevStep[0].type === 'checkbox') {
prevStep.push(f)
} else {
acc.push([f])
}
return acc
}, [])
},
2023-05-25 22:47:52 +03:00
attachmentsIndex () {
return this.attachments.reduce((acc, a) => {
acc[a.uuid] = a
return acc
}, {})
},
submitPath () {
2023-06-13 00:29:07 +03:00
return `/s/${this.submitterSlug}`
2023-05-25 22:47:52 +03:00
}
},
mounted () {
this.currentStep = Math.min(
2023-06-16 00:04:05 +03:00
this.stepFields.indexOf([...this.stepFields].reverse().find((fields) => fields.some((f) => !!this.values[f.uuid]))) + 1,
this.stepFields.length - 1
2023-05-25 22:47:52 +03:00
)
2023-07-03 00:03:29 +03:00
if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) {
2023-08-12 12:34:02 +03:00
this.$nextTick(() => {
const root = this.$root.$el.parentNode.getRootNode()
const scrollbox = root.getElementById('scrollbox')
const parent = root.body || root.querySelector('div')
2023-07-03 00:03:29 +03:00
2023-08-12 12:34:02 +03:00
parent.style.overflow = 'hidden'
scrollbox.classList.add('h-full', 'overflow-y-auto')
scrollbox.parentNode.classList.add('h-screen', 'overflow-y-auto')
scrollbox.parentNode.style.maxHeight = '-webkit-fill-available'
})
2023-07-03 00:03:29 +03:00
}
2023-05-25 22:47:52 +03:00
},
methods: {
2023-08-30 02:00:55 +03:00
t,
2023-06-16 00:04:05 +03:00
goToStep (step, scrollToArea = false, clickUpload = false) {
this.currentStep = this.stepFields.indexOf(step)
2023-05-25 22:47:52 +03:00
this.$nextTick(() => {
if (scrollToArea) {
2023-06-16 00:04:05 +03:00
this.$refs.areas.scrollIntoField(step[0])
2023-05-25 22:47:52 +03:00
}
this.$refs.form.querySelector('input[type="date"], input[type="text"], select')?.focus()
2023-06-15 01:05:04 +03:00
2023-07-22 14:31:13 +03:00
if (clickUpload && !this.values[this.currentField.uuid] && ['file', 'image'].includes(this.currentField.type)) {
2023-06-15 01:05:04 +03:00
this.$refs.form.querySelector('input[type="file"]')?.click()
}
2023-05-25 22:47:52 +03:00
})
},
2023-07-08 20:51:37 +03:00
saveStep (formData) {
2023-06-27 00:57:29 +03:00
if (this.isCompleted) {
return Promise.resolve({})
} else {
2023-08-12 12:34:02 +03:00
return fetch(this.baseUrl + this.submitPath, {
2023-06-27 00:57:29 +03:00
method: 'POST',
2023-07-08 20:51:37 +03:00
body: formData || new FormData(this.$refs.form)
2023-06-27 00:57:29 +03:00
})
}
2023-06-20 00:24:39 +03:00
},
2023-05-25 22:47:52 +03:00
async submitStep () {
this.isSubmitting = true
const stepPromise = this.currentField.type === 'signature'
? this.$refs.currentStep.submit
: () => Promise.resolve({})
await stepPromise()
2023-07-08 20:51:37 +03:00
const formData = new FormData(this.$refs.form)
if (this.currentStep === this.stepFields.length - 1) {
formData.append('completed', 'true')
}
this.saveStep(formData).then(response => {
2023-06-16 00:04:05 +03:00
const nextStep = this.stepFields[this.currentStep + 1]
2023-05-25 22:47:52 +03:00
2023-06-16 00:04:05 +03:00
if (nextStep) {
this.goToStep(this.stepFields[this.currentStep + 1], true)
2023-05-25 22:47:52 +03:00
} else {
this.isCompleted = true
}
}).catch(error => {
console.error('Error submitting form:', error)
}).finally(() => {
this.isSubmitting = false
})
}
}
}
</script>