-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathInputAsset.vue
More file actions
160 lines (153 loc) · 3.86 KB
/
InputAsset.vue
File metadata and controls
160 lines (153 loc) · 3.86 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<template>
<v-toolbar-items>
<v-btn
v-if="url && !isEditing"
:href="publicUrl || url"
target="_blank"
color="info"
text>
<v-icon>mdi-open-in-new</v-icon>
</v-btn>
<upload-btn
v-if="allowFileUpload"
v-show="!file && isEditing"
@upload="val => (file = val) && (urlInput = null)"
:uploading.sync="uploading"
:validate="{ ext: extensions }"
:confirm-deletion="false"
:repository-id="repositoryId"
:label="uploadLabel"
class="upload-btn" />
<template v-if="file">
<v-btn
v-if="isEditing"
@click.stop="file = null"
color="red"
text>
<v-icon>mdi-delete</v-icon>
</v-btn>
<v-text-field
:value="fileName"
readonly hide-details filled />
</template>
<validation-provider
v-if="!uploading && (urlInput || !hasAsset)"
ref="provider"
v-slot="{ errors }"
:rules="{
url: {
protocols: ['http', 'https'],
require_protocol: true,
require_valid_protocol: true
}
}"
name="URL">
<v-text-field
v-model="urlInput"
:error-messages="errors"
:disabled="!isEditing"
:placeholder="allowFileUpload ? 'or paste a URL...' : 'Paste a URL...'"
filled clearable />
</validation-provider>
<v-btn
v-if="!isEditing"
@click.stop="isEditing = true"
text
class="action">
Edit
</v-btn>
<template v-else>
<v-btn
v-if="hasChanges"
@click.stop="save"
:disabled="uploading"
text
class="action">
Save
</v-btn>
<v-btn
v-if="hasChanges || url"
@click.stop="cancel"
:disabled="uploading"
text
class="action">
Cancel
</v-btn>
</template>
</v-toolbar-items>
</template>
<script>
import get from 'lodash/get';
import last from 'lodash/last';
import pick from 'lodash/pick';
import UploadBtn from 'tce-core/UploadBtn';
function isUploaded(url) {
try {
return url && new URL(url).protocol === 'storage:';
} catch (e) {
return false;
}
}
export default {
name: 'input-asset',
props: {
repositoryId: { type: Number, default: null },
url: { type: String, default: null },
publicUrl: { type: String, default: null },
extensions: { type: Array, required: true },
allowFileUpload: { type: Boolean, default: true },
uploadLabel: { type: String, default: 'Select file' }
},
data() {
const isLinked = !isUploaded(this.url);
return {
isEditing: !this.url,
uploading: false,
file: isLinked ? null : pick(this, ['url', 'publicUrl']),
urlInput: isLinked ? this.url : null
};
},
computed: {
hasAsset: vm => vm.file || vm.urlInput,
isLinked: vm => !!vm.urlInput,
hasChanges: vm => vm.url !== (vm.isLinked ? vm.urlInput : get(vm, 'file.url', null)),
fileName() {
if (!this.file) return null;
return last(this.file.url.split('___'));
}
},
methods: {
async save() {
if (this.$refs.provider) {
const { valid } = await this.$refs.provider.validate();
if (!valid) return;
}
this.isEditing = false;
const payload = this.file || { url: this.urlInput, publicUrl: this.urlInput };
this.$emit('input', payload);
},
cancel() {
const isLinked = !isUploaded(this.url);
this.urlInput = isLinked ? this.url : null;
this.file = isLinked ? null : pick(this, ['url', 'publicUrl']);
this.isEditing = !this.url;
}
},
components: { UploadBtn }
};
</script>
<style lang="scss" scoped>
.v-text-field {
min-width: 21.875rem;
margin: 0.5rem 0.75rem 0 1.75rem;
}
.action ::v-deep .v-btn__content {
min-width: 4rem !important;
}
.upload-btn ::v-deep .v-btn {
height: 100%;
.v-btn__content {
padding: 1.5rem 0;
}
}
</style>