2024-03-08 14:39:50 +00:00
|
|
|
|
|
|
|
class MockData {
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
getDataValue(path) {
|
|
|
|
if (!path) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
|
|
|
|
if (this.active) {
|
|
|
|
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({
|
|
|
|
value: (valueKey) => {
|
|
|
|
return this.getDataValue(this.fields[valueKey].valuePath);
|
|
|
|
},
|
|
|
|
isEmpty: () => {
|
|
|
|
let isEmpty = true;
|
|
|
|
Object.values(this.fields).forEach((valueField) => {
|
|
|
|
if (this.getDataValue(valueField.valuePath)) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MockData;
|