class AddFieldSheet extends HTMLElement {
constructor() {
super();
this.onSubmit = null;
this.inputType = null;
this.attachShadow({mode: 'open'});
this.render();
this.addFieldSheet = this.shadowRoot.querySelector('#addFieldSheet');
this.overlay = this.shadowRoot.getElementById('overlay');
this.shadowRoot.querySelector('#addFieldForm').onsubmit = (e) => this.handleSubmit(e);
this.shadowRoot.querySelector('#colorPicker').oninput = (e) => this.updateFieldValue(e);
this.overlay.onclick = () => this.hideAddFieldForm();
}
render() {
this.shadowRoot.innerHTML = `
`;
}
show(inputType, onSubmit) {
this.onSubmit = onSubmit;
this.inputType = inputType;
const fieldName = this.shadowRoot.querySelector('#fieldName');
const fieldValue = this.shadowRoot.querySelector('#fieldValue');
const colorPickerContainer = this.shadowRoot.querySelector('.color-picker-container');
fieldName.value = '';
fieldValue.value = '';
colorPickerContainer.style.display = inputType === 'color' ? 'block' : 'none';
this.addFieldSheet.style.display = 'block';
this.overlay.classList.add('show');
setTimeout(() => this.addFieldSheet.classList.add('show'), 10);
}
hideAddFieldForm() {
this.addFieldSheet.classList.remove('show');
this.overlay.classList.remove('show');
setTimeout(() => {
this.addFieldSheet.style.display = 'none';
}, 300);
}
handleSubmit(e) {
e.preventDefault();
const fieldName = this.shadowRoot.querySelector('#fieldName').value;
let defaultValue = this.shadowRoot.querySelector('#fieldValue').value;
if (this.inputType === 'color' && !defaultValue.startsWith('#')) {
defaultValue = '#' + defaultValue;
}
if (this.onSubmit) {
this.onSubmit(fieldName, defaultValue);
}
this.hideAddFieldForm();
}
updateFieldValue(event) {
const fieldValue = this.shadowRoot.querySelector('#fieldValue');
fieldValue.value = event.target.value;
}
}
customElements.define('add-field-sheet', AddFieldSheet);