Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

71 changes: 50 additions & 21 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
props: ['resourceName', 'resourceId', 'field'],

mounted() {
this.registerDependencyWatchers(this.$root, function() {
this.updateDependencyStatus();
});
this.isNestedForm = this.$parent.$parent.$options.name == 'form-nested-form';
this.registerDependencyWatchers(this.$root, function() {
this.updateDependencyStatus();
}, true);
},

data() {
Expand All @@ -38,9 +39,10 @@
methods: {

// @todo: refactor entire watcher procedure, this approach isn't maintainable ..
registerDependencyWatchers(root, callback) {
registerDependencyWatchers(root, callback, onMounted) {
callback = callback || null;
root.$children.forEach(component => {
root = onMounted && this.isNestedForm ? this.$parent.$parent : root;
root.$children.forEach(component => {
if (this.componentIsDependency(component)) {

// @todo: change `findWatchableComponentAttribute` to return initial state(s) of current dependency.
Expand All @@ -67,7 +69,7 @@
this.dependencyValues[component.field.attribute] = initial_value;
}

this.registerDependencyWatchers(component)
this.registerDependencyWatchers(component, null, false)
});

if (callback !== null) {
Expand All @@ -81,9 +83,12 @@
switch(component.field.component) {
case 'belongs-to-many-field':
case 'belongs-to-field':
case 'nested-form-belongs-to-many-field':
case 'nested-form-belongs-to-field':
attribute = 'selectedResource';
break;
case 'morph-to-field':
case 'morph-to-field':
case 'nested-form-morph-to-field':
attribute = 'fieldTypeName';
break;
default:
Expand All @@ -98,10 +103,17 @@
}

for (let dependency of this.field.dependencies) {
// #93 compatability with flexible-content, which adds a generated attribute for each field
if (component.field.attribute === (this.field.attribute + dependency.field)) {
return true;
}
// #93 compatability with flexible-content, which adds a generated attribute for each field
let attributeToCheck;
if(this.isNestedForm) {
attributeToCheck = this.field.attribute + '[' + dependency.field + ']';
} else {
attributeToCheck = this.field.attribute + dependency.field;
}

if (component.field.attribute === attributeToCheck) {
return true;
}
}

return false;
Expand All @@ -111,8 +123,15 @@
updateDependencyStatus() {
for (let dependency of this.field.dependencies) {

let attribute;
if(this.isNestedForm) {
attribute = this.field.attribute + '[' + dependency.field + ']';
} else {
attribute = this.field.attribute + dependency.field;
}

// #93 compatability with flexible-content, which adds a generated attribute for each field
let dependencyValue = this.dependencyValues[(this.field.attribute + dependency.field)];
let dependencyValue = this.dependencyValues[attribute];
if (dependency.hasOwnProperty('empty') && !dependencyValue) {
this.dependenciesSatisfied = true;
return;
Expand Down Expand Up @@ -142,15 +161,25 @@
this.dependenciesSatisfied = false;
},

fill(formData) {
if (this.dependenciesSatisfied) {
_.each(this.field.fields, field => {
if (field.fill) {
field.fill(formData)
}
})
}
}
fill(formData) {
if (this.dependenciesSatisfied) {
_.each(this.field.fields, field => {
if (field.fill) {
if(this.isNestedForm) {
field.fill(formData);
for (const [key, value] of formData.entries()) {
if (key == field.attribute) {
formData.append(`${this.field.attribute}[${field.attribute}]`, value)
formData.delete(key)
}
}
} else {
field.fill(formData);
}
}
})
}
}

}
}
Expand Down