|
15 | 15 |
|
16 | 16 | <script>
|
17 | 17 | import {FormField, HandlesValidationErrors} from 'laravel-nova'
|
| 18 | + import _ from 'lodash'; |
18 | 19 |
|
19 | 20 | export default {
|
20 | 21 | mixins: [FormField, HandlesValidationErrors],
|
21 | 22 |
|
22 | 23 | props: ['resourceName', 'resourceId', 'field'],
|
23 | 24 |
|
24 | 25 | mounted() {
|
25 |
| - this.registerDependencyWatchers(this.$root, function() { |
26 |
| - this.updateDependencyStatus(); |
27 |
| - }); |
| 26 | + this.initializeComponent(); |
28 | 27 | },
|
29 | 28 |
|
30 | 29 | data() {
|
31 | 30 | return {
|
32 | 31 | dependencyValues: {},
|
33 | 32 | 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 |
34 | 78 | }
|
35 | 79 | },
|
36 | 80 |
|
37 | 81 | methods: {
|
38 | 82 |
|
| 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 | +
|
39 | 117 | // @todo: refactor entire watcher procedure, this approach isn't maintainable ..
|
40 | 118 | registerDependencyWatchers(root, callback) {
|
41 | 119 | callback = callback || null;
|
|
94 | 172 | if(component.field === undefined) {
|
95 | 173 | return false;
|
96 | 174 | }
|
97 |
| -
|
98 | 175 | for (let dependency of this.field.dependencies) {
|
99 |
| - if(component.field.attribute === dependency.field) { |
| 176 | + if(component.field.attribute === (this.field.attribute + dependency.field)) { |
100 | 177 | return true;
|
101 | 178 | }
|
102 | 179 | }
|
|
108 | 185 | updateDependencyStatus() {
|
109 | 186 | for (let dependency of this.field.dependencies) {
|
110 | 187 |
|
111 |
| - let dependencyValue = this.dependencyValues[dependency.field]; |
| 188 | + let dependencyValue = this.dependencyValues[(this.field.attribute + dependency.field)]; |
112 | 189 | if(dependency.hasOwnProperty('empty') && !dependencyValue) {
|
113 | 190 | this.dependenciesSatisfied = true;
|
114 | 191 | return;
|
|
0 commit comments