Skip to content

Commit 3c08134

Browse files
committed
[NOVA]
1 parent 387ad1d commit 3c08134

File tree

3 files changed

+28204
-8
lines changed

3 files changed

+28204
-8
lines changed

dist/js/field.js

+28,118-1
Large diffs are not rendered by default.

resources/js/components/FormField.vue

+83-6
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,105 @@
1515

1616
<script>
1717
import {FormField, HandlesValidationErrors} from 'laravel-nova'
18+
import _ from 'lodash';
1819
1920
export default {
2021
mixins: [FormField, HandlesValidationErrors],
2122
2223
props: ['resourceName', 'resourceId', 'field'],
2324
2425
mounted() {
25-
this.registerDependencyWatchers(this.$root, function() {
26-
this.updateDependencyStatus();
27-
});
26+
this.initializeComponent();
2827
},
2928
3029
data() {
3130
return {
3231
dependencyValues: {},
3332
dependenciesSatisfied: false,
33+
34+
/**
35+
* compatibility between packages
36+
*/
37+
packageCompat: {
38+
// whitecubes flexible-content
39+
// flexible-contents adds fields on the fly by generating a hashed attribute for each added layout (group)
40+
// @hash "hash-LayoutName__fieldattribute"
41+
packages: {
42+
flexibleContent: {
43+
// the package component name
44+
key: 'flexibleContent',
45+
name: 'nova-flexible-content',
46+
// return value of `check`
47+
is: false,
48+
// check if container needs to run in compatMode for `package_name`
49+
check: function (container) {
50+
let component = container.$parent,
51+
is;
52+
53+
if ((is = typeof component.field !== 'undefined' &&
54+
component.field.component === this.name)) {
55+
56+
container.compatMode = this.key;
57+
// gather settings
58+
this.settings.group_key = component.group.key;
59+
}
60+
61+
return is;
62+
},
63+
// settings to gather
64+
settings: {
65+
group_key: null
66+
},
67+
// callbacks to execute
68+
callbacks: {
69+
getRootComponent() {
70+
return this.$parent;
71+
}
72+
}
73+
}
74+
}
75+
},
76+
77+
compatMode: null
3478
}
3579
},
3680
3781
methods: {
3882
83+
checkCompatability() {
84+
_.each(this.packageCompat.packages, function(_package) {
85+
_package.check(this);
86+
}.bind(this));
87+
},
88+
89+
getCompatibilityCallback(callback_name) {
90+
if(this.compatMode !== null) {
91+
let callback = this.packageCompat.packages[this.compatMode].callbacks[callback_name];
92+
if(typeof callback !== 'undefined') {
93+
return callback.bind(this);
94+
}
95+
}
96+
return null;
97+
},
98+
99+
initializeComponent() {
100+
// first check if we need to consider any compatibilities
101+
this.checkCompatability();
102+
// register dependency watchers for any changes
103+
this.registerDependencyWatchers(this.getRootComponent(), function() {
104+
this.updateDependencyStatus();
105+
});
106+
},
107+
108+
getRootComponent() {
109+
let callback;
110+
if((callback = this.getCompatibilityCallback('getRootComponent')) !== null) {
111+
return callback();
112+
}
113+
// default
114+
return this.$root;
115+
},
116+
39117
// @todo: refactor entire watcher procedure, this approach isn't maintainable ..
40118
registerDependencyWatchers(root, callback) {
41119
callback = callback || null;
@@ -94,9 +172,8 @@
94172
if(component.field === undefined) {
95173
return false;
96174
}
97-
98175
for (let dependency of this.field.dependencies) {
99-
if(component.field.attribute === dependency.field) {
176+
if(component.field.attribute === (this.field.attribute + dependency.field)) {
100177
return true;
101178
}
102179
}
@@ -108,7 +185,7 @@
108185
updateDependencyStatus() {
109186
for (let dependency of this.field.dependencies) {
110187
111-
let dependencyValue = this.dependencyValues[dependency.field];
188+
let dependencyValue = this.dependencyValues[(this.field.attribute + dependency.field)];
112189
if(dependency.hasOwnProperty('empty') && !dependencyValue) {
113190
this.dependenciesSatisfied = true;
114191
return;

src/HasDependencies.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Laravel\Nova\Fields\FieldCollection;
1010
use Laravel\Nova\Http\Requests\NovaRequest;
1111
use Laravel\Nova\Fields\MorphTo;
12+
use Illuminate\Support\Facades\Log;
1213

1314
trait HasDependencies
1415
{
@@ -27,7 +28,8 @@ public function availableFields(NovaRequest $request)
2728
foreach ($fields as $field) {
2829
if ($field instanceof NovaDependencyContainer) {
2930
$availableFields[] = $this->filterFieldForRequest($field, $request);
30-
if($field->areDependenciesSatisfied($request) || $this->extractableRequest($request, $this->model())) {
31+
$extractableRequest = $this->extractableRequest($request, $this->model());
32+
if($field->areDependenciesSatisfied($request) || $extractableRequest) {
3133
if ($this->doesRouteRequireChildFields()) {
3234
$this->extractChildFields($field->meta['fields']);
3335
}

0 commit comments

Comments
 (0)