formforge-geostudio / components /device-preview.js
mapolanski's picture
Onde esta tudo o que eu pedi? So apareceu um botao enviar dados e alguns textos. O sistemas que quero é um gerador de formularios e nao um formulario apenas
b2363b3 verified
Raw History Blame Contribute Delete
9.81 kB
class GeoDevicePreview extends HTMLElement {
constructor() {
super();
this.deviceType = 'mobile';
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
}
static get observedAttributes() {
return ['device-type'];
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'device-type') {
this.deviceType = newValue;
this.render();
}
}
render() {
const deviceStyles = {
mobile: 'width: 375px; height: 812px; border-radius: 40px; border: 12px solid #1f2937;',
tablet: 'width: 768px; height: 1024px; border-radius: 20px; border: 12px solid #1f2937;',
desktop: 'width: 100%; max-width: 1024px; height: 768px; border-radius: 8px; border: 8px solid #1f2937;'
};
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
}
.device-frame {
${deviceStyles[this.deviceType] || deviceStyles.mobile}
background: white;
overflow: hidden;
position: relative;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
transition: all 0.3s ease;
margin: 0 auto;
}
.dark .device-frame {
background: #1f2937;
border-color: #111827;
}
.device-screen {
width: 100%;
height: 100%;
overflow-y: auto;
padding: 1rem;
background: #f9fafb;
}
.dark .device-screen {
background: #111827;
}
.form-header {
background: linear-gradient(135deg, #6366f1, #8b5cf6);
color: white;
padding: 1.5rem;
margin: -1rem -1rem 1rem -1rem;
text-align: center;
}
.form-title {
font-size: 1.25rem;
font-weight: 700;
margin: 0 0 0.25rem 0;
}
.form-subtitle {
font-size: 0.875rem;
opacity: 0.9;
margin: 0;
}
.form-element {
background: white;
border-radius: 0.75rem;
padding: 1rem;
margin-bottom: 0.75rem;
border: 2px solid transparent;
transition: all 0.2s;
cursor: pointer;
}
.form-element:hover {
border-color: #6366f1;
box-shadow: 0 4px 6px -1px rgba(99, 102, 241, 0.1);
}
.form-element.selected {
border-color: #6366f1;
background: #eef2ff;
}
.element-label {
display: block;
font-size: 0.875rem;
font-weight: 500;
color: #374151;
margin-bottom: 0.5rem;
}
.element-required {
color: #ef4444;
}
.element-input {
width: 100%;
padding: 0.625rem;
border: 1px solid #d1d5db;
border-radius: 0.5rem;
font-size: 0.875rem;
background: #f9fafb;
}
.element-input:focus {
outline: none;
border-color: #6366f1;
ring: 2px solid #6366f1;
}
.empty-state {
text-align: center;
padding: 3rem 1rem;
color: #9ca3af;
}
.empty-icon {
width: 4rem;
height: 4rem;
margin: 0 auto 1rem;
opacity: 0.5;
}
.delete-btn {
position: absolute;
top: -8px;
right: -8px;
width: 24px;
height: 24px;
background: #ef4444;
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
opacity: 0;
transition: opacity 0.2s;
font-size: 12px;
}
.form-element:hover .delete-btn {
opacity: 1;
}
</style>
<div class="device-frame">
<div class="device-screen" id="dropZone">
<div class="form-header">
<h2 class="form-title" id="formTitle">Novo Formulário</h2>
<p class="form-subtitle" id="formSubtitle">Arraste campos para começar</p>
</div>
<div id="formContent">
<div class="empty-state">
<svg class="empty-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1">
<rect x="3" y="3" width="18" height="18" rx="2" ry="2"/>
<line x1="12" y1="8" x2="12" y2="16"/>
<line x1="8" y1="12" x2="16" y2="12"/>
</svg>
<p>Arraste ferramentas da caixa ao lado</p>
</div>
</div>
</div>
</div>
`;
this.setupDragAndDrop();
}
setupDragAndDrop() {
const dropZone = this.shadowRoot.getElementById('dropZone');
const formContent = this.shadowRoot.getElementById('formContent');
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.style.background = 'rgba(99, 102, 241, 0.05)';
});
dropZone.addEventListener('dragleave', () => {
dropZone.style.background = '';
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.style.background = '';
const fieldType = e.dataTransfer.getData('fieldType');
if (fieldType) {
this.addField(fieldType);
}
});
}
addField(type) {
const formContent = this.shadowRoot.getElementById('formContent');
const emptyState = formContent.querySelector('.empty-state');
if (emptyState) {
emptyState.remove();
}
const fieldId = 'field_' + Date.now();
const fieldData = this.getFieldData(type);
const element = document.createElement('div');
element.className = 'form-element';
element.dataset.fieldId = fieldId;
element.dataset.fieldType = type;
element.innerHTML = `
<span class="delete-btn" onclick="this.closest('.form-element').remove()">×</span>
<label class="element-label">
${fieldData.label}
${fieldData.required ? '<span class="element-required">*</span>' : ''}
</label>
${this.getFieldHTML(type, fieldData)}
`;
element.addEventListener('click', (e) => {
if (!e.target.classList.contains('delete-btn')) {
this.selectField(element);
}
});
formContent.appendChild(element);
this.selectField(element);
// Dispatch event for parent
this.dispatchEvent(new CustomEvent('fieldAdded', {
detail: { fieldId, type, data: fieldData }
}));
}
getFieldData(type) {
const defaults = {
text: { label: 'Campo de Texto', placeholder: 'Digite aqui...', required: false },
textarea: { label: 'Texto Longo', placeholder: 'Digite aqui...', required: false },
number: { label: 'Número', required: false },
select: { label: 'Seleção', options: ['Opção 1', 'Opção 2', 'Opção 3'], required: false },
date: { label: 'Data', required: false },
photo: { label: 'Foto', required: false },
map: { label: 'Localização', geometryType: 'point', required: false },
signature: { label: 'Assinatura', required: false }
};
return defaults[type] || defaults.text;
}
getFieldHTML(type, data) {
switch (type) {
case 'text':
return `<input type="text" class="element-input" placeholder="${data.placeholder}" disabled>`;
case 'textarea':
return `<textarea class="element-input" rows="3" placeholder="${data.placeholder}" disabled></textarea>`;
case 'number':
return `<input type="number" class="element-input" disabled>`;
case 'select':
return `<select class="element-input" disabled><option>Selecione...</option>${data.options.map(o => `<option>${o}</option>`).join('')}</select>`;
case 'date':
return `<input type="date" class="element-input" disabled>`;
case 'photo':
return `<div class="element-input" style="text-align: center; padding: 2rem; color: #9ca3af;"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="margin: 0 auto 0.5rem;"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>Tocar para foto</div></div>`;
case 'map':
return `<div class="element-input" style="height: 120px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; color: white; font-size: 0.75rem;">🗺️ Mapa ${data.geometryType === 'point' ? '(Ponto)' : data.geometryType === 'line' ? '(Linha)' : '(Polígono)'}</div>`;
case 'signature':
return `<div class="element-input" style="height: 100px; border: 2px dashed #d1d5db; display: flex; align-items: center; justify-content: center; color: #9ca3af; font-size: 0.75rem;">✍️ Área de Assinatura</div>`;
default:
return `<input type="text" class="element-input" disabled>`;
}
}
selectField(element) {
this.shadowRoot.querySelectorAll('.form-element').forEach(el => {
el.classList.remove('selected');
});
element.classList.add('selected');
const fieldData = {
id: element.dataset.fieldId,
type: element.dataset.fieldType
};
this.dispatchEvent(new CustomEvent('fieldSelected', {
detail: fieldData
}));
}
setFormTitle(title, subtitle) {
const titleEl = this.shadowRoot.getElementById('formTitle');
const subtitleEl = this.shadowRoot.getElementById('formSubtitle');
if (titleEl) titleEl.textContent = title;
if (subtitleEl) subtitleEl.textContent = subtitle;
}
}
customElements.define('geo-device-preview', GeoDevicePreview);