2024-03-08 14:39:50 +00:00
|
|
|
|
2024-04-08 11:31:12 +01:00
|
|
|
class FormFieldGroup {
|
2024-03-08 14:39:50 +00:00
|
|
|
|
|
|
|
constructor(config) {
|
|
|
|
this.data = {};
|
|
|
|
|
|
|
|
this.baseSelector = config.baseSelector;
|
|
|
|
this.contentSelector = config.contentSelector;
|
|
|
|
this.fields = config.fields || {};
|
|
|
|
this.template = config.template;
|
|
|
|
|
|
|
|
this.active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setData(data) {
|
|
|
|
this.data = data;
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
2024-04-10 18:27:21 +01:00
|
|
|
dataValue(fieldKey) {
|
|
|
|
if (!fieldKey || !this.fields[fieldKey]) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2024-04-11 12:03:31 +01:00
|
|
|
if (typeof this.fields[fieldKey].valueCallback === 'function') {
|
|
|
|
return this.fields[fieldKey].valueCallback(this.data);
|
|
|
|
}
|
|
|
|
|
2024-04-10 18:27:21 +01:00
|
|
|
const path = this.fields[fieldKey].valuePath;
|
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
if (!path) {
|
|
|
|
return '';
|
|
|
|
}
|
2024-04-10 18:27:21 +01:00
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
const value = path.split('.').reduce((acc, key) => (acc && acc[key] !== undefined) ? acc[key] : undefined, this.data);
|
|
|
|
return value ? value : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
activate() {
|
|
|
|
this.active = true;
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
this.active = false;
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
this.active ? this.deactivate() : this.activate();
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
let content = document.querySelector(this.contentSelector);
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
content.innerHTML = '';
|
|
|
|
|
2024-03-14 10:54:15 +00:00
|
|
|
if (!this.active) {
|
|
|
|
this.hideField(this.contentSelector);
|
|
|
|
} else {
|
|
|
|
this.showField(this.contentSelector);
|
|
|
|
}
|
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
Object.keys(this.fields).forEach((key) => {
|
|
|
|
const field = this.fields[key];
|
|
|
|
|
2024-04-11 12:03:31 +01:00
|
|
|
if (this.active && !field.showInput) {
|
2024-03-08 14:39:50 +00:00
|
|
|
this.hideField(field.selector);
|
|
|
|
} else {
|
|
|
|
this.showField(field.selector);
|
|
|
|
}
|
2024-03-14 10:54:15 +00:00
|
|
|
});
|
2024-03-08 14:39:50 +00:00
|
|
|
|
2024-03-14 10:54:15 +00:00
|
|
|
if (typeof this.template === 'function') {
|
|
|
|
content.innerHTML = this.template({
|
2024-04-10 18:27:21 +01:00
|
|
|
value: (fieldKey) => {
|
|
|
|
return this.dataValue(fieldKey);
|
2024-03-14 10:54:15 +00:00
|
|
|
},
|
|
|
|
isEmpty: () => {
|
|
|
|
let isEmpty = true;
|
2024-04-10 18:27:21 +01:00
|
|
|
Object.keys(this.fields).forEach((fieldKey) => {
|
|
|
|
if (this.dataValue(fieldKey)) {
|
2024-03-14 10:54:15 +00:00
|
|
|
isEmpty = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return isEmpty;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-03-08 14:39:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
showField(selector) {
|
|
|
|
const field = document.querySelector(this.baseSelector + ' ' + selector);
|
|
|
|
if (field) {
|
|
|
|
field.classList.remove('ppcp-axo-field-hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hideField(selector) {
|
|
|
|
const field = document.querySelector(this.baseSelector + ' ' + selector);
|
|
|
|
if (field) {
|
|
|
|
field.classList.add('ppcp-axo-field-hidden');
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 11:31:12 +01:00
|
|
|
|
2024-04-10 18:27:21 +01:00
|
|
|
inputElement(name) {
|
2024-04-10 15:51:19 +01:00
|
|
|
const baseSelector = this.fields[name].selector;
|
2024-04-08 14:53:09 +01:00
|
|
|
|
2024-04-10 18:27:21 +01:00
|
|
|
const select = document.querySelector(baseSelector + ' select');
|
2024-04-10 15:51:19 +01:00
|
|
|
if (select) {
|
2024-04-10 18:27:21 +01:00
|
|
|
return select;
|
2024-04-10 15:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const input = document.querySelector(baseSelector + ' input');
|
|
|
|
if (input) {
|
2024-04-10 18:27:21 +01:00
|
|
|
return input;
|
2024-04-10 15:51:19 +01:00
|
|
|
}
|
|
|
|
|
2024-04-10 18:27:21 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputValue(name) {
|
|
|
|
const el = this.inputElement(name);
|
|
|
|
return el ? el.value : '';
|
|
|
|
}
|
|
|
|
|
2024-04-15 17:20:02 +01:00
|
|
|
toSubmitData(data) {
|
2024-04-10 18:27:21 +01:00
|
|
|
Object.keys(this.fields).forEach((fieldKey) => {
|
|
|
|
const field = this.fields[fieldKey];
|
|
|
|
|
|
|
|
if (!field.valuePath || !field.selector) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputElement = this.inputElement(fieldKey);
|
|
|
|
|
|
|
|
if (!inputElement) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-15 17:20:02 +01:00
|
|
|
data[inputElement.name] = this.dataValue(fieldKey);
|
2024-04-10 18:27:21 +01:00
|
|
|
});
|
2024-04-08 11:31:12 +01:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:39:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 11:31:12 +01:00
|
|
|
export default FormFieldGroup;
|