-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathFormField.vue
139 lines (122 loc) · 4.44 KB
/
FormField.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<template>
<component :is="field.fullSize ? 'FullWidthField' : 'DefaultField'" :field="currentField" :errors="errors" :show-help-text="showHelpText">
<template #field>
<div :class="{'px-8 pt-6': field.fullSize}">
<gallery slot="value" ref="gallery" v-if="hasSetInitialValue"
v-model="value" :editable="!field.readonly" :removable="field.removable" custom-properties :field="field" :multiple="field.multiple" :uploads-to-vapor="field.uploadsToVapor"
:has-error="hasError" :first-error="firstError"/>
<div v-if="field.existingMedia">
<OutlineButton type="button" class="mt-2" @click.prevent="existingMediaOpen = true">
{{ openExistingMediaLabel }}
</OutlineButton>
<existing-media :open="existingMediaOpen" @close="existingMediaOpen = false" @select="addExistingItem"/>
</div>
<help-text
class="error-text mt-2 text-danger"
v-if="showErrors && hasError"
>
{{ firstError }}
</help-text>
</div>
</template>
</component>
</template>
<script>
import {DependentFormField, HandlesValidationErrors} from 'laravel-nova';
import Gallery from '../Gallery';
import FullWidthField from '../FullWidthField';
import ExistingMedia from '../ExistingMedia';
import objectToFormData from 'object-to-formdata';
import get from 'lodash/get';
export default {
mixins: [DependentFormField, HandlesValidationErrors],
components: {
Gallery,
FullWidthField,
ExistingMedia
},
props: ['resourceName', 'resourceId', 'field'],
data() {
return {
hasSetInitialValue: false,
existingMediaOpen: false,
}
},
computed: {
openExistingMediaLabel() {
const type = this.field.type === 'media' ? 'Media' : 'File';
if (this.field.multiple || this.value.length === 0) {
return this.__(`Add Existing ${type}`);
}
return this.__(`Use Existing ${type}`);
}
},
methods: {
/*
* Set the initial, internal value for the field.
*/
setInitialValue() {
let value = this.field.value || [];
if (!this.field.multiple) {
value = value.slice(0, 1);
}
this.value = value;
this.hasSetInitialValue = true;
},
/**
* Fill the given FormData object with the field's internal value.
*/
fill(formData) {
const field = this.field.attribute;
this.value.forEach((file, index) => {
const isNewImage = !file.id;
if (isNewImage) {
if (file.isVaporUpload) {
// In case of Vapor upload, do not send the file's binary data over the wire.
// The file can already be found in the bucket.
formData.append(`__media__[${field}][${index}][is_vapor_upload]`, true);
formData.append(`__media__[${field}][${index}][key]`, file.vaporFile.key);
formData.append(`__media__[${field}][${index}][uuid]`, file.vaporFile.uuid);
formData.append(`__media__[${field}][${index}][file_name]`, file.vaporFile.filename);
formData.append(`__media__[${field}][${index}][file_size]`, file.vaporFile.file_size);
formData.append(`__media__[${field}][${index}][mime_type]`, file.vaporFile.mime_type);
} else {
formData.append(`__media__[${field}][${index}]`, file.file, file.name);
}
} else {
formData.append(`__media__[${field}][${index}]`, file.id);
}
objectToFormData({
[`__media-custom-properties__[${field}][${index}]`]: this.getImageCustomProperties(file)
}, {}, formData);
});
},
getImageCustomProperties(image) {
return (this.field.customPropertiesFields || []).reduce((properties, {attribute: property}) => {
properties[property] = get(image, `custom_properties.${property}`);
// Fixes checkbox problem
if (properties[property] === true) {
properties[property] = 1;
}
return properties;
}, {})
},
/**
* Update the field's internal value.
*/
handleChange(value) {
this.value = value
},
addExistingItem(item) {
// Copy to trigger watcher to recognize differnece between new and old values
// https://github.com/vuejs/vue/issues/2164
let copiedArray = this.value.slice(0)
if (!this.field.multiple) {
copiedArray.splice(0, 1);
}
copiedArray.push(item);
this.value = copiedArray;
}
},
};
</script>