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

341 lines
8.9 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
<div
2023-06-03 00:53:46 +03:00
class="absolute overflow-visible group"
2023-05-21 17:59:36 +03:00
:style="positionStyle"
2023-06-03 00:53:46 +03:00
@pointerdown.stop
@mousedown.stop="startDrag"
2023-05-21 17:59:36 +03:00
>
<div
2023-06-27 00:57:29 +03:00
v-if="isSelected || isDraw"
2023-06-07 01:16:43 +03:00
class="top-0 bottom-0 right-0 left-0 absolute border border-1.5 pointer-events-none"
:class="borderColors[submitterIndex]"
/>
2023-06-27 00:57:29 +03:00
<div
v-if="field.type === 'cells' && (isSelected || isDraw)"
class="top-0 bottom-0 right-0 left-0 absolute"
>
<div
v-for="(cellW, index) in cells"
:key="index"
class="absolute top-0 bottom-0 border-r"
:class="borderColors[submitterIndex]"
:style="{ left: (cellW / area.w * 100) + '%' }"
>
<span
v-if="index === 0"
class="h-2.5 w-2.5 rounded-full -bottom-1 border-gray-400 bg-white shadow-md border absolute cursor-ew-resize z-10"
style="left: -4px"
@mousedown.stop="startResizeCell"
/>
</div>
</div>
2023-06-07 01:16:43 +03:00
<div
v-if="field?.type"
2023-06-04 01:08:45 +03:00
class="absolute bg-white rounded-t border overflow-visible whitespace-nowrap group-hover:flex group-hover:z-10"
2023-06-07 01:16:43 +03:00
:class="{ 'flex z-10': isNameFocus || isSelected, hidden: !isNameFocus && !isSelected }"
2023-06-03 00:53:46 +03:00
style="top: -25px; height: 25px"
@mousedown.stop
@pointerdown.stop
2023-05-21 17:59:36 +03:00
>
2023-06-07 01:16:43 +03:00
<FieldSubmitter
v-model="field.submitter_uuid"
class="border-r"
:compact="true"
:menu-classes="'dropdown-content bg-white menu menu-xs p-2 shadow rounded-box w-52 rounded-t-none -left-[1px]'"
:submitters="template.submitters"
2023-06-24 16:17:07 +03:00
@update:model-value="save"
2023-06-07 01:16:43 +03:00
@click="selectedAreaRef.value = area"
/>
2023-06-04 01:08:45 +03:00
<FieldType
v-model="field.type"
:button-width="27"
:button-classes="'px-1'"
:menu-classes="'bg-white rounded-t-none'"
2023-06-24 16:17:07 +03:00
@update:model-value="[maybeUpdateOptions(), save()]"
2023-06-04 01:08:45 +03:00
@click="selectedAreaRef.value = area"
/>
<span
2023-06-16 00:04:05 +03:00
v-if="field.type !== 'checkbox' || field.name"
2023-06-04 01:08:45 +03:00
ref="name"
contenteditable
class="pr-1 cursor-text outline-none block"
style="min-width: 2px"
@keydown.enter.prevent="onNameEnter"
@focus="onNameFocus"
@blur="onNameBlur"
>{{ field.name || defaultName }}</span>
2023-06-20 00:24:39 +03:00
<div
v-if="isNameFocus"
class="flex items-center ml-1.5"
>
<input
:id="`required-checkbox-${field.uuid}`"
v-model="field.required"
type="checkbox"
class="checkbox checkbox-xs no-animation rounded"
@mousedown.prevent
>
<label
:for="`required-checkbox-${field.uuid}`"
class="label text-xs"
@click.prevent="field.required = !field.required"
@mousedown.prevent
>Required</label>
</div>
2023-06-03 00:53:46 +03:00
<button
2023-06-20 00:24:39 +03:00
v-else
2023-06-08 00:20:42 +03:00
class="pr-1"
2023-06-20 00:24:39 +03:00
title="Remove"
2023-06-04 01:08:45 +03:00
@click.prevent="$emit('remove')"
2023-06-03 00:53:46 +03:00
>
2023-06-08 00:20:42 +03:00
<IconX width="14" />
2023-06-03 00:53:46 +03:00
</button>
</div>
<div
2023-06-07 01:16:43 +03:00
class="opacity-50 flex items-center justify-center h-full w-full"
:class="bgColors[submitterIndex]"
2023-06-03 00:53:46 +03:00
>
2023-06-04 01:08:45 +03:00
<span
v-if="field"
2023-06-08 00:20:42 +03:00
class="flex justify-center items-center space-x-1 h-full"
2023-06-04 01:08:45 +03:00
>
2023-06-08 00:20:42 +03:00
<component
:is="fieldIcons[field.type]"
width="100%"
height="100%"
class="max-h-10"
/>
2023-06-04 01:08:45 +03:00
</span>
2023-05-21 17:59:36 +03:00
</div>
2023-06-04 01:08:45 +03:00
<div
2023-06-07 01:16:43 +03:00
class="absolute top-0 bottom-0 right-0 left-0 cursor-pointer"
2023-06-04 01:08:45 +03:00
/>
2023-05-21 17:59:36 +03:00
<span
2023-06-08 00:20:42 +03:00
v-if="field?.type"
class="h-2.5 w-2.5 -right-1 rounded-full -bottom-1 border-gray-400 bg-white shadow-md border absolute cursor-nwse-resize"
2023-05-21 17:59:36 +03:00
@mousedown.stop="startResize"
/>
</div>
</template>
<script>
2023-06-07 01:16:43 +03:00
import FieldSubmitter from './field_submitter'
2023-06-04 01:08:45 +03:00
import FieldType from './field_type'
import Field from './field'
2023-06-08 00:20:42 +03:00
import { IconX } from '@tabler/icons-vue'
2023-06-03 00:53:46 +03:00
2023-05-21 17:59:36 +03:00
export default {
name: 'FieldArea',
2023-06-04 01:08:45 +03:00
components: {
2023-06-07 01:16:43 +03:00
FieldType,
2023-06-08 00:20:42 +03:00
FieldSubmitter,
IconX
2023-06-04 01:08:45 +03:00
},
2023-06-24 16:17:07 +03:00
inject: ['template', 'selectedAreaRef', 'save'],
2023-05-21 17:59:36 +03:00
props: {
2023-06-04 01:08:45 +03:00
area: {
2023-05-21 17:59:36 +03:00
type: Object,
2023-06-04 01:08:45 +03:00
required: true
2023-05-21 17:59:36 +03:00
},
2023-06-27 00:57:29 +03:00
isDraw: {
type: Boolean,
required: false,
default: false
},
2023-05-21 17:59:36 +03:00
field: {
type: Object,
required: false,
default: null
}
},
2023-06-04 01:08:45 +03:00
emits: ['start-resize', 'stop-resize', 'start-drag', 'stop-drag', 'remove'],
2023-05-21 17:59:36 +03:00
data () {
return {
isResize: false,
2023-06-24 16:17:07 +03:00
isDragged: false,
2023-06-04 01:08:45 +03:00
isNameFocus: false,
2023-05-21 17:59:36 +03:00
dragFrom: { x: 0, y: 0 }
}
},
computed: {
2023-06-04 01:08:45 +03:00
defaultName: Field.computed.defaultName,
2023-06-27 00:57:29 +03:00
fieldNames: FieldType.computed.fieldNames,
2023-06-04 01:08:45 +03:00
fieldIcons: FieldType.computed.fieldIcons,
2023-06-27 00:57:29 +03:00
cells () {
const cells = []
let currentWidth = 0
while (currentWidth + (this.area.cell_w + this.area.cell_w / 4) < this.area.w) {
currentWidth += this.area.cell_w || 9999999
cells.push(currentWidth)
}
return cells
},
2023-06-07 01:16:43 +03:00
submitter () {
return this.template.submitters.find((s) => s.uuid === this.field.submitter_uuid)
},
submitterIndex () {
return this.template.submitters.indexOf(this.submitter)
},
borderColors () {
return [
'border-red-500',
'border-sky-500',
'border-emerald-500',
'border-yellow-300',
'border-purple-600'
]
},
bgColors () {
return [
'bg-red-100',
'bg-sky-100',
'bg-emerald-100',
'bg-yellow-100',
'bg-purple-100'
]
},
2023-06-04 01:08:45 +03:00
isSelected () {
return this.selectedAreaRef.value === this.area
2023-06-03 00:53:46 +03:00
},
2023-05-21 17:59:36 +03:00
positionStyle () {
2023-06-04 01:08:45 +03:00
const { x, y, w, h } = this.area
2023-05-21 17:59:36 +03:00
return {
2023-05-23 00:22:17 +03:00
top: y * 100 + '%',
left: x * 100 + '%',
width: w * 100 + '%',
height: h * 100 + '%'
2023-05-21 17:59:36 +03:00
}
}
},
methods: {
2023-06-04 01:08:45 +03:00
onNameFocus (e) {
this.selectedAreaRef.value = this.area
this.isNameFocus = true
this.$refs.name.style.minWidth = this.$refs.name.clientWidth + 'px'
if (!this.field.name) {
setTimeout(() => {
this.$refs.name.innerText = ' '
}, 1)
}
},
2023-06-27 00:57:29 +03:00
startResizeCell (e) {
document.addEventListener('mousemove', this.onResizeCell)
document.addEventListener('mouseup', this.stopResizeCell)
this.$emit('start-resize', 'ew')
},
stopResizeCell (e) {
document.removeEventListener('mousemove', this.onResizeCell)
document.removeEventListener('mouseup', this.stopResizeCell)
this.$emit('stop-resize')
this.save()
},
onResizeCell (e) {
2023-06-30 23:22:57 +03:00
if (e.target.id === 'mask') {
const positionX = e.layerX / (e.target.clientWidth - 1)
2023-06-27 00:57:29 +03:00
if (positionX > this.area.x) {
this.area.cell_w = positionX - this.area.x
}
}
},
2023-06-08 00:20:42 +03:00
maybeUpdateOptions () {
2023-06-16 00:04:05 +03:00
if (!['radio', 'multiple', 'select'].includes(this.field.type)) {
2023-06-07 01:16:43 +03:00
delete this.field.options
}
2023-06-08 00:20:42 +03:00
2023-06-16 00:04:05 +03:00
if (['select', 'multiple', 'radio'].includes(this.field.type)) {
2023-06-08 00:20:42 +03:00
this.field.options ||= ['']
}
2023-06-27 00:57:29 +03:00
(this.field.areas || []).forEach((area) => {
if (this.field.type === 'cells') {
area.cell_w = area.w * 2 / Math.floor(area.w / area.h)
} else {
delete area.cell_w
}
})
2023-06-07 01:16:43 +03:00
},
2023-06-04 01:08:45 +03:00
onNameBlur (e) {
this.isNameFocus = false
this.$refs.name.style.minWidth = ''
if (e.target.innerText.trim()) {
this.field.name = e.target.innerText.trim()
} else {
this.field.name = ''
this.$refs.name.innerText = this.defaultName
}
2023-06-24 16:17:07 +03:00
this.save()
2023-06-04 01:08:45 +03:00
},
onNameEnter (e) {
this.$refs.name.blur()
2023-06-03 00:53:46 +03:00
},
2023-05-21 17:59:36 +03:00
resize (e) {
2023-06-30 23:22:57 +03:00
if (e.target.id === 'mask') {
this.area.w = e.layerX / e.target.clientWidth - this.area.x
this.area.h = e.layerY / e.target.clientHeight - this.area.y
2023-06-03 00:53:46 +03:00
}
2023-05-21 17:59:36 +03:00
},
drag (e) {
2023-06-30 23:22:57 +03:00
if (e.target.id === 'mask') {
2023-06-24 16:17:07 +03:00
this.isDragged = true
2023-06-30 23:22:57 +03:00
this.area.x = (e.layerX - this.dragFrom.x) / e.target.clientWidth
this.area.y = (e.layerY - this.dragFrom.y) / e.target.clientHeight
2023-05-21 17:59:36 +03:00
}
},
startDrag (e) {
2023-06-04 01:08:45 +03:00
this.selectedAreaRef.value = this.area
2023-05-21 17:59:36 +03:00
const rect = e.target.getBoundingClientRect()
this.dragFrom = { x: e.clientX - rect.left, y: e.clientY - rect.top }
document.addEventListener('mousemove', this.drag)
document.addEventListener('mouseup', this.stopDrag)
this.$emit('start-drag')
},
stopDrag () {
document.removeEventListener('mousemove', this.drag)
document.removeEventListener('mouseup', this.stopDrag)
2023-06-24 16:17:07 +03:00
if (this.isDragged) {
this.save()
}
this.isDragged = false
2023-05-21 17:59:36 +03:00
this.$emit('stop-drag')
},
startResize () {
2023-06-04 01:08:45 +03:00
this.selectedAreaRef.value = this.area
2023-05-21 17:59:36 +03:00
document.addEventListener('mousemove', this.resize)
document.addEventListener('mouseup', this.stopResize)
2023-06-27 00:57:29 +03:00
this.$emit('start-resize', 'nwse')
2023-05-21 17:59:36 +03:00
},
stopResize () {
document.removeEventListener('mousemove', this.resize)
document.removeEventListener('mouseup', this.stopResize)
this.$emit('stop-resize')
2023-06-24 16:17:07 +03:00
this.save()
2023-05-21 17:59:36 +03:00
}
}
}
</script>