|
| 1 | +import { CancelToken } from 'axios' |
| 2 | +import debounce from 'lodash/debounce' |
| 3 | +import forIn from 'lodash/forIn' |
| 4 | +import get from 'lodash/get' |
| 5 | +import identity from 'lodash/identity' |
| 6 | +import isNil from 'lodash/isNil' |
| 7 | +import pickBy from 'lodash/pickBy' |
| 8 | +import FormField from './FormField' |
| 9 | +import { mapProps } from './propTypes' |
| 10 | + |
| 11 | +export default { |
| 12 | + extends: FormField, |
| 13 | + props: mapProps([ |
| 14 | + 'shownViaNewRelationModal', |
| 15 | + 'field', |
| 16 | + 'viaResource', |
| 17 | + 'viaResourceId', |
| 18 | + 'viaRelationship', |
| 19 | + 'resourceName', |
| 20 | + 'resourceId', |
| 21 | + 'relatedResourceName', |
| 22 | + 'relatedResourceId', |
| 23 | + ]), |
| 24 | + |
| 25 | + data: () => ({ |
| 26 | + debouncer: null, |
| 27 | + canceller: null, |
| 28 | + watchedFields: {}, |
| 29 | + watchedEvents: {}, |
| 30 | + syncedField: null, |
| 31 | + pivot: false, |
| 32 | + editMode: 'create', |
| 33 | + }), |
| 34 | + |
| 35 | + created() { |
| 36 | + this.debouncer = debounce(callback => callback(), 50) |
| 37 | + }, |
| 38 | + |
| 39 | + mounted() { |
| 40 | + if (this.relatedResourceName !== '' && !isNil(this.relatedResourceName)) { |
| 41 | + this.pivot = true |
| 42 | + |
| 43 | + if (this.relatedResourceId !== '' && !isNil(this.relatedResourceId)) { |
| 44 | + this.editMode = 'update-attached' |
| 45 | + } else { |
| 46 | + this.editMode = 'attach' |
| 47 | + } |
| 48 | + } else { |
| 49 | + if (this.resourceId !== '' && !isNil(this.resourceId)) { |
| 50 | + this.editMode = 'update' |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (this.dependsOn.length > 0) { |
| 55 | + this.dependsOn.forEach(dependsOn => { |
| 56 | + this.watchedEvents[dependsOn] = value => { |
| 57 | + this.watchedFields[dependsOn] = value |
| 58 | + |
| 59 | + this.debouncer(() => this.syncField()) |
| 60 | + } |
| 61 | + |
| 62 | + Nova.$on( |
| 63 | + this.getFieldAttributeChangeEventName(dependsOn), |
| 64 | + this.watchedEvents[dependsOn] |
| 65 | + ) |
| 66 | + }) |
| 67 | + } |
| 68 | + }, |
| 69 | + |
| 70 | + beforeUnmount() { |
| 71 | + if (this.dependsOn.length > 0) { |
| 72 | + forIn(this.watchedEvents, (event, dependsOn) => { |
| 73 | + Nova.$off(this.getFieldAttributeChangeEventName(event.dependsOn), event) |
| 74 | + }) |
| 75 | + } |
| 76 | + }, |
| 77 | + |
| 78 | + methods: { |
| 79 | + /* |
| 80 | + * Set the initial value for the field |
| 81 | + */ |
| 82 | + setInitialValue() { |
| 83 | + this.value = !( |
| 84 | + this.currentField.value === undefined || |
| 85 | + this.currentField.value === null |
| 86 | + ) |
| 87 | + ? this.currentField.value |
| 88 | + : this.value |
| 89 | + }, |
| 90 | + |
| 91 | + syncField() { |
| 92 | + if (this.canceller !== null) this.canceller() |
| 93 | + |
| 94 | + Nova.request() |
| 95 | + .patch(this.syncFieldEndpoint, this.watchedFields, { |
| 96 | + params: pickBy( |
| 97 | + { |
| 98 | + editing: true, |
| 99 | + editMode: this.editMode, |
| 100 | + viaResource: this.viaResource, |
| 101 | + viaResourceId: this.viaResourceId, |
| 102 | + viaRelationship: this.viaRelationship, |
| 103 | + field: this.field.attribute, |
| 104 | + }, |
| 105 | + identity |
| 106 | + ), |
| 107 | + cancelToken: new CancelToken(canceller => { |
| 108 | + this.canceller = canceller |
| 109 | + }), |
| 110 | + }) |
| 111 | + .then(response => { |
| 112 | + this.syncedField = response.data |
| 113 | + |
| 114 | + if (isNil(this.syncedField.value)) { |
| 115 | + this.syncedField.value = this.field.value |
| 116 | + } else { |
| 117 | + this.setInitialValue() |
| 118 | + } |
| 119 | + |
| 120 | + this.onSyncedField() |
| 121 | + }) |
| 122 | + }, |
| 123 | + |
| 124 | + onSyncedField() { |
| 125 | + // |
| 126 | + }, |
| 127 | + }, |
| 128 | + |
| 129 | + computed: { |
| 130 | + /** |
| 131 | + * Determine if the field is in readonly mode |
| 132 | + */ |
| 133 | + currentField() { |
| 134 | + return this.syncedField || this.field |
| 135 | + }, |
| 136 | + |
| 137 | + /** |
| 138 | + * Determine if the field is in readonly mode |
| 139 | + */ |
| 140 | + currentlyIsReadonly() { |
| 141 | + if (this.syncedField !== null) { |
| 142 | + return Boolean( |
| 143 | + this.syncedField.readonly || |
| 144 | + get(this.syncedField, 'extraAttributes.readonly') |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + return Boolean( |
| 149 | + this.field.readonly || get(this.field, 'extraAttributes.readonly') |
| 150 | + ) |
| 151 | + }, |
| 152 | + |
| 153 | + dependsOn() { |
| 154 | + return this.field.dependsOn || [] |
| 155 | + }, |
| 156 | + |
| 157 | + syncFieldEndpoint() { |
| 158 | + if (this.editMode === 'update-attached') { |
| 159 | + return `/nova-api/${this.resourceName}/${this.resourceId}/update-pivot-fields/${this.relatedResourceName}/${this.relatedResourceId}` |
| 160 | + } else if (this.editMode == 'attach') { |
| 161 | + return `/nova-api/${this.resourceName}/${this.resourceId}/creation-pivot-fields/${this.relatedResourceName}` |
| 162 | + } else if (this.editMode === 'update') { |
| 163 | + return `/nova-api/${this.resourceName}/${this.resourceId}/update-fields` |
| 164 | + } |
| 165 | + |
| 166 | + return `/nova-api/${this.resourceName}/creation-fields` |
| 167 | + }, |
| 168 | + }, |
| 169 | +} |
0 commit comments